# Segmented Control

Pick one of a few options that sit side by side.

Pick one of a few options that sit side by side. Content-sized segments on a field track, with a surface thumb that slides to the selected one. It is a radio group: one tab stop, arrow keys move and select. Behaviour comes from the Zag radio-group machine, through Ark UI's SegmentGroup in React, Vue and Svelte. Without JavaScript the native radios still work and the selected segment paints itself.

Kind: component · Category: Inputs · Status: stable · Targets: React, Vue, Svelte · Behaviour: Zag `radio-group` machine, through Ark UI

## Add

```bash
npx fndn add segmented-control
```

It writes the folder `segmented-control/` into the project's ui folder (`index.ts`, `segmented-control.tsx`, `SegmentedControl.vue`, `SegmentedControl.svelte`, `segmented-control.recipe.ts`), and what it is built from: [Count](/components/count.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { SegmentedControl } from "@/components/ui/segmented-control";
```

```vue
import { SegmentedControl } from "@/components/ui/segmented-control";
```

```svelte
import { SegmentedControl } from "$lib/components/ui/segmented-control";
```

## Parts

In the DOM the root carries `data-slot="segmented-control"` and each part `data-slot="segmented-control-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `segmented-control` |  | role=radiogroup. The track: its height is the segments' height plus 2px padding (chips: no track, a 4px-padded row that scrolls sideways). Labels are never cut: when the options do not fit, the engine glue (segmented-control.logic.ts) sets data-overflow and the row scrolls sideways with no scrollbar, a veil in the track colour on the edge(s) with more (chips: a 16px mask fade; data-overflow-start and data-overflow-end, kept in step with the scroll position by lib/veil.ts), the focus ring on the option's edge, and the selected option scrolled into view at mount and when it changes. Options that fit: nothing changes. |
| `thumb` | `div` | `segmented-control-thumb` |  | The engine's indicator. It is given the selected segment's box as --left / --top / --width / --height on itself and travels on transform; hidden until measured. Carries kbd-instant. |
| `option` | `label` | `segmented-control-option` | repeats | One segment; data-state=checked\|unchecked, data-focus-visible, data-disabled. An icon may precede the label. |
| `label` | `span` | `segmented-control-label` |  | The segment's text and accessible name; with icons it is also visually hidden (sr-only). In chips it is the label slot: an optional status dot, the text and an optional count. |
| `dot` | `span` | `segmented-control-dot` | optional | Status dot inside the label (aria-hidden): 6px, coloured by the option's dot tone: blue\|green\|orange\|red\|purple\|cyan\|yellow\|pink (the tag hues), ink-3 without a tone. |
| `input` | `input` | `segmented-control-input` |  | Native radio, visually hidden: focus, arrow keys and form value come from it. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | `{ value: string; label: string; disabled?: boolean; dot?: string; count?: number\|string }[]` |  | Segments in order. Strings are accepted as value = label. Each may carry an icon (a node in React, the `icon` slot in Vue, the `icon` snippet in Svelte), a status `dot` (a tag hue: blue, green, orange, red, purple, cyan, yellow, pink; true for ink-3) and a `count` (a plain `count`), both rendered inside the label. |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). Selected value. Defaults to the first option. |
| `variant` | `"segments" \| "chips"` | `"segments"` | segments: content-sized segments on a field track. chips: separate 26px chips with a 4px gap and no track, for a filter row; the thumb still slides between them, and the row scrolls sideways when it overflows. size does not apply to chips. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The track's height, on the one control scale (an sm segmented control lines up with an sm Button or Select): xs 24px (20px segments, 12px labels; the compact switcher), sm 28px (24px, 12px), md 32px (28px, 13px), lg 36px (32px, 14px). Dimensions only: the track, thumb, hover, press and focus are the same at every size. Does not apply to chips. |
| `shape` | `"pill" \| "rounded"` |  | Left out, the app's shape: a pill, or rounded in a rounded app ("shape": "rounded" in the project's fndn.json). Named, it is that shape whatever the app's. pill: fully round track and thumb. rounded: the radius of a field of the same height on the track (radius.chip at xs and sm, radius.control at md and lg). The thumb is concentric (the track's radius less the 2px padding). |
| `fill` | `boolean` | `false` | Equal-width segments that share the root's width. |
| `icons` | `boolean` | `false` | Icon-only square segments; each label stays as the accessible name. |
| `disabled` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name of the radios. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string) |

