Checkbox
Checkboxes allow users to select multiple items from a list of individual items, or to mark one individual item as selected.
Installation
npx nextui-cli@latest add checkbox
The above command is for individual installation only. You may skip this step if @nextui-org/react
is already installed globally.
Import
Usage
Disabled
Sizes
Colors
Radius
Indeterminate
The isIndeterminate
prop sets a Checkbox
to an indeterminate state, overriding its appearance and maintaining it until set to false
, regardless of user interaction.
Line Through
Custom Check Icon
By default,
IconProps
will be passed to your icon component. Please make sure thatisSelected
,isIndeterminate
, anddisableAnimation
are not passed to a DOM element.
Controlled
Note: NextUI
Checkbox
also supports native events likeonChange
, useful for form libraries such as Formik and React Hook Form.
Slots
- base: Checkbox wrapper, it handles alignment, placement, and general appearance.
- wrapper: An inner container that includes styles for relative positioning, flex properties, overflow handling and managing hover and selected states.
- icon: Icon within the checkbox, controlling size, visibility, and changes when checked.
- label: The text associated with the checkbox.
Custom Styles
You can customize the Checkbox
component by passing custom Tailwind CSS classes to the component slots.
Custom Implementation
In case you need to customize the checkbox even further, you can use the useCheckbox
hook to create your own implementation.
Note: We used Tailwind Variants to implement the styles above, you can use any other library such as clsx to achieve the same result.
Data Attributes
Checkbox
has the following attributes on the base
element:
- data-selected:
When the checkbox is checked. Based on
isSelected
prop. - data-pressed: When the checkbox is pressed. Based on usePress
- data-invalid:
When the checkbox is invalid. Based on
validationState
prop. - data-readonly:
When the checkbox is readonly. Based on
isReadOnly
prop. - data-indeterminate:
When the checkbox is indeterminate. Based on
isIndeterminate
prop. - data-hover: When the checkbox is being hovered. Based on useHover
- data-focus: When the checkbox is being focused. Based on useFocusRing.
- data-focus-visible: When the checkbox is being focused with the keyboard. Based on useFocusRing.
- data-disabled:
When the checkbox is disabled. Based on
isDisabled
prop. - data-loading:
When the checkbox is loading. Based on
isLoading
prop.
Accessibility
- Built with a native HTML
<input>
element. - Full support for browser features like form autofill.
- Keyboard focus management and cross browser normalization.
- Keyboard event support for Tab and Space keys.
- Labeling support for assistive technology.
- Indeterminate state support.
API
Checkbox Props
Prop | Type | Default |
children |
| |
icon |
| |
value |
| |
name |
| |
size |
| "md" |
color |
| "primary" |
radius |
| |
lineThrough |
| false |
isSelected |
| |
defaultSelected |
| |
isRequired |
| false |
isReadOnly |
| |
isDisabled |
| false |
isIndeterminate |
| |
isInvalid |
| false |
validationState |
| |
disableAnimation |
| false |
classNames |
|
Checkbox Events
Prop | Type | Default |
onChange |
| |
onValueChange |
|
Types
Checkbox Icon Props
type IconProps = {"data-checked": string;isSelected: boolean;isIndeterminate: boolean;disableAnimation: boolean;className: string;};type CheckboxIconProps = ReactNode | ((props: IconProps) => ReactNode);