Skip to main content

Checkbox

Selects options that take effect when a form is submitted.

import { Checkbox } from "@/fragments/ui";Source

Preview

<StatefulCheckbox label="Accept terms and conditions" />

Installation

npx @usefragments/cli add checkbox

Writes src/fragments/ui/components/Checkbox plus what it imports, the globals stylesheet, and .fragments/registry-lock.json, then prints the packages to add. No account, no key.

Your own registry: publish from Fragments Cloud and add --registry org/name. Connect a workspace.

Examples

With Helper Text

Second line spells out what opting in does.

<StatefulCheckbox
  label="Email notifications"
  helperText="Receive email updates about your account activity"
/>

Checked

Starts on.

<StatefulCheckbox defaultChecked label="Subscribe to newsletter" />

Indeterminate

A dash on the parent means only some children are on.

<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
  <Checkbox indeterminate label="Select all" />
  <div style={{ marginLeft: '24px', display: 'flex', flexDirection: 'column', gap: '8px' }}>
    <StatefulCheckbox defaultChecked label="Option 1" />
    <StatefulCheckbox label="Option 2" />
    <StatefulCheckbox defaultChecked label="Option 3" />

Sizes

Three box sizes: sm, md, lg.

<div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
  <StatefulCheckbox size="sm" label="Small checkbox" />
  <StatefulCheckbox size="md" label="Medium checkbox" />
  <StatefulCheckbox size="lg" label="Large checkbox" />
</div>

Disabled

Locked in both positions, on and off.

<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
  <Checkbox disabled label="Disabled unchecked" />
  <Checkbox disabled checked label="Disabled checked" />
</div>

Custom Styling Targets

controlClassName styles the box, contentClassName the text.

<Checkbox
  label="Styled checkbox"
  helperText="Control and content wrappers can be targeted independently"
  controlClassName="demo-checkbox-control"
  contentClassName="demo-checkbox-content"
/>

API

Component props
Prop
Type
Default
Description
checkedbooleanNot setControlled checked state
defaultCheckedbooleanNot setDefault checked state (uncontrolled)
onCheckedChangefunctionNot setCalled when checked state changes
onChangefunctionNot setAlias for onCheckedChange: (checked: boolean) => void
indeterminatebooleanfalseIndeterminate state (partial selection)
disabledbooleanfalseDisable the checkbox
requiredbooleanfalseWhether the checkbox is required
sizeenumsmmdlg"md"Checkbox size
variantenumoutlineNot setChrome. Omit for the inline checkbox beside its label; 'outline' renders a full-width bordered surface with the checkbox tucked inside.
labelstringNot setLabel text
helperTextstringNot setHelper text shown below the label (preferred)
descriptionstringNot setDeprecated alias for helperText
namestringNot setName attribute for form submission
valuestringNot setValue attribute for form submission
idstringNot setID for the checkbox input
controlClassNamestringNot setClass name for the checkbox control element (stable styling target)
contentClassNamestringNot setClass name for the label/description content wrapper
aria-labelstringNot setAccessible label for icon-only mode
aria-labelledbystringNot setAccessible labelled-by relationship for icon-only mode
aria-describedbystringNot setAccessible described-by relationship
formstringNot setID of the form that owns the hidden input
inputRefunionNot setRef to the hidden input element
parentbooleanNot setWhether this checkbox controls child checkboxes in a checkbox group
readOnlybooleanfalseWhether the checkbox cannot be changed by the user
uncheckedValuestringNot setValue submitted when unchecked

Accessibility

  • Label is tied to the box
  • Space toggles it
  • Focus ring stays visible
  • Indeterminate is announced as mixed

Guidance

Use when

  • Choices that apply only once the form is submitted
  • Picking several items from a list
  • Accepting terms
  • Filter and preference lists

Avoid when

  • Settings that apply instantly (use Switch)
  • One choice from a few (use RadioGroup)
  • Many options (use Select)
  • SwitchalternativeUse Switch for immediate-effect settings
  • InputsiblingCheckbox handles boolean; Input handles text

Next steps