| 
 | 1 | +---  | 
 | 2 | +title: Checkbox  | 
 | 3 | +description: A checkbox component for selecting multiple options  | 
 | 4 | +icon: preview  | 
 | 5 | +links:  | 
 | 6 | +  rac: Checkbox  | 
 | 7 | +  source: checkbox/checkbox.tsx  | 
 | 8 | +  styles: checkbox.css  | 
 | 9 | +  storybook: Components/Forms/Checkbox  | 
 | 10 | +  figma: true  | 
 | 11 | +---  | 
 | 12 | + | 
 | 13 | +## Import  | 
 | 14 | + | 
 | 15 | +```tsx  | 
 | 16 | +import { Checkbox, Label } from '@heroui/react';  | 
 | 17 | +```  | 
 | 18 | + | 
 | 19 | +### Usage  | 
 | 20 | + | 
 | 21 | +<ComponentPreview  | 
 | 22 | +  name="checkbox-basic"  | 
 | 23 | +/>  | 
 | 24 | + | 
 | 25 | +### Anatomy  | 
 | 26 | + | 
 | 27 | +Import the Checkbox component and access all parts using dot notation.  | 
 | 28 | + | 
 | 29 | +```tsx  | 
 | 30 | +import { Checkbox, Label } from '@heroui/react';  | 
 | 31 | + | 
 | 32 | +export default () => (  | 
 | 33 | +  <Checkbox>  | 
 | 34 | +    <Checkbox.Control>  | 
 | 35 | +      <Checkbox.Indicator />  | 
 | 36 | +    </Checkbox.Control>  | 
 | 37 | +    <Label>Accept terms</Label>  | 
 | 38 | +  </Checkbox>  | 
 | 39 | +);  | 
 | 40 | +```  | 
 | 41 | + | 
 | 42 | +The Checkbox component follows a composition pattern. It works with external `Label` and `Description` components using standard HTML `id` and `htmlFor` attributes:  | 
 | 43 | + | 
 | 44 | +```tsx  | 
 | 45 | +import { Checkbox, Label, Description } from '@heroui/react';  | 
 | 46 | + | 
 | 47 | +export default () => (  | 
 | 48 | +  <div className="flex gap-3">  | 
 | 49 | +    <Checkbox className="mt-0.5" id="notifications">  | 
 | 50 | +      <Checkbox.Control>  | 
 | 51 | +        <Checkbox.Indicator />  | 
 | 52 | +      </Checkbox.Control>  | 
 | 53 | +    </Checkbox>  | 
 | 54 | +    <div className="flex flex-col gap-1">  | 
 | 55 | +      <Label htmlFor="notifications">Email notifications</Label>  | 
 | 56 | +      <Description>Get notified when someone mentions you</Description>  | 
 | 57 | +    </div>  | 
 | 58 | +  </div>  | 
 | 59 | +);  | 
 | 60 | +```  | 
 | 61 | + | 
 | 62 | +### Disabled  | 
 | 63 | + | 
 | 64 | +<ComponentPreview  | 
 | 65 | +  name="checkbox-disabled"  | 
 | 66 | +/>  | 
 | 67 | + | 
 | 68 | +### Default Selected  | 
 | 69 | + | 
 | 70 | +<ComponentPreview  | 
 | 71 | +  name="checkbox-default-selected"  | 
 | 72 | +/>  | 
 | 73 | + | 
 | 74 | +### Controlled  | 
 | 75 | + | 
 | 76 | +<ComponentPreview  | 
 | 77 | +  name="checkbox-controlled"  | 
 | 78 | +/>  | 
 | 79 | + | 
 | 80 | +### Indeterminate  | 
 | 81 | + | 
 | 82 | +<ComponentPreview  | 
 | 83 | +  name="checkbox-indeterminate"  | 
 | 84 | +/>  | 
 | 85 | + | 
 | 86 | +### With Label  | 
 | 87 | + | 
 | 88 | +<ComponentPreview  | 
 | 89 | +  name="checkbox-with-label"  | 
 | 90 | +/>  | 
 | 91 | + | 
 | 92 | +### With Description  | 
 | 93 | + | 
 | 94 | +<ComponentPreview  | 
 | 95 | +  name="checkbox-with-description"  | 
 | 96 | +/>  | 
 | 97 | + | 
 | 98 | +### Render Props  | 
 | 99 | + | 
 | 100 | +<ComponentPreview  | 
 | 101 | +  name="checkbox-render-props"  | 
 | 102 | +/>  | 
 | 103 | + | 
 | 104 | +### Form Integration  | 
 | 105 | + | 
 | 106 | +<ComponentPreview  | 
 | 107 | +  name="checkbox-form"  | 
 | 108 | +/>  | 
 | 109 | + | 
 | 110 | +### Custom Styles  | 
 | 111 | + | 
 | 112 | +<ComponentPreview  | 
 | 113 | +  name="checkbox-custom-styles"  | 
 | 114 | +/>  | 
 | 115 | + | 
 | 116 | +## Styling  | 
 | 117 | + | 
 | 118 | +### Passing Tailwind CSS classes  | 
 | 119 | + | 
 | 120 | +You can customize individual Checkbox components:  | 
 | 121 | + | 
 | 122 | +```tsx  | 
 | 123 | +import { Checkbox, Label } from '@heroui/react';  | 
 | 124 | + | 
 | 125 | +function CustomCheckbox() {  | 
 | 126 | +  return (  | 
 | 127 | +    <div className="flex items-center gap-3">  | 
 | 128 | +      <Checkbox id="custom">  | 
 | 129 | +        <Checkbox.Control className="border-2 border-purple-500 data-[selected=true]:bg-purple-500">  | 
 | 130 | +          <Checkbox.Indicator className="text-white" />  | 
 | 131 | +        </Checkbox.Control>  | 
 | 132 | +      </Checkbox>  | 
 | 133 | +      <Label htmlFor="custom">Custom Checkbox</Label>  | 
 | 134 | +    </div>  | 
 | 135 | +  );  | 
 | 136 | +}  | 
 | 137 | +```  | 
 | 138 | + | 
 | 139 | +### Customizing the component classes  | 
 | 140 | + | 
 | 141 | +To customize the Checkbox component classes, you can use the `@layer components` directive.  | 
 | 142 | +<br/>[Learn more](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes).  | 
 | 143 | + | 
 | 144 | +```css  | 
 | 145 | +@layer components {  | 
 | 146 | +  .checkbox {  | 
 | 147 | +    @apply inline-flex gap-3 items-center;  | 
 | 148 | +  }  | 
 | 149 | + | 
 | 150 | +  .checkbox__control {  | 
 | 151 | +    @apply size-5 border-2 border-gray-400 rounded data-[selected=true]:bg-blue-500 data-[selected=true]:border-blue-500;  | 
 | 152 | +  }  | 
 | 153 | + | 
 | 154 | +  .checkbox__indicator {  | 
 | 155 | +    @apply text-white;  | 
 | 156 | +  }  | 
 | 157 | + | 
 | 158 | +  .checkbox__content {  | 
 | 159 | +    @apply flex flex-col gap-1;  | 
 | 160 | +  }  | 
 | 161 | +}  | 
 | 162 | +```  | 
 | 163 | + | 
 | 164 | +HeroUI follows the [BEM](https://getbem.com/) methodology to ensure component variants and states are reusable and easy to customize.  | 
 | 165 | + | 
 | 166 | +### CSS Classes  | 
 | 167 | + | 
 | 168 | +The Checkbox component uses these CSS classes ([View source styles](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/checkbox.css)):  | 
 | 169 | + | 
 | 170 | +- `.checkbox` - Base checkbox container  | 
 | 171 | +- `.checkbox__control` - Checkbox control box  | 
 | 172 | +- `.checkbox__indicator` - Checkbox checkmark indicator  | 
 | 173 | +- `.checkbox__content` - Optional content container  | 
 | 174 | + | 
 | 175 | +### Interactive States  | 
 | 176 | + | 
 | 177 | +The checkbox supports both CSS pseudo-classes and data attributes for flexibility:  | 
 | 178 | + | 
 | 179 | +- **Selected**: `[data-selected="true"]` (shows checkmark and background color change)  | 
 | 180 | +- **Indeterminate**: `[data-indeterminate="true"]` (shows indeterminate state with dash)  | 
 | 181 | +- **Hover**: `:hover` or `[data-hovered="true"]`  | 
 | 182 | +- **Focus**: `:focus-visible` or `[data-focus-visible="true"]` (shows focus ring)  | 
 | 183 | +- **Disabled**: `:disabled` or `[aria-disabled="true"]` (reduced opacity, no pointer events)  | 
 | 184 | +- **Pressed**: `:active` or `[data-pressed="true"]`  | 
 | 185 | + | 
 | 186 | +## API Reference  | 
 | 187 | + | 
 | 188 | +### Checkbox Props  | 
 | 189 | + | 
 | 190 | +Inherits from [React Aria Checkbox](https://react-spectrum.adobe.com/react-aria/Checkbox.html).  | 
 | 191 | + | 
 | 192 | +| Prop | Type | Default | Description |  | 
 | 193 | +|------|------|---------|-------------|  | 
 | 194 | +| `isSelected` | `boolean` | `false` | Whether the checkbox is checked |  | 
 | 195 | +| `defaultSelected` | `boolean` | `false` | Whether the checkbox is checked by default (uncontrolled) |  | 
 | 196 | +| `isIndeterminate` | `boolean` | `false` | Whether the checkbox is in an indeterminate state |  | 
 | 197 | +| `isDisabled` | `boolean` | `false` | Whether the checkbox is disabled |  | 
 | 198 | +| `isReadOnly` | `boolean` | `false` | Whether the checkbox is read only |  | 
 | 199 | +| `name` | `string` | - | The name of the input element, used when submitting an HTML form |  | 
 | 200 | +| `value` | `string` | - | The value of the input element, used when submitting an HTML form |  | 
 | 201 | +| `onChange` | `(isSelected: boolean) => void` | - | Handler called when the checkbox value changes |  | 
 | 202 | +| `children` | `React.ReactNode \| (values: CheckboxRenderProps) => React.ReactNode` | - | Checkbox content or render prop |  | 
 | 203 | + | 
 | 204 | +### CheckboxRenderProps  | 
 | 205 | + | 
 | 206 | +When using the render prop pattern, these values are provided:  | 
 | 207 | + | 
 | 208 | +| Prop | Type | Description |  | 
 | 209 | +|------|------|-------------|  | 
 | 210 | +| `isSelected` | `boolean` | Whether the checkbox is currently checked |  | 
 | 211 | +| `isIndeterminate` | `boolean` | Whether the checkbox is in an indeterminate state |  | 
 | 212 | +| `isHovered` | `boolean` | Whether the checkbox is hovered |  | 
 | 213 | +| `isPressed` | `boolean` | Whether the checkbox is currently pressed |  | 
 | 214 | +| `isFocused` | `boolean` | Whether the checkbox is focused |  | 
 | 215 | +| `isFocusVisible` | `boolean` | Whether the checkbox is keyboard focused |  | 
 | 216 | +| `isDisabled` | `boolean` | Whether the checkbox is disabled |  | 
 | 217 | +| `isReadOnly` | `boolean` | Whether the checkbox is read only |  | 
0 commit comments