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.

Tabs · PlaygroundLive
Three pages are waiting for review.
keyboardActivation
JSX
<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

TSX
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.

Tabs · SettingsLive
TSX
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.

Tabs · ControlledLive

Step 1 of 3.

TSX
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:

CSS
.plonk-tablist {
  --plonk-tabs-radius: var(--plonk-radius-sm);
}

Accessibility

  • Tab moves into the tab list at the selected tab and then into the panel.
  • ← and → move between tabs (reversed in right-to-left languages), and Home and End jump to the ends.
  • With keyboardActivation="manual", arrows only move focus; Enter or Space selects.
  • Tabs and panels are linked with aria-controls and aria-labelledby.

API

Tabs

PropTypeDefaultDescription
classNamestring—Extra classes for the wrapper.
selectedKeyKey—Controlled selected tab's id.
defaultSelectedKeyKey—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.
disabledKeysIterable<Key>—Ids of tabs that can't be selected.
isDisabledboolean—Disables every tab.

TabList

PropTypeDefaultDescription
classNamestring—Extra classes for the bar.
aria-labelstring—Name of the tab list, for screen readers.

Tab

PropTypeDefaultDescription
classNamestring—Extra classes for the tab.
idKey—Matches the id of its TabPanel.
isDisabledboolean—Can't be selected.

TabPanel

PropTypeDefaultDescription
classNamestring—Extra classes for the panel.
idKey—Matches the id of its Tab.