--- title: Checkbox description: "`Checkbox` is a component used for allowing users to select multiple values from multiple options." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/checkbox - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-checkbox--basic --- ```tsx キングダム ハーツ ``` ## Usage ```tsx import { Checkbox, CheckboxGroup } from "@yamada-ui/react" ``` ```tsx import { Checkbox, CheckboxGroup } from "@/components/ui" ``` ```tsx import { Checkbox, CheckboxGroup } from "@workspaces/ui" ``` ```tsx ``` ```tsx ``` ### Group ```tsx ソラ リク カイリ ``` ### Use Items ```tsx const items = useMemo( () => [ { label: "ソラ", value: "sora" }, { label: "リク", value: "riku" }, { label: "カイリ", value: "kairi" }, ], [], ) return ``` ### Change Variant ```tsx const items = useMemo( () => [ { label: "Checked", value: "1" }, { label: "No checked", value: "2" }, ], [], ) return ( {(variant) => ( )} ) ``` ### Change Size ```tsx const items = useMemo( () => [ { label: "Checked", value: "1" }, { label: "No checked", value: "2" }, ], [], ) return ( {(size) => ( )} ) ``` ### Set Default Value To set a default value, set `defaultValue` to an array of values. ```tsx const items = useMemo( () => [ { label: "ソラ", value: "sora" }, { label: "リク", value: "riku" }, { label: "カイリ", value: "kairi" }, ], [], ) return ``` ### Default Checked To default checked, set `defaultChecked` to `true`. ```tsx キングダム ハーツ ``` ### Indeterminate To make indeterminate, set `indeterminate` to `true`. ```tsx preview functional client const [id0, id1, id2] = useIds() const [values, setValues] = useState([true, false, false]) const allChecked = values.every(Boolean) const indeterminate = values.some(Boolean) && !allChecked return ( setValues([ev.target.checked, ev.target.checked, ev.target.checked]) } > デスティニーアイランド setValues([ev.target.checked, values[1]!, values[2]!]) } > ソラ setValues([values[0]!, ev.target.checked, values[2]!]) } > リク setValues([values[0]!, values[1]!, ev.target.checked]) } > カイリ ) ``` ### Max Selection To limit the maximum number of selections, set `max` to a number. ```tsx const items = useMemo( () => [ { label: "ソラ", value: "sora" }, { label: "リク", value: "riku" }, { label: "カイリ", value: "kairi" }, ], [], ) return ``` ### Change Direction To change the direction, set `orientation` to `"horizontal"` or `"vertical"`. The default is `"vertical"`. ```tsx const items = useMemo( () => [ { label: "ソラ", value: "sora" }, { label: "リク", value: "riku" }, { label: "カイリ", value: "kairi" }, ], [], ) return ( {(orientation) => ( )} ) ``` ### Change Shape To change the shape, set `shape` to `"rounded"` or `"square"`. The default is `"rounded"`. ```tsx {(shape) => ( {toTitleCase(shape)} )} ``` ### Disable To disable, set `disabled` to `true`. ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### Read-Only To read-only, set `readOnly` to `true`. ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### Invalid To make invalid, set `invalid` to `true`. ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### Change Border Color To change the border color, set `focusBorderColor` or `errorBorderColor` to a color value. ```tsx Default Border Color Custom Border Color Custom Error Border Color ``` ### Customize Icon To customize icon, set `checkedIcon` to a `ReactNode`. ```tsx const items = useMemo( () => [ { label: "ソラ", value: "sora" }, { label: "リク", value: "riku" }, { label: "カイリ", value: "kairi", checkedIcon: }, ], [], ) return } items={items} /> ``` ### Use Custom Component ```tsx const { getInputProps, getLabelProps, getRootProps } = useCheckboxGroup({ defaultValue: ["sora"], max: 2, }) const CustomCheckbox = ({ children, ...rest }) => { return ( {children} ) } return ( ソラ リク カイリ ) ``` ### Control ```tsx const [checked, { toggle }] = useBoolean(false) return ( ソラ ) ``` ## Uses Components & Hooks ## Used By Components & Hooks ## Props ### Checkbox | Prop | Default | Type | Description | | ------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `checked` | - | `boolean` | If `true`, the checkbox will be checked. | | `checkedIcon` | - | `ReactNode` | The icon to display in the checkbox when it is checked. | | `defaultChecked` | `false` | `boolean` | If `true`, the checkbox will be initially checked. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is focused. | | `id` | - | `string` | id assigned to input. | | `indeterminate` | `false` | `boolean` | If `true`, the checkbox will be indeterminate. | | `indeterminateIcon` | - | `ReactNode` | The icon to display in the checkbox when it is indeterminate. | | `indicatorProps` | - | `CheckboxIndicatorProps` | Props for the indicator component. | | `inputProps` | - | `HTMLStyledProps<"input">` | Props for the input element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `labelProps` | - | `CheckboxLabelProps` | Props for the label component. | | `name` | - | `string` | The name of the input field in a checkbox. | | `onBlur` | - | `FocusEventHandler` | The callback invoked when the checkbox is blurred. | | `onChange` | - | `ChangeEventHandler` | The callback invoked when the checked state changes. | | `onFocus` | - | `FocusEventHandler` | The callback invoked when the checkbox is focused. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `rootProps` | - | `HTMLStyledProps<"label">` | Props for the label element. | | `value` | - | `Y` | The value of the checkbox. | ### CheckboxGroup.Root | Prop | Default | Type | Description | | ------------------ | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `align` | - | `"-moz-initial" \| "anchor-center" \| "baseline" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "normal" ...` | The CSS `align-items` property. | | `basis` | - | `"-moz-fit-content" \| "-moz-initial" \| "-moz-max-content" \| "-moz-min-content" \| "-webkit-auto" \| "0.5" \| "1.5" \| "1" \| "1/12" \| "1/2" ...` | The CSS `flex-basis` property. | | `checkedIcon` | - | `string \| number \| bigint \| boolean \| ReactElement> \| Iterable \| ReactPortal \| Promise<...>` | The icon to display in the checkbox when it is checked. | | `defaultValue` | `[]` | `Y[]` | The initial value of the checkbox group. | | `direction` | - | `"-moz-initial" \| "column-reverse" \| "column" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "row-reverse" \| "row" \| "unset" ...` | The CSS `flex-direction` property. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is focused. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `items` | `[]` | `CheckboxGroupItem[]` | If provided, generate options based on items. | | `justify` | - | `"-moz-initial" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "left" \| "normal" \| "revert-layer" ...` | The CSS `justify-content` property. | | `max` | - | `number` | The maximum number of checkboxes that can be checked. | | `onChange` | - | `(value: Y[]) => void` | The callback fired when any children checkbox is checked or unchecked. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `shrink` | - | `"-moz-initial" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "unset" \| number & {} ...` | The CSS `flex-shrink` property. | | `value` | - | `Y[]` | The value of the checkbox group. | | `wrap` | - | `"-moz-initial" \| "inherit" \| "initial" \| "nowrap" \| "revert-layer" \| "revert" \| "unset" \| "wrap-reverse" \| "wrap" ...` | The CSS `flex-wrap` property. | ### CheckboxGroup.Item | Prop | Default | Type | Description | | ------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `checked` | - | `boolean` | If `true`, the checkbox will be checked. | | `checkedIcon` | - | `ReactNode` | The icon to display in the checkbox when it is checked. | | `defaultChecked` | `false` | `boolean` | If `true`, the checkbox will be initially checked. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is focused. | | `id` | - | `string` | id assigned to input. | | `indeterminate` | `false` | `boolean` | If `true`, the checkbox will be indeterminate. | | `indeterminateIcon` | - | `ReactNode` | The icon to display in the checkbox when it is indeterminate. | | `indicatorProps` | - | `CheckboxIndicatorProps` | Props for the indicator component. | | `inputProps` | - | `HTMLStyledProps<"input">` | Props for the input element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `labelProps` | - | `CheckboxLabelProps` | Props for the label component. | | `name` | - | `string` | The name of the input field in a checkbox. | | `onBlur` | - | `FocusEventHandler` | The callback invoked when the checkbox is blurred. | | `onChange` | - | `ChangeEventHandler` | The callback invoked when the checked state changes. | | `onFocus` | - | `FocusEventHandler` | The callback invoked when the checkbox is focused. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `rootProps` | - | `HTMLStyledProps<"label">` | Props for the label element. | | `value` | - | `Y` | The value of the checkbox. | ## Accessibility The `Checkbox` follows the [WAI-ARIA - Checkbox Pattern](https://www.w3.org/WAI/ARIA/apg/patterns/checkbox/) for accessibility. If you are not using `Field.Root`, set `aria-label` or `aria-labelledby` to `CheckboxGroup.Root`. ```tsx ソラ リク カイリ ``` ```tsx Character ソラ リク カイリ ``` ### Keyboard Navigation | Key | Description | State | | ------- | -------------------------------------------------- | ----- | | `Space` | Toggles the checked state of the focused checkbox. | - | ### ARIA Roles and Attributes | Component | Roles and Attributes | Usage | | -------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `CheckboxGroup.Root` | `role="group"` | Indicates that it's a group. | | | `aria-labelledby` | If `CheckboxGroup.Root` is within a `Field.Root` and `Field.Root` has a `label` or `Field.Label`, sets its `id`. | | | `aria-describedby` | If `CheckboxGroup.Root` is within a `Field.Root` and `Field.Root` has an `errorMessage`, `helperMessage`, or a `Field.ErrorMessage`, `Field.HelperMessage`, sets its `id`. | | `input` | `aria-checked` | Sets to `"true"` if the checkbox is checked, `"false"` if it is not checked, and `"mixed"` if `indeterminate` is set. | | | `aria-describedby` | If `Checkbox` is within a `Field.Root` and `Field.Root` has an `errorMessage`, `helperMessage`, or a `Field.ErrorMessage`, `Field.HelperMessage`, sets its `id`. | | | `aria-disabled` | Sets to `"true"` if `readOnly` is set, or if the checkbox is not checked and `max` is reached in a group. | | | `aria-invalid` | Sets to `"true"` if `invalid` is set. | | | `aria-required` | Sets to `"true"` if `required` is set. | | `CheckboxIndicator` | `aria-hidden` | Excludes the element from the accessibility tree. |