Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Segmented Control for Astro

Shared style recipes for related radio choices, navigation links, and pressed buttons.

Radio group, small
Radio group
Radio group, large
Current links
Pressed toggles

Installation

bunx --bun shadcn@latest add @gnostic/segmented-control
npx shadcn@latest add @gnostic/segmented-control
pnpm dlx shadcn@latest add @gnostic/segmented-control

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/lib/segmented-control.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 segmentedControlRootClassName =
    'relative z-0 flex w-fit items-center justify-center gap-0.5 rounded-lg bg-muted p-0.5'

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 rounded-md 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',
        },
        variants: {
            size: segmentedControlItemSizeClassNames,
            state: {
                checked:
                    'aria-checked:bg-background aria-checked:text-foreground dark:aria-checked:bg-input aria-checked:shadow-sm/5',
                current:
                    'aria-[current=page]:bg-background aria-[current=page]:text-foreground dark:aria-[current=page]:bg-input aria-[current=page]:shadow-sm/5',
                // adapted for `@data-slot/tabs`
                pressed:
                    'aria-selected:bg-background aria-selected:text-foreground dark:aria-selected:bg-input aria-selected:shadow-sm/5',
            },
        },
    },
)

Usage

import {
    segmentedControlRootClassName,
    segmentedControlItemVariants,
    segmentedControlItemLayoutClassName,
} from "@/lib/segmented-control"

Apply the root class 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. Its flex-row class changes the group’s usual column layout.

API reference

size accepts sm, default, or lg. state accepts checked, current, or pressed. These recipes read data-checked, aria-current="page", and aria-pressed="true", respectively. Read the source 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 when those options share one selection or several selections. Use 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 for joined action buttons with visible borders.