Components
Segmented control
Mutually exclusive choices. The current segment carries aria-pressed=true, aria-current or aria-selected=true. --touch is the phone size; --scroll scrolls sideways.
Live example
Rendered with the real classes from /site/chitransit.css, light (left) and dark (right).
.ct-segmented [role=radio][aria-checked].ct-segmented--fill.ct-segmented--touch.ct-segmented--scrollCode
<div class="ct-segmented" role="radiogroup" aria-label="Appearance (demo)"><button role="radio" aria-checked="true" tabindex="0">System</button><button role="radio" aria-checked="false" tabindex="-1">Light</button><button role="radio" aria-checked="false" tabindex="-1">Dark</button></div>
<div class="ct-segmented ct-segmented--fill" style="--count:2" role="radiogroup" aria-label="Time display"><button role="radio" aria-checked="true" tabindex="0">Minutes</button><button role="radio" aria-checked="false" tabindex="-1">Arrival time</button></div>
<div class="ct-segmented ct-segmented--touch" role="radiogroup" aria-label="Appearance on a phone (demo)"><button role="radio" aria-checked="true" tabindex="0">System</button><button role="radio" aria-checked="false" tabindex="-1">Light</button><button role="radio" aria-checked="false" tabindex="-1">Dark</button></div>
<div class="ct-segmented ct-segmented--scroll" role="radiogroup" aria-label="Response class"><button role="radio" aria-checked="true" tabindex="0">All</button><button role="radio" aria-checked="false" tabindex="-1">2xx</button><button role="radio" aria-checked="false" tabindex="-1">3xx</button><button role="radio" aria-checked="false" tabindex="-1">4xx</button><button role="radio" aria-checked="false" tabindex="-1">5xx</button><button role="radio" aria-checked="false" tabindex="-1">Errors</button></div>
Tokens
From tokens/components.tokens.json. Override one instance by setting the property without the ct- prefix.
| Custom property | Value | Alias | Resolves to |
|---|---|---|---|
--ct-segmented-background | var(--card-2) | {colors.card2} | #E9EBF1 #1F2733 |
--ct-segmented-background-selected | var(--paper) | {colors.paper} | #FFFFFF #0F141C |
--ct-segmented-color | var(--muted) | {colors.muted} | #5B6472 #B0BACA |
--ct-segmented-color-hover | var(--ink) | {colors.ink} | #101A2E #EEF1F6 |
--ct-segmented-color-selected | var(--ink) | {colors.ink} | #101A2E #EEF1F6 |
--ct-segmented-font-size | var(--fs-sub) | {type.sub} | 13 |
--ct-segmented-font-weight | var(--weight-medium) | {weight.medium} | 500 |
--ct-segmented-font-weight-selected | var(--weight-medium) | {weight.medium} | 500 |
--ct-segmented-frame | var(--shadow-frame) | {shadows.frame} | |
--ct-segmented-gap | var(--space-2) | {space.s2} | 2 pt |
--ct-segmented-height | var(--size-minimum-tap-target) | {size.minimumTapTarget} | 44 pt |
--ct-segmented-height-touch | 48px | ||
--ct-segmented-padding | var(--space-2) | {space.s2} | 2 pt |
--ct-segmented-radius | var(--r-control) | {radius.control} | 10 pt |
--ct-segmented-segment-min-size-touch | var(--size-minimum-tap-target) | {size.minimumTapTarget} | 44 pt |
--ct-segmented-segment-padding-x | var(--space-12) | {space.s12} | 12 pt |
--ct-segmented-segment-radius | var(--radius-segment) | {radius.segment} | 8 pt |
--ct-segmented-shadow-selected | var(--shadow-control) | {shadows.control} |
Contract
Contract: components/segmented.yaml. States are drawn live by transit.js.
Loading the contract…
Variants
The default sizes segments to their labels, 36 tall. --fill makes --count equal columns across the width; --scroll scrolls sideways instead of shrinking; --touch is the 48 phone size, and the default on a touch screen. Nothing else exists.
- The control spans its container:
--fillwith--count. - The segments may not fit:
--scroll; with a caption it is a Filter. - On a phone: add
--touch.
<!-- Do -->
<div class="ct-segmented ct-segmented--fill" style="--count:2" role="radiogroup" aria-label="Time display">
<button role="radio" aria-checked="true" tabindex="0">Minutes</button>
<button role="radio" aria-checked="false" tabindex="-1">Arrival time</button>
</div>
<!-- Don't: actions as segments -->
<div class="ct-segmented" role="group" aria-label="Stop">
<button type="button">Share</button><button type="button">Delete</button>
</div>
Usage
Do
- Use for two to four mutually exclusive choices, such as the theme switch.
- Use
--fillwhen the control spans its container. - Use
--touchon phones: a 48pt control with 44pt segments. - Use
--scrollwhen the segments may not fit; it scrolls sideways instead of shrinking them. A labeled one is a filter segment.
Don't
- Don't use for more than four options or for actions.
Accessibility
- A choice is a
role="radiogroup"with anaria-labelofrole="radio"segments; the current one hasaria-checked="true". Only the checked segment is in the Tab order (tabindex="0", the rest-1), and the arrow keys move and check. - A set of buttons where one opens a dialog, like this site's header theme switch (Custom), is a
role="group"ofaria-pressedbuttons instead.aria-pressedandaria-currentdraw the selection too. - iOS segmented controls don’t scale with Dynamic Type. At accessibility sizes, show the choices as an inline list with a checkmark on the current one.