string
Checkbox
Checkbox component is used in forms when a user needs to select multiple values from several options.
The Checkbox
component is a multipart component. The styling needs to be
applied to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
control
- B:
icon
- C:
container
- D:
label
Theming properties#
The properties that affect the theming of the Checkbox
component are:
size
: The size of the checkbox. Defaults tomd
.colorScheme
: The color scheme of the checkbox. Defaults toblue
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)
Customizing the default theme#
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylelabel: {fontFamily: 'mono', // change the font family of the label},control: {padding: 3, // change the padding of the controlborderRadius: 0, // change the border radius of the control},})export const checkboxTheme = defineMultiStyleConfig({ baseStyle })
After customizing the checkbox theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { checkboxTheme } from './components/checkbox.ts'export const theme = extendTheme({components: { Checkbox: checkboxTheme },})
This is a crucial step to make sure that any changes that we make to the checkbox theme are applied.
Adding a custom size#
Let's assume we want to include an extra large checkbox size. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const sizes = {xl: definePartsStyle({control: defineStyle({boxSize: 14}),label: defineStyle({fontSize: "2xl",marginLeft: 6})}),}export const checkboxTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Checkbox size="xl">...</Checkbox>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom circular variant. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const circular = definePartsStyle({control: defineStyle({rounded: "full"})})export const checkboxTheme = defineMultiStyleConfig({variants: { circular },})// Now we can use the new `circular` variant<Checkbox variant="circular">...</Checkbox>
Changing the default properties#
Let's assume we want to change the default size and variant of every checkbox in our app. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(checkboxAnatomy.keys,)export const checkboxTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'circular',},})// This saves you time, instead of manually setting the size and variant every time you use a checkbox:<Checkbox size="xl" variant="circular">...</Checkbox>
Showcase#
import { Box, SimpleGrid, IconButton, Checkbox, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative"> <SimpleGrid gap={12} p={12} columns={2}> <Checkbox>Themed Checkbox</Checkbox> <Checkbox size="xl">Themed XL Checkbox</Checkbox> <Checkbox variant="circular">Themed Circular Checkbox</Checkbox> <Checkbox variant="circular" size="xl">Themed XL Circular Checkbox</Checkbox> <Checkbox variant="circular" size="xl" colorScheme="red">Themed Red XL Circular Checkbox</Checkbox> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }
Props#
Checkbox Props#
aria-describedby
aria-describedby
aria-invalid
aria-invalid
true
aria-label
aria-label
Defines the string that labels the checkbox element.
string
aria-labelledby
aria-labelledby
Refers to the id
of the element that labels the checkbox element.
string
colorScheme
colorScheme
The visual color appearance of the component
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
blue
icon
icon
The checked icon to use
ReactElement<any, string | JSXElementConstructor<any>>
CheckboxIcon
iconColor
iconColor
The color of the checkbox icon when checked or indeterminate
string
iconSize
iconSize
The size of the checkbox icon when checked or indeterminate
string | number
id
id
id assigned to input
string
inputProps
inputProps
Additional props to be forwarded to the input
element
InputHTMLAttributes<HTMLInputElement>
isChecked
isChecked
If true
, the checkbox will be checked.
You'll need to pass onChange
to update its value (since it is now controlled)
boolean
false
isDisabled
isDisabled
If true
, the checkbox will be disabled
boolean
false
isFocusable
isFocusable
If true
and isDisabled
is passed, the checkbox will
remain tabbable but not interactive
boolean
false
isIndeterminate
isIndeterminate
If true
, the checkbox will be indeterminate.
This only affects the icon shown inside checkbox
and does not modify the isChecked property.
boolean
false
isInvalid
isInvalid
If true
, the checkbox is marked as invalid.
Changes style of unchecked state.
boolean
false
isReadOnly
isReadOnly
If true
, the checkbox will be readonly
boolean
false
isRequired
isRequired
If true
, the checkbox input is marked as required,
and required
attribute will be added
boolean
false
name
name
The name of the input field in a checkbox (Useful for form submission).
string
onChange
onChange
The callback invoked when the checked state of the Checkbox
changes.
(event: ChangeEvent<HTMLInputElement>) => void
onFocus
onFocus
The callback invoked when the checkbox is focused
(event: FocusEvent<HTMLInputElement, Element>) => void
size
size
The size of the Checkbox
"sm" | "md" | "lg"
md
spacing
spacing
The spacing between the checkbox and its label text
ResponsiveValue<string | number | (string & {})>
0.5rem
tabIndex
tabIndex
The tab-index property of the underlying input element.
number
value
value
The value to be used in the checkbox input. This is the value that will be returned on form submission.
string | number
variant
variant
The variant of the Checkbox
string
CheckboxGroup Props#
defaultValue
defaultValue
The initial value of the checkbox group
(string | number)[]
isDisabled
isDisabled
If true
, all wrapped checkbox inputs will be disabled
boolean
false
isNative
isNative
If true
, input elements will receive
checked
attribute instead of isChecked
.
This assumes, you're using native radio inputs
boolean
false
onChange
onChange
The callback fired when any children Checkbox is checked or unchecked
(value: (string | number)[]) => void
value
value
The value of the checkbox group
(string | number)[]
Shared Style Props#
The following style props can be set on CheckboxGroup
and they will be
forwarded to all children Checkbox
components.
Name | Type | Default | Description |
---|---|---|---|
colorScheme | string | The color of the checkbox when it is checked. This should be one of the color keys in the theme (e.g."green", "red"). | |
children | React.ReactNode | The content of the checkbox group. | |
size | sm , md , lg | md | The size of the checkbox. |
variant | string | The variant of the checkbox. |
The Checkbox
component is a multipart component. The styling needs to be
applied to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
control
- B:
icon
- C:
container
- D:
label
Theming properties#
The properties that affect the theming of the Checkbox
component are:
size
: The size of the checkbox. Defaults tomd
.colorScheme
: The color scheme of the checkbox. Defaults toblue
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)
Customizing the default theme#
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylelabel: {fontFamily: 'mono', // change the font family of the label},control: {padding: 3, // change the padding of the controlborderRadius: 0, // change the border radius of the control},})export const checkboxTheme = defineMultiStyleConfig({ baseStyle })
After customizing the checkbox theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { checkboxTheme } from './components/checkbox.ts'export const theme = extendTheme({components: { Checkbox: checkboxTheme },})
This is a crucial step to make sure that any changes that we make to the checkbox theme are applied.
Adding a custom size#
Let's assume we want to include an extra large checkbox size. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const sizes = {xl: definePartsStyle({control: defineStyle({boxSize: 14}),label: defineStyle({fontSize: "2xl",marginLeft: 6})}),}export const checkboxTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Checkbox size="xl">...</Checkbox>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom circular variant. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(checkboxAnatomy.keys)const circular = definePartsStyle({control: defineStyle({rounded: "full"})})export const checkboxTheme = defineMultiStyleConfig({variants: { circular },})// Now we can use the new `circular` variant<Checkbox variant="circular">...</Checkbox>
Changing the default properties#
Let's assume we want to change the default size and variant of every checkbox in our app. Here's how we can do that:
import { checkboxAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(checkboxAnatomy.keys,)export const checkboxTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'circular',},})// This saves you time, instead of manually setting the size and variant every time you use a checkbox:<Checkbox size="xl" variant="circular">...</Checkbox>
Showcase#
import { Box, SimpleGrid, IconButton, Checkbox, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative"> <SimpleGrid gap={12} p={12} columns={2}> <Checkbox>Themed Checkbox</Checkbox> <Checkbox size="xl">Themed XL Checkbox</Checkbox> <Checkbox variant="circular">Themed Circular Checkbox</Checkbox> <Checkbox variant="circular" size="xl">Themed XL Circular Checkbox</Checkbox> <Checkbox variant="circular" size="xl" colorScheme="red">Themed Red XL Circular Checkbox</Checkbox> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }