Checkbox
A checkbox allows a user to select multiple items from a list of individual items, or to mark one individual item as selected.
install | yarn add react-aria-components |
---|---|
version | 3.17.0 |
usage | import {Checkbox} from 'react-aria-components' |
Example#
import {Checkbox} from 'react-aria-components';
<Checkbox>
<div className="checkbox">
<svg viewBox="0 0 18 18">
<polyline points="1 9 7 14 15 4" />
</svg>
</div>
Unsubscribe
</Checkbox>
import {Checkbox} from 'react-aria-components';
<Checkbox>
<div className="checkbox">
<svg viewBox="0 0 18 18">
<polyline points="1 9 7 14 15 4" />
</svg>
</div>
Unsubscribe
</Checkbox>
import {Checkbox} from 'react-aria-components';
<Checkbox>
<div className="checkbox">
<svg viewBox="0 0 18 18">
<polyline points="1 9 7 14 15 4" />
</svg>
</div>
Unsubscribe
</Checkbox>
Show CSS
.react-aria-Checkbox {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
& .checkbox {
width: 16px;
height: 16px;
border: 2px solid gray;
border-radius: 4px;
transition: all 200ms;
display: flex;
align-items: center;
justify-content: center;
}
& svg {
width: 14px;
height: 14px;
fill: none;
stroke: white;
stroke-width: 3px;
stroke-dasharray: 22px;
stroke-dashoffset: 66;
transition: all 200ms;
}
&[data-pressed] .checkbox {
border-color: dimgray;
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
border-color: slateblue;
background: slateblue;
}
&[data-pressed] .checkbox {
border-color: #4837b5;
background: #4837b5;
}
& svg {
stroke-dashoffset: 44;
}
}
&[data-validation-state=invalid] {
& .checkbox {
border-color: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
border-color: var(--spectrum-global-color-static-red-700);
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
background: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
background: var(--spectrum-global-color-static-red-700);
}
}
}
&[data-indeterminate] {
& svg {
stroke: none;
fill: white;
}
}
&[data-focus-visible] .checkbox {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px slateblue;
}
&[data-disabled] {
opacity: 0.4;
}
}
.react-aria-Checkbox {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
& .checkbox {
width: 16px;
height: 16px;
border: 2px solid gray;
border-radius: 4px;
transition: all 200ms;
display: flex;
align-items: center;
justify-content: center;
}
& svg {
width: 14px;
height: 14px;
fill: none;
stroke: white;
stroke-width: 3px;
stroke-dasharray: 22px;
stroke-dashoffset: 66;
transition: all 200ms;
}
&[data-pressed] .checkbox {
border-color: dimgray;
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
border-color: slateblue;
background: slateblue;
}
&[data-pressed] .checkbox {
border-color: #4837b5;
background: #4837b5;
}
& svg {
stroke-dashoffset: 44;
}
}
&[data-validation-state=invalid] {
& .checkbox {
border-color: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
border-color: var(--spectrum-global-color-static-red-700);
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
background: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
background: var(--spectrum-global-color-static-red-700);
}
}
}
&[data-indeterminate] {
& svg {
stroke: none;
fill: white;
}
}
&[data-focus-visible] .checkbox {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px slateblue;
}
&[data-disabled] {
opacity: 0.4;
}
}
.react-aria-Checkbox {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
& .checkbox {
width: 16px;
height: 16px;
border: 2px solid gray;
border-radius: 4px;
transition: all 200ms;
display: flex;
align-items: center;
justify-content: center;
}
& svg {
width: 14px;
height: 14px;
fill: none;
stroke: white;
stroke-width: 3px;
stroke-dasharray: 22px;
stroke-dashoffset: 66;
transition: all 200ms;
}
&[data-pressed] .checkbox {
border-color: dimgray;
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
border-color: slateblue;
background: slateblue;
}
&[data-pressed] .checkbox {
border-color: #4837b5;
background: #4837b5;
}
& svg {
stroke-dashoffset: 44;
}
}
&[data-validation-state=invalid] {
& .checkbox {
border-color: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
border-color: var(--spectrum-global-color-static-red-700);
}
&[data-selected],
&[data-indeterminate] {
& .checkbox {
background: var(--spectrum-global-color-static-red-600);
}
&[data-pressed] .checkbox {
background: var(--spectrum-global-color-static-red-700);
}
}
}
&[data-indeterminate] {
& svg {
stroke: none;
fill: white;
}
}
&[data-focus-visible] .checkbox {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px slateblue;
}
&[data-disabled] {
opacity: 0.4;
}
}
Features#
Checkboxes can be built with the <input> HTML element, but this can be difficult to style. Checkbox
helps achieve accessible checkboxes that can be styled as needed.
- Styleable – Hover, press, keyboard focus, selection, and indeterminate states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.
- Accessible – Uses a visually hidden
<input>
element under the hood, which also enables HTML form integration and autofill. A label element is built-in to ensure the checkbox is usable with assistive technologies. - Cross-browser – Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.
Anatomy#
A checkbox consists of a visual selection indicator and a label. Checkboxes support three selection states: checked, unchecked, and indeterminate. Users may click or touch a checkbox to toggle the selection state, or use the Tab key to navigate to it and the Space key to toggle it.
In most cases, checkboxes should have a visual label. If the checkbox does not have a visible label,
an aria-label
or aria-labelledby
prop must be passed instead to identify the element to assistive
technology.
Props#
Name | Type | Default | Description |
isIndeterminate | boolean | — | Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction. |
defaultSelected | boolean | — | Whether the element should be selected (uncontrolled). |
isSelected | boolean | — | Whether the element should be selected (controlled). |
value | string | — | The value of the input element, used when submitting an HTML form. See MDN. |
name | string | — | The name of the input element, used when submitting an HTML form. See MDN. |
isDisabled | boolean | — | Whether the input is disabled. |
isReadOnly | boolean | — | Whether the input can be selected but not changed by the user. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | — | Whether user input is required on the input before form submission.
Often paired with the |
autoFocus | boolean | — | Whether the element should receive focus on render. |
children | ReactNode | (
(values: CheckboxRenderProps
)) => ReactNode | — | |
className | string | (
(values: CheckboxRenderProps
)) => string | — | |
style | CSSProperties | (
(values: CheckboxRenderProps
)) => CSSProperties | — |
Events
Name | Type | Default | Description |
onChange | (
(isSelected: boolean
)) => void | — | Handler that is called when the element's selection state changes. |
onFocus | (
(e: FocusEvent
)) => void | — | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | — | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | — | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | — | Handler that is called when a key is released. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
excludeFromTabOrder | boolean | — | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
aria-controls | string | — | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
aria-errormessage | string | — | Identifies the element that provides an error message for the object. |
Styling#
React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className
attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName
naming convention.
.react-aria-Checkbox {
/* ... */
}
.react-aria-Checkbox {
/* ... */
}
.react-aria-Checkbox {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<Checkbox className="my-checkbox">
{/* ... */}
</Checkbox>
<Checkbox className="my-checkbox">
{/* ... */}
</Checkbox>
<Checkbox className="my-checkbox">
{/* ... */}
</Checkbox>;
In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using DOM attributes, which you can target in CSS selectors. These are ARIA attributes wherever possible, or data attributes when a relevant ARIA attribute does not exist. For example:
.react-aria-Checkbox[data-pressed] {
/* ... */
}
.react-aria-Checkbox[data-pressed] {
/* ... */
}
.react-aria-Checkbox[data-pressed] {
/* ... */
}
The className
and style
props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.
<Checkbox
className={({ isPressed }) => isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>;
<Checkbox
className={({ isPressed }) =>
isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>;
<Checkbox
className={(
{ isPressed }
) =>
isPressed
? 'bg-gray-700'
: 'bg-gray-600'}
/>;
Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could render an extra element when the checkbox is selected.
<Checkbox>
{({isSelected}) => (
<>
{isSelected && <CheckIcon />}
Subscribe
</>
)}
</Checkbox>
<Checkbox>
{({isSelected}) => (
<>
{isSelected && <CheckIcon />}
Subscribe
</>
)}
</Checkbox>
<Checkbox>
{({ isSelected }) => (
<>
{isSelected && (
<CheckIcon />
)}
Subscribe
</>
)}
</Checkbox>;
The states, selectors, and render props for Checkbox
are documented below.
Name | CSS Selector | Description |
isSelected | [data-selected] | Whether the checkbox is selected. |
isIndeterminate | [data-indeterminate] | Whether the checkbox is selected. |
isHovered | [data-hovered] | Whether the checkbox is currently hovered with a mouse. |
isPressed | [data-pressed] | Whether the checkbox is currently in a pressed state. |
isFocused | [data-focused] | Whether the checkbox is focused, either via a mouse or keyboard. |
isFocusVisible | [data-focus-visible] | Whether the checkbox is keyboard focused. |
isDisabled | [data-disabled] | Whether the checkbox is disabled. |
isReadOnly | [data-readonly] | Whether the checkbox is read only. |
validationState | [data-validation-state="valid | invalid"] | Whether the checkbox is valid or invalid. |
isRequired | [data-required] | Whether the checkbox is required. |
Reusable wrappers#
If you will use a Checkbox in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.
This example wraps Checkbox
and all of its children together into a single component. It also shows how to use render props to conditionally render a different indicator icon when the checkbox is in an indeterminate state.
function MyCheckbox({children, ...props}) {
return (
<Checkbox {...props}>
{({isIndeterminate}) => <>
<div className="checkbox">
<svg viewBox="0 0 18 18">
{isIndeterminate
? <rect x={1} y={7.5} width={15} height={3} />
: <polyline points="1 9 7 14 15 4" />
}
</svg>
</div>
{children}
</>}
</Checkbox>
);
}
<MyCheckbox>Unsubscribe</MyCheckbox>
function MyCheckbox({children, ...props}) {
return (
<Checkbox {...props}>
{({isIndeterminate}) => <>
<div className="checkbox">
<svg viewBox="0 0 18 18">
{isIndeterminate
? <rect x={1} y={7.5} width={15} height={3} />
: <polyline points="1 9 7 14 15 4" />
}
</svg>
</div>
{children}
</>}
</Checkbox>
);
}
<MyCheckbox>Unsubscribe</MyCheckbox>
function MyCheckbox(
{ children, ...props }
) {
return (
<Checkbox {...props}>
{(
{
isIndeterminate
}
) => (
<>
<div className="checkbox">
<svg viewBox="0 0 18 18">
{isIndeterminate
? (
<rect
x={1}
y={7.5}
width={15}
height={3}
/>
)
: (
<polyline points="1 9 7 14 15 4" />
)}
</svg>
</div>
{children}
</>
)}
</Checkbox>
);
}
<MyCheckbox>
Unsubscribe
</MyCheckbox>
Usage#
The following examples show how to use the MyCheckbox
component created in the above example.
Default value#
Checkboxes are not selected by default. The defaultSelected
prop can be used to set the default state.
<MyCheckbox defaultSelected>Subscribe</MyCheckbox>
<MyCheckbox defaultSelected>Subscribe</MyCheckbox>
<MyCheckbox
defaultSelected
>
Subscribe
</MyCheckbox>
Controlled value#
The isSelected
prop can be used to make the selected state controlled. The onChange
event is fired when the user presses the checkbox, and receives the new value.
function Example() {
let [selected, setSelection] = React.useState(false);
return (
<>
<MyCheckbox isSelected={selected} onChange={setSelection}>
Subscribe
</MyCheckbox>
<p>{`You are `}</p>
</>
);
}
function Example() {
let [selected, setSelection] = React.useState(false);
return (
<>
<MyCheckbox
isSelected={selected}
onChange={setSelection}
>
Subscribe
</MyCheckbox>
<p>
{`You are `}
</p>
</>
);
}
function Example() {
let [
selected,
setSelection
] = React.useState(
false
);
return (
<>
<MyCheckbox
isSelected={selected}
onChange={setSelection}
>
Subscribe
</MyCheckbox>
<p>
{`You are `}
</p>
</>
);
}
Indeterminate#
A Checkbox can be in an indeterminate state, controlled using the isIndeterminate
prop.
This overrides the appearance of the Checkbox, whether selection is controlled or uncontrolled.
The Checkbox will visually remain indeterminate until the isIndeterminate
prop is set to false, regardless of user interaction.
<MyCheckbox isIndeterminate>Subscribe</MyCheckbox>
<MyCheckbox isIndeterminate>Subscribe</MyCheckbox>
<MyCheckbox
isIndeterminate
>
Subscribe
</MyCheckbox>
Validation#
Checkboxes can display a validation state to communicate to the user if the current value is invalid.
Implement your own validation logic in your app and pass "invalid"
to the Checkbox via the validationState
prop.
<MyCheckbox validationState="invalid">
I accept the terms and conditions
</MyCheckbox>
<MyCheckbox validationState="invalid">
I accept the terms and conditions
</MyCheckbox>
<MyCheckbox validationState="invalid">
I accept the terms
and conditions
</MyCheckbox>
Disabled#
Checkboxes can be disabled using the isDisabled
prop.
<MyCheckbox isDisabled>Subscribe</MyCheckbox>
<MyCheckbox isDisabled>Subscribe</MyCheckbox>
<MyCheckbox isDisabled>
Subscribe
</MyCheckbox>
Read only#
The isReadOnly
prop makes the selection immutable. Unlike isDisabled
, the Checkbox remains focusable.
See the MDN docs for more information.
<MyCheckbox isSelected isReadOnly>Agree</MyCheckbox>
<MyCheckbox isSelected isReadOnly>Agree</MyCheckbox>
<MyCheckbox
isSelected
isReadOnly
>
Agree
</MyCheckbox>
HTML forms#
Checkbox supports the name
and value
props for integration with HTML forms.
<MyCheckbox name="newsletter" value="subscribe">Subscribe</MyCheckbox>
<MyCheckbox name="newsletter" value="subscribe">
Subscribe
</MyCheckbox>
<MyCheckbox
name="newsletter"
value="subscribe"
>
Subscribe
</MyCheckbox>
Advanced customization#
Hooks#
If you need to customize things further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useCheckbox for more details.