Segmented control
A tab-style switch whose highlight slides and stretches between options like a drop of liquid.
"use client";
import { useId, useLayoutEffect, useRef, useState } from "react";
import "./segmented-control.css";
type SegmentedControlProps = {
options: string[];
defaultValue?: string;
onChange?: (value: string) => void;
label: string; // read by screen readers, e.g. "Billing period"
theme?: "light" | "dark";
};
/**
* SegmentedControl
* Built on real radio inputs, so arrow keys, Tab and screen readers work for free.
* The highlight pill is measured and moved with CSS variables. While moving it
* stretches to cover both the old and new option, then snaps to size.
*/
export function SegmentedControl({
options,
defaultValue,
onChange,
label,
theme = "light",
}: SegmentedControlProps) {
const [value, setValue] = useState(defaultValue ?? options[0]);
const root = useRef<HTMLDivElement>(null);
const name = useId();
// Measure the selected option and move the pill there.
useLayoutEffect(() => {
const el = root.current;
const active = el?.querySelector<HTMLElement>(
`[data-value="${CSS.escape(value)}"]`,
);
if (!el || !active) return;
const prevLeft = parseFloat(
el.style.getPropertyValue("--left") || `${active.offsetLeft}`,
);
const prevWidth = parseFloat(
el.style.getPropertyValue("--width") || `${active.offsetWidth}`,
);
const left = active.offsetLeft;
const width = active.offsetWidth;
// Stretch phase: cover from the old spot to the new one...
const from = Math.min(prevLeft, left);
const to = Math.max(prevLeft + prevWidth, left + width);
el.style.setProperty("--left", `${from}px`);
el.style.setProperty("--width", `${to - from}px`);
// ...then settle on the new option on the next frame.
const id = requestAnimationFrame(() =>
requestAnimationFrame(() => {
el.style.setProperty("--left", `${left}px`);
el.style.setProperty("--width", `${width}px`);
}),
);
return () => cancelAnimationFrame(id);
}, [value]);
return (
<div
ref={root}
className="gu-seg"
data-theme={theme}
role="radiogroup"
aria-label={label}
>
<span className="gu-seg__pill" aria-hidden="true" />
{options.map((option) => (
<label
key={option}
className="gu-seg__option"
data-value={option}
data-checked={option === value || undefined}
>
<input
type="radio"
name={name}
value={option}
checked={option === value}
onChange={() => {
setValue(option);
onChange?.(option);
}}
/>
{option}
</label>
))}
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Segmented control
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A pill-shaped segmented control (like iOS tabs) where the orange highlight slides between options and briefly stretches to cover both the old and the new option, like a drop of liquid, before settling.
## Layout and look
- Track: inline-flex, 5px padding, 2px gap, fully rounded, background #e9e2d3 (light) / #1f1f23 (dark).
- Options: min-height 44px, padding 0 20px, system sans-serif 600 15px, color #57534b (light) / #aea89c (dark); selected and hovered text #0e0e10.
- Highlight pill: #ff5a1f, fully rounded, inset 5px top and bottom, shadow 0 6px 16px -6px rgba(255,90,31,0.7).
## Behavior and motion
1. Build on real radio inputs inside labels (inputs visually hidden but covering the label), inside a role="radiogroup" with an aria-label.
2. When the value changes, measure the selected label's offsetLeft and offsetWidth.
3. Stretch phase: set the pill to span from the old option's left edge to the new option's right edge (or vice versa).
4. Two animation frames later, set the pill to exactly the new option.
5. left and width transition over 420ms with an overshooting curve cubic-bezier(0.34, 1.4, 0.5, 1).
## Touch
Tap targets are at least 44px tall. Tapping behaves exactly like clicking.
## Accessibility
- Arrow keys move between options natively (radio group). Focus ring: 2px outline in currentColor around the focused label.
- prefers-reduced-motion: the pill moves instantly with no transition.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### SegmentedControl.tsx
```tsx
"use client";
import { useId, useLayoutEffect, useRef, useState } from "react";
import "./segmented-control.css";
type SegmentedControlProps = {
options: string[];
defaultValue?: string;
onChange?: (value: string) => void;
label: string; // read by screen readers, e.g. "Billing period"
theme?: "light" | "dark";
};
/**
* SegmentedControl
* Built on real radio inputs, so arrow keys, Tab and screen readers work for free.
* The highlight pill is measured and moved with CSS variables. While moving it
* stretches to cover both the old and new option, then snaps to size.
*/
export function SegmentedControl({
options,
defaultValue,
onChange,
label,
theme = "light",
}: SegmentedControlProps) {
const [value, setValue] = useState(defaultValue ?? options[0]);
const root = useRef<HTMLDivElement>(null);
const name = useId();
// Measure the selected option and move the pill there.
useLayoutEffect(() => {
const el = root.current;
const active = el?.querySelector<HTMLElement>(
`[data-value="${CSS.escape(value)}"]`,
);
if (!el || !active) return;
const prevLeft = parseFloat(
el.style.getPropertyValue("--left") || `${active.offsetLeft}`,
);
const prevWidth = parseFloat(
el.style.getPropertyValue("--width") || `${active.offsetWidth}`,
);
const left = active.offsetLeft;
const width = active.offsetWidth;
// Stretch phase: cover from the old spot to the new one...
const from = Math.min(prevLeft, left);
const to = Math.max(prevLeft + prevWidth, left + width);
el.style.setProperty("--left", `${from}px`);
el.style.setProperty("--width", `${to - from}px`);
// ...then settle on the new option on the next frame.
const id = requestAnimationFrame(() =>
requestAnimationFrame(() => {
el.style.setProperty("--left", `${left}px`);
el.style.setProperty("--width", `${width}px`);
}),
);
return () => cancelAnimationFrame(id);
}, [value]);
return (
<div
ref={root}
className="gu-seg"
data-theme={theme}
role="radiogroup"
aria-label={label}
>
<span className="gu-seg__pill" aria-hidden="true" />
{options.map((option) => (
<label
key={option}
className="gu-seg__option"
data-value={option}
data-checked={option === value || undefined}
>
<input
type="radio"
name={name}
value={option}
checked={option === value}
onChange={() => {
setValue(option);
onChange?.(option);
}}
/>
{option}
</label>
))}
</div>
);
}
```
### segmented-control.css
```css
.gu-seg {
--track: #e9e2d3;
--text: #57534b;
--text-on: #0e0e10;
--pill: #ff5a1f;
--left: 0px;
--width: 0px;
position: relative;
display: inline-flex;
padding: 5px;
gap: 2px;
border-radius: 999px;
background: var(--track);
font:
600 0.95rem/1 system-ui,
sans-serif;
}
.gu-seg[data-theme="dark"] {
--track: #1f1f23;
--text: #aea89c;
--text-on: #0e0e10;
}
/* The moving highlight. */
.gu-seg__pill {
position: absolute;
top: 5px;
bottom: 5px;
left: var(--left);
width: var(--width);
border-radius: 999px;
background: var(--pill);
box-shadow: 0 6px 16px -6px rgb(255 90 31 / 0.7);
transition:
left 0.42s cubic-bezier(0.34, 1.4, 0.5, 1),
width 0.42s cubic-bezier(0.34, 1.4, 0.5, 1);
}
.gu-seg__option {
position: relative; /* sits above the pill */
display: inline-flex;
align-items: center;
min-height: 44px;
padding: 0 1.25rem;
border-radius: 999px;
color: var(--text);
cursor: pointer;
transition: color 0.25s;
-webkit-tap-highlight-color: transparent;
user-select: none;
}
.gu-seg__option:hover {
color: var(--text-on);
}
.gu-seg[data-theme="dark"] .gu-seg__option:hover:not([data-checked]) {
color: #f3eee3;
}
.gu-seg__option[data-checked] {
color: var(--text-on);
}
/* Hide the radio dot but keep it focusable and accessible. */
.gu-seg__option input {
position: absolute;
inset: 0;
opacity: 0;
margin: 0;
cursor: pointer;
}
.gu-seg__option:has(input:focus-visible) {
outline: 2px solid currentColor;
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.gu-seg__pill {
transition: none;
}
}
```
You might also want
Next piece: Infinite marqueeCommand menu
Components, intermediate
Announcement bar
Components, beginner
Collapsible sidebar
Components, intermediate