Tabs for Astro
Astro tab panels with shared pill and underline variants.
Installation
bunx --bun shadcn@latest add @gnostic/tabsnpx shadcn@latest add @gnostic/tabspnpm dlx shadcn@latest add @gnostic/tabsComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/tabsCopy the files
src/components/ui/tabs/index.ts
export { default as Tabs } from './tabs.astro'
export { default as TabsList } from './tabs-list.astro'
export { default as TabsTrigger } from './tabs-trigger.astro'
export { default as TabsContent } from './tabs-content.astro'
export {
tabsListVariants,
type TabsListVariantProps,
type TabsSize,
type TabsVariant,
} from './tabs-list-variants'
src/components/ui/tabs/tabs-content.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div"> & {
value?: string
}
const { class: className, hidden = true, value, ...props } = Astro.props
---
<div
data-slot="tabs-content"
hidden={hidden}
data-value={value}
class={cn("flex-1 outline-none", className)}
{...props}
>
<slot />
</div>
src/components/ui/tabs/tabs-list-variants.ts
import { cva, type VariantProps } from 'class-variance-authority'
import { cn } from 'cn'
import type { SegmentedControlSize } from '../../../lib/segmented-control.ts'
/**
* Optical sizes from the coss segmented control.
* The list writes these as CSS variables. A trigger reads them
* unless that trigger sets its own size.
*
* @see https://coss.com/ui/docs/components/tabs
*/
export type TabsSize = SegmentedControlSize
export type TabsVariant = 'default' | 'underline'
const tabsListStyles = cva("text-muted-foreground relative z-0 flex w-fit items-center justify-center gap-x-0.5 group-data-[orientation=vertical]/tabs:flex-col group/tabs-list", {
variants: {
variant: {
default: "bg-muted text-muted-foreground/72 rounded-lg p-0.5 group-data-[orientation=vertical]/tabs:rounded-[min(var(--radius-lg),1rem)]",
underline: "*:data-[slot=tabs-trigger]:hover:bg-accent group-data-[orientation=horizontal]/tabs:py-1 group-data-[orientation=vertical]/tabs:px-1",
},
size: {
sm: "[--tabs-item-height:--spacing(7.5)] [--tabs-item-padding-inline:calc(var(--spacing)*2-1px)] sm:[--tabs-item-height:--spacing(6.5)]",
default: "[--tabs-item-height:--spacing(8.5)] [--tabs-item-padding-inline:calc(var(--spacing)*2.5-1px)] sm:[--tabs-item-height:--spacing(7.5)]",
lg: "[--tabs-item-height:--spacing(9.5)] [--tabs-item-padding-inline:calc(var(--spacing)*3-1px)] sm:[--tabs-item-height:--spacing(8.5)]",
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
})
export const tabsListVariants = (...args: Parameters<typeof tabsListStyles>) =>
cn(tabsListStyles(...args))
export type TabsListVariantProps = VariantProps<typeof tabsListVariants>
/**
* The indicator follows the active trigger through the CSS variables
* that @data-slot/tabs writes on this element.
*/
const tabsIndicatorStyles = cva("pointer-events-none absolute top-(--active-tab-top) left-(--active-tab-left) h-(--active-tab-height) w-(--active-tab-width) transition-all duration-200 ease-in-out", {
variants: {
variant: {
default: "bg-background dark:bg-input -z-1 rounded-md shadow-sm/5 group-data-[orientation=vertical]/tabs:rounded-[min(var(--radius-md),1rem)]",
underline: "bg-primary z-10 group-data-[orientation=horizontal]/tabs:top-[calc(var(--active-tab-top)+var(--active-tab-height)-1px)] group-data-[orientation=horizontal]/tabs:h-0.5 group-data-[orientation=vertical]/tabs:left-[calc(var(--active-tab-left)-1px)] group-data-[orientation=vertical]/tabs:w-0.5",
},
},
defaultVariants: {
variant: 'default',
},
})
export const tabsIndicatorVariants = (
...args: Parameters<typeof tabsIndicatorStyles>
) => cn(tabsIndicatorStyles(...args))
src/components/ui/tabs/tabs-list.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import {
tabsListVariants,
tabsIndicatorVariants,
type TabsSize,
type TabsVariant,
} from "./tabs-list-variants"
type Props = HTMLAttributes<"div"> & {
variant?: TabsVariant
/** Sets the size of every trigger in this list. */
size?: TabsSize
}
export { tabsIndicatorVariants } from "./tabs-list-variants"
const {
class: className,
variant = "default",
size = "default",
...props
} = Astro.props
---
<div
data-slot="tabs-list"
data-variant={variant}
data-size={size}
class={cn(tabsListVariants({ variant, size }), className)}
{...props}
>
<slot />
{/* Sliding active surface. The tabs script positions it. */}
<div
data-slot="tabs-indicator"
aria-hidden="true"
class={tabsIndicatorVariants({ variant })}
></div>
</div>
src/components/ui/tabs/tabs-trigger.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import { type TabsSize } from "./tabs-list-variants"
import { segmentedControlItemSizeClassNames } from "../../../lib/segmented-control.ts"
type Props = HTMLAttributes<"button"> & {
value?: string
/** Replaces the size inherited from TabsList. */
size?: TabsSize
}
const { class: className, value, size, type = "button", ...props } = Astro.props
---
<button
data-slot="tabs-trigger"
data-value={value}
data-size={size}
type={type}
class={cn(
"relative flex shrink-0 grow cursor-pointer items-center justify-center gap-1.5 rounded-md border border-transparent font-medium whitespace-nowrap text-base outline-none transition-[color,background-color,box-shadow] hover:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-64 data-disabled:pointer-events-none data-disabled:opacity-64 aria-disabled:pointer-events-none aria-disabled:opacity-64 group-data-[orientation=vertical]/tabs:w-full group-data-[orientation=vertical]/tabs:justify-start data-[state=active]:text-foreground sm:text-sm [&_svg]:pointer-events-none [&_svg]:-mx-0.5 [&_svg]:shrink-0 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 group-data-[orientation=vertical]/tabs:rounded-[min(var(--radius-md),1rem)]",
size
? segmentedControlItemSizeClassNames[size]
: "h-(--tabs-item-height) px-(--tabs-item-padding-inline)",
className,
)}
{...props}
>
<slot />
</button>
src/components/ui/tabs/tabs.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { TabsProps } from "./types.ts"
type Props = HTMLAttributes<"div"> & TabsProps
const {
class: className,
defaultValue,
orientation = "horizontal",
activationMode,
...props
} = Astro.props
---
{/*
data-orientation is the value @data-slot/tabs reads for keyboard navigation.
Styles select that value with data-[orientation=horizontal] and
data-[orientation=vertical].
*/}
<div
data-slot="tabs"
data-default-value={defaultValue}
data-orientation={orientation}
data-activation-mode={activationMode}
class={cn("flex flex-col gap-2 data-[orientation=vertical]:flex-row group/tabs", className)}
{...props}
>
<slot />
</div>
<script>
import { create } from "@data-slot/tabs"
import type { TabsController } from "@data-slot/tabs"
const controllers: TabsController[] = []
// Place the active indicator on the same turn the tabs bind.
// The library also schedules a frame; this call covers the first paint.
const initialize = () => {
const next = create()
for (const controller of next) controller.updateIndicator()
controllers.push(...next)
}
initialize()
document.addEventListener("astro:page-load", initialize)
document.addEventListener("astro:before-swap", () => {
for (const controller of controllers) controller.destroy()
controllers.length = 0
})
</script>
src/components/ui/tabs/types.ts
import type { TabsOptions } from '@data-slot/tabs'
import type { VariantProps } from 'class-variance-authority'
import type { tabsListVariants } from './tabs-list-variants.ts'
/** Tabs configuration rendered as data attributes. */
export type TabsProps = Pick<
TabsOptions,
'defaultValue' | 'orientation' | 'activationMode'
>
export type TabsListProps = VariantProps<typeof tabsListVariants>
Usage
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
<Tabs defaultValue="tab-1">
<TabsList>
<TabsTrigger value="tab-1">Tab 1</TabsTrigger>
<TabsTrigger value="tab-2">Tab 2</TabsTrigger>
</TabsList>
<TabsContent value="tab-1"> Content for tab 1 </TabsContent>
<TabsContent value="tab-2"> Content for tab 2 </TabsContent>
</Tabs>
Composition
Use the following composition to build Tabs:
Tabs
├── TabsList
│ ├── TabsTrigger
│ └── TabsTrigger
├── TabsContent
└── TabsContent
Examples
Underline
Use variant="underline" on TabsList for a line under the active tab.
Small
size on TabsList sets the size of every tab. Use sm, default, or lg.
Vertical
Use orientation="vertical" for side navigation patterns.
Disabled
Grid
RTL
Set dir="rtl" on the document or the relevant region. Check text order, focus, and directional icons in the rendered Astro page. No React provider is required.
API Reference
Read the installed source for the complete prop types. See the data-slot docs for more information on interactivity.
Events
Dispatch these events on the tabs root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
tabs:change |
{ value: string } |
Inbound events
| Event | Detail |
|---|---|
tabs:set |
{ value: string } |
When to use Tabs
Use Tabs for peer views of the same subject. Give each trigger a stable value that matches one content panel. Use the existing list variant and size props to fit the page design.
Keyboard and behavior
The data-slot controller manages selected state and keyboard movement. Test orientation, disabled triggers, and focus after changing panels. Tabs switch local content; use links when each destination has its own route.
Theme and layout
The indicator reads size and position from controller variables. Long labels need a narrow-screen layout check. Keep vertical lists wide enough for readable text and test the selected preset at each supported size.