## Slots

| Slot | Content |
| --- | --- |
| `icon` | Icon of an option (Vue slot / Svelte snippet, called with the option). In React an option carries `icon` itself. |

## States

`selected`, `hover`, `press`, `press-selected`, `focus-visible`, `disabled`, `rounded`, `chips`

## Keyboard and accessibility

Role: radiogroup.

Keyboard:

- Tab reaches the selected segment; the group is one tab stop
- ArrowRight / ArrowDown select the next segment, ArrowLeft / ArrowUp the previous; they wrap
- Home / End select the first / last enabled segment
- Space selects the focused segment

Notes:

- Give the group an aria-label (or aria-labelledby).
- Icon-only segments keep their label as the accessible name.
- A selection made from the keyboard moves the thumb instantly; the slide is for the pointer.
- To select from outside, control `value`: React value / onValueChange, Vue v-model:value, Svelte bind:value.

## Motion

- thumb: transform and width over duration.move with easing.move, only when the user selects; no slide on mount and none when fonts load or the container resizes
- segment text: colour over duration.base
- press: no background anywhere in the control. The pressed segment's label goes to ink and the segment scales to motion.press-scale over duration.press; pressing the selected segment also scales the thumb (its ::before, with the individual scale property, so the engine's translate is untouched)
- chips: the same thumb, hover, press and keyboard rules as segments; an unselected chip never paints the thumb's look
- overflow: the edge veils (chips: the edge fades) show and hide at once from the scroll position (lib/veil.ts, the same in every browser; no animation, no time involved); the selected option is scrolled into view instantly at mount, from the keyboard and under reduced motion, smoothly after a pointer pick; the thumb rides the scrolled content and never re-measures for a scroll; an option wider than the room between the veils shows its start, in both directions
- mount: the thumb, data-overflow and the veils are measured before the first paint (a layout effect), so a fresh control (a docs page swap) never paints a frame without its thumb

## Tokens

`color.field`, `color.surface`, `color.hover-2`, `color.ink`, `color.ink-2`, `color.ink-3`, `color.accent`, `shadow.btn`, `radius.pill`, `radius.shape`, `radius.shape-sm`, `radius.control`, `radius.chip`, `text.sm`, `text.md`, `duration.base`, `duration.press`, `duration.move`, `easing.move`, `easing.out-strong`, `motion.press-scale`, `text.xs`, `color.tag-blue`, `color.tag-green`, `color.tag-orange`, `color.tag-red`, `color.tag-purple`, `color.tag-cyan`, `color.tag-yellow`, `color.tag-pink`

## Dependencies

- [Count](/components/count.md)

## Examples

### Range of a usage figure

React · `chart-range.tsx`

```tsx
import { useState } from "react";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* one of a few ranges, side by side; the figure under it follows the choice */
const ranges = [
  { value: "24h", label: "24h" },
  { value: "7d", label: "7d" },
  { value: "30d", label: "30d" },
  { value: "90d", label: "90d" },
];
const REQUESTS: Record<string, string> = { "24h": "182,400", "7d": "1.2M", "30d": "5.6M", "90d": "16.9M" };

export default function ChartRange() {
  const [range, setRange] = useState("7d");
  return (
    <div className="flex w-80 flex-col gap-1">
      <div className="flex items-center justify-between gap-3">
        <p className="text-sm font-medium text-ink">Requests</p>
        <SegmentedControl size="sm" options={ranges} value={range} onValueChange={setRange} aria-label="Range" />
      </div>
      <p className="text-2xl font-semibold text-ink tabular-nums">{REQUESTS[range]}</p>
      <p className="text-xs text-ink-2">web-app · Production · 98.2% under 300 ms</p>
    </div>
  );
}
```

