Vastu

Tabs <va-tabs>

Switches between panels of related content in the same place on the page.

When to use it

Use it
To switch between a few views of related content in one place, such as sections of a settings page.
Don't use it
For navigation between pages, for steps in a sequence, or for more than about 6 tabs.
Use instead
Links for navigation; a stepper for sequences; a list or menu for many options.

Examples

Settings sections

Tabs are buttons in the tab slot; panels follow in the same order. Arrow keys move between tabs.

Your name, photo and bio.

Choose what we email you about.

Plan, invoices and payment method.

<va-tabs label="Account settings">
  <button slot="tab">Profile</button>
  <button slot="tab">Notifications</button>
  <button slot="tab">Billing</button>
  <div slot="panel"><p>Your name, photo and bio.</p></div>
  <div slot="panel"><p>Choose what we email you about.</p></div>
  <div slot="panel"><p>Plan, invoices and payment method.</p></div>
</va-tabs>

Manual activation

With activation="manual", arrow keys only move focus; Enter or Space selects. Use it when showing a panel is slow.

Today's numbers.

This week's numbers.

This month's numbers.

<va-tabs label="Reports" activation="manual" selected="1">
  <button slot="tab">Daily</button>
  <button slot="tab">Weekly</button>
  <button slot="tab">Monthly</button>
  <div slot="panel"><p>Today's numbers.</p></div>
  <div slot="panel"><p>This week's numbers.</p></div>
  <div slot="panel"><p>This month's numbers.</p></div>
</va-tabs>

Install and import

npm install @firstprateek/vastu @firstprateek/vastu-tokens
import '@firstprateek/vastu-tokens/lekha.css'; // once per page
import '@firstprateek/vastu/tabs';

API

Attributes and properties

AttributePropertyTypeDefaultDescription
selectedselectednumber0The index of the selected tab, from 0.
activationactivation'automatic' | 'manual''automatic'automatic selects a tab as soon as it gets focus; manual waits for Enter or Space.
labellabelstring''Accessible name of the row of tabs.

Events

EventDescription
changeWhen the user selects a different tab. Read event.target.selected.

Slots

SlotDescription
tabThe tabs, as <button slot="tab"> elements, in order.
panelThe panels, as elements with slot="panel", in the same order as the tabs.

Styling

Components read the brand's tokens. To adjust one component, set its custom properties or style its parts.

Custom propertyTypeDescription
--va-tabs-border<color>Colour of the line under the row of tabs.
--va-tabs-indicator<color>Colour of the selected tab's underline.
--va-tabs-text<color>Text colour of tabs that aren't selected.
PartDescription
::part(tablist)The row of tabs.
::part(panels)The wrapper around the panels.

Accessibility

Role
tablist
Accessible name
The label attribute, such as "Account settings".
Note
Each tab gets role="tab", aria-selected and aria-controls; each panel gets role="tabpanel" and aria-labelledby.
Note
Only the selected tab is in the Tab order (roving tabindex); arrow keys move between tabs.

Keyboard

KeyDoes
ArrowRightMoves to the next tab, wrapping round. Selects it unless `activation="manual"`.
ArrowLeftMoves to the previous tab, wrapping round.
HomeMoves to the first tab.
EndMoves to the last tab.
EnterSelects the focused tab (needed when `activation="manual"`).
TabMoves focus from the tabs into the selected panel.

Every row on this page is checked by contract tests generated from the same manifest, in Chromium, Firefox and WebKit. Read the manifest.