Skip to content
EspañolEnglish
All components (204)

Checkbox

Choice · Checkbox · .amset-checkbox

What it is and when to use it

A checkbox: yes or no on one thing, or ticking several from a list. It is pressed anywhere on the label, not just on the square.

When to use it

  • Accepting something that is sent with a button: "I accept the terms of sale". The change counts when Submit is pressed, not before.
  • Ticking several options from a short list. With indeterminate, the "Select all" checkbox when only some are ticked.
  • In a table row, with no text next to it: the name then goes in aria-label. The types do not allow a mute checkbox to be built.

When not to

  • A setting that is applied right away, without Save: Switch.
  • One option among several that exclude each other: RadioGroup, or SegmentedGroup if the change is seen instantly.
  • The facets of a catalog, with a counter and "Show the remaining N": FilterGroup.

Keyboard: Tab reaches it; Space ticks and unticks it. When disabled it leaves the Tab order.

Accessibility: it is a real <input type="checkbox">, hidden under the square that is seen, and its name is the text. With name and value it travels in a native form. The description is read as a description, after the name: it is not part of it. The indeterminate state is announced as "partially checked", not only with the dash. The tick and the dash in the square are decorative.

Sizes: 14, 16 or 20 px box, centered on the first line of the text. With a finger, the row reaches 44 px; on its own (in a table), the pressable area grows outwards up to 24 px with a mouse and 44 with a finger, without moving anything.

States: hover, pressed (the box gives in a little), keyboard focus, checked or half-checked (the tick grows in), invalid, read-only (dashed border and no accent fill) and disabled.

Fine-tuning: variables --amset-checkbox-box, -radius, -font, -description, -border, -background, -checked (the fill when ticked) and -brand (the color of the tick).

What it looks like

<Checkbox defaultSelected>I accept the terms</Checkbox>
<Checkbox description="You can unsubscribe whenever you want">Email alerts</Checkbox>

Props

Import it with import { Checkbox } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element that provides a detailed, extended description for the object.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
autoFocusboolean—
childrenReactNode—
classNamestringMerged with the library classes; it does not replace them.
defaultSelectedbooleanChecked at the start, governed internally.
descriptionstringSecondary text under the label. The screen reader reads it as a description, after the name: it is not part of it. It is only painted if the checkbox has a visible label.
disabledboolean—
excludeFromTabOrderbooleanTakes it out of the tab order without removing focus programmatically.
formstringThe `id` of the `<form>` it belongs to, if it is not wrapped by it.
idstringGoes to the `<input>`, which is the focusable node.
indeterminatebooleanThe third face: neither checked nor unchecked. It is presentation only and does not change when pressed; it is for the checkbox that governs a half-selected list.
inputRefRef<HTMLInputElement>To reach the real `<input>`.
invalidboolean—
namestringThe name it travels under in the form.
onBlur(event: FocusEvent<HTMLInputElement>) => void—
onChange(selected: boolean) => void—
onChangeEvent(event: ChangeEvent<HTMLInputElement>) => voidLike `onChange`, but with the native event of the `<input>`: `e.target.checked`... It exists to migrate a front end that already writes its handlers with the event (AMS-735). If you only want to know whether it is checked, `onChange` already gives you that.
onFocus(event: FocusEvent<HTMLInputElement>) => void—
onPress(event: ChoicePressEvent) => voidCalled when it is pressed, saying what it was pressed with.
readOnlybooleanIt is read and focused, but does not change.
requiredboolean—
selectedbooleanChecked, governed from outside.
size"sm" | "md" | "lg"Size of the box and the text: box of 14, 16 or 20 px. With a finger, the row reaches 44 px.
validationBehaviorValidationBehavior`native` by default, as until now: it sets the browser's `required`.
valuestringIts key within a group of checkboxes, and the value it travels with in the form when checked.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

CSS variables

These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.

VariableDefault
--amset-checkbox-box1rem
--amset-checkbox-radiusvar(--amset-radius-sm)
--amset-checkbox-fontvar(--amset-text-base)
--amset-checkbox-descriptionvar(--amset-text-sm)
--amset-checkbox-bordervar(--amset-border-bold)
--amset-checkbox-backgroundvar(--amset-surface)
--amset-checkbox-checkedvar(--amset-accent)
--amset-checkbox-brandvar(--amset-accent-ink)
--amset-checkbox-linecalc(var(--amset-checkbox-font) * var(--amset-line-tight))
--amset-checkbox-target1.5rem

How to change it

.amset-checkbox {
  --amset-checkbox-box: ...;
  --amset-checkbox-radius: ...;
}

In the test bench

Every state and variant, to try one at a time, on its page at ui.amset.tech: