Component · in flow
Checkbox, radio and switch
Native checkboxes, radio buttons and switches in the kit's own look. The keyboard, forms and use without JavaScript are the browser's.
States
Needed to go on.
Tick this first.
This does not fit.
Not saved.
Layouts
A very long label that has to wrap onto more lines on a narrow screen, with averyveryverylongwordthatdoesnotfitanywhereonitsown
A very long label that has to wrap onto more lines on a narrow screen, with averyveryverylongwordthatdoesnotfitanywhereonitsown
React
One e-mail a day. See an example
Changes:
Sent:
Usage
Astro:
---
import Checkbox from '@subfloor/astro/Checkbox.astro'
import CheckboxGroup from '@subfloor/astro/CheckboxGroup.astro'
import RadioGroup from '@subfloor/astro/RadioGroup.astro'
import Switch from '@subfloor/astro/Switch.astro'
---
<Checkbox id="terms" name="terms" label="I agree to the terms" required>
<Fragment slot="hint">Read <a href="/terms/">the terms</a>.</Fragment>
</Checkbox>
<CheckboxGroup id="days" label="Days" error={errors.days}>
<Checkbox id="mon" name="days" value="mon" label="Monday" />
<Checkbox id="tue" name="days" value="tue" label="Tuesday" />
</CheckboxGroup>
<RadioGroup id="plan" name="plan" label="Plan" value="free" options={[
{ value: 'free', label: 'Free' },
{ value: 'pro', label: 'Pro', hint: 'With support.' },
]} />
<Switch id="alerts" name="alerts" label="Alerts" checked />React:
import { Checkbox, RadioGroup, Switch } from '@subfloor/react'
<Checkbox
label="All"
checked={all}
indeterminate={some && !all}
onCheckedChange={chooseAll}
/>
// In a row of your own: without a visible label, aria-label is required.
<Checkbox aria-label={`All of ${name}`} checked={all} onCheckedChange={chooseAll} />
<RadioGroup label="Plan" value={plan} onValueChange={setPlan} options={plans} />
<Switch label="Alerts" checked={on} onCheckedChange={save}
hint={<>One e-mail a day. <a href="/example">See an example</a></>} />Which one
- Checkbox: something to agree to or include. It counts once the form is sent.
- CheckboxGroup: any number of a few. Browsers cannot require “at least one”: check it yourself and set
error. - RadioGroup: one of a few, all in view, with room for a hint each. For two to five short choices in a row, use Segmented; for many, a Select.
- Switch: a setting that is on or off, often saved at once. Saving, and saying that it is saved, is up to you.
Markup
<div class="sf-choice">
<input class="sf-choice__input" type="checkbox" id="terms" name="terms"
aria-describedby="terms-hint">
<label class="sf-choice__label" for="terms">I agree to the terms</label>
<p class="sf-field__hint" id="terms-hint">You can change this later.</p>
</div>
<!-- A switch: the same, with role="switch" on the input.
Partly ticked at first: data-sf-indeterminate on the checkbox. -->
<fieldset class="sf-choice-group" role="radiogroup" data-invalid
aria-invalid="true" aria-describedby="plan-error">
<legend class="sf-choice-group__label">Plan</legend>
<div class="sf-choice">
<input class="sf-choice__input" type="radio" id="free" name="plan" value="free">
<label class="sf-choice__label" for="free">Free</label>
</div>
…
<p class="sf-field__error" id="plan-error" role="alert">Choose a plan.</p>
</fieldset>
<!-- A group of checkboxes: the same, without the role and aria-invalid. -->
<script type="module">
import { initChoices, setIndeterminate } from '@subfloor/core'
// data-sf-indeterminate, and the focus ring after arrow keys in WebKit.
initChoices()
// Later: setIndeterminate(checkbox, true)
</script>Behaviour
- The whole row can be pressed, at least 44px tall, except links in a label or hint. A checkbox on its own has a touch area of 44px too: leave room for it.
- Hints and errors are outside the label: they describe the control and are not part of its name.
- Partly ticked is only how a box looks and what is read out;
checkedis what a form sends. A press ends it. In React, it followsindeterminateagain after the press, also when the box is uncontrolled. “Select all” is up to your project. - In React, a change made before React hydrates is kept and reported once, to
onCheckedChangeoronValueChange(not toonChange, as there is no event). A parent that keeps its value gets it back. - In React, RadioGroup is part of a form, and
requiredis checked, only if it has aname. In Astro,nameis required. A controlled RadioGroup with nothing chosen yet has the value''. - Off and on, ticked and not, differ by shape, not only by colour. They print with their backgrounds. The switch’s knob does not slide under reduced motion.
Without JavaScript
Everything works, except that a partly ticked box shows as ticked or not, and WebKit loses a radio button’s focus ring after an arrow key.
Customisation
:root {
--sf-choice-checked-bg: var(--brand); /* keep 3:1 against the surface */
--sf-choice-checked-color: white;
--sf-radius-choice: 4px;
}| Token | Default |
|---|---|
--sf-color-focus | #2563eb |
--sf-color-danger | #b91c1c |
--sf-choice-size | 24px |
--sf-choice-gap | var(--sf-space-3) |
--sf-choice-bg | var(--sf-color-surface) |
--sf-choice-border-color | var(--sf-field-border-color) |
--sf-choice-border-width | 1.5px |
--sf-choice-checked-bg | var(--sf-button-primary-bg) |
--sf-choice-checked-color | var(--sf-button-primary-color) |
--sf-radius-choice | 6px |
--sf-switch-width | 44px |
--sf-switch-height | 26px |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.