Skip to content

Signal

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--scroll

Code

<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 propertyValueAliasResolves to
--ct-segmented-backgroundvar(--card-2){colors.card2}#E9EBF1 #1F2733
--ct-segmented-background-selectedvar(--paper){colors.paper}#FFFFFF #0F141C
--ct-segmented-colorvar(--muted){colors.muted}#5B6472 #B0BACA
--ct-segmented-color-hovervar(--ink){colors.ink}#101A2E #EEF1F6
--ct-segmented-color-selectedvar(--ink){colors.ink}#101A2E #EEF1F6
--ct-segmented-font-sizevar(--fs-sub){type.sub}13
--ct-segmented-font-weightvar(--weight-medium){weight.medium}500
--ct-segmented-font-weight-selectedvar(--weight-medium){weight.medium}500
--ct-segmented-framevar(--shadow-frame){shadows.frame}
--ct-segmented-gapvar(--space-2){space.s2}2 pt
--ct-segmented-heightvar(--size-minimum-tap-target){size.minimumTapTarget}44 pt
--ct-segmented-height-touch48px
--ct-segmented-paddingvar(--space-2){space.s2}2 pt
--ct-segmented-radiusvar(--r-control){radius.control}10 pt
--ct-segmented-segment-min-size-touchvar(--size-minimum-tap-target){size.minimumTapTarget}44 pt
--ct-segmented-segment-padding-xvar(--space-12){space.s12}12 pt
--ct-segmented-segment-radiusvar(--radius-segment){radius.segment}8 pt
--ct-segmented-shadow-selectedvar(--shadow-control){shadows.control}

Contract

Contract: components/segmented.yaml. States are drawn live by transit.js.

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: --fill with --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 --fill when the control spans its container.
  • Use --touch on phones: a 48pt control with 44pt segments.
  • Use --scroll when 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 an aria-label of role="radio" segments; the current one has aria-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" of aria-pressed buttons instead. aria-pressed and aria-current draw 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.