Vue · `chart-range.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* one of a few ranges, side by side; the figure under it follows the choice */
const ranges = [
  { value: "24h", label: "24h" },
  { value: "7d", label: "7d" },
  { value: "30d", label: "30d" },
  { value: "90d", label: "90d" },
];
const REQUESTS: Record<string, string> = { "24h": "182,400", "7d": "1.2M", "30d": "5.6M", "90d": "16.9M" };

const range = ref("7d");
</script>

<template>
  <div class="flex w-80 flex-col gap-1">
    <div class="flex items-center justify-between gap-3">
      <p class="text-sm font-medium text-ink">Requests</p>
      <SegmentedControl v-model:value="range" size="sm" :options="ranges" aria-label="Range" />
    </div>
    <p class="text-2xl font-semibold text-ink tabular-nums">{{ REQUESTS[range] }}</p>
    <p class="text-xs text-ink-2">web-app · Production · 98.2% under 300 ms</p>
  </div>
</template>
```

Svelte · `chart-range.svelte`

```svelte
<script lang="ts">
  import { SegmentedControl } from "$lib/components/ui/segmented-control";

  /* one of a few ranges, side by side; the figure under it follows the choice */
  const ranges = [
    { value: "24h", label: "24h" },
    { value: "7d", label: "7d" },
    { value: "30d", label: "30d" },
    { value: "90d", label: "90d" },
  ];
  const REQUESTS: Record<string, string> = { "24h": "182,400", "7d": "1.2M", "30d": "5.6M", "90d": "16.9M" };

  let range = $state("7d");
</script>

<div class="flex w-80 flex-col gap-1">
  <div class="flex items-center justify-between gap-3">
    <p class="text-sm font-medium text-ink">Requests</p>
    <SegmentedControl bind:value={range} size="sm" options={ranges} aria-label="Range" />
  </div>
  <p class="text-2xl font-semibold text-ink tabular-nums">{REQUESTS[range]}</p>
  <p class="text-xs text-ink-2">web-app · Production · 98.2% under 300 ms</p>
</div>
```

### Theme, icon-only

React · `theme.tsx`

```tsx
import { Monitor, Moon, Sun } from "@phosphor-icons/react";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* icons: square cells; each label stays in the markup as the accessible name */
const themes = [
  { value: "light", label: "Light", icon: <Sun data-slot="icon" weight="fill" /> },
  { value: "dark", label: "Dark", icon: <Moon data-slot="icon" weight="fill" /> },
  { value: "system", label: "System", icon: <Monitor data-slot="icon" weight="bold" /> },
];

export default function Theme() {
  return (
    <div className="flex items-center gap-3">
      <span className="text-sm text-ink-2">Theme</span>
      <SegmentedControl options={themes} defaultValue="system" size="sm" icons aria-label="Theme" />
    </div>
  );
}
```

Vue · `theme.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhMonitor, PhMoon, PhSun } from "@phosphor-icons/vue";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* icons: square cells; each label stays in the markup as the accessible name */
const themes = [
  { value: "light", label: "Light" },
  { value: "dark", label: "Dark" },
  { value: "system", label: "System" },
];
const theme = ref("system");
</script>

<template>
  <div class="flex items-center gap-3">
    <span class="text-sm text-ink-2">Theme</span>
    <SegmentedControl v-model:value="theme" :options="themes" size="sm" icons aria-label="Theme">
      <template #icon="{ option }">
        <PhSun v-if="option.value === 'light'" data-slot="icon" weight="fill" />
        <PhMoon v-else-if="option.value === 'dark'" data-slot="icon" weight="fill" />
        <PhMonitor v-else data-slot="icon" weight="bold" />
      </template>
    </SegmentedControl>
  </div>
</template>
```

Svelte · `theme.svelte`

```svelte
<script lang="ts">
  import Monitor from "phosphor-svelte/lib/MonitorIcon";
  import Moon from "phosphor-svelte/lib/MoonIcon";
  import Sun from "phosphor-svelte/lib/SunIcon";
  import { SegmentedControl } from "$lib/components/ui/segmented-control";

  /* icons: square cells; each label stays in the markup as the accessible name */
  const themes = [
    { value: "light", label: "Light" },
    { value: "dark", label: "Dark" },
    { value: "system", label: "System" },
  ];
  let theme = $state("system");
</script>

<div class="flex items-center gap-3">
  <span class="text-sm text-ink-2">Theme</span>
  <SegmentedControl bind:value={theme} options={themes} size="sm" icons aria-label="Theme">
    {#snippet icon({ option })}
      {#if option.value === "light"}<Sun data-slot="icon" weight="fill" />
      {:else if option.value === "dark"}<Moon data-slot="icon" weight="fill" />
      {:else}<Monitor data-slot="icon" weight="bold" />{/if}
    {/snippet}
  </SegmentedControl>
</div>
```

### List or board, beside a small button

React · `view-switch.tsx`

```tsx
import { Columns, ListBullets, Plus } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* sm (28px) beside an sm Button: one control height across the toolbar */
const views = [
  { value: "list", label: "List", icon: <ListBullets data-slot="icon" weight="bold" /> },
  { value: "board", label: "Board", icon: <Columns data-slot="icon" weight="bold" /> },
];

export default function ViewSwitch() {
  return (
    <div className="flex w-80 items-center justify-between gap-3">
      <SegmentedControl size="sm" options={views} defaultValue="board" aria-label="View" />
      <Button variant="primary" size="sm" icon={<Plus data-slot="icon" weight="bold" />}>New project</Button>
    </div>
  );
}
```

Vue · `view-switch.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhColumns, PhListBullets, PhPlus } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* sm (28px) beside an sm Button: one control height across the toolbar */
const views = [
  { value: "list", label: "List" },
  { value: "board", label: "Board" },
];
const view = ref("board");
</script>

<template>
  <div class="flex w-80 items-center justify-between gap-3">
    <SegmentedControl v-model:value="view" size="sm" :options="views" aria-label="View">
      <template #icon="{ option }">
        <PhListBullets v-if="option.value === 'list'" data-slot="icon" weight="bold" />
        <PhColumns v-else data-slot="icon" weight="bold" />
      </template>
    </SegmentedControl>
    <Button variant="primary" size="sm">
      <template #icon><PhPlus data-slot="icon" weight="bold" /></template>
      New project
    </Button>
  </div>
</template>
```

Svelte · `view-switch.svelte`

```svelte
<script lang="ts">
  import Columns from "phosphor-svelte/lib/ColumnsIcon";
  import ListBullets from "phosphor-svelte/lib/ListBulletsIcon";
  import Plus from "phosphor-svelte/lib/PlusIcon";
  import { Button } from "$lib/components/ui/button";
  import { SegmentedControl } from "$lib/components/ui/segmented-control";

  /* sm (28px) beside an sm Button: one control height across the toolbar */
  const views = [
    { value: "list", label: "List" },
    { value: "board", label: "Board" },
  ];
  let view = $state("board");
</script>

<div class="flex w-80 items-center justify-between gap-3">
  <SegmentedControl bind:value={view} size="sm" options={views} aria-label="View">
    {#snippet icon({ option })}
      {#if option.value === "list"}<ListBullets data-slot="icon" weight="bold" />
      {:else}<Columns data-slot="icon" weight="bold" />{/if}
    {/snippet}
  </SegmentedControl>
  <Button variant="primary" size="sm">
    {#snippet icon()}<Plus data-slot="icon" weight="bold" />{/snippet}
    New project
  </Button>
</div>
```

### Filter chips with counts

React · `status-chips.tsx`

```tsx
import type { ComponentProps } from "react";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* chips: a filter row over a list, each with a status dot and a count */
type Props = ComponentProps<typeof SegmentedControl>;

const states: Props["options"] = [
  { value: "all", label: "All", count: 31 },
  { value: "open", label: "Open", dot: "green", count: 12 },
  { value: "draft", label: "Draft", dot: true, count: 4 },
  { value: "merged", label: "Merged", dot: "purple", count: 15 },
];

export default function StatusChips() {
  return <SegmentedControl variant="chips" options={states} defaultValue="open" aria-label="Pull requests by status" />;
}
```

Vue · `status-chips.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* chips: a filter row over a list, each with a status dot and a count */
const states = [
  { value: "all", label: "All", count: 31 },
  { value: "open", label: "Open", dot: "green" as const, count: 12 },
  { value: "draft", label: "Draft", dot: true as const, count: 4 },
  { value: "merged", label: "Merged", dot: "purple" as const, count: 15 },
];

const status = ref("open");
</script>

<template>
  <SegmentedControl v-model:value="status" variant="chips" :options="states" aria-label="Pull requests by status" />
</template>
```

Svelte · `status-chips.svelte`

```svelte
<script lang="ts">
  import { SegmentedControl } from "$lib/components/ui/segmented-control";

  /* chips: a filter row over a list, each with a status dot and a count */
  const states = [
    { value: "all", label: "All", count: 31 },
    { value: "open", label: "Open", dot: "green" as const, count: 12 },
    { value: "draft", label: "Draft", dot: true as const, count: 4 },
    { value: "merged", label: "Merged", dot: "purple" as const, count: 15 },
  ];

  let status = $state("open");
</script>

<SegmentedControl bind:value={status} variant="chips" options={states} aria-label="Pull requests by status" />
```

### Backups, one option not on your plan

React · `backup-frequency.tsx`

```tsx
import { SegmentedControl } from "@/components/ui/segmented-control";

/* fill: equal segments across a settings panel; an option the plan lacks is disabled and explained */
const frequencies = [
  { value: "hourly", label: "Hourly", disabled: true },
  { value: "daily", label: "Daily" },
  { value: "weekly", label: "Weekly" },
];

export default function BackupFrequency() {
  return (
    <div className="flex w-80 flex-col gap-2">
      <p id="backup-label" className="text-sm font-medium text-ink">Database backups</p>
      <SegmentedControl fill options={frequencies} defaultValue="daily" aria-labelledby="backup-label" />
      <p className="text-xs text-ink-2">Hourly backups are on the Business plan.</p>
    </div>
  );
}
```

Vue · `backup-frequency.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SegmentedControl } from "@/components/ui/segmented-control";

/* fill: equal segments across a settings panel; an option the plan lacks is disabled and explained */
const frequencies = [
  { value: "hourly", label: "Hourly", disabled: true },
  { value: "daily", label: "Daily" },
  { value: "weekly", label: "Weekly" },
];
const frequency = ref("daily");
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <p id="backup-label" class="text-sm font-medium text-ink">Database backups</p>
    <SegmentedControl v-model:value="frequency" fill :options="frequencies" aria-labelledby="backup-label" />
    <p class="text-xs text-ink-2">Hourly backups are on the Business plan.</p>
  </div>
</template>
```

Svelte · `backup-frequency.svelte`

```svelte
<script lang="ts">
  import { SegmentedControl } from "$lib/components/ui/segmented-control";

  /* fill: equal segments across a settings panel; an option the plan lacks is disabled and explained */
  const frequencies = [
    { value: "hourly", label: "Hourly", disabled: true },
    { value: "daily", label: "Daily" },
    { value: "weekly", label: "Weekly" },
  ];
  let frequency = $state("daily");
</script>

<div class="flex w-80 flex-col gap-2">
  <p id="backup-label" class="text-sm font-medium text-ink">Database backups</p>
  <SegmentedControl bind:value={frequency} fill options={frequencies} aria-labelledby="backup-label" />
  <p class="text-xs text-ink-2">Hourly backups are on the Business plan.</p>
</div>
```

## Recipe

`segmented-control.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* segmented-control — a switcher on the radio-group engine, in the app's shape (a pill unless the
 * app is rounded).
 * The track is on the one control height scale: an sm segmented control lines up with
 * an sm Button or Select. Segments are the track less its 2px padding. Size changes dimensions
 * only; every size has the same track, thumb and focus.
 *   xs            24px track · 20px segments · 12px   (the compact switcher)
 *   sm            28px track · 24px segments · 12px
 *   md (default)  32px track · 28px segments · 13px
 *   lg            36px track · 32px segments · 14px
 *   chips         separate 26px chips with a gap and no track, for a filter row (size does not apply)
 * Segments are <label>s around a visually hidden native radio, so focus, arrow keys and the form
 * value are the browser's own. The track has no height of its own: segments set it, the track adds
 * 2px of padding. That is what keeps icon cells centred on the thumb. Labels are always whole: when
 * the options do not fit, the engine glue sets data-overflow and the row scrolls sideways.
 *
 * Everything is here, motion included. Parts are found by data-slot (segmented-control,
 * segmented-control-thumb, -option, -label, -dot, -input); the root slot is `base`. data-overflow
 * is written by the engine glue (segmented-control.logic.ts), data-overflow-start and
 * data-overflow-end by watchVeils (lib/veil.ts), not by the adapters, so they stay attributes. */

/* The edge veils of an overflowing row (segments only: chips have no track). The edge the row
 * continues past fades into the track, so an option cut by the edge never reads as a stray letter:
 * two sticky veils ride the scroller's edges, each shown while there is more on its side, at once
 * (no time involved): data-overflow-start and data-overflow-end follow the scroll position (as in
 * selection-actions). Each nets to nothing in the row (16px minus the 2px padding it reaches into minus
 * 14px): the options do not move. Without data-overflow none of this applies. */
const VEILS = [
  "data-overflow:[&:dir(rtl)]:[--_end:left] data-overflow:[&:dir(rtl)]:[--_start:right]",
  "data-overflow:before:content-[''] data-overflow:before:sticky data-overflow:before:z-3 data-overflow:before:flex-[0_0_16px] data-overflow:before:-my-0.5 data-overflow:before:pointer-events-none data-overflow:before:opacity-0",
  "data-overflow:after:content-[''] data-overflow:after:sticky data-overflow:after:z-3 data-overflow:after:flex-[0_0_16px] data-overflow:after:-my-0.5 data-overflow:after:pointer-events-none data-overflow:after:opacity-0",
  "data-overflow:before:start-0 data-overflow:before:ms-[-2px] data-overflow:before:me-[-14px] data-overflow:before:rounded-s-(--_radius) data-overflow:before:bg-[linear-gradient(to_var(--_end,right),var(--_veil),transparent)]",
  "data-overflow:after:end-0 data-overflow:after:ms-[-14px] data-overflow:after:me-[-2px] data-overflow:after:rounded-e-(--_radius) data-overflow:after:bg-[linear-gradient(to_var(--_start,left),var(--_veil),transparent)]",
  "data-overflow:data-overflow-start:before:opacity-100 data-overflow:data-overflow-end:after:opacity-100",
];

/* until the thumb is measured (first paint, server render) the selected segment paints itself
 * exactly like the thumb, so the hand-over is invisible */
const UNMEASURED = "[[data-slot=segmented-control-thumb][hidden]~&]:data-[state=checked]";

export const segmentedControl = tv({
  slots: {
    /* --_radius: the track's radius, the app's shape unless `shape` names one; the overflow veils
     * read it too. --_thumb-radius: the thumb's, concentric (the track's less the 2px padding) */
    base: [
      "relative inline-flex items-stretch max-w-full p-0.5 bg-field select-none",
      "[--_radius:var(--fndn-radius-shape)] [--_thumb-radius:calc(var(--_radius)-2px)] rounded-(--_radius)",
    ],
    /* the engine's indicator: it hands the thumb its box as --left / --top / --width / --height and
     * switches the transition on only while a selection is being made (no slide on mount, none when
     * fonts load or the container resizes). We choose what travels: transform instead of left (the
     * engine's inline `left: var(--left)` is overridden: offsets are physical, also right-to-left).
     * width travels too: the thumb is childless and scaleX would distort its radius.
     * Its paint lives on ::before, so pressing the selected segment scales the paint while the
     * engine keeps the thumb's own transition off at rest. */
    thumb: [
      "kbd-instant",
      "absolute top-0 [left:0]! w-(--width) h-(--height) rounded-(--_thumb-radius) pointer-events-none",
      "[transform:translate(var(--left,0px),var(--top,0px))]",
      "[--transition-property:transform,width]! [--transition-duration:var(--fndn-duration-move)] [--transition-timing-function:var(--fndn-easing-move)]",
      "before:absolute before:inset-0 before:rounded-[inherit] before:bg-surface before:shadow-btn",
      "before:motion-transform-press [[data-slot=segmented-control]:has(>[data-slot=segmented-control-option][data-state=checked]:active)>&]:before:squeeze-press",
    ],
    /* press: no background (a grey block under the pressed segment would vanish on release while
     * the thumb is still on its way). The label goes to ink and the segment gives a little. */
    option: [
      "relative z-1 inline-flex items-center justify-center gap-1 shrink-0",
      "rounded-(--_thumb-radius) font-medium leading-none whitespace-nowrap text-ink-2",
      "cursor-pointer touch-manipulation [-webkit-touch-callout:none]",
      "motion-colors-base motion-transform-press kbd:duration-0",
      "hover:text-ink active:text-ink active:squeeze-press data-[state=checked]:text-ink",
      `${UNMEASURED}:bg-surface ${UNMEASURED}:shadow-btn`,
      "data-focus-visible:z-2 data-focus-visible:outline-2 data-focus-visible:outline-solid data-focus-visible:outline-accent data-focus-visible:outline-offset-2",
      "data-disabled:opacity-50 data-disabled:cursor-default data-disabled:pointer-events-none",
    ],
    /* never cut (the row scrolls instead); keeps its own direction on either page */
    label: "[unicode-bidi:plaintext]",
    /* status dot in a label: 6px, coloured by tone (the tag hues; none = ink-3) */
    dot: "shrink-0 size-1.5 rounded-[50%] bg-ink-3",
    /* the native radio: present for focus, keys and forms, never seen */
    input: "sr-only opacity-0 pointer-events-none",
  },
  variants: {
    /* dimensions only: the segment's height (the track adds its 2px padding), padding, text, icon */
    size: {
      xs: { base: "[--_radius:var(--fndn-radius-shape-sm)]", option: "h-5 px-2 text-xs [&>[data-slot=icon]]:text-[12px]" },
      sm: { base: "[--_radius:var(--fndn-radius-shape-sm)]", option: "h-6 px-2.5 text-xs [&>[data-slot=icon]]:text-[13px]" },
      md: { option: "h-7 px-3 text-sm [&>[data-slot=icon]]:text-[14px]" },
      lg: { option: "h-8 px-3.5 text-md [&>[data-slot=icon]]:text-[16px]" },
    },
    /* named, it is that shape whatever the app's */
    shape: {
      pill: { base: "[--_radius:var(--fndn-radius-pill)]" },
      rounded: { base: "[--_radius:var(--fndn-radius-control)]" },
    },
    /* equal-width segments sharing the root's width; a segment never gets narrower than its label */
    fill: {
      true: { base: "inline-grid grid-flow-col auto-cols-[minmax(max-content,1fr)]" },
    },
    /* square cells as tall as the segment; the label stays only as the accessible name */
    icons: {
      true: { option: "px-0", label: "sr-only" },
    },
    variant: {
      /* overflow (data-overflow, set by the engine glue): the row scrolls sideways with no
       * scrollbar; the focus ring moves onto the option's edge so the scroller does not clip it */
      segments: {
        base: [
          ...VEILS,
          /* the veil is the track's colour */
          "data-overflow:[--_veil:var(--fndn-field)]",
          "data-overflow:overflow-x-auto data-overflow:overflow-y-hidden data-overflow:overscroll-x-contain",
          "data-overflow:[scrollbar-width:none] data-overflow:[&::-webkit-scrollbar]:hidden",
        ],
        option: "[[data-slot=segmented-control][data-overflow]>&]:data-focus-visible:outline-offset-0",
      },
      /* the one thumb still slides between the chips; the 4px padding keeps the focus ring and the
       * thumb's shadow (3px below it) inside the scroller. The label holds a dot, the text and a count.
       * Overflowing, the edge the row continues past fades out: a mask, not a veil, because the row
       * has no colour of its own to fade into. Each side's fade is 16px while there is more on that
       * side (data-overflow-start, data-overflow-end: the scroll position, at once, no time
       * involved), the room revealSelected keeps clear; 0px otherwise. */
      chips: {
        base: [
          "flex gap-1 p-1 bg-transparent overflow-x-auto overscroll-x-contain [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
          "[&:dir(rtl)]:[--_end:left]",
          "data-overflow:[mask-image:linear-gradient(to_var(--_end,right),transparent,#000_var(--_fade-start,0px),#000_calc(100%_-_var(--_fade-end,0px)),transparent)]",
          "data-overflow-start:[--_fade-start:16px] data-overflow-end:[--_fade-end:16px]",
        ],
        thumb: "before:shadow-btn",
        option: ["shrink-0 h-[26px] px-2.5 gap-1.5 text-xs", `${UNMEASURED}:shadow-btn`],
        label: "inline-flex items-center gap-1.5 overflow-visible",
      },
    },
    tone: {
      none: {},
      blue: { dot: "bg-tag-blue" },
      green: { dot: "bg-tag-green" },
      orange: { dot: "bg-tag-orange" },
      red: { dot: "bg-tag-red" },
      purple: { dot: "bg-tag-purple" },
      cyan: { dot: "bg-tag-cyan" },
      yellow: { dot: "bg-tag-yellow" },
      pink: { dot: "bg-tag-pink" },
    },
  },
  compoundVariants: [
    /* icon-only: square cells as tall as the segment */
    { icons: true, size: "xs", class: { option: "w-5" } },
    { icons: true, size: "sm", class: { option: "w-6" } },
    { icons: true, size: "md", class: { option: "w-7" } },
    { icons: true, size: "lg", class: { option: "w-8" } },
    { icons: true, fill: true, class: { option: "w-auto" } },
    /* a control that fills its row is a grid of equal columns; scrolling, it is a row again (the
     * options at their own width, the veils at theirs). Only here does a state set the display:
     * elsewhere the display stays the app's to set (a class that hides the control keeps it hidden) */
    { variant: "segments", fill: true, class: { base: "data-overflow:inline-flex" } },
    /* rounded takes the radius of a field of the same height: chip up to 28px, control from 32px */
    { shape: "rounded", size: ["xs", "sm"], class: { base: "[--_radius:var(--fndn-radius-chip)]" } },
  ],
  defaultVariants: { size: "md", variant: "segments", tone: "none" },
});

export type SegmentedControlRecipeProps = VariantProps<typeof segmentedControl>;
```
