Toggle group

Buttons that stay pressed. single is one of several, like text alignment; multiple is any of several, like bold and italic. With a name, the group posts like the radios or checkboxes it stands in for.

Toggle group

Needs Alpine and the package's script. See getting started.

Use it

One of several:

<x-ds::toggle-group label="Alignment" name="align">
    <x-ds::toggle value="left" icon="bars-3-bottom-left" label="Left" :iconOnly="true" :pressed="$align === 'left'" />
    <x-ds::toggle value="center" icon="bars-3" label="Center" :iconOnly="true" :pressed="$align === 'center'" />
    <x-ds::toggle value="right" icon="bars-3-bottom-right" label="Right" :iconOnly="true" :pressed="$align === 'right'" />
</x-ds::toggle-group>

The request carries align=center.

Any of several, posting filters[]:

<x-ds::toggle-group label="Filters" type="multiple" name="filters">
    @foreach ($filters as $key => $words)
        <x-ds::toggle :value="$key" :label="$words" :pressed="in_array($key, old('filters', $active))" />
    @endforeach
</x-ds::toggle-group>

Leave name out and nothing posts: the group is then only state for the page (see binding, below).

Inside a toolbar, the toggles join the toolbar's single Tab stop, its arrow keys and its highlight, and take its size.

Livewire and Alpine

Put the binding on the group. single binds a string ('' when none is pressed), multiple an array of the pressed values in the order they appear:

<x-ds::toggle-group label="Text style" type="multiple" wire:model.live="marks">
    <x-ds::toggle value="bold" icon="bold" label="Bold" :iconOnly="true" :pressed="in_array('bold', $marks)" />
    <x-ds::toggle value="italic" icon="italic" label="Italic" :iconOnly="true" :pressed="in_array('italic', $marks)" />
</x-ds::toggle-group>

Setting the property from code presses and releases the toggles to match. Each press also dispatches input and change from the toggle, for @change listeners.

Props

ds::toggle-group:

PropTypeDefaultWhat it does
labelstring—What the toggles set ("Alignment", "Text style"). The group's accessible name. Required.
hideLabelbooltruePass false to show label above the toggles, in the field label's style.
helpstring—Guidance under the toggles, tied to the group by aria-describedby. Replaced by error, never stacked with it.
errorstring—Validation message under the toggles, in help's place (aria-invalid on a single group, a radiogroup).
typestringsinglesingle (one pressed at a time, radio semantics) or multiple (any, each a pressed button). Anything else throws.
namestring—Posts the pressed values through hidden inputs; [] is added for multiple.
sizestringmdsm (32px) or md (36px). Inside a toolbar, the toolbar's unless set here.

With a shown label, help or error, the group sits in a box that holds them, and your class goes on that box; the group keeps id and your other attributes. With none of them, the group is the root, as before.

ds::toggle:

PropTypeDefaultWhat it does
valuestring—What the toggle stands for, and posts while pressed. Required.
labelstring—The words, and the accessible name. Required.
iconstring—A Heroicons name, drawn before the words.
iconOnlyboolfalseSquare, the icon alone; label stays the name. Needs icon.
pressedboolfalsePressed on the first paint.
disabledboolfalseCannot be pressed, and does not post.
tooltipstring, bool—A tooltip: the text, or true for the label.

What it does

  • Single: pressing a toggle presses it and releases the other. Pressing the one already pressed keeps it; there is always a choice once one is made.
  • Multiple: each press flips that toggle.
  • The state is rendered from PHP. pressed draws the face and sets the hidden input before any script, so the first paint is right.
  • Posting: only pressed, enabled toggles post. A disabled toggle never does, pressed or not, as a disabled checkbox would not.
  • On its own the group is one Tab stop, with the arrow keys moving between toggles (either axis, wrapping, skipping disabled) and Home and End. In single the arrows also press, as radios do. Inside a toolbar, the toolbar's keys apply and Space or Enter presses.
  • A highlight follows the pointer and keyboard focus, as in a toolbar.

Accessibility

  • single: role="radiogroup" named by label, each toggle role="radio" with aria-checked.
  • multiple: role="group" named by label, each toggle a button with aria-pressed.
  • Pressed shows as a filled face, not by colour alone.
  • Icon-only toggles keep label as visually hidden text; add :tooltip="true".

Don't

  • Don't use it for actions. A toggle sets a state; a button group runs commands.
  • Don't use single to switch views. That is a segmented control, which works without script.
  • Don't use it for a long list. A chip group wraps; a toggle group stays on one line.
  • Don't drop pressed and rely on the script. The page should be right before it runs.

More in specs/toggle-group.md.


From the package repo: edit this page on GitHub.

Was this page helpful?