Segmented Control for React
An iOS-style segmented control for React where a glass pill slides to the chosen segment and the arrow keys move between segments.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/segmented.jsonShowing the list view
SegmentedControl
Using it in a form? See the react-hook-form and zod recipe.
With icons
When its container gets narrow, the control tightens its padding, then shows icons only (each label stays the segment's accessible name and becomes its tooltip). Labels only truncate as a last resort. Resize the window to see it.
Showing the board view
import { useState } from "react";
import { SegmentedControl } from "@/components/sheenui/segmented/segmented";
// Package build (after the first npm release, or a packed tarball): import { SegmentedControl } from "@sheenui/theme";
import { LayoutDashboard as BoardIcon, Calendar as CalendarIcon, List as ListIcon } from "lucide-react";
export function Example() {
const [view, setView] = useState("board");
return (
<SegmentedControl
aria-label="View"
value={view}
onValueChange={setView}
items={[
{ value: "list", label: "List", icon: <ListIcon /> },
{ value: "board", label: "Board", icon: <BoardIcon /> },
{ value: "calendar", label: "Calendar", icon: <CalendarIcon /> },
]}
/>
);
}Files to copy
5 folders · 6 files · 4 packages
- From
theme/src/ components/segmented/hooks/{use-latest.ts, use-media-query.ts, use-preferences.ts}motion/tokens.tslib/{cn.ts, form.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/segmented.jsonRegistry item: segmented. The styles need the Tailwind v4 glass UI setup.- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Sizes
sm, md and lg. On touch screens every size is at least 44px tall.
import { SegmentedControl } from "@/components/sheenui/segmented/segmented";
// Package build (after the first npm release, or a packed tarball): import { SegmentedControl } from "@sheenui/theme";
export function Example() {
const ranges = [
{ value: "day", label: "Day" },
{ value: "week", label: "Week" },
{ value: "month", label: "Month" },
];
return (
<div className="flex w-full min-w-0 flex-col items-center gap-4">
{(["sm", "md", "lg"] as const).map((size) => (
<SegmentedControl key={size} size={size} aria-label={`Range (${size})`} defaultValue="week" items={ranges} />
))}
</div>
);
}Full width and icon only
fullWidth shares the row equally, which suits phones and narrow cards. iconOnly hides the labels; each one becomes the segment's accessible name and tooltip.
import { useState } from "react";
import { SegmentedControl } from "@/components/sheenui/segmented/segmented";
// Package build (after the first npm release, or a packed tarball): import { SegmentedControl } from "@sheenui/theme";
import { Monitor as MonitorIcon, Moon as MoonIcon, Sun as SunIcon } from "lucide-react";
const ranges = [
{ value: "day", label: "Day" },
{ value: "week", label: "Week" },
{ value: "month", label: "Month" },
];
export function Example() {
const [range, setRange] = useState("week");
const [theme, setTheme] = useState("system");
return (
<div className="glass-2 flex w-full max-w-sm flex-col gap-4 rounded-sheen-lg p-4">
<SegmentedControl fullWidth aria-label="Range" value={range} onValueChange={setRange} items={ranges} />
<div className="flex items-center justify-between gap-3">
<span id="theme-label" className="text-sm font-medium">Theme</span>
<SegmentedControl
iconOnly
size="sm"
aria-labelledby="theme-label"
value={theme}
onValueChange={setTheme}
items={[
{ value: "light", label: "Light", icon: <SunIcon /> },
{ value: "dark", label: "Dark", icon: <MoonIcon /> },
{ value: "system", label: "System", icon: <MonitorIcon /> },
]}
/>
</div>
</div>
);
}Props
Keyboard
Accessibility
- A group of toggle buttons with aria-pressed: name the group with aria-label or aria-labelledby.
- iconOnly segments keep their label as the accessible name. Segments are 44 px tall on touch screens.
- In forced colors the chosen segment is outlined and underlined, since the pill's fill is dropped.
When to use the segmented control
- Use it as a toggle button group for a chart toolbar that switches between Day, Week and Month, where the choice changes how the same data is shown and a sliding glass pill marks the current one. It gives you an iOS segmented control look in CSS without custom styling, with one glass pill that slides to the chosen segment.
- Use it for a List or Grid view toggle above a gallery, with icons on each item and iconOnly for narrow headers where labels become accessible names and tooltips.
- Use it inside a narrow sidebar or a mobile header, since the adaptive default tightens padding first, then switches to icons only if every segment has one, and only then truncates labels.
- Skip it when each choice swaps a whole panel of content; Tabs gives the right semantics there, and RadioGroup fits a form answer with longer labels and descriptions.
- Pair it with a visible label that points at it through aria-labelledby (or aria-label), Button or Badge elsewhere in the same toolbar, and the chart or DataTable you are filtering by the chosen value.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .