Skip to Content

Segmented Control

Segmented control: a pill toggle group with a muted track where a lifted thumb glides to the pressed item, staying still under reduced motion. Horizontal only; values are arrays.

Installation

pnpm dlx shadcn@latest add @01orbital/segmented-control

Usage

"use client"

import { useState } from "react"
import {
  TextAlignCenterIcon,
  TextAlignLeftIcon,
  TextAlignRightIcon,
} from "@phosphor-icons/react"

import {
  SegmentedControl,
  SegmentedControlItem,
} from "@/components/toggle/segmented-control"

// Year is disabled, so that state shows beside the live ones.
const RANGES = ["Day", "Week", "Month", "Year"] as const

const ALIGNMENTS = [
  { value: "left", label: "Align left", Icon: TextAlignLeftIcon },
  { value: "center", label: "Align center", Icon: TextAlignCenterIcon },
  { value: "right", label: "Align right", Icon: TextAlignRightIcon },
] as const

export default function SegmentedControlDemo() {
  const [range, setRange] = useState<string>("Week")
  return (
    <div className="flex flex-wrap items-center justify-center gap-8">
      {/* Controlled: one item is always on, so there is no empty update. */}
      <SegmentedControl
        aria-label="Range"
        value={[range]}
        onValueChange={(next) => {
          if (next[0]) setRange(next[0])
        }}
      >
        {RANGES.map((value) => (
          <SegmentedControlItem
            key={value}
            value={value}
            disabled={value === "Year"}
          >
            {value}
          </SegmentedControlItem>
        ))}
      </SegmentedControl>
      <SegmentedControl aria-label="Text alignment" defaultValue={["left"]}>
        {ALIGNMENTS.map(({ value, label, Icon }) => (
          <SegmentedControlItem key={value} value={value} aria-label={label}>
            <Icon aria-hidden="true" />
          </SegmentedControlItem>
        ))}
      </SegmentedControl>
    </div>
  )
}

This example installs with everything it uses:

pnpm dlx shadcn@latest add @01orbital/segmented-control-demo

Examples

Workshop Formatsegmented-control-workshop-format

Segmented Control example: a control that switches a ceramics workshop between wheel and hand-building details. Shows a tab-like switcher that changes content below.

pnpm dlx shadcn@latest add @01orbital/segmented-control-workshop-format

Sleep Periodsegmented-control-sleep-period

Segmented Control example: a control in a dark card that swaps a week and month sleep summary. Shows a time range switcher for dashboards.

pnpm dlx shadcn@latest add @01orbital/segmented-control-sleep-period

Invoice Intervalsegmented-control-invoice-interval

Segmented Control example: a warm inset billing pill with generous labels and a small annual-saving note, on paper. Shows a monthly and yearly pricing toggle.

pnpm dlx shadcn@latest add @01orbital/segmented-control-invoice-interval

Night Desksegmented-control-night-desk

Segmented Control example: a squared track with a filled selection and monospace labels. Shows a sharp-cornered, technical variation of the toggle.

pnpm dlx shadcn@latest add @01orbital/segmented-control-night-desk

Reading Listsegmented-control-reading-list

Segmented Control example: terracotta multiple-selection chips, each with its own raised thumb. Shows multi-select filter chips for a reading list.

pnpm dlx shadcn@latest add @01orbital/segmented-control-reading-list

Training Logsegmented-control-training-log

Segmented Control example: a training log with three heavy letters, the pressed one on an ink pill. Shows a compact single-select toggle.

pnpm dlx shadcn@latest add @01orbital/segmented-control-training-log

Medium Filtersegmented-control-medium-filter

Segmented Control Medium Filter: an example of segmented-control.

pnpm dlx shadcn@latest add @01orbital/segmented-control-medium-filter

Anatomy

Segmented Control is built from 2 parts. In the example they nest like this:

SegmentedControl
└─ SegmentedControlItem
  • SegmentedControl: A row of toggle buttons on a rounded track. The pressed one sits on a raised thumb that slides over from the last one pressed. Put SegmentedControlItem parts inside.
  • SegmentedControlItem: One option in a SegmentedControl, built on Base UI's Toggle. Give it a string value. It is square for one character or an icon and widens to fit a word.

API reference

SegmentedControl

A row of toggle buttons on a rounded track. The pressed one sits on a raised thumb that slides over from the last one pressed. Put SegmentedControlItem parts inside.

It is Base UI's ToggleGroup, so value, defaultValue and onValueChange use arrays of item values. Without multiple, one item is always on: pressing the active item again does nothing, and onValueChange is not called with an empty array. With multiple, every item can be turned off. It is horizontal only, and className takes a string.

Prop

Type

It also accepts the props of the Base UI part it wraps, and the HTML attributes of the element it renders.

SegmentedControlItem

One option in a SegmentedControl, built on Base UI's Toggle. Give it a string value. It is square for one character or an icon and widens to fit a word.

The pressed item sits on the thumb, a card-colored pill with a soft shadow (a light wash in dark mode) that glides over from the last item pressed; reduced motion moves it at once. The other items stay muted and come up to full ink on hover. When the control has multiple, each pressed item gets its own thumb, without the glide.

Prop

Type

It also accepts the props of the Base UI part it wraps, and the HTML attributes of the element it renders.

Dependencies

npm: @base-ui/react, cn, motion. The CLI installs them for you.

Used in blocks

One block is built with Segmented Control.

Last updated on