Checkbox

A checkbox with a mixed state, a label and form field wiring.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/checkbox

Usage

import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"
const Message = UiSchema.TaggedEnum({ ToggledArchive: { checked: Schema.Boolean } })export const ArchiveDone = (h: Html.H<typeof Message.Type>, archive: boolean) =>	Checkbox(h, {		checked: archive,		label: "Archive finished issues",		onToggle: (checked) => Message.ToggledArchive({ checked }),	})

Examples

In a form

Inside a FormField, the label, description and error are wired to the box. Submit without ticking it to see the error.

You can read them any time in Settings.

Read-only and disabled

Read-only stays in the Tab order and is announced as read-only, for a setting someone else controls. Disabled leaves the Tab order.

API reference

Options

Checkbox options
NameTypeDefaultDescription
keystring—Names the checkbox among others in the same view; needed for more than one
checkedrequiredboolean—Whether it's ticked. The caller keeps it and passes back what onToggle asks for.
indeterminateboolean—Some of what it stands for is ticked: shown as a dash, announced as mixed
disabledboolean—Can't be pressed or focused, and is skipped when its form submits.
readOnlyboolean—Shows the value and stays focusable, but a press doesn't change it: a setting only an admin can change.
requiredboolean—Must be ticked before its form submits.
namestring—The form field's name: a ticked checkbox submits its value under it
valuestringonWhat a ticked box submits under name; on by default.
labelstring—The visible label beside the box; leave it out inside a FormField
ariaLabelstring—The accessible name when there's no visible label (a row's selection box)
onTogglerequired(checked: boolean) => Message—What a press asks for: the value to store and pass back as checked
fieldFieldControlAttrs—A FormField's attributes: its label names the box, its description and error describe it
styleStyle—StyleX styles applied after the checkbox’s own.

Accessibility

  • A button with role="checkbox": Tab reaches it and Space toggles it. A mixed box is announced as mixed.
  • The visible label names it; without one, give it an ariaLabel. Inside a field, the field's label, hint and error are tied to it.
  • A disabled checkbox can't be focused and is skipped when its form submits.

Source