Appearance
Toggle
Switches between a few ways of showing the same content, such as the list's view mode.
base-basetogglegroup--defaultOpen in Storybook ↗When to use
- Choosing one of a few views of the same results: List view, Grid view, Table view.
- Choosing one display option that applies straight away, such as the card size of the grid view: Large or Compact.
When not to use
- An option that is saved with a form: use a checkbox.
- Many options, or options with long labels: use a dropdown.
- An action: use a button. A toggle segment never runs anything; it only changes what is shown.
Anatomy
- Group: one capsule with a
--color-border-defaultborder,--radius-inputcorners and a--color-surfacefill. The segments sit side by side without gaps. - Segment: a button
--view-switch-sizehigh. An icon segment is square; a text segment has--view-switch-text-padon each side and a bold--text-uilabel. - Active segment:
--color-accent-washfill with--color-accent-inkicon or text. Inactive segments use--color-text-secondary. - Tooltip (icon segments): the segment's label, shown under it on hover and focus.
Variants
| Variant | Where | Segment content |
|---|---|---|
| Icon segments | the view-mode switch in the list toolbar | an icon, named by its label (Grid view) |
| Text segments | the card-size switch next to it, in grid view | a short visible label (Large, Compact) |
| Single icon toggle | — | one square icon button that swaps its icon when on |
base-basetogglegroup--text-segmentsOpen in Storybook ↗Exactly one segment of a group is on. Turning a segment on turns the others off. If the page sets none, the first segment turns on. The view-mode switch only shows the views the entity type allows, in the configured order.
States
| State | What you see | Trigger |
|---|---|---|
| Off | no fill, secondary ink | — |
| Hover (off) | --color-surface-row-hover fill; icon segments show their tooltip | pointer |
| On | accent wash fill, accent ink | click, Enter or Space |
| Focus | --focus-ring-width ring in --color-focus-ring on the segment | keyboard |
base-basetogglegroup--statesOpen in Storybook ↗Segments have no disabled state.
Keyboard & accessibility
| Key | Action |
|---|---|
| Tab / Shift+Tab | moves to the next or previous segment; each segment is its own stop |
| Enter / Space | turns the focused segment on |
- The group is a
role="group"with anaria-label: View for the view mode switch, Card size for the card-size switch. - Each segment is a native
<button>witharia-pressed, so the segment that is on is announced as pressed. - Icon segments need a label: it becomes the segment's
aria-labeland its tooltip. - Arrow keys do not move between segments.
- The focus ring is cut off at the top and bottom edge of the group, so only its sides show.
- The single icon toggle is not a button: Tab does not reach it and it has no accessible name. Use a group or a button instead.
Content
- Label each segment with what it shows, in sentence case: Grid view, Compact.
- Text segments hold one short word; longer choices belong in a dropdown.
- Name the group after the choice, not the control: Card size, not Size toggle.
- Translation keys:
library.view-mode,library.view-modes.*andlibrary.grid-size.*.
Related
Button · Checkbox · Tooltip · Icons · Accessibility