Skip to Content
CodeUI

SegmentedControl

@codeui/styles v0.3.0

A row of mutually exclusive segments in plain HTML, with a sliding indicator behind the selected one.

Load @codeui/styles/segmentedcontrol.css, or all.css. The Solid component is documented in SegmentedControl.

Demo

segmented-control/basic.html
<div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid">  <div role="tablist" aria-label="Editor theme" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>    <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>    <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>  </div></div>

Markup

PartElementClassdata-cui
Rootdivcui-segmentedcontrolsegmentedcontrol
Listdiv role="tablist"cui-segmentedcontrol-listsegmentedcontrol-list
Segmentbutton role="tab"cui-segmentedcontrol-segmentsegmentedcontrol-item
Indicatordiv role="presentation"cui-segmentedcontrol-indicatorsegmentedcontrol-indicator

The indicator is the last child of the list. It is absolutely positioned, and the selected segment's text sits above it.

AttributeElementValuesDefault
data-sizeRootxs, sm, md, lgsm
data-variantRootsolid, borderedsolid
data-accentRootPresenceOff
data-pillRootPresenceOff
data-fluidRootPresenceOff
data-auto-widthRootPresenceOff

Indicator position

The styles do not place the indicator. Measure the selected segment and set the indicator's width and transform: translateX() from its offsetWidth and offsetLeft, as the Solid component does. The demos write the measured values inline. Measure again when the selection, the font, or the control's width changes.

Variants

Sizes

segmented-control/sizes.html
<div class="cui-stack" data-gap="4" data-align="start">  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="xs" data-variant="solid">    <div role="tablist" aria-label="Editor theme (xs)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 39px; transform: translateX(44px)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid">    <div role="tablist" aria-label="Editor theme (sm)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="md" data-variant="solid">    <div role="tablist" aria-label="Editor theme (md)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 62px; transform: translateX(66px)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="lg" data-variant="solid">    <div role="tablist" aria-label="Editor theme (lg)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 74px; transform: translateX(79px)"></div>    </div>  </div></div>

xs, sm, md, and lg are 28px, 36px, 44px, and 52px at comfortable density. Omitting data-size renders sm.

Bordered

segmented-control/bordered.html
<div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="bordered">  <div role="tablist" aria-label="Editor theme" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>    <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>    <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>  </div></div>

solid fills the track. bordered drops the fill and draws a 2px ring around the track.

Pill

segmented-control/pill.html
<div class="cui-stack" data-gap="4" data-align="start">  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid" data-pill>    <div role="tablist" aria-label="Editor theme (solid pill)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="bordered" data-pill>    <div role="tablist" aria-label="Editor theme (bordered pill)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>    </div>  </div></div>

data-pill rounds the track and the indicator fully. It combines with either variant.

Accent

segmented-control/primary.html
<div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid" data-accent>  <div role="tablist" aria-label="Editor theme" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>    <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>    <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>    <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>  </div></div>

data-accent gives the indicator the brand fill and the selected label the brand text color.

Fluid

segmented-control/fluid.html
<div class="cui-stack" data-gap="4" data-align="stretch" style="max-width: 420px; margin-inline: auto">  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid" data-fluid>    <div role="tablist" aria-label="Editor theme (fluid)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="left: calc((100% + var(--cui-space-1)) / 3); width: calc((100% - 2 * var(--cui-space-1)) / 3)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid" data-fluid data-autowidth>    <div role="tablist" aria-label="Editor theme (fluid, auto width)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="left: calc((100% + var(--cui-space-1)) / 3); width: calc((100% - 2 * var(--cui-space-1)) / 3)"></div>    </div>  </div></div>

data-fluid sets the width to 100%. data-auto-width sets each segment to a 1px base width with equal flex growth and removes horizontal padding, so the segments share the width equally. It does not measure text widths. With equal segments, the indicator can use percentages instead of measured pixels, as this demo does: left: calc((100% + var(--cui-space-1)) / 3) and width: calc((100% - 2 * var(--cui-space-1)) / 3) for the second of three segments.

States

Selected

data-selected on a segment sets the selected text color. Set aria-selected="true" on it, and "false" on the others.

Disabled

segmented-control/disabled.html
<div class="cui-stack" data-gap="4" data-align="start">  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid">    <div role="tablist" aria-label="Editor theme" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" disabled data-disabled class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>    </div>  </div>  <div class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="sm" data-variant="solid" data-disabled>    <div role="tablist" aria-label="Editor theme (disabled)" aria-orientation="horizontal" class="cui-segmentedcontrol-list" data-cui="segmentedcontrol-list">      <button type="button" role="tab" aria-selected="false" tabindex="-1" disabled data-disabled class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Light</button>      <button type="button" role="tab" aria-selected="true" tabindex="0" disabled data-disabled data-selected class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">Dark</button>      <button type="button" role="tab" aria-selected="false" tabindex="-1" disabled data-disabled class="cui-segmentedcontrol-segment" data-cui="segmentedcontrol-item">System</button>      <div role="presentation" class="cui-segmentedcontrol-indicator" data-cui="segmentedcontrol-indicator" style="width: 54px; transform: translateX(58px)"></div>    </div>  </div></div>

data-disabled on a segment fades it to 20% opacity and sets the not-allowed cursor. data-disabled on the root fades the whole control to 50%. Set the native disabled attribute on each disabled segment too.

What you supply

  • Selection on click, and the data-selected and aria-selected attributes.
  • Arrow-key navigation between segments, with a roving tabindex: 0 on the selected segment and -1 on the others.
  • The indicator's width and transform. With html[data-cui-input="keyboard"], the indicator moves without a transition.
  • An aria-label on the list.

Helper

import "@codeui/styles/segmentedcontrol.css";
import { applyStyleProps } from "@codeui/styles";
import { segmentedControlStyleProps } from "@codeui/styles/segmentedcontrol";

applyStyleProps(document.querySelector("[data-cui=segmentedcontrol]")!, segmentedControlStyleProps({ size: "md", variant: "bordered", pill: true }));
// class="cui-segmentedcontrol" data-cui="segmentedcontrol" data-size="md" data-variant="bordered" data-pill=""

On this page