Skip to content
Slotted

Checkbox

Turns one option on or off, around a native checkbox that keeps the value, the name and the platform's keyboard.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Checkbox, CheckboxDefaultsProvider } from '@slotted/react';

Sizes

size sets the box, the label and the gap to one step of the control scale: sm for dense tables and toolbars, md for most forms, lg for sparse forms. Every step keeps a 24-pixel target around the box. Inside a checkbox group, a field or a field group, the box takes their size when it states none of its own.

Tiles

variant="tile" frames the choice and makes the whole surface the target, so a press anywhere on the tile toggles it. A chosen tile draws an accent edge as well as a tint, and the edge stays visible in the high-contrast scheme. Lay a row of tiles out with Grid.

One copy a day, kept for 30 days.

Who changed what, and when, exported as CSV.

Available on the Scale plan.

Label placement

labelPlacement="start" puts the box after its label, at the end of the row. A settings list uses it so the boxes line up on one edge whatever the length of each label.

States

readOnly keeps the value, the focus ring and the tab stop, and refuses every change; a read-only value is still submitted. disabled sets the native disabled, which takes the box out of the tab order and out of form submission. invalid sets aria-invalid and draws a thicker border in the danger tone. All three default to the checkbox group or field around the box.

Set by the customer's tax certificate.

Not offered from the Seattle warehouse.

Confirm the count before closing the cycle.

Description

description writes help text under the label and joins the control’s accessible description, so a screen reader hears it after the name. It sits outside the label: pressing the description does not toggle the box.

One message per failed payment, sent to the billing contact.

Sent at 08:00 in the account's time zone, and skipped on days with no failures.

Long labels

A label that wraps keeps the box on its first line, and an unbroken string such as a URL breaks inside the column instead of widening the page.

Notices go to the billing contact on the account.

Mixed state

indeterminate draws a dash and exposes the mixed state to assistive technology. It is a state, not a third value: the submitted value is still on or off, and the next press checks the box. Use it when one box stands for several records that disagree, as in a bulk edit.

Edit 3 products

Taxed on 2 of the 3 selected products.

Parent checkbox

A parent box inside a checkbox group with allValues checks and unchecks the whole set. It is checked when every value is selected, mixed when some are, and unchecked when none are; you write no state for it.

Delivery days

Controlled

Pass checked to own the state and update it from onCheckedChange in React or (checkedChange) in Angular. The request carries the new value and its reason, pointer or keyboard, and cancel() refuses the change before the box moves.

Ships to 12 Orchestra Terrace, Walla Walla, WA 99362.

Forms

A box with name submits its value when checked and nothing when unchecked, like a native checkbox. defaultChecked is the state a native form reset returns to. Change the boxes, save, then reset.

Reports emailed each morning

Required

Inside a field, the box takes the field’s required as aria-required, its invalid as aria-invalid, and its error as part of the accessible description. Validate on submit, then set invalid on the field and write the error. The Angular example binds the box to a reactive form control with Validators.requiredTrue. Submit without accepting, then accept and submit again.

Error:

Without a visible label

In a row whose other cells already say what the box selects, name the box with visually hidden text in its label. The hover wash then closes around the box alone, and the box keeps its 24-pixel target.

INV-1042Alfreds Futterkiste$1,250.00
INV-1043Ernst Handel$4,318.40
INV-1044Island Trading$872.15

Defaults for a section

CheckboxDefaultsProvider in React and provideCheckboxDefaults in Angular set size, variant and labelPlacement once for every box under them. A box that states a value of its own keeps it.

Accessibility

