Components / Structure
Tabs22
Switch between views of the same thing. The indicator springs to the new tab and squashes on the way in Sticker, becomes a slanted red panel in Comic and blinks into place in Arcade.
Overview
Activity
Settings
Three pages are waiting for review.
keyboardActivation
<Tabs>
<TabList aria-label="Project">
<Tab id="overview">Overview</Tab>
<Tab id="activity">Activity</Tab>
<Tab id="settings">Settings</Tab>
</TabList>
<TabPanel id="overview">…</TabPanel>
<TabPanel id="activity">…</TabPanel>
<TabPanel id="settings">…</TabPanel>
</Tabs>Usage
import { Tab, TabList, TabPanel, Tabs } from "plonk-ui";
export function Project() {
return (
<Tabs>
<TabList aria-label="Project">
<Tab id="overview">Overview</Tab>
<Tab id="settings">Settings</Tab>
</TabList>
<TabPanel id="overview">…</TabPanel>
<TabPanel id="settings">…</TabPanel>
</Tabs>
);
}Each Tab and its TabPanel share an id. Give the TabList an
aria-label that says what the tabs switch between. On narrow screens the bar
scrolls sideways instead of wrapping.
Panels with content
Panels can hold anything, like forms. A tab can be disabled with isDisabled.
Profile
Alerts
Billing
import { Input, Switch, Tab, TabList, TabPanel, Tabs } from "plonk-ui";
export default function Settings() {
return (
<Tabs className="w-full max-w-sm">
<TabList aria-label="Settings">
<Tab id="profile">Profile</Tab>
<Tab id="notifications">Alerts</Tab>
<Tab id="billing" isDisabled>
Billing
</Tab>
</TabList>
<TabPanel id="profile">
<div className="flex flex-col gap-4">
<Input label="Display name" defaultValue="Ada" />
<Input label="Email" type="email" defaultValue="ada@studio.com" />
</div>
</TabPanel>
<TabPanel id="notifications">
<div className="flex flex-col gap-4">
<Switch label="Mentions" defaultChecked />
<Switch label="Weekly digest" />
</div>
</TabPanel>
<TabPanel id="billing">Billing is managed by your admin.</TabPanel>
</Tabs>
);
}Controlled
selectedKey and onSelectionChange let other controls switch tabs, like a
"Next" button in a multi-step form.
1. Details
2. Team
3. Review
Step 1 of 3.
import { useState } from "react";
import { Button, Tab, TabList, TabPanel, Tabs } from "plonk-ui";
const STEPS = ["details", "team", "review"];
export default function Controlled() {
const [step, setStep] = useState("details");
const index = STEPS.indexOf(step);
const next = STEPS[index + 1];
return (
<Tabs
className="w-full max-w-sm"
selectedKey={step}
onSelectionChange={(key) => setStep(String(key))}
>
<TabList aria-label="New project">
<Tab id="details">1. Details</Tab>
<Tab id="team">2. Team</Tab>
<Tab id="review">3. Review</Tab>
</TabList>
{STEPS.map((id) => (
<TabPanel key={id} id={id}>
<div className="flex flex-col items-start gap-3">
<p>Step {STEPS.indexOf(id) + 1} of 3.</p>
{next ? (
<Button size="sm" onPress={() => setStep(next)}>
Next
</Button>
) : (
<Button size="sm" onPress={() => setStep("details")}>
Start over
</Button>
)}
</div>
</TabPanel>
))}
</Tabs>
);
}Personalities
- Sticker: the indicator springs to the new tab and squashes like jelly; the panel rises in.
- Retro: the indicator jumps in steps.
- Comic: square panels, a slanted red indicator, and content that slides in like the next frame of a strip.
- Arcade: a neon indicator that blinks three times when it lands, and a panel that's wiped in from the top.
Corner hook
Comic squares the tabs by setting --plonk-tabs-radius. It rounds the bar, the
tabs and the indicator together:
.plonk-tablist {
--plonk-tabs-radius: var(--plonk-radius-sm);
}Accessibility
Tabmoves into the tab list at the selected tab and then into the panel.←and→move between tabs (reversed in right-to-left languages), andHomeandEndjump to the ends.- With
keyboardActivation="manual", arrows only move focus;EnterorSpaceselects. - Tabs and panels are linked with
aria-controlsandaria-labelledby.
API
Tabs
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the wrapper. |
selectedKey | Key | — | Controlled selected tab's id. |
defaultSelectedKey | Key | — | Selected tab on first render. Defaults to the first one. |
onSelectionChange | (key: Key) => void | — | Called with the new tab's id. |
keyboardActivation | "manual" | "automatic" | "automatic" | manual: arrow keys only move focus, and Enter or Space selects. Use it when panels are slow to render. |
disabledKeys | Iterable<Key> | — | Ids of tabs that can't be selected. |
isDisabled | boolean | — | Disables every tab. |
TabList
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the bar. |
aria-label | string | — | Name of the tab list, for screen readers. |
Tab
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the tab. |
id | Key | — | Matches the id of its TabPanel. |
isDisabled | boolean | — | Can't be selected. |
TabPanel
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the panel. |
id | Key | — | Matches the id of its Tab. |