apps/www/content/docs/components/checkbox.mdx
import { Checkbox } from "@chakra-ui/react"
<Checkbox.Root>
<Checkbox.HiddenInput />
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
</Checkbox.Root>
:::info
If you prefer a closed component composition, check out the snippet below.
:::
The Checkbox component also provides a set of shortcuts for common use cases.
This component renders the Checkbox.Indicator within it by default.
This works:
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
This might be more concise, if you don't need to customize the indicator:
<Checkbox.Control />
Pass the variant prop to the Checkbox.Root component to change the visual
style of the checkbox.
Pass the colorPalette prop to the Checkbox.Root component to change the
color of the checkbox.
Pass the size prop to the Checkbox.Root component to change the size of the
checkbox.
Pass the disabled or invalid prop to the Checkbox.Root component to change
the visual state of the checkbox.
Use the checked and onCheckedChange props to control the state of the
checkbox.
Here's an example of how to change the label position to the right.
<ExampleTabs name="checkbox-with-label-position" />An alternative way to control the checkbox is to use the RootProvider
component and the useCheckbox store hook.
This way you can access the checkbox state and methods from outside the checkbox.
<ExampleTabs name="checkbox-with-store" />Here's an example of how to compose a checkbox with a field component.
<ExampleTabs name="checkbox-with-form" />Here's an example of how to use the Checkbox component with the
react-hook-form library.
Use the CheckboxGroup component to group multiple checkboxes together.
Here's an example of how to use the CheckboxGroup component with the
react-hook-form library.
Render a custom icon within Checkbox.Control to change the icon of the
checkbox.
Set the checked prop to indeterminate to show the checkbox in an
indeterminate state.
Here's an example of how to add some further description to the checkbox.
<ExampleTabs name="checkbox-with-description" />Render an anchor tag within the Checkbox.Label to add a link to the label.
Here's how to setup the Checkbox for a closed component composition.
<ExampleCode name="checkbox-closed-component" />If you want to automatically add the closed component to your project, run the command:
npx @chakra-ui/cli snippet add checkbox
Here's how to use the it
<Checkbox>Accept terms and conditions</Checkbox>
When working with multiple checkboxes, it's important to understand the semantic
difference between Field and Fieldset:
Field.Root for proper form field structure
with labels and helper textFieldset.Root, not Field.RootA checkbox group represents a collection of related options and should be marked
up as a fieldset with a legend, not as a single field. Wrapping CheckboxGroup
in Field.Root can cause interaction issues where only the first checkbox
responds to clicks.
✅ Correct Usage:
<Fieldset.Root>
<CheckboxGroup name="framework">
<Fieldset.Legend>Select framework</Fieldset.Legend>
</CheckboxGroup>
</Fieldset.Root>
❌ Incorrect Usage:
// Don't wrap CheckboxGroup with Field.Root
<Field.Root>
<CheckboxGroup></CheckboxGroup>
</Field.Root>
Explore the Checkbox component parts interactively. Click on parts in the
sidebar to highlight them in the preview.