KeyEffectFocus
Spacetoggles the checkboxstays on the control
  • Checkbox follows the WAI-ARIA Authoring Practices checkbox pattern (w3.org/WAI/ARIA/apg/patterns/checkbox/), and the native input supplies every part of it: the role, the checked and mixed states, Space to toggle, Enter left to the form, and disabled behavior. Checkbox adds no key listener, so the keyboard behavior is the platform’s in every language and in right-to-left documents.
  • The label names the control through for, which points at an identifier Checkbox generates when you pass no id. The description sits outside the label and reaches the control through aria-describedby, so it is read as a description, and a press on it toggles nothing.
  • Inside a required field the control carries aria-required, never the native required, because the native attribute turns on browser constraint validation and changes how the form submits. Pass required on the box to opt into the platform’s validation; Checkbox leaves it unchanged. disabled is the native attribute.
  • A native checkbox has no read-only state, so Checkbox enforces one: it refuses the click that a pointer and Space both produce, and aria-readonly announces the state. The box keeps its tab stop, its focus ring and its full contrast, because a read-only value is real and must not look disabled.
  • The invalid state does not rely on color alone. In the high-contrast scheme the theme paints every border the same color, so an invalid box also draws a thicker border. The message that says what is wrong belongs to the field or field group.
  • The target is larger than the box. A 24-pixel square around the box meets WCAG 2.2 success criterion 2.5.8 at every size and density, including size="sm", where the box is 14 pixels. The label extends the target to the end of its text, and the hover wash covers exactly that area. The target is drawn around the box, so the row keeps its line height.
  • The mark stays visible under forced colors. It is a mask over a background color, and forced colors replace both, so Checkbox restates the pair in system colors.

API

Checkbox

Props

checkedboolean
Makes the state controlled for the life of the component. Without it the input keeps its own state, and a native form reset restores defaultChecked. Inside a checkbox group, the group’s selection decides instead.
defaultCheckedbooleanDefault false
The state an uncontrolled box starts from. It is read once and written to the input as the native attribute, which is what a form reset returns to.
indeterminatebooleanDefault false
The mixed state, drawn as a dash and announced as mixed. It is a state, not a third value of checked: the submitted value stays boolean, because indeterminate is a DOM property that leaves checked unchanged.
parentbooleanDefault false
Makes the box command the set its checkbox group lists in allValues. It is checked when every value is selected and mixed when some are, and a press selects or clears the whole set. Outside a group with allValues it has no effect.
size'sm' | 'md' | 'lg'Default md
On Checkbox, CheckboxDefaultsProviderThe step of the control scale. When unset, the box takes the size of its checkbox group, then of its field or field group, then of the provider. The theme restates the scale per density.
variant'plain' | 'tile' | 'control'Default plain
On Checkbox, CheckboxDefaultsProviderHow the choice is drawn. tile frames it and makes the whole surface the target; the label still names the control, so the keyboard and the accessible name are unchanged. control is for a row of fields and buttons: the root is one control tall and holds the hover halo inside its own box, where the plain halo stands outside the root and is shorter than a control. When unset, the box takes the variant of its checkbox group, then of the provider.
labelPlacement'end' | 'start'Default end
On Checkbox, CheckboxDefaultsProviderWhich side of its label the box sits on. When unset, the box takes the placement of its checkbox group, then of the provider. It is never read from a field, whose labelPlacement places the field’s own label.
disabledbooleanDefault from the group or field, or false
Unset rather than false, so the box follows its checkbox group or field. Written as the native attribute, which removes the box from the tab order and from form submission.
readOnlybooleanDefault from the group or field, or false
Unset rather than false. A native checkbox has no readonly, so Checkbox refuses every change and writes aria-readonly. The box keeps its tab stop, its focus ring and its contrast, and its value is still submitted.
invalidbooleanDefault from the group or field, or false
Unset rather than false. Writes aria-invalid on the input and data-invalid on the root, which draws a thicker border in the danger tone.
requiredboolean
The native attribute, passed through unchanged; Checkbox never writes it. A field marks necessity with aria-required instead, because the native attribute turns on browser constraint validation.
descriptionReactNode
Help text under the label, referenced by aria-describedby. It sits outside the label, so it describes the control instead of naming it.
name / valuestringDefault — / on
The native submission pair: a checked box submits value under name. Inside a checkbox group, a box without a name takes the group’s, and value is the entry the group’s selection holds.

Events

NamePayloadWhen
onCheckedChange(checked: boolean, request: ChangeRequest<boolean>)Before the box moves. request.value is the new state and request.previous the state it replaces. request.reason is pointer or keyboard, read from the click’s detail, and request.source is the originating event. request.cancel() refuses the change before anything commits, including the platform’s own toggle.
onChange, onClick, onFocus, onBlurthe element’s own eventsWhenever the input fires them. Checkbox composes your handler with its own instead of replacing it: yours runs first, and always runs. Calling preventDefault() in onClick stops the change as well.

