File size: 3,840 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
---
title: Checkbox
description:
Used in forms when a user needs to select multiple values from several options
links:
source: components/checkbox
storybook: components-checkbox--basic
recipe: checkbox
ark: https://ark-ui.com/react/docs/components/checkbox
---
<ExampleTabs name="checkbox-basic" />
## Usage
```tsx
import { Checkbox } from "@chakra-ui/react"
```
```tsx
<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](#closed-component).
:::
## Shortcuts
The `Checkbox` component also provides a set of shortcuts for common use cases.
### CheckboxControl
This component renders the `Checkbox.Indicator` within it by default.
This works:
```tsx
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
```
This might be more concise, if you don't need to customize the indicator:
```tsx
<Checkbox.Control />
```
## Examples
### Variants
Pass the `variant` prop to the `Checkbox.Root` component to change the visual
style of the checkbox.
<ExampleTabs name="checkbox-with-variants" />
### Colors
Pass the `colorPalette` prop to the `Checkbox.Root` component to change the
color of the checkbox.
<ExampleTabs name="checkbox-with-colors" />
### Sizes
Pass the `size` prop to the `Checkbox.Root` component to change the size of the
checkbox.
<ExampleTabs name="checkbox-with-sizes" />
### States
Pass the `disabled` or `invalid` prop to the `Checkbox.Root` component to change
the visual state of the checkbox.
<ExampleTabs name="checkbox-with-states" />
### Controlled
Use the `checked` and `onCheckedChange` props to control the state of the
checkbox.
<ExampleTabs name="checkbox-controlled" />
### Label Position
Here's an example of how to change the label position to the right.
<ExampleTabs name="checkbox-with-label-position" />
### Store
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" />
### Composition
Here's an example of how to compose a checkbox with a field component.
<ExampleTabs name="checkbox-with-form" />
### Hook Form
Here's an example of how to use the `Checkbox` component with the
`react-hook-form` library.
<ExampleTabs name="checkbox-with-hook-form" />
### Group
Use the `CheckboxGroup` component to group multiple checkboxes together.
<ExampleTabs name="checkbox-with-group" />
### Group Hook Form
Here's an example of how to use the `CheckboxGroup` component with the
`react-hook-form` library.
<ExampleTabs name="checkbox-with-group-hook-form" />
### Custom Icon
Render a custom icon within `Checkbox.Control` to change the icon of the
checkbox.
<ExampleTabs name="checkbox-with-custom-icon" />
### Indeterminate
Set the `checked` prop to `indeterminate` to show the checkbox in an
indeterminate state.
<ExampleTabs name="checkbox-indeterminate" />
### Description
Here's an example of how to add some further description to the checkbox.
<ExampleTabs name="checkbox-with-description" />
### Link
Render an anchor tag within the `Checkbox.Label` to add a link to the label.
<ExampleTabs name="checkbox-with-link" />
### Closed Component
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:
```bash
npx @chakra-ui/cli snippet add checkbox
```
Here's how to use the it
```tsx
<Checkbox>Accept terms and conditions</Checkbox>
```
## Props
### Root
<PropTable component="Checkbox" part="Root" />
|