Checkbox

Checkboxes allow users to select multiple items from a list of individual items, or to mark one individual item as selected.


Installation

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 that isSelected, isIndeterminate, and disableAnimation are not passed to a DOM element.

Controlled

Note: NextUI Checkbox also supports native events like onChange, 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.
  • hiddenInput: The hidden input element that is used to handle the checkbox state.
  • 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

PropTypeDefault
children
ReactNode
icon
CheckboxIconProps
value
string
name
string
size
sm | md | lg
"md"
color
default | primary | secondary | success | warning | danger
"primary"
radius
none | sm | md | lg | full
lineThrough
boolean
false
isSelected
boolean
defaultSelected
boolean
isRequired
boolean
false
isReadOnly
boolean
isDisabled
boolean
false
isIndeterminate
boolean
isInvalid
boolean
false
validationState
valid | invalid
disableAnimation
boolean
false
classNames
Partial<Record<"base"| "wrapper"| "icon"| "label", string>>

Checkbox Events

PropTypeDefault
onChange
React.ChangeEvent<HTMLInputElement>
onValueChange
(isSelected: boolean) => void

Types

Checkbox Icon Props