Playground

Tabs

Switch between related views in the same place.

  • Built on radix-ui/Tabs
  • 4 parts, named exports
  • role=tablist, tab and tabpanel with aria-selected and aria-controls.
  • Arrow keys move between tabs, Home and End jump; only the active tab is a Tab stop.
  • Give the list an accessible name with aria-label.
  • Styles in one file: tabs.css

Import

import '@monoline/css';
import {Tabs} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add tabs

It needs radix-ui; install it if your project does not have it:

npm install radix-ui

Usage

import {Tabs, TabsContent, TabsList, TabsTrigger} from '@monoline/react';

<Tabs defaultValue="overview">
  <TabsList aria-label="Project">
    <TabsTrigger value="overview">Overview</TabsTrigger>
    <TabsTrigger value="activity">Activity</TabsTrigger>
    <TabsTrigger value="settings">Settings</TabsTrigger>
  </TabsList>
  <TabsContent value="overview">Overview content.</TabsContent>
  <TabsContent value="activity">Activity content.</TabsContent>
  <TabsContent value="settings">Settings content.</TabsContent>
</Tabs>

Anatomy

Import the parts you need and nest them like this.

import {Tabs, TabsList, TabsTrigger, TabsContent} from '@monoline/react';

<Tabs>
  <TabsList />
  <TabsTrigger />
  <TabsContent />
</Tabs>

Examples

Horizontal

Vertical

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-orientation="horizontal"]tabs.css
[data-orientation="vertical"]tabs.css
[data-state="active"]tabs.css
[aria-current="page"]18-navigation.css
[aria-current="step"]18-navigation.css
[aria-selected="true"]18-navigation.css
[data-state="done"]18-navigation.css

CSS classes

ClassStylesheet
.tabs-contenttabs.css
.tabs-listtabs.css
.tabs-roottabs.css
.tabs-triggertabs.css
.brand18-navigation.css
.crumbs18-navigation.css
.pager18-navigation.css
.step18-navigation.css
.steps18-navigation.css
.tab18-navigation.css
.tabpanel18-navigation.css
.topbar18-navigation.css
.topbar-nav18-navigation.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.tabs-content {
  border-color: var(--accent);
}
.tabs-content[data-orientation="horizontal"] {
  background: var(--wash);
}

Try the props

Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.

Overview content.
example.tsx
<Tabs defaultValue="overview">
  <TabsList aria-label="Project">
    <TabsTrigger value="overview">Overview</TabsTrigger>
    <TabsTrigger value="activity">Activity</TabsTrigger>
    <TabsTrigger value="settings">Settings</TabsTrigger>
  </TabsList>
  <TabsContent value="overview">Overview content.</TabsContent>
  <TabsContent value="activity">Activity content.</TabsContent>
  <TabsContent value="settings">Settings content.</TabsContent>
</Tabs>

Accessibility

  • role=tablist, tab and tabpanel with aria-selected and aria-controls.
  • Arrow keys move between tabs, Home and End jump; only the active tab is a Tab stop.
  • Give the list an accessible name with aria-label.

Keyboard

KeysAction
Arrow keysMove between tabs
Home / EndJump