Fragiola

API: components

Checkbox

A two-state or mixed choice the user toggles, with native form support, following the APG's checkbox pattern, in each framework.

A choice the user checks or clears (WAI-ARIA APG checkbox pattern, the mixed state included). It works in a native <form>: it submits, resets and validates as an <input type="checkbox"> would. Several checkboxes sharing one array value, with a parent that checks or clears them all, are a CheckboxGroup, shown in the example.

Pizza toppings
Gallery

Behaviour

  • Toggle: a click on the Root or on its <label>, or Space; Enter does nothing (the APG, and a native checkbox).
  • Mixed: indeterminate shows the mixed state (aria-checked="mixed"); a toggle from it checks. The app sets it, or the group sets it on its parent checkbox.
  • Forms: a visually hidden checkbox input right after the Root submits name=value while checked (value defaults to "on"), nothing while not. It is never in the accessibility tree nor the tab order. required blocks the submission while unchecked and focuses the Root; a form reset returns an uncontrolled checkbox to its default.
  • In a Field: the Root takes the field's ids, name, disabled and states.

Anatomy

partelementdata-primitives-partrole
Rootbuttoncheckbox-rootthe checkbox; holds its state
Indicatorspancheckbox-indicatorthe check or the dash; rendered only while checked or mixed
(hidden input)input type="checkbox"checkbox-inputwhat the form submits

Usage

import { Checkbox } from "@fragiola/primitives-react/checkbox";

<label>
    <Checkbox.Root name="terms" required onCheckedChange={(checked) => console.log(checked)}>
        <Checkbox.Indicator>✓</Checkbox.Indicator>
    </Checkbox.Root>
    Accept terms
</label>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:checked, @checked-change), and Angular as inputs and outputs ([(checked)], (checkedChangeDetails)).

Root

proptypedefaultdescription
checkedbooleanthe checked state, controlled
defaultCheckedbooleanfalsethe initial state, uncontrolled
onCheckedChange(checked: boolean, details: { reason }) => voidevery change (reason none)
indeterminatebooleanfalseshows the mixed state
disabledbooleanfalseignores clicks and keys
requiredbooleanfalsethe form cannot submit while unchecked
namestringthe form name (a field's wins)
valuestring"on"the submitted value; in a CheckboxGroup, the checkbox's value in the group's array
parentbooleanfalsein a CheckboxGroup, the parent checkbox that checks or clears every value
id (react, vue)stringgeneratedthe button's id; Angular takes it as a plain attribute

Indicator

proptypedefaultdescription
keepMountedbooleanfalserendered in every state (Angular: *checkboxIndicator="true")

State and part attributes

partattributes
Rootrole="checkbox", type="button", aria-checked ("true", "false", "mixed"), aria-required, disabled, data-checked / data-unchecked / data-indeterminate (exactly one), data-disabled, data-required; a parent checkbox has aria-controls (its children's ids); in a Field, the field's states
Indicatoraria-hidden="true", data-checked / data-unchecked / data-indeterminate, data-disabled
hidden inputtype="checkbox", name, value, checked, required, disabled, aria-hidden="true", tabindex="-1", visually hidden

Keyboard

keyaction
Spacetoggles
Enternothing
Tabmoves on; every checkbox is a tab stop

Exports

entryexports
@fragiola/primitives-react/checkboxChangeDetails, ChangeReason, Checkbox, CheckboxIndicatorProps, CheckboxRootProps, CheckboxState, RenderProp
@fragiola/primitives-vue/checkboxChangeDetails, ChangeReason, Checkbox, CheckboxIndicatorProps, CheckboxRootEmits, CheckboxRootProps, CheckboxState, RenderSlotProps
@fragiola/primitives-angular/checkboxChangeDetails, ChangeReason, CheckboxIndicator, CheckboxRoot, CheckboxState