Skip to content
TopClass UI

Checkbox

Checked, unchecked or mixed. A hidden input sends the default value before the page hydrates, and a form reset brings it back.

client - size not measured yet

Checkbox

client - size not measured yet

Checked, unchecked or mixed. A hidden input sends the default value before the page hydrates, and a form reset brings it back.

Install

Try it

size
invalid

The code

checkbox.tsx
import { Checkbox } from 'topclassui/checkbox'

<div className="flex items-center gap-2">
  <Checkbox id="checkbox-playground" defaultChecked />
  <label htmlFor="checkbox-playground" className="text-sm text-body">I accept the terms</label>
</div>

The box is a native <button role="checkbox">, and a hidden checkbox beside it is what the form sends.

Every state

9 states

Import from the package

topclassui/checkbox
import { Checkbox } from 'topclassui/checkbox'

Install once:npm i topclassui@beta zod

Examples

  • For a choice the form sends later. For a setting that takes effect at once, a Switch reads better.
  • Unticked sends nothing and ticked sends on, before the page hydrates and after. An unchecked value can make an unticked box send a string of its own.
  • A mixed box draws a dash and sets aria-checked to mixed; it is yours to clear, and it sends nothing while it is mixed.
  • Checkbox draws no label of its own: write the label beside it, or around it, and give the box an id.
  • In Ocean the xs and sm boxes are circles: only the tick or the dash tells them from a radio.
basic
labelled
form
sizes
In Arabic, right to left

Props

The props of Checkbox
PropTypeDefaultLimitsDescription
aria-labelstring--The name read out when no visible label names the control.
aria-labelledbystring--The id of the visible text that names the control.
idstring--The id a <label htmlFor> points at.
classNamestring--Merged last, so a consumer class wins a conflict with the checkbox's own.
invalidbooleanfalse-Marks the value as wrong: sets aria-invalid, and the line of the box turns to danger and thickens.
refRef<HTMLButtonElement>--The root element, which is a <button>.
sizexs | sm | md | lg | xl | custom"md"-The toggle scale of the drawn box and its mark; custom emits no size classes.

Keyboard and accessibility

The keyboard of Checkbox
KeysWhat it does
TabMoves the focus to the box; a disabled box is skipped.
SpaceTicks or unticks the box.
EnterSubmits the form the box is in, as a native checkbox does; it does not tick it.
  • A name is required: id for a <label htmlFor>, or aria-label, or aria-labelledby. A click on the label presses the box.
  • The target is 24px, 44px under a touch pointer, whatever size draws: the focus ring is around the box.
  • invalid sets aria-invalid, and the line of the box turns to danger and thickens, ticked or not.

Right to left

The box sits at the right, before its label, with no change to your code: the order is logical, not physical.

Server and client

A client component: it renders on the server and hydrates in the browser.

Files: index.tsx, client.tsx.

On the chip: client - size not measured yet.