---
title: Segmented Control for Astro
description: Shared style recipes for related radio choices, navigation links, and pressed buttons.
seo:
    title: Segmented Control for Astro | ShadCN & Tailwind CSS
related:
    - /components/tabs
    - /components/group
sidebar:
    label: Segmented Control
    badge: new
---

```astro
---
import { RadioGroup } from "@/components/ui/radio-group"
import { Toggle } from "@/components/ui/toggle"
import {
  segmentedControlRootVariants,
  segmentedControlItemVariants,
} from "@/lib/segmented-control"

/** Radio rows share one choice. The label names the size of that group. */
const radioSizes = [
  { size: "sm", label: "Radio group, small" },
  { size: "default", label: "Radio group" },
  { size: "lg", label: "Radio group, large" },
] as const

const tiers = [
  { value: "all", label: "All", count: 63 },
  { value: "s-tier", label: "S Tier", count: 32 },
  { value: "a-tier", label: "A Tier", count: 31 },
]
---

<dl class="grid w-fit max-w-xl grid-cols-[max-content_minmax(0,1fr)] items-center gap-x-8 gap-y-5">
  {radioSizes.map(({ size, label }) => (
    <Fragment>
      <dt class="text-muted-foreground text-sm">{label}</dt>
      <dd>
        <RadioGroup
          name={`billing-${size}`}
          defaultValue="monthly"
          aria-label={`Billing period, ${size}`}
          class={segmentedControlRootVariants()}
        >
          <button
            type="button"
            data-slot="radio-group-item"
            data-value="monthly"
            class={segmentedControlItemVariants({ size, state: "checked" })}
          >
            Monthly
          </button>
          <button
            type="button"
            data-slot="radio-group-item"
            data-value="yearly"
            class={segmentedControlItemVariants({ size, state: "checked" })}
          >
            Yearly
          </button>
        </RadioGroup>
      </dd>
    </Fragment>
  ))}
  <dt class="text-muted-foreground text-sm">Inset outline</dt>
  <dd>
    <RadioGroup
      name="tier"
      defaultValue="all"
      aria-label="Tier"
      class={segmentedControlRootVariants({ variant: "inset-outline" })}
    >
      {tiers.map(({ value, label, count }) => (
        <button
          type="button"
          data-slot="radio-group-item"
          data-value={value}
          class={segmentedControlItemVariants({
            variant: "inset-outline",
            state: "checked",
          })}
        >
          {label}
          <span class="text-muted-foreground/70 tabular-nums">{count}</span>
        </button>
      ))}
    </RadioGroup>
  </dd>
  <dt class="text-muted-foreground text-sm">Current links</dt>
  <dd>
    <nav aria-label="Project guides" class={segmentedControlRootVariants()}>
      <a
        class={segmentedControlItemVariants({ state: "current" })}
        aria-current="page"
      >
        Choices
      </a>
      <a class={segmentedControlItemVariants({ state: "current" })}>Tabs</a>
      <a class={segmentedControlItemVariants({ state: "current" })}>Group</a>
    </nav>
  </dd>
  <dt class="text-muted-foreground text-sm">Pressed toggles</dt>
  <dd>
    <div
      role="group"
      aria-label="Independent view options"
      class={segmentedControlRootVariants()}
    >
      <Toggle
        defaultPressed
        class={segmentedControlItemVariants({ state: "pressed" })}
      >
        Guides
      </Toggle>
      <Toggle class={segmentedControlItemVariants({ state: "pressed" })}>
        Grid
      </Toggle>
    </div>
  </dd>
</dl>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/segmented-control
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/lib/segmented-control.ts

```ts
// Style recipe adapted from Coss UI - https://coss.com/ui/docs/components/segmented-control#about

import { cva } from 'class-variance-authority'

export type SegmentedControlSize = 'default' | 'lg' | 'sm'

export const segmentedControlItemSizeClassNames: Record<
    SegmentedControlSize,
    string
> = {
    default: 'h-8.5 px-[calc(--spacing(2.5)-1px)] sm:h-7.5',
    lg: 'h-9.5 px-[calc(--spacing(3)-1px)] sm:h-8.5',
    sm: 'h-7.5 px-[calc(--spacing(2)-1px)] sm:h-6.5',
}

export const segmentedControlRootVariants = cva(
    'relative z-0 flex w-fit flex-row items-center justify-center gap-0.5 p-0.5',
    {
        variants: {
            variant: {
                default: 'bg-muted rounded-lg',
                'inset-outline': 'bg-secondary rounded-full',
            },
        },
        defaultVariants: { variant: 'default' },
    },
)

export const segmentedControlRootClassName = segmentedControlRootVariants()

export const segmentedControlItemLayoutClassName =
    "gap-1.5 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:-mx-0.5 [&_svg]:shrink-0"

export const segmentedControlItemVariants = cva(
    [
        'text-muted-foreground/72 hover:text-muted-foreground focus-visible:outline-ring relative inline-flex shrink-0 cursor-pointer items-center justify-center border border-transparent text-base font-medium whitespace-nowrap outline-2 outline-transparent transition-[outline-color] select-none hover:bg-transparent disabled:pointer-events-none disabled:opacity-64 data-disabled:pointer-events-none data-disabled:opacity-64 sm:text-sm',
        segmentedControlItemLayoutClassName,
    ],
    {
        defaultVariants: {
            size: 'default',
            variant: 'default',
        },
        variants: {
            size: segmentedControlItemSizeClassNames,
            variant: {
                default: 'rounded-md',
                'inset-outline': 'rounded-full',
            },
            /**
             * The state that will determine the active state of the item. Only applies when the item is checked, current, or pressed.
             */
            state: {
                checked: 'aria-checked:text-foreground',
                current: 'aria-[current=page]:text-foreground',
                pressed:
                    'aria-pressed:text-foreground aria-selected:text-foreground',
            },
        },
        compoundVariants: [
            {
                variant: 'default',
                state: 'checked',
                class: 'aria-checked:bg-background dark:aria-checked:bg-input aria-checked:shadow-sm/5',
            },
            {
                variant: 'default',
                state: 'current',
                class: 'aria-[current=page]:bg-background dark:aria-[current=page]:bg-input aria-[current=page]:shadow-sm/5',
            },
            {
                variant: 'default',
                state: 'pressed',
                class: 'aria-pressed:bg-background dark:aria-pressed:bg-input aria-selected:bg-background dark:aria-selected:bg-input aria-pressed:shadow-sm/5 aria-selected:shadow-sm/5',
            },
            {
                variant: 'inset-outline',
                state: 'checked',
                class: 'aria-checked:bg-card aria-checked:ring-foreground/8 dark:aria-checked:ring-foreground/12 aria-checked:shadow-xs aria-checked:ring-1',
            },
            {
                variant: 'inset-outline',
                state: 'current',
                class: 'aria-[current=page]:bg-card aria-[current=page]:ring-foreground/8 dark:aria-[current=page]:ring-foreground/12 aria-[current=page]:shadow-xs aria-[current=page]:ring-1',
            },
            {
                variant: 'inset-outline',
                state: 'pressed',
                class: '[[aria-pressed=true],[aria-selected=true]]:bg-card [[aria-pressed=true],[aria-selected=true]]:ring-foreground/8 dark:[[aria-pressed=true],[aria-selected=true]]:ring-foreground/12 [[aria-pressed=true],[aria-selected=true]]:shadow-xs [[aria-pressed=true],[aria-selected=true]]:ring-1',
            },
        ],
    },
)
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    segmentedControlRootVariants,
    segmentedControlItemVariants,
    segmentedControlItemLayoutClassName,
} from "@/lib/segmented-control"
```

Apply the root recipe to a group or navigation element. Apply the item recipe to
each child. The radio example uses the existing RadioGroup controller with
button elements marked `data-slot="radio-group-item"` and stable `data-value`
attributes.

## API reference

Set `variant: "inset-outline"` on both recipes for a rounded secondary surface.
The selected item uses the card background, a small shadow, and a thin ring.
Both recipes use `default` when you omit `variant`.

`size` accepts `sm`, `default`, or `lg`. `state` accepts `checked`, `current`, or
`pressed`. These recipes read `aria-checked="true"`, `aria-current="page"`, and
`aria-pressed="true"`, respectively. Read the [source](/ui/r/segmented-control.json)
for all exported classes and types.

## Choose the underlying control

Use Radio Group for one form value. Use links for destinations, with
`aria-current="page"` on the current route. Use Toggle for independent options.
Use [Toggle Group](/ui/components/toggle-group) when those options share one
selection or several selections. Use [Tabs](/ui/components/tabs) when a choice
shows a matching content panel. Tabs keeps its own indicator and panel
relationships.

## Keyboard and form behavior

The recipe has no client script. Radio Group keeps its arrow-key selection and
named form value. Links keep browser navigation and open-in-new-tab behavior.
Toggle keeps its pressed state and Space activation. Give each group a name
that explains the shared choice and test disabled items in the final page.

## Theme and layout

The outer muted surface and selected item use shared theme tokens. Keep the
labels short and choose one size for the group. A long row can need a different
layout on a phone; a segmented appearance does not provide overflow controls.
Use [Group](/ui/components/group) for joined action buttons with visible borders.
