Segmented control
A pill that slides between options.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/, installsradix-ui@base-ui/reactand the colour tokens go into your CSS. This is the Radix build. Drop --radix for Base UI.This is the Base UI build, the default. Want Radix? Add --radix.$ pnpm dlx asta-ui@latest add segmented-control --radixOr install the package
Don’t want the file? Import from
asta-uiasta-ui/baseand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-ui radix-ui @base-ui/reactimport { SegmentedControl } from "asta-ui";import { SegmentedControl } from "asta-ui/base";Or copy it by hand
Install
radix-ui@base-ui/react, then copy this file. Keep the paths, or fix the@/imports. Then add the tokens fromglobals.css."use client"; import { RadioGroup as R } from "radix-ui"; import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); export interface Segment { value: string; label: ReactNode; icon?: ReactNode; } export interface SegmentedControlProps { segments: Segment[]; value?: string; defaultValue?: string; onChange?: (value: string) => void; size?: "sm" | "md"; /** Stretch to the parent's width, segments sharing it evenly. */ full?: boolean; label?: string; className?: string; } /** * A pill slides between options. While it travels it stretches toward the target and settles back, * which reads as a small squash rather than a plain slide. Built on Radix Radio Group, so ← → move and select. */ export function SegmentedControl({ segments, value, defaultValue, onChange, size = "md", full, label, className }: SegmentedControlProps) { const [inner, setInner] = useState(defaultValue ?? segments[0]?.value); const current = value ?? inner; const refs = useRef<(HTMLButtonElement | null)[]>([]); const box = useRef<HTMLDivElement>(null); const [pill, setPill] = useState<{ x: number; w: number } | null>(null); const [squash, setSquash] = useState(false); const index = Math.max(0, segments.findIndex((s) => s.value === current)); useLayoutEffect(() => { const measure = () => { const el = refs.current[index]; if (el) setPill({ x: el.offsetLeft, w: el.offsetWidth }); }; // ResizeObserver reports once right after layout, so there is no layout read while the page mounts. const ro = new ResizeObserver(measure); if (box.current) ro.observe(box.current); return () => ro.disconnect(); }, [index]); const pick = (v: string) => { if (v === current) return; setSquash(true); setTimeout(() => setSquash(false), 180); if (value === undefined) setInner(v); onChange?.(v); }; const h = size === "sm" ? "h-8 text-[13px]" : "h-10 text-[14px]"; return ( <R.Root ref={box} value={current} onValueChange={pick} orientation="horizontal" aria-label={label} className={cx( "relative inline-flex rounded-[10px] bg-paper-2 p-1 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_6%,transparent)]", full && "flex w-full", className, )} > {pill && ( <span aria-hidden // perf-ok: a small indicator whose size follows the item under it, once per change. className="absolute left-0 top-1 bottom-1 rounded-[7px] bg-paper shadow-[0_1px_2px_rgb(0_0_0/0.08),0_3px_10px_-4px_rgb(0_0_0/0.18),inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_8%,transparent)] transition-[transform,width] duration-500 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none" style={{ transform: `translateX(${pill.x}px) scaleY(${squash ? 0.86 : 1})`, width: pill.w }} /> )} {segments.map((s, i) => { const on = i === index; return ( <R.Item key={s.value} value={s.value} ref={(el) => { refs.current[i] = el; }} className={cx( "relative z-10 inline-flex items-center justify-center gap-1.5 whitespace-nowrap rounded-[7px] font-medium tracking-[-0.01em] outline-none transition-colors duration-300 focus-visible:ring-2 focus-visible:ring-brand/50 [&_svg]:h-[1.05em] [&_svg]:w-[1.05em]", h, size === "sm" ? "px-3" : "px-4", full && "flex-1", on ? "text-ink" : "text-mute hover:text-ink", )} > {s.icon} {s.label} </R.Item> ); })} </R.Root> ); } export default SegmentedControl;"use client"; import { Radio } from "@base-ui/react/radio"; import { RadioGroup } from "@base-ui/react/radio-group"; import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); export interface Segment { value: string; label: ReactNode; icon?: ReactNode; } export interface SegmentedControlProps { segments: Segment[]; value?: string; defaultValue?: string; onChange?: (value: string) => void; size?: "sm" | "md"; /** Stretch to the parent's width, segments sharing it evenly. */ full?: boolean; label?: string; className?: string; } /** * A pill slides between options. While it travels it stretches toward the target and settles back, * which reads as a small squash rather than a plain slide. Built on Base UI Radio Group, so ← → move and select. */ export function SegmentedControl({ segments, value, defaultValue, onChange, size = "md", full, label, className }: SegmentedControlProps) { const [inner, setInner] = useState(defaultValue ?? segments[0]?.value); const current = value ?? inner; const refs = useRef<(HTMLSpanElement | null)[]>([]); const box = useRef<HTMLDivElement>(null); const [pill, setPill] = useState<{ x: number; w: number } | null>(null); const [squash, setSquash] = useState(false); const index = Math.max(0, segments.findIndex((s) => s.value === current)); useLayoutEffect(() => { const measure = () => { const el = refs.current[index]; if (el) setPill({ x: el.offsetLeft, w: el.offsetWidth }); }; // ResizeObserver reports once right after layout, so there is no layout read while the page mounts. const ro = new ResizeObserver(measure); if (box.current) ro.observe(box.current); return () => ro.disconnect(); }, [index]); const pick = (v: string) => { if (v === current) return; setSquash(true); setTimeout(() => setSquash(false), 180); if (value === undefined) setInner(v); onChange?.(v); }; const h = size === "sm" ? "h-8 text-[13px]" : "h-10 text-[14px]"; return ( <RadioGroup ref={box} value={current} onValueChange={(v) => pick(String(v))} aria-label={label} className={cx( "relative inline-flex rounded-[10px] bg-paper-2 p-1 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_6%,transparent)]", full && "flex w-full", className, )} > {pill && ( <span aria-hidden // perf-ok: a small indicator whose size follows the item under it, once per change. className="absolute left-0 top-1 bottom-1 rounded-[7px] bg-paper shadow-[0_1px_2px_rgb(0_0_0/0.08),0_3px_10px_-4px_rgb(0_0_0/0.18),inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_8%,transparent)] transition-[transform,width] duration-500 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none" style={{ transform: `translateX(${pill.x}px) scaleY(${squash ? 0.86 : 1})`, width: pill.w }} /> )} {segments.map((s, i) => { const on = i === index; return ( <Radio.Root key={s.value} value={s.value} ref={(el) => { refs.current[i] = el; }} className={cx( "relative z-10 inline-flex cursor-pointer select-none items-center justify-center gap-1.5 whitespace-nowrap rounded-[7px] font-medium tracking-[-0.01em] outline-none transition-colors duration-300 focus-visible:ring-2 focus-visible:ring-brand/50 [&_svg]:h-[1.05em] [&_svg]:w-[1.05em]", h, size === "sm" ? "px-3" : "px-4", full && "flex-1", on ? "text-ink" : "text-mute hover:text-ink", )} > {s.icon} {s.label} </Radio.Root> ); })} </RadioGroup> ); } export default SegmentedControl;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Segmented control component to my project. A pill that slides between options. It includes: sm, md, icons. Install it (Radix build): ```sh pnpm dlx asta-ui@latest add segmented-control --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/segmented-control.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { SegmentedControl } from "@/components/astabyte/segmented-control"; ``` Usage example and props: https://ui.astabyte.com/segmented-control Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Radix primitives from the radix-ui package). - Stay on Radix: install other Astabyte UI components with `--radix` so the project uses one primitive library. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Add the Astabyte UI Segmented control component to my project. A pill that slides between options. It includes: sm, md, icons. Install it (Base UI build): ```sh pnpm dlx asta-ui@latest add segmented-control ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/base/segmented-control.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { SegmentedControl } from "@/components/astabyte/segmented-control"; ``` Usage example and props: https://ui.astabyte.com/segmented-control Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Base UI primitives from the @base-ui/react package). - Stay on Base UI: install other Astabyte UI components without `--radix` so the project uses one primitive library. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<form style="display:flex;flex-direction:column;align-items:flex-start;gap:16px"><div style="display:flex;flex-wrap:wrap;align-items:flex-start;gap:24px"><div role="radiogroup" data-asta-segmented aria-label="View" class="asta-segmented"><label class="asta-segmented-item"><input type="radio" name="view" value="list" > <span aria-hidden="true" data-asta-ind class="asta-segmented-pill"></span> <span class="asta-segmented-label"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 4h10M3 8h10M3 12h10"></path></svg> List</span></label><label class="asta-segmented-item"><input type="radio" name="view" value="board" checked > <span aria-hidden="true" data-asta-ind class="asta-segmented-pill"></span> <span class="asta-segmented-label"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 3h4v10H3zM9 3h4v6H9z"></path></svg> Board</span></label><label class="asta-segmented-item"><input type="radio" name="view" value="calendar" > <span aria-hidden="true" data-asta-ind class="asta-segmented-pill"></span> <span class="asta-segmented-label"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 4h10v9H3zM3 7h10M6 2v3M10 2v3"></path></svg> Calendar</span></label></div><div role="radiogroup" data-asta-segmented aria-label="Billing" class="asta-segmented asta-segmented-sm"><label class="asta-segmented-item"><input type="radio" name="plan" value="monthly" checked > <span aria-hidden="true" data-asta-ind class="asta-segmented-pill"></span> <span class="asta-segmented-label">Monthly</span></label><label class="asta-segmented-item"><input type="radio" name="plan" value="yearly" > <span aria-hidden="true" data-asta-ind class="asta-segmented-pill"></span> <span class="asta-segmented-label">Yearly</span></label></div></div></form>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { SegmentedControl } from "@/components/astabyte/segmented-control";
const Grid = () => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<rect x="4" y="4" width="6.5" height="6.5" rx="1.5" />
<rect x="13.5" y="4" width="6.5" height="6.5" rx="1.5" />
<rect x="4" y="13.5" width="6.5" height="6.5" rx="1.5" />
<rect x="13.5" y="13.5" width="6.5" height="6.5" rx="1.5" />
</svg>
);
const List = () => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d="M9 6.5h11" />
<path d="M9 12h11" />
<path d="M9 17.5h11" />
<path d="M4.5 6.5h.01" />
<path d="M4.5 12h.01" />
<path d="M4.5 17.5h.01" />
</svg>
);
const Board = () => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<rect x="3.5" y="4.5" width="17" height="15" rx="2" />
<path d="M8 8.5v6" />
<path d="M12 8.5v3" />
<path d="M16 8.5v8" />
</svg>
);
export default function SegmentedControlDemo() {
const [range, setRange] = useState("week");
return (
<div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
<div className="flex w-full max-w-md flex-col items-center gap-8">
<div className="flex flex-col items-center gap-3">
<SegmentedControl
label="Range"
value={range}
onChange={setRange}
segments={[
{ value: "day", label: "Day" },
{ value: "week", label: "Week" },
{ value: "month", label: "Month" },
{ value: "year", label: "Year" },
]}
/>
<p className="text-[13px] text-mute">
Showing the last <span className="text-ink">{range}</span>
</p>
</div>
<SegmentedControl
label="Layout"
size="sm"
defaultValue="grid"
segments={[
{ value: "grid", label: "Grid", icon: <Grid /> },
{ value: "list", label: "List", icon: <List /> },
{ value: "board", label: "Board", icon: <Board /> },
]}
/>
<SegmentedControl
label="Billing"
full
defaultValue="yearly"
segments={[
{ value: "monthly", label: "Monthly" },
{ value: "yearly", label: "Yearly · save 20%" },
]}
/>
</div>
</div>
);
}