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.

Read the terms first.

Days

When we may call you.

Plan

You can change it later.

With support.

At most one e-mail a day.

States

Needed to go on.

None chosen yet
Colours
All disabled

Layouts

A row of your own

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

Toppings

As many as you like.

Plan

With support.

Size, uncontrolled
Held by its parent

Changes: none

Not in the form
In the form

Sent: nothing

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

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

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;
}
TokenDefault
--sf-color-focus#2563eb
--sf-color-danger#b91c1c
--sf-choice-size24px
--sf-choice-gapvar(--sf-space-3)
--sf-choice-bgvar(--sf-color-surface)
--sf-choice-border-colorvar(--sf-field-border-color)
--sf-choice-border-width1.5px
--sf-choice-checked-bgvar(--sf-button-primary-bg)
--sf-choice-checked-colorvar(--sf-button-primary-color)
--sf-radius-choice6px
--sf-switch-width44px
--sf-switch-height26px

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.