Checkbox

Checkbox component is used in forms when a user needs to select multiple values from several options.

SourceTheme Source@chakra-ui/checkbox

Import#

import { Checkbox } from '@chakra-ui/react'

Usage#

<Checkbox.Root defaultChecked>
<Checkbox.Control />
<Checkbox.Label> Checkbox</Checkbox.Label>
</Checkbox.Root>

Disabling the checkbox#

<Stack gap={5} direction='row'>
<Checkbox.Root disabled>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root disabled defaultChecked>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
</Stack>

Changing the color palette#

Pass the colorPalette prop to change the color of the Checkbox.

<Stack gap={5} direction='row'>
<Checkbox.Root colorPalette='red' defaultChecked>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root colorPalette='green' defaultChecked>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
</Stack>

Changing the size#

Pass the size prop to change the size of the Checkbox. Values can be either sm, md or lg.

<Stack gap={[1, 5]} direction={['column', 'row']}>
<Checkbox.Root size='sm' colorPalette='red'>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root size='md' colorPalette='green' defaultChecked>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root size='lg' colorPalette='orange' defaultChecked>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>
</Stack>

Making the checkbox invalid#

<Checkbox.Root invalid>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>

Changing the gap between the checkbox and label#

Use the gap prop to customize the spacing between the checkbox and label text.

<Checkbox.Root gap='1rem'>
<Checkbox.Control />
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>

Changing the rendered icon#

Use the Checkbox.Indicator component to customize the rendered icon when checked or indeterminate.

<Checkbox.Root iconColor='blue.400' iconSize='1rem'>
<Checkbox.Control>
<Checkbox.Indicator checked={<MdCheckCircle />} />
</Checkbox.Control>
<Checkbox.Label>Checkbox</Checkbox.Label>
</Checkbox.Root>

Indeterminate#

function IndeterminateExample() {
const [checkedItems, setCheckedItems] = React.useState([false, false])
const allChecked = checkedItems.every(Boolean)
const indeterminate = checkedItems.some(Boolean) && !allChecked
return (
<>
<Checkbox.Root
checked={allChecked}
indeterminate={indeterminate}
onChange={(e) => setCheckedItems([e.target.checked, e.target.checked])}
>
<Checkbox.Control />
<Checkbox.Label> Parent Checkbox</Checkbox.Label>
</Checkbox.Root>
<Stack paddingStart={6} marginTop={1} gap={1}>
<Checkbox.Root
checked={checkedItems[0]}
onChange={(e) => setCheckedItems([e.target.checked, checkedItems[1]])}
>
<Checkbox.Control />
<Checkbox.Label> Child Checkbox 1</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root
checked={checkedItems[1]}
onChange={(e) => setCheckedItems([checkedItems[0], e.target.checked])}
>
<Checkbox.Control />
<Checkbox.Label> Child Checkbox 2</Checkbox.Label>
</Checkbox.Root>
</Stack>
</>
)
}

Managing a group of checkboxes#

Use the Checkbox.Group component to manage the checked state of its children checkboxes.

<Checkbox.Group colorPalette='green' defaultValue={['naruto', 'kakashi']}>
<Stack gap={[1, 5]} direction={['column', 'row']}>
<Checkbox.Root value='naruto'>
<Checkbox.Control />
<Checkbox.Label> Naruto</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root value='sasuke'>
<Checkbox.Control />
<Checkbox.Label> Sasuke</Checkbox.Label>
</Checkbox.Root>
<Checkbox.Root value='kakashi'>
<Checkbox.Control />
<Checkbox.Label> Kakashi</Checkbox.Label>
</Checkbox.Root>
</Stack>
</Checkbox.Group>

Hooks#

The useCheckbox hook is exported with state and focus management logic for use in creating tailor-made checkbox component for your application. Read more about the useCheckbox hook here.

The useCheckboxGroup hook is exported with state management logic for use in creating tailor-made checkbox group component for your application. Read more about the useCheckboxGroup hook here.

Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel