Segmented control
Two or three buttons in one frame, one of them always chosen. For a choice of view that takes effect at once: Grid or List, Light or Dark, 7 or 28 days.
- Sea HareCatamaran, 8 guests
- Minted OneCatamaran, 10 guests
- MischievousSailing yacht, 6 guests
- Scuba DooCatamaran, 8 guests
- Sea HareCatamaran, 8 guests
- Minted OneCatamaran, 10 guests
- MischievousSailing yacht, 6 guests
- Scuba DooCatamaran, 8 guests
Show code
<div class="sn-segmented" role="group" aria-label="Show as">
<button type="button" data-value="grid" aria-pressed="true">Grid</button>
<button type="button" data-value="list" aria-pressed="false">List</button>
</div>
<script>
import { segmented } from '@somnia/core/lib/segmented.js';
const group = document.querySelector('.sn-segmented');
segmented(group, (value) => { list.dataset.view = value; });
</script> When to use it
- Yes: the same content shown another way, or over another period, changing as soon as it's pressed.
- No: a value sent with a form (use radio buttons, which a form already understands), moving between pages (use links), or anything with more than three options (use a drop-down).
- If the choice is on or off rather than this or that, it's a switch.
Anatomy
- One frame, lightly tinted, holding the buttons side by side with 2px between them.
- The chosen button is filled with the accent, its words in the accent's contrast colour. The others are plain, in the softer text colour, and tint on hover.
- Words: one word each where possible, of about the same length. A 16px icon may go before the word, never in place of it.
- Each button is at least 34px high. A long row wraps on a phone when the group is given
flex-wrap.
Behaviour
- There is always exactly one chosen. Pressing the chosen one does nothing.
- The page changes at once; there is no Apply button.
- Remember the choice where people come back: Somnia CMS keeps Grid or List for the media library on the device.
segmented(group, onChange)fromlib/segmented.jskeeps the pressed state and tells the page what was chosen. It returns the currentvalueand aset(value)for restoring a remembered choice.
Do and don't
Do
- Two or three short options
- Label the group, not each button, with what it changes
Don't
- Use it as tabs for different content
- Leave none chosen, or allow two
Accessibility
What it does for you
- Real buttons, so Tab, Enter and Space work without script.
- The chosen one filled, not just tinted, so it differs by more than hue.
- Each option at least 34px high.
What you need to do
- Give the group a name for what it changes:
role="group"andaria-label="Show as". - Set
aria-pressedon every button (segmented()keeps it right after that). - If the change isn’t obvious to someone who can’t see it, say it in a polite live region: "Showing 24 as a list".
- Use it for a view only. A value sent with a form is a set of radio buttons.
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves to each option in turn. |
| Enter or Space | Chooses the option in focus. The view changes at once and the focus stays put. |
What a screen reader is told
- A group named for what it changes ("Show as").
- Each option: a button named by its word, pressed or not pressed.
This is what the browser reports to assistive technology. What each screen reader then says varies, and has not been listened to yet.
WCAG 2.2
| Criterion | Level | How this meets it |
|---|---|---|
| 1.4.1 Use of Color | A | The chosen option is a filled shape among plain ones, not a different colour of the same thing. |
| 2.1.1 Keyboard | A | Native buttons. |
| 3.2.2 On Input | A | Pressing an option is the request to change the view; nothing changes on focus alone. |
| 4.1.2 Name, Role, Value | A | A named group of buttons, each saying whether it is pressed. |
The criteria this component has a part in. A page that uses it still has to meet the rest.
Checked
| Check | How | Result |
|---|---|---|
| Automated (axe-core) | WCAG 2.0, 2.1 and 2.2 rules at A and AA, the page in light and in dark | Passed Nothing found, light or dark |
| Narrow screen | No sideways scrolling at 320px wide (WCAG 1.4.10) | Passed No sideways scrolling |
| Keyboard and what the browser reports | Real key presses on the live example, and the role, name and state given to assistive technology | Passed 6 of 6 checks pass |
| On paper | Somnia’s print check: no overlaps, stranded headings or controls on the page | Passed No faults on 5 sheets |
| Screen reader | Listened to by a person, in VoiceOver and NVDA | Not yet Not yet checked by a person |
The 6 checks made with the keyboard, on the example at the top of this page:
- Passed Tab reaches each option in turn
- Passed Enter chooses the option in focus, and the view changes at once
- Passed Space does the same
- Passed There is always exactly one chosen: pressing the chosen one changes nothing
- Passed The focus stays on the option that was pressed
- Passed The browser reports a group named for what it changes, holding pressed and unpressed buttons
Run by script on against Somnia DS 1.89.0, in Chromium with axe-core 4.14.0. How these checks work, and every component's results.
Not yet checked
- How it sounds in a screen reader (VoiceOver, NVDA, TalkBack) has not been checked by a person. The roles, names and states the browser reports are checked by script; what is actually spoken is not.
- Arrow keys do not move between options. That is right for a group of toggle buttons, and wrong if it is ever presented as a set of radio buttons or tabs.
- Windows high contrast (forced colours) has not been looked at by a person.
Evidence
Where it's used
| Where | For |
|---|---|
| Somnia CMS, Media library | Grid or List, remembered on the device. |
| Somnia CMS, Visitors | The period (7 days to 12 months), and Page views or Visits on the chart. |
| Somnia CMS, editor preview and Version history | Light or Dark; how to show a version. |
What we saw, and what changed
| When | What we saw | What changed |
|---|---|---|
| Somnia CMS’s media library gained a list view beside its grid, and needed a way to choose between them. | Buttons in one frame, the chosen one marked (Somnia 1.60.0). | |
| The chosen option had only a faint tint. Grid / List and Light / Dark were unclear at a glance. | The chosen option is filled with the accent, in a tinted frame (1.67.0). | |
| Five places in Somnia CMS each keep the pressed state with their own few lines. | segmented() does it once for new pages. The five have not been moved onto it yet. |
From building and using it on real sites, each entry taken from the changelog or the build's own notes. This is observation by the studio and its clients, not research sessions with users.
What it rests on
- ARIA Authoring Practices: Button (toggle button):
aria-pressedfor a button that stays chosen.
What we don't know yet
- Whether people expect arrow keys here has not been tested with keyboard users.