Checkbox

Lets the user turn an option on or off, or pick several options from a list.

Usage

Code
import { Checkbox } from '@leverui/components';
Code
<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.

Code
<Checkbox.Control>
  <Checkbox.Indicator />
</Checkbox.Control>
Code
<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.

Checked: false

Group

Use the CheckboxGroup component to group multiple checkboxes together.

Select framework

Group Hook Form

Here's an example of how to use the CheckboxGroup component with form state held in a Solid signal.

Select your framework
Values: []

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.

Render an anchor tag within the Checkbox.Label to add a link to the label.

Closed Component

Code
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>
  );
}
Code
<ClosedCheckbox>Accept terms and conditions</ClosedCheckbox>

Guides

CheckboxGroup + Field vs Fieldset

  • Single Checkbox: wrap in Field.Root for a label and helper text.
  • CheckboxGroup: wrap in Fieldset.Root with a legend, not Field.Root.

Correct usage:

Code
<Fieldset.Root>
  <CheckboxGroup name="framework">
    <Fieldset.Legend>Select framework</Fieldset.Legend>
    {/* ... checkboxes ... */}
  </CheckboxGroup>
</Fieldset.Root>

Incorrect usage:

Code
// Don't wrap CheckboxGroup with Field.Root
<Field.Root>
  <CheckboxGroup>{/* ... checkboxes ... */}</CheckboxGroup>
</Field.Root>

Props

Root

PropDefaultTypeDescription
valueonstringValue sent with the form.
colorPalettegraystringColor palette, built-in or your own.
sizemd'xs' | 'sm' | 'md' | 'lg'Checkbox size.
variantsolid'outline' | 'solid' | 'subtle'Checkbox style.
aslabelNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the checkbox's props.
unstyledfalsebooleanTurn off the Lever UI styles.
checked—CheckedStateChecked state, when you control it.
defaultChecked—CheckedStateChecked state on first render.
disabled—booleanDisable the checkbox.
form—stringID of the form the checkbox belongs to.
id—stringUnique ID for the checkbox.
ids—Element ID mapYour own IDs in place of the generated ones.
invalid—booleanMark the checkbox as invalid.
name—stringName submitted with the form.
onCheckedChange—(details) => voidCalled when the checked state changes.
readOnly—booleanMake the checkbox read-only.
required—booleanMark 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