---
title: "Tabs · Base UI — StyleX Blocks"
description: "Switch between panels with line, enclosed, outline, pills, and ghost tab lists."
url: "https://stylexblocks.com/components/base-ui/tabs/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Tabs

Switch between panels with line, enclosed, outline, pills, and ghost tab lists.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/tabs.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/tabs.md)
- Category: Display
- Variants: line, enclosed, outline, pills, ghost
- Sizes: xs, sm, md, lg
- HTML page: https://stylexblocks.com/components/base-ui/tabs/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add tabs
```

Equivalent: `npx shadcn add @stylexblocks/tabs`. Registry item JSON: https://stylexblocks.com/r/base-ui/tabs.json

## Usage

```tsx
import { Tabs } from "#components/ui/base-ui/tabs.tsx";

export function Example() {
  return (
    // variant/size resolve once on Root; every part inherits them via
    // context — selection, roving focus, and keyboard come from the machine
    <Tabs.Root defaultValue="overview" variant="line" size="md">
      <Tabs.List>
        <Tabs.Tab value="overview">Overview</Tabs.Tab>
        <Tabs.Tab value="members">Members</Tabs.Tab>
      </Tabs.List>
      <Tabs.Panel value="overview">…</Tabs.Panel>
      <Tabs.Panel value="members">…</Tabs.Panel>
    </Tabs.Root>
  );
}

// controlled: <Tabs.Root value={value} onValueChange={setValue}>
// composition per part via render
```

## Source

`components/ui/base-ui/tabs.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Tabs as BaseTabs } from "@base-ui/react/tabs";
import { createSlotRecipeContext } from "#lib/base-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  fromMap,
  resolveSlotStyles,
} from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

const tabsRoot = stylex.create({
  base: {
    '--tabs-trigger-radius': semanticRadii.control,
    position: 'relative',
  },
})

const tabsRootSizes = stylex.create({
  xs: {
    '--tabs-height': sizes.controlXs,
    '--tabs-content-padding': spacing._2,
  },
  sm: {
    '--tabs-height': sizes.controlSm,
    '--tabs-content-padding': spacing._3,
  },
  md: {
    '--tabs-height': sizes.controlMd,
    '--tabs-content-padding': spacing._4,
  },
  lg: {
    '--tabs-height': sizes.controlLg,
    '--tabs-content-padding': spacing._4_5,
  },
})

const tabsList = stylex.create({
  base: {
    display: 'inline-flex',
    position: 'relative',
    isolation: 'isolate',
    '--tabs-indicator-shadow': semanticShadows.xs,
    '--tabs-indicator-bg': semanticColors.bg,
    minHeight: 'var(--tabs-height)',
  },
})

const tabsListVariants = stylex.create({
  line: {
    display: 'flex',
    borderColor: semanticColors.border,
  },
  pills: {
    gap: spacing._2,
  },
  ghost: {
    gap: spacing._2,
  },
  enclosed: {
    backgroundColor: semanticColors.bgMuted,
    boxShadow: semanticShadows.inset,
    borderRadius: radii.md,
    borderWidth: '1px',
    padding: spacing._1,
    minHeight: 'calc(var(--tabs-height) - 4px)',
  },
  outline: {
    '--line-thickness': '1px',
    '--line-offset': 'calc(var(--line-thickness) * -1)',
    borderColor: semanticColors.border,
    display: 'flex',
  },
})

const tabsListFitteds = stylex.create({
  true: {
    display: 'flex',
  },
})

const tabsListJustifys = stylex.create({
  start: {
    justifyContent: 'flex-start',
  },
  center: {
    justifyContent: 'center',
  },
  end: {
    justifyContent: 'flex-end',
  },
})

const tabsTrigger = stylex.create({
  base: {
    outline: 'none',
    minWidth: 'var(--tabs-height)',
    height: 'var(--tabs-height)',
    display: 'flex',
    alignItems: 'center',
    fontWeight: fontWeights.medium,
    position: 'relative',
    cursor: {
      default: cursor.button,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
    gap: spacing._2,
    zIndex: {
      default: null,
      ':focus-visible': 1,
    },
    outlineWidth: {
      default: null,
      ':focus-visible': '1px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const tabsTriggerSizes = stylex.create({
  xs: {
    paddingBlock: spacing._1,
    paddingInline: spacing._2,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
  },
  sm: {
    paddingBlock: spacing._1,
    paddingInline: spacing._3,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  md: {
    paddingBlock: spacing._2,
    paddingInline: spacing._4,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  lg: {
    paddingBlock: spacing._2,
    paddingInline: spacing._4_5,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
  },
})

const tabsTriggerVariants = stylex.create({
  line: {
    color: {
      default: semanticColors.fgMuted,
      ':hover': semanticColors.fgSubtle,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        semanticColors.fg,
    },
    boxShadow: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])': `inset 0 -2px 0 0 ${colorPalette.solid}`,
    },
    backgroundColor: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': {
        default: null,
        ':active': 'initial',
      },
    },
  },
  subtle: {
    borderRadius: 'var(--tabs-trigger-radius)',
    color: {
      default: semanticColors.fgMuted,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        colorPalette.fg,
    },
    backgroundColor: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        colorPalette.muted,
    },
  },
  pills: {
    borderRadius: 'var(--tabs-trigger-radius)',
    color: {
      default: semanticColors.fgMuted,
      ':hover': semanticColors.fgSubtle,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        semanticColors.fg,
    },
    borderWidth: '1px',
    borderColor: semanticColors.borderEmphasized,
    backgroundColor: {
      default: null,
      ':hover': semanticColors.bgMuted,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-bg)',
    },
    boxShadow: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-shadow)',
    },
  },
  ghost: {
    borderRadius: 'var(--tabs-trigger-radius)',
    color: {
      default: semanticColors.fgSubtle,
      ':hover': colorPalette.fg,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        colorPalette.fg,
    },
    backgroundColor: {
      default: null,
      ':hover': colorPalette.muted,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        colorPalette.muted,
    },
  },
  enclosed: {
    justifyContent: 'center',
    color: {
      default: semanticColors.fgMuted,
      ':hover': semanticColors.fgSubtle,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        semanticColors.fg,
    },
    borderRadius: `calc(${radii.md} - 1px)`,
    backgroundColor: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-bg)',
    },
    boxShadow: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-shadow)',
    },
  },
  outline: {
    color: {
      default: semanticColors.fgMuted,
      ':hover': semanticColors.fgSubtle,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        semanticColors.fg,
    },
    borderWidth: '1px',
    borderColor: 'transparent',
    backgroundColor: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-bg)',
    },
    boxShadow: {
      default: null,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        'var(--tabs-indicator-shadow)',
    },
  },
  plain: {
    color: {
      default: semanticColors.fgMuted,
      ':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
        semanticColors.fg,
    },
    borderRadius: 'var(--tabs-trigger-radius)',
  },
})

