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-tokensimport '@firstprateek/vastu-tokens/lekha.css'; // once per page
import '@firstprateek/vastu/tabs';API
Attributes and properties
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
selected | selected | number | 0 | The index of the selected tab, from 0. |
activation | activation | 'automatic' | 'manual' | 'automatic' | automatic selects a tab as soon as it gets focus; manual waits for Enter or Space. |
label | label | string | '' | Accessible name of the row of tabs. |
Events
| Event | Description |
|---|---|
change | When the user selects a different tab. Read event.target.selected. |
Slots
| Slot | Description |
|---|---|
tab | The tabs, as <button slot="tab"> elements, in order. |
panel | The 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 property | Type | Description |
|---|---|---|
--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. |
| Part | Description |
|---|---|
::part(tablist) | The row of tabs. |
::part(panels) | The wrapper around the panels. |
Accessibility
- Role
tablist- Accessible name
- The
labelattribute, such as "Account settings". - Note
- Each tab gets
role="tab",aria-selectedandaria-controls; each panel getsrole="tabpanel"andaria-labelledby. - Note
- Only the selected tab is in the Tab order (roving tabindex); arrow keys move between tabs.
Keyboard
| Key | Does |
|---|---|
| ArrowRight | Moves to the next tab, wrapping round. Selects it unless `activation="manual"`. |
| ArrowLeft | Moves to the previous tab, wrapping round. |
| Home | Moves to the first tab. |
| End | Moves to the last tab. |
| Enter | Selects the focused tab (needed when `activation="manual"`). |
| Tab | Moves 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.