Checkbox API
- Component status:
-
stable
Import
Section titled “Import”import { Checkbox } from "@stratakit/bricks";// ORimport Checkbox from "@stratakit/bricks/Checkbox";Checkbox
Section titled “Checkbox”A styled checkbox element, typically used for selecting one or more options from a list.
Use with the Field components to automatically handle ID associations for
labels and descriptions:
<Field.Root> <Field.Label>Check me</Field.Label> <Field.Control render={<Checkbox />} /></Field.Root>Without the Field components you will need to manually associate labels,
descriptions, etc.:
<Checkbox id="newsletter" name="newsletter" aria-describedby="newsletter-description" /><Label htmlFor="newsletter">Sign me up for the newsletter.</Label><Description id="newsletter-description">No spam, we promise.</Description>Underneath, it’s an HTML checkbox, i.e. <input type="checkbox">, so it supports the same props,
including value, defaultChecked, checked, and onChange.
accessibleWhenDisabled
Section titled “accessibleWhenDisabled”boolean | undefined (optional) Indicates whether the element should be focusable even when it is
disabled.
This is important when discoverability is a concern. For example:
A toolbar in an editor contains a set of special smart paste functions that are disabled when the clipboard is empty or when the function is not applicable to the current content of the clipboard. It could be helpful to keep the disabled buttons focusable if the ability to discover their functionality is primarily via their presence on the toolbar.
Learn more on Focusability of disabled controls.
autoFocus
Section titled “autoFocus”boolean | undefined (optional) false Automatically focuses the element upon mounting, similar to the native
autoFocus prop. This addresses an issue where the element with the native
autoFocus attribute might receive focus before React effects are
executed.
The autoFocus prop can also be used with
Focusable elements within a
Dialog component, establishing the
initial focus as the dialog opens.
Note: For this prop to work, the
focusable prop must be
set to true, if it’s not set by default.
checked
Section titled “checked”boolean | "mixed" | undefined (optional) The checked state of the checkbox. This will override the value inferred
from store prop, if
provided. This can be "mixed" to indicate that the checkbox is partially
checked.
defaultChecked
Section titled “defaultChecked”boolean | "mixed" | undefined (optional) The default checked state of the checkbox. This prop is ignored if the
checked or the
store props are provided.
disabled
Section titled “disabled”boolean | undefined (optional) false Determines if the element is disabled. This sets the aria-disabled
attribute accordingly, enabling support for all elements, including those
that don’t support the native disabled attribute.
This feature can be combined with the
accessibleWhenDisabled
prop to make disabled elements still accessible via keyboard.
Note: For this prop to work, the
focusable prop must be
set to true, if it’s not set by default.
onChange
Section titled “onChange”ChangeEventHandler<HTMLInputElement, HTMLInputElement> | undefined (optional) A function that is called when the checkbox’s checked state changes.
render
Section titled “render”(props: P) => React.ReactNode | React.ReactElement | undefined (optional) Allows the component to be rendered as a different HTML element or React component. The value can be a React element or a function that takes in the original component props and gives back a React element with the props merged.
Check out the Composition guide for more details.
string | number | readonly string[] | undefined (optional) The value of the checkbox. This is useful when the same checkbox store is
used for multiple Checkbox
elements, in which case the value will be an array of checked values.