Composition

render(props: CheckboxRootProps) => ReactNode
Replaces the root element and receives every attribute it would carry. The props are typed against HTMLAttributes<HTMLElement>, so they can be spread onto a li or a label. The input cannot be replaced, because it holds the value and the platform’s keyboard behavior.
refRef<HTMLInputElement>
Reaches the input. focus() and click() are the element’s own methods.
className / stylestring / CSSProperties
Reach the root. Every other native prop reaches the input.
CheckboxDefaultsProvider{ size, variant, labelPlacement }
On a subtreeSets size, variant and labelPlacement once for every box under it, merged over an outer provider. A box that states a value keeps it. A checkbox group around the box outranks the provider for all three, and a field or field group outranks it for size.
useField()() => FieldContextValue | undefined
On inside a FieldWhat the box reads from a field: its disabled, readOnly, invalid and required states, its description and error, and its size. The box never takes the field’s identifier: it has its own label, and the field’s label would name the same control twice.

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe two-column grid: the box in one column, the label and the description in the other. It carries data-ui="checkbox", data-size, data-variant and data-label-placement, and data-checked, data-indeterminate, data-disabled, data-invalid and data-readonly. It has no role, so the native control is the only element announced. className and style reach this element.
controlinput type="checkbox"YesThe native checkbox. It holds the value, the name and the identifier, and receives every native attribute you write: form, required, autoFocus, every aria-* attribute and every handler. It draws the box itself with appearance: none. ref reaches it, so focus() and click() are the element’s own.
indicatorspanYesThe mark, drawn over the control as a mask from the glyph scale: a check when checked, a dash when mixed. It is a sibling element rather than a pseudo-element, because Firefox renders neither ::before nor ::after on an input. It takes no pointer events, so every press reaches the control underneath.
labellabelNoNames the control through for. It carries the target and the hover wash: a 24-pixel square around the box, and a wash behind the box and the text that extends half the control gap past the box and half as far again past the text. It is as wide as its text, so the empty space beside it does not toggle the box. It renders without children too, for a box named by aria-label, and the wash then closes around the box. When its only content is visually hidden text, the root carries data-label-hidden and the wash closes the same way.
descriptionpNoHelp text under the label, aligned to the label rather than to the box, and referenced by aria-describedby. It sits outside the label: inside, it would join the accessible name, and a press on it would toggle the value.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every checkbox inside it.

Checkbox tokens