const tabsTriggerFitteds = stylex.create({
  true: {
    flex: 1,
    textAlign: 'center',
    justifyContent: 'center',
  },
})

const tabsContent = stylex.create({
  base: {
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
})

const tabsIndicator = stylex.create({
  base: {
    width: 'var(--width)',
    height: 'var(--height)',
    borderRadius: 'var(--tabs-indicator-radius)',
    backgroundColor: 'var(--tabs-indicator-bg)',
    boxShadow: 'var(--tabs-indicator-shadow)',
    zIndex: -1,
  },
})

export type TabsFitted = keyof typeof tabsListFitteds

export type TabsJustify = keyof typeof tabsListJustifys

export type TabsSize = keyof typeof tabsRootSizes

export type TabsVariant = keyof typeof tabsListVariants

// What the generated recipe doesn't carry: the tab's browser-button reset,
// the panel padding fed by the root's `--tabs-content-padding`, and the
// selected-tab treatment per variant (palette-aware). StyleX keys those off
// `data-selected` / `data-active` — the engine stamps `data-active`; chrome
// also matches it so selected styles apply without a second recipe context.
const chrome = stylex.create({
  triggerReset: {
    appearance: "none",
    borderWidth: 0,
    backgroundColor: "transparent",
  },
  content: { padding: "var(--tabs-content-padding)" },
});

const tabsTriggerSelected = stylex.create({
  line: {
    color: {
      default: null,
      ":is([data-selected], [data-active])": semanticColors.fg,
    },
    boxShadow: {
      default: null,
      ":is([data-selected], [data-active])": `inset 0 -2px 0 0 ${colorPalette.solid}`,
    },
  },
  enclosed: {
    backgroundColor: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
    },
    color: {
      default: null,
      ":is([data-selected], [data-active])": semanticColors.fg,
    },
    boxShadow: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
    },
  },
  outline: {
    backgroundColor: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
    },
    color: {
      default: null,
      ":is([data-selected], [data-active])": semanticColors.fg,
    },
    boxShadow: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
    },
  },
  pills: {
    backgroundColor: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
    },
    color: {
      default: null,
      ":is([data-selected], [data-active])": semanticColors.fg,
    },
    boxShadow: {
      default: null,
      ":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
    },
  },
  ghost: {
    backgroundColor: {
      default: null,
      ":is([data-selected], [data-active])": colorPalette.muted,
    },
    color: {
      default: null,
      ":is([data-selected], [data-active])": colorPalette.fg,
    },
  },
  subtle: {
    backgroundColor: {
      default: null,
      ":is([data-selected], [data-active])": colorPalette.muted,
    },
    color: {
      default: null,
      ":is([data-selected], [data-active])": colorPalette.fg,
    },
  },
  plain: {
    color: {
      default: null,
      ":is([data-selected], [data-active])": semanticColors.fg,
    },
  },
});

// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis.
// The list's variant map is sparse (no `subtle`/`plain`) — the mechanical
// resolver skips axis values a slot doesn't style.
const tabsSlotRecipe = defineSlotRecipe({
  slots: {
    root: { styles: tabsRoot, sizes: tabsRootSizes },
    list: { styles: tabsList, variants: tabsListVariants },
    trigger: {
      styles: tabsTrigger,
      sizes: tabsTriggerSizes,
      variants: tabsTriggerVariants,
    },
    content: { styles: tabsContent },
    indicator: { styles: tabsIndicator },
  },
  defaultVariants: { size: "md", variant: "line" },
});

const { withProvider, withContext } = createSlotRecipeContext(tabsSlotRecipe);

/**
 * Tabs — Base UI's tabs machine wearing the generated recipe. `<Tabs.Root
 * variant size>` resolves the recipe once and every part inherits it through
 * context; behavior (roving focus, keyboard activation, controlled value) is
 * the machine's. Composition per part via `render`.
 */
export const Tabs = {
  Root: withProvider(BaseTabs.Root, "root", "TabsRoot"),
  List: withContext(BaseTabs.List, "list", "TabsList"),
  Tab: withContext(
    BaseTabs.Tab,
    (v) => [
      chrome.triggerReset,
      ...resolveSlotStyles(tabsSlotRecipe.slots.trigger, v),
      fromMap(tabsTriggerSelected, v.variant),
    ],
    "TabsTab",
  ),
  Panel: withContext(
    BaseTabs.Panel,
    () => [tabsContent.base, chrome.content],
    "TabsPanel",
  ),
  Indicator: withContext(BaseTabs.Indicator, "indicator", "TabsIndicator"),
};
```
