Checkbox
Lets the user turn an option on or off, or pick several options from a list.
Usage
import { Checkbox } from '@leverui/components';
<Checkbox.Root>
<Checkbox.HiddenInput />
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
</Checkbox.Root>
For one ready-made component, see the snippet below.
Shortcuts
CheckboxControl
Checkbox.Control renders a Checkbox.Indicator by default, so these two are
the same.
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Control />
Examples
Variants
Pass the variant prop to the Checkbox.Root component to change the visual style of the checkbox.
outline
subtle
solid
Colors
Pass the colorPalette prop to the Checkbox.Root component to change the color of the checkbox.
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
Sizes
Pass the size prop to the Checkbox.Root component to change the size of the checkbox.
States
Pass the disabled or invalid prop to the Checkbox.Root component to change the visual state of the checkbox.
Controlled
Use the checked and onCheckedChange props to control the state of the checkbox.
Label Position
Here's an example of how to change the label position to the right.
Store
An alternative way to control the checkbox is to use the RootProvider component and the useCheckbox store hook.
Composition
Here's an example of how to compose a checkbox with a field component.
Hook Form
Here's an example of how to use the Checkbox component with form state held in a Solid signal.
Group
Use the CheckboxGroup component to group multiple checkboxes together.
Group Hook Form
Here's an example of how to use the CheckboxGroup component with form state held in a Solid signal.
Custom Icon
Render a custom icon within Checkbox.Control to change the icon of the checkbox.
Indeterminate
Set the checked prop to indeterminate to show the checkbox in an indeterminate state.
Description
Here's an example of how to add some further description to the checkbox.
Link
Render an anchor tag within the Checkbox.Label to add a link to the label.
Closed Component
import { Show, omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { Checkbox, type CheckboxRootProps } from '@leverui/components';
export type ClosedCheckboxProps = CheckboxRootProps & {
icon?: JSX.Element;
};
export function ClosedCheckbox(props: ClosedCheckboxProps) {
const root = omit(props, 'icon', 'children');
return (
<Checkbox.Root {...root}>
<Checkbox.HiddenInput />
<Checkbox.Control>{props.icon ?? <Checkbox.Indicator />}</Checkbox.Control>
<Show when={props.children}>
<Checkbox.Label>{props.children}</Checkbox.Label>
</Show>
</Checkbox.Root>
);
}
<ClosedCheckbox>Accept terms and conditions</ClosedCheckbox>
Guides
CheckboxGroup + Field vs Fieldset
- Single Checkbox: wrap in
Field.Rootfor a label and helper text. - CheckboxGroup: wrap in
Fieldset.Rootwith a legend, notField.Root.
Correct usage:
<Fieldset.Root>
<CheckboxGroup name="framework">
<Fieldset.Legend>Select framework</Fieldset.Legend>
{/* ... checkboxes ... */}
</CheckboxGroup>
</Fieldset.Root>
Incorrect usage:
// Don't wrap CheckboxGroup with Field.Root
<Field.Root>
<CheckboxGroup>{/* ... checkboxes ... */}</CheckboxGroup>
</Field.Root>
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
value | on | string | Value sent with the form. |
colorPalette | gray | string | Color palette, built-in or your own. |
size | md | 'xs' | 'sm' | 'md' | 'lg' | Checkbox size. |
variant | solid | 'outline' | 'solid' | 'subtle' | Checkbox style. |
as | label | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the checkbox's props. |
unstyled | false | boolean | Turn off the Lever UI styles. |
checked | — | CheckedState | Checked state, when you control it. |
defaultChecked | — | CheckedState | Checked state on first render. |
disabled | — | boolean | Disable the checkbox. |
form | — | string | ID of the form the checkbox belongs to. |
id | — | string | Unique ID for the checkbox. |
ids | — | Element ID map | Your own IDs in place of the generated ones. |
invalid | — | boolean | Mark the checkbox as invalid. |
name | — | string | Name submitted with the form. |
onCheckedChange | — | (details) => void | Called when the checked state changes. |
readOnly | — | boolean | Make the checkbox read-only. |
required | — | boolean | Mark the checkbox as required. |
Keep HiddenInput in every Checkbox. It handles focus, validation, the
accessible label, and native form submission.
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts