Segmented Control for Astro
Shared style recipes for related radio choices, navigation links, and pressed buttons.
Installation
bunx --bun shadcn@latest add @gnostic/segmented-controlnpx shadcn@latest add @gnostic/segmented-controlpnpm dlx shadcn@latest add @gnostic/segmented-controlCopy the files
src/lib/segmented-control.ts
Usage
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
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.