---
title: Tabs for Astro
description: "Astro tab panels with shared pill and underline variants."
seo:
    title: Tabs for Astro | Shadcn & Tailwind CSS
related:
    - /components/navigation-menu
    - /components/accordion
sidebar:
    label: "Tabs"
---

```astro
---
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="tab-1" class="w-full max-w-sm self-start">
  <TabsList class="grid w-full grid-cols-2">
    <TabsTrigger value="tab-1">Account</TabsTrigger>
    <TabsTrigger value="tab-2">Password</TabsTrigger>
  </TabsList>
  <TabsContent value="tab-1">
    <div class="rounded-md border p-4">
      <h3 class="mb-2 font-semibold">Account</h3>
      <p class="text-muted-foreground text-sm">
        Review the name and email used for project updates.
      </p>
    </div>
  </TabsContent>
  <TabsContent value="tab-2">
    <div class="rounded-md border p-4">
      <h3 class="mb-2 font-semibold">Password</h3>
      <p class="text-muted-foreground text-sm">
        Keep access details separate from the public project profile.
      </p>
    </div>
  </TabsContent>
</Tabs>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/tabs
```

### Manual

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

Install the component packages:

```sh
bun add @data-slot/tabs
```

#### src/components/ui/tabs/index.ts

```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

```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

```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

```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

```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

```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

```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>
```

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 { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
```

```astro
<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`:

```text
Tabs
├── TabsList
│   ├── TabsTrigger
│   └── TabsTrigger
├── TabsContent
└── TabsContent
```

## Examples

### Underline

Use `variant="underline"` on `TabsList` for a line under the active tab.

```astro
---
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="overview" class="w-full max-w-md">
  <TabsList variant="underline">
    <TabsTrigger value="overview">Overview</TabsTrigger>
    <TabsTrigger value="analytics">Analytics</TabsTrigger>
    <TabsTrigger value="reports">Reports</TabsTrigger>
  </TabsList>
</Tabs>
```

### Small

`size` on `TabsList` sets the size of every tab. Use `sm`, `default`, or `lg`.

```astro
---
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="overview" class="w-full max-w-md">
  <TabsList size="sm">
    <TabsTrigger value="overview">Overview</TabsTrigger>
    <TabsTrigger value="analytics">Analytics</TabsTrigger>
    <TabsTrigger value="reports">Reports</TabsTrigger>
  </TabsList>
</Tabs>
```

### Vertical

Use `orientation="vertical"` for side navigation patterns.

```astro
---
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="account" orientation="vertical" class="w-full max-w-sm">
  <TabsList>
    <TabsTrigger value="account">Account</TabsTrigger>
    <TabsTrigger value="password">Password</TabsTrigger>
    <TabsTrigger value="notifications">Notifications</TabsTrigger>
  </TabsList>
</Tabs>
```

### Disabled

```astro
---
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="account" class="w-full max-w-sm">
  <TabsList class="grid w-full grid-cols-2">
    <TabsTrigger value="home">Home</TabsTrigger>
    <TabsTrigger value="disabled" disabled>
      Disabled
    </TabsTrigger>
  </TabsList>
</Tabs>
```

### Grid

```astro
---
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"
---

<Tabs defaultValue="account" class="w-full max-w-sm">
  <TabsList class="grid w-full grid-cols-3">
    <TabsTrigger value="account">Account</TabsTrigger>
    <TabsTrigger value="password">Password</TabsTrigger>
    <TabsTrigger value="team">Team</TabsTrigger>
  </TabsList>
</Tabs>
```

## 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](/r/tabs.json) for the complete prop types.
See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/tabs/README.md) 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.
