Toggle group
A set of toggles that share one value.
mq-toggle-group is web patient's ToggleGroup: a wrapper and one
mq-toggle-group-item per option. It ships from @mediquo/ui as custom
elements and as React components.
Install
// Custom elements — the import registers <mq-toggle-group> and its item
import "@mediquo/ui/components/toggle-group";// React
import { ToggleGroup, ToggleGroupItem } from "@mediquo/ui/react/toggle-group";Value
type="single" (the default) keeps at most one item pressed, and clicking the
pressed item releases it. type="multiple" presses any number.
Either way value is the list of pressed values. As an attribute it is
space-separated; in React it is an array, and onValueChange reports the new
one.
<ToggleGroup
label="Vista"
value={[view]}
onValueChange={(event) => setView(event.value[0] ?? "")}
>
<ToggleGroupItem value="day">Día</ToggleGroupItem>
<ToggleGroupItem value="week">Semana</ToggleGroupItem>
</ToggleGroup>Look
variant and size go on the group, which passes them to every item; they
take the same values as Toggle. spacing
is the gap between items in quarter-rem steps. At 0, the default, the items
join into one bar.
Accessibility
- The group has
role="group", named bylabel. Each item is a toggle button witharia-pressed. - Only one item is in the tab order: the one last focused, else the first
pressed one. The arrow keys move between items and wrap around,
HomeandEndjump to the ends, and Enter or Space presses the focused item. - Disabled items, and every item in a disabled group, are announced as disabled and skipped by the arrows.
- Icon-only items need a
label, which becomes their accessible name.