TokenDefault
--slotted-checkbox-active-backgroundvar(--slotted-tone-accent-subtle-active)
--slotted-checkbox-backgroundvar(--slotted-surface, Canvas)
--slotted-checkbox-border-colorvar(--slotted-border-strong, ButtonBorder)
--slotted-checkbox-border-widthvar(--slotted-border-width-medium, 1.5px)
--slotted-checkbox-checked-active-backgroundvar(--slotted-tone-accent-solid-active)
--slotted-checkbox-checked-backgroundvar(--slotted-tone-accent-solid)
--slotted-checkbox-checked-border-colorvar(--slotted-tone-accent-solid)
--slotted-checkbox-checked-hover-backgroundvar(--slotted-tone-accent-solid-hover)
--slotted-checkbox-colorvar(--slotted-text, CanvasText)
--slotted-checkbox-description-colorvar(--slotted-text-muted, GrayText)
--slotted-checkbox-description-font-sizevar(--slotted-font-size-xs, 0.75rem)
--slotted-checkbox-disabled-backgroundvar(--slotted-disabled-background)
--slotted-checkbox-disabled-border-colorvar(--slotted-disabled-border)
--slotted-checkbox-disabled-colorvar(--slotted-disabled-foreground)
--slotted-checkbox-disabled-mark-colorvar(--slotted-disabled-foreground)
--slotted-checkbox-halo-colorvar(--slotted-tone-accent-text)
--slotted-checkbox-hover-border-colorvar(--slotted-tone-accent-border)
--slotted-checkbox-invalid-border-colorvar(--slotted-tone-danger-border)
--slotted-checkbox-invalid-checked-backgroundvar(--slotted-tone-danger-solid)
--slotted-checkbox-invalid-edge-widthvar(--slotted-control-invalid-edge-width, 1px)
--slotted-checkbox-invalid-focus-ring-colorvar(--slotted-focus-ring-color-invalid, Highlight)
--slotted-checkbox-line-heightvar(--slotted-line-height-md, 1.5)
--slotted-checkbox-mark-colorvar(--slotted-tone-accent-on-solid)
--slotted-checkbox-mark-inset2px
--slotted-checkbox-radiusvar(--slotted-radius-xs, 2px)
--slotted-checkbox-readonly-background/** * The ground `textarea` already gives a read-only value, and not the * sunken surface: at the `light` scheme the sunken step is #f8fafd * against a white surface, a difference a reader cannot see, so the state * existed in the accessibility tree and nowhere on screen. */ var(--slotted-tone-secondary-subtle-hover)
--slotted-checkbox-sizevar(--slotted-selection-size-md, 16px)
--slotted-checkbox-tile-active-backgroundvar(--slotted-tone-accent-subtle-active)
--slotted-checkbox-tile-backgroundvar(--slotted-surface, Canvas)
--slotted-checkbox-tile-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-checkbox-tile-checked-backgroundvar(--slotted-tone-accent-surface)
--slotted-checkbox-tile-checked-border-colorvar(--slotted-tone-accent-solid)
--slotted-checkbox-tile-checked-hover-border-colorvar(--slotted-tone-accent-solid-hover)
--slotted-checkbox-tile-description-gapvar(--slotted-space-2, 0.25rem)
--slotted-checkbox-tile-disabled-backgroundvar(--slotted-disabled-background)
--slotted-checkbox-tile-disabled-border-colorvar(--slotted-disabled-border)
--slotted-checkbox-tile-hover-backgroundvar(--slotted-tone-accent-subtle-hover)
--slotted-checkbox-tile-hover-border-colorvar(--slotted-tone-accent-border)
--slotted-checkbox-tile-insetvar(--slotted-control-padding-inline-sm, 10px)
--slotted-checkbox-tile-invalid-border-colorvar(--slotted-tone-danger-border)
--slotted-checkbox-tile-invalid-checked-backgroundvar(--slotted-tone-danger-surface)
--slotted-checkbox-tile-invalid-checked-border-colorvar(--slotted-tone-danger-solid)
--slotted-checkbox-tile-radiusvar(--slotted-radius-md, 6px)
--slotted-checkbox-tile-readonly-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-checkbox-tile-readonly-border-colorvar(--slotted-border-strong)

Theme tokens it reads

TokenDefault
--slotted-border-strongSet per scheme by the theme
--slotted-border-width-medium1.5px
--slotted-border-width-thick2px
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-invalid-edge-widthvar(--slotted-border-width-thin)
--slotted-control-label-gap-lgcomfortable 12px · compact 8px · dense 8px
--slotted-control-label-gap-mdcomfortable 12px · compact 8px · dense 6px
--slotted-control-label-gap-smcomfortable 8px · compact 6px · dense 6px
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-shadowSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-medium200ms
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-color-invalidSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-size-xs0.75rem
--slotted-line-height-md1.5
--slotted-line-height-xs1.4
--slotted-radius-md6px
--slotted-radius-xs2px
--slotted-selection-halo-opacity0.14
--slotted-selection-size-lg18px
--slotted-selection-size-md16px
--slotted-selection-size-sm14px
--slotted-space-20.25rem
--slotted-space-30.375rem
--slotted-surfaceSet per scheme by the theme
--slotted-target-min24px
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-on-solidSet per scheme by the theme
--slotted-tone-accent-solidSet per scheme by the theme
--slotted-tone-accent-solid-activeSet per scheme by the theme
--slotted-tone-accent-solid-hoverSet per scheme by the theme
--slotted-tone-accent-subtle-activeSet per scheme by the theme
--slotted-tone-accent-subtle-hoverSet per scheme by the theme
--slotted-tone-accent-surfaceSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-danger-solidSet per scheme by the theme
--slotted-tone-danger-surfaceSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme