Segmented Control
Pick one of a few options that are all visible at once. Filled, underlined, icon-only or expanding.
Pill
Pick one of two to five options. The selection glides between segments on a shared surface.
$12 per month
Installation
$npx shadcn@latest add @stillui/segmented-controlOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/segmented-control
import { motion, MotionConfig, useReducedMotion } from "motion/react";
import { useId, useRef, useState, type KeyboardEvent } from "react";
import { cn } from "@/lib/utils";
// The indicator moves within the same surface, so it uses an on-screen spring with no overshoot.
const GLIDE = { type: "spring", stiffness: 420, damping: 40, mass: 0.8 } as const;
type Option = { value: string; label: string };
type SegmentedControlProps = {
options: Option[];
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
/** Accessible name for the group, e.g. "Billing period". */
label: string;
size?: "sm" | "md";
className?: string;
};
export function SegmentedControl({
options,
value,
defaultValue,
onValueChange,
label,
size = "md",
className,
}: SegmentedControlProps) {
const reduce = useReducedMotion();
const layoutId = useId();
const [internal, setInternal] = useState(defaultValue ?? options[0]?.value);
const current = value ?? internal;
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const select = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
// Arrow keys move and select, matching native radio groups.
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const index = options.findIndex((o) => o.value === current);
const step = event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 0;
if (!step) return;
event.preventDefault();
const next = (index + step + options.length) % options.length;
select(options[next].value);
refs.current[next]?.focus();
};
return (
<MotionConfig transition={reduce ? { duration: 0 } : GLIDE}>
<div
role="radiogroup"
aria-label={label}
onKeyDown={onKeyDown}
className={cn("inline-flex items-center rounded-lg bg-muted p-1", className)}
>
{options.map((option, i) => {
const selected = option.value === current;
return (
<button
key={option.value}
ref={(el) => {
refs.current[i] = el;
}}
type="button"
role="radio"
aria-checked={selected}
tabIndex={selected ? 0 : -1}
onClick={() => select(option.value)}
className={cn(
"relative rounded-md font-medium outline-offset-0 transition-[color,background-color,box-shadow,scale] active:scale-[0.96] duration-150",
size === "sm" ? "h-7 px-3 text-[13px]" : "h-8 px-4 text-sm",
selected ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{selected ? (
<motion.span
layoutId={layoutId}
className="absolute inset-0 rounded-md bg-card shadow-raised"
aria-hidden
/>
) : null}
<span className="relative">{option.label}</span>
</button>
);
})}
</div>
</MotionConfig>
);
}States
- Selected
- Raised surface and full-contrast text.
- Unselected
- Muted text; full contrast on hover.
- Keyboard
- One tab stop. Arrow keys move and select, like native radios.
- Reduced motion
- The selection jumps instead of gliding.
Notes
More than five options, or labels longer than a word or two, belong in a select or tabs.
Underline
Text options with a short line that glides under the chosen one. Lighter than a filled control, with optional counts.
Installation
$npx shadcn@latest add @stillui/segmented-underlineOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/segmented-control#underline
import { motion, useReducedMotion } from "motion/react";
import { useId, useRef, useState, type KeyboardEvent } from "react";
import { cn } from "@/lib/utils";
const GLIDE = { type: "spring", stiffness: 480, damping: 42 } as const;
type Option = { value: string; label: string; count?: number };
type SegmentedUnderlineProps = {
options: Option[];
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
label: string;
className?: string;
};
/** Text options with a line that glides under the chosen one. Lighter than a filled control. */
export function SegmentedUnderline({ options, value, defaultValue, onValueChange, label, className }: SegmentedUnderlineProps) {
const reduce = useReducedMotion();
const layoutId = useId();
const [internal, setInternal] = useState(defaultValue ?? options[0]?.value);
const current = value ?? internal;
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const select = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const step = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0;
if (!step) return;
event.preventDefault();
const index = options.findIndex((o) => o.value === current);
const next = (index + step + options.length) % options.length;
select(options[next].value);
refs.current[next]?.focus();
};
return (
<div role="radiogroup" aria-label={label} onKeyDown={onKeyDown} className={cn("inline-flex items-center gap-1", className)}>
{options.map((option, i) => {
const selected = option.value === current;
return (
<button
key={option.value}
ref={(el) => {
refs.current[i] = el;
}}
type="button"
role="radio"
aria-checked={selected}
tabIndex={selected ? 0 : -1}
onClick={() => select(option.value)}
className={cn(
"relative flex h-10 items-center gap-1.5 rounded-md px-3 text-sm font-medium transition-[color,background-color,box-shadow,scale] active:scale-[0.97] duration-150",
selected ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{option.label}
{option.count !== undefined ? (
<span className={cn("text-[12px] tabular-nums", selected ? "text-muted-foreground" : "text-faint-foreground")}>{option.count}</span>
) : null}
{selected ? (
<motion.span
layoutId={layoutId}
aria-hidden
className="absolute inset-x-3 -bottom-px h-0.5 rounded-full bg-foreground"
transition={reduce ? { duration: 0 } : GLIDE}
/>
) : null}
</button>
);
})}
</div>
);
}States
- Selected
- Full-color text with a line underneath.
- Others
- Muted text that brightens on hover.
- Switching
- The line glides across on a spring with no overshoot.
- Counts
- An optional number beside each label, in a quieter color.
- Keyboard
- One tab stop; arrow keys move and select, like a radio group.
- Reduced motion
- The line jumps instead of gliding.
Icons
Icon-only options for view switches like grid, list and board. Each option keeps a text name for screen readers.
Installation
$npx shadcn@latest add @stillui/segmented-iconsOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/segmented-control#icons
import { motion, useReducedMotion } from "motion/react";
import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const GLIDE = { type: "spring", stiffness: 420, damping: 40, mass: 0.8 } as const;
type Option = { value: string; label: string; icon: ReactNode };
type SegmentedIconsProps = {
options: Option[];
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
label: string;
className?: string;
};
/** Icon-only options, for view switches like grid and list. Each option keeps a text name for screen readers. */
export function SegmentedIcons({ options, value, defaultValue, onValueChange, label, className }: SegmentedIconsProps) {
const reduce = useReducedMotion();
const layoutId = useId();
const [internal, setInternal] = useState(defaultValue ?? options[0]?.value);
const current = value ?? internal;
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const select = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const step = event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 0;
if (!step) return;
event.preventDefault();
const index = options.findIndex((o) => o.value === current);
const next = (index + step + options.length) % options.length;
select(options[next].value);
refs.current[next]?.focus();
};
return (
<div role="radiogroup" aria-label={label} onKeyDown={onKeyDown} className={cn("inline-flex items-center rounded-lg bg-muted p-1", className)}>
{options.map((option, i) => {
const selected = option.value === current;
return (
<button
key={option.value}
ref={(el) => {
refs.current[i] = el;
}}
type="button"
role="radio"
aria-checked={selected}
aria-label={option.label}
title={option.label}
tabIndex={selected ? 0 : -1}
onClick={() => select(option.value)}
className={cn(
"relative grid size-8 place-items-center rounded-md transition-[color,background-color,box-shadow,scale] active:scale-[0.92] duration-150 [&_svg]:relative [&_svg]:size-4",
selected ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{selected ? (
<motion.span
layoutId={layoutId}
aria-hidden
className="absolute inset-0 rounded-md bg-card shadow-raised"
transition={reduce ? { duration: 0 } : GLIDE}
/>
) : null}
{option.icon}
</button>
);
})}
</div>
);
}States
- Selected
- The icon sits on a raised surface that glides between options.
- Others
- Muted icons that brighten on hover; the name shows as a native tooltip.
- Keyboard
- One tab stop; arrow keys move and select.
- Screen readers
- Every icon has a label, so the group reads as “Layout, Grid, selected”.
- Reduced motion
- The surface jumps instead of gliding.
Notes
Only use icons people already know. If you need a legend, use the labelled control instead.
Expanding
Icons that make room for the chosen option’s label. Compact like an icon control, but never mysterious.
Installation
$npx shadcn@latest add @stillui/segmented-expandOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/segmented-control#expand
import { AnimatePresence, motion, MotionConfig, useReducedMotion } from "motion/react";
import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const SPRING = { type: "spring", stiffness: 460, damping: 38 } as const;
type Option = { value: string; label: string; icon: ReactNode };
type SegmentedExpandProps = {
options: Option[];
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
label: string;
className?: string;
};
/** Icons that make room for the chosen option’s label. The control stays compact but never mysterious. */
export function SegmentedExpand({ options, value, defaultValue, onValueChange, label, className }: SegmentedExpandProps) {
const reduce = useReducedMotion();
const layoutId = useId();
const [internal, setInternal] = useState(defaultValue ?? options[0]?.value);
const current = value ?? internal;
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const select = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const step = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0;
if (!step) return;
event.preventDefault();
const index = options.findIndex((o) => o.value === current);
const next = (index + step + options.length) % options.length;
select(options[next].value);
refs.current[next]?.focus();
};
return (
<MotionConfig transition={reduce ? { duration: 0 } : SPRING}>
<div role="radiogroup" aria-label={label} onKeyDown={onKeyDown} className={cn("inline-flex items-center rounded-full bg-muted p-1", className)}>
{options.map((option, i) => {
const selected = option.value === current;
return (
<motion.button
key={option.value}
ref={(el) => {
refs.current[i] = el;
}}
layout={!reduce}
type="button"
role="radio"
aria-checked={selected}
aria-label={option.label}
tabIndex={selected ? 0 : -1}
onClick={() => select(option.value)}
style={{ borderRadius: 999 }}
className={cn(
"relative flex h-8 items-center px-2 text-[13px] font-medium transition-[color,background-color,box-shadow,scale] active:scale-[0.95] duration-150 [&_svg]:relative [&_svg]:size-4 [&_svg]:shrink-0",
selected ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{selected ? (
<motion.span layoutId={layoutId} aria-hidden className="absolute inset-0 bg-card shadow-raised" style={{ borderRadius: 999 }} />
) : null}
{option.icon}
<AnimatePresence initial={false}>
{selected ? (
<motion.span
aria-hidden
className="relative overflow-hidden whitespace-nowrap"
initial={{ width: 0, opacity: 0 }}
animate={{ width: "auto", opacity: 1 }}
exit={{ width: 0, opacity: 0 }}
>
<span className="block pl-1.5 pr-1">{option.label}</span>
</motion.span>
) : null}
</AnimatePresence>
</motion.button>
);
})}
</div>
</MotionConfig>
);
}States
- Selected
- Icon and label on a raised pill.
- Others
- Icons only, muted, brightening on hover.
- Switching
- The old label tucks away while the new one slides out, and the pill glides between them in one spring.
- Keyboard
- One tab stop; left and right arrows move and select.
- Screen readers
- Every option has its label as its name, shown or not.
- Reduced motion
- Labels swap and the pill jumps without sliding.