Toggle group
Buttons that hold a pressed state, one of (single) or any of (multiple),
optionally posting through hidden inputs. Needs Alpine and the package's
script (dsToggleGroup, x-ds-glide.hover). Covers toggle.
Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.
Anatomy
╭───────────────────╮
│ [≡] ≡ ≡ │ ← div[role=radiogroup|group]: muted tray
╰───────────────────╯
└ button[role=radio aria-checked] > icon + words + input[type=hidden]
- Group:
div[data-ds-toggle-group],role="radiogroup"(single) orrole="group"(multiple),aria-label,x-data="dsToggleGroup({type})",x-modelable="dsValue"beside the host's binding,x-ds-glide.hover. The muted tray; inside a toolbar, no tray of its own (in-data-ds-toolbar:). - Highlight: as in toolbar.
- Toggle:
button[type=button][data-ds-toggle][data-ds-value], also a toolbar item and a glide item. Single:role="radio"andaria-checked. Multiple:aria-pressed. Holdsinput[type=hidden][data-ds-toggle-input]namedname(single) orname[](multiple),disabledunless the toggle is pressed and enabled.
States
| State | Treatment |
|---|---|
| off | text-fg-body |
| on | bg-fg/10 text-fg (aria-pressed: / aria-checked:) |
| hover, focus, pressing | as in toolbar: the highlight |
| disabled | text-fg-muted, skipped, input disabled |
Behaviour
- Single: pressing an off toggle turns it on and the other off; pressing the on toggle does nothing. Standalone, the arrows move AND press, as radios do, and the tab stop is the pressed toggle.
- Multiple: pressing flips one toggle. The arrows only move focus.
- Every press updates the ARIA state and the hidden input, writes
dsValue(a string, or an array in DOM order), and dispatchesinputandchangefrom the toggle. - The host setting the bound value presses and releases toggles to match, without events.
- Standalone the group runs its own roving tabindex (both arrow axes, wrapping, skipping disabled, Home/End). Inside a toolbar it does not; its toggles are the toolbar's items.
Sizes
sm (32px) and md (36px), as toolbar. Unset inside a toolbar, the
toolbar's.
Choices and why
typedefaults tosingle, the more common case (alignment, view), and the one that takes the stricter semantics.- Single cannot be emptied by pressing again. That is how radios behave,
and what
role=radiopromises. - Both arrow axes standalone, since the group lays out either way and has no orientation prop; a toolbar's orientation governs once inside one.
- State from PHP.
pressedsets the ARIA attribute and the hidden input'sdisabledbefore any script, so a page without the script posts what it shows. - Hidden inputs, not checkboxes. The toggle is a button so it can carry
aria-pressedand be a toolbar item; the input under it is only what posts.
Accessibility
| Role | single: radiogroup of radio; multiple: group of buttons with aria-pressed |
| Name | label; each toggle its label (sr-only with iconOnly) |
| State | aria-checked / aria-pressed; disabled |
| Keyboard | Standalone: one Tab stop, arrows, Home/End, Space/Enter. In a toolbar: the toolbar's |
| Focus | Ring on the toggle, highlight under it |
| Not colour alone | The filled face |
| Motion | Highlight only, motion-safe |
Help and error
Since batch 30B, help and error in the one field pattern every control
shares (partials/field-message, the markup ds::input draws): the help as
muted meta text, or in its place the error with its icon in the danger
colour, never both. The message's id ({id}-help, {id}-error) is named by
the control's aria-describedby, after anything the host passed. With
neither prop the control renders exactly as before
(tests/FieldMessagesUnchangedTest.php).
hideLabel defaults to true, the group's name staying an aria-label as before; false draws it above in the field label's style and names the group by it. A shown label or a message needs a box around the group: the host's class goes on that box then, and id and the rest stay on the group. A single group (a radiogroup) takes aria-invalid with an error; a multiple group (role group) does not support it, and the message says it.
Do not
- Do not use for actions. Use buttons.
- Do not leave
pressedto the script. The first paint must be right. - Do not add
roleoraria-pressedby hand on a toggle;typedecides.
From the package repo: edit this page on GitHub.