Segmented control

Free

Two to five mutually exclusive choices on one glass track, with a thumb that glides to the one you pick and jumps at once when you arrow through them.

npx shadcn@latest add @pixilui/segmented-control

Preview

A glass thumb glides to your pick; arrow keys move it at once.

When to use

Use it when

  • Switching the view of the same data: Day, Week, Month on a usage chart, or List and Board on a run queue.
  • Short labels, two to five of them, where every choice fits on one row.

Use something else when

  • Navigating between pages or panels with their own content: use tabs.
  • Turning one thing on or off: use a toggle button or a switch.
  • More than five choices or long labels: use a select.

Installation

Terminal
npx shadcn@latest add @pixilui/segmented-control

Adds segmented-control.tsx, and installs clsx and tailwind-merge. First PixilUI item in this app? Add the registry once, as in Installation.

Usage

example.tsx
import { SegmentedControl } from "@/components/pixilui/segmented-control";

<SegmentedControl
  aria-label={aria-label}
  options={options}
  onValueChange={onValueChange}
/>

As wide as its labels, never wider than its container. Keep labels short so it fits at 375 wide.

API reference

PropTypeDefault
aria-labelRequiredstring–What the choice is about, for screen readers ("View").
optionsRequiredSegmentedControlOption[]–The choices, in order. A string is both the value and the label.
valuestring–The chosen value, controlled. Pair with onValueChange.
defaultValuestring–The starting value when uncontrolled. Defaults to the first option.
onValueChange(value: string) => void | Promise<void>–Runs when a choice is made, by click or arrow key.

Also takes every native <div> attribute except defaultValue, onChange, and a ref.

Keyboard

TabMoves focus to the chosen option; Tab again leaves the control.
← →Chooses the previous or next option and moves focus to it, wrapping at the ends.
↑ ↓The same as left and right.

Accessibility

  • A radiogroup of radio buttons with aria-checked; aria-label names the group and is required.
  • Roving tabindex: only the chosen option is in the tab order.
  • The thumb is decoration; the checked state carries the meaning.

Motion

  • A click lets the thumb glide to the new choice on the snappy spring, its width following the label.
  • Arrow keys move the thumb at once, so scanning by keyboard never waits on motion.
  • The thumb is placed again after the font loads, so it never sits on the fallback font's widths.
  • Reduced motion: the thumb moves at once.

PixilUI Pro opens Thu 10 Dec. The first 20 licenses are $99.