Tabs
Layered panels of content, one shown at a time.
Tabs follows the Tabs WAI-ARIA design pattern, with automatic or manual activation.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Tabs } from '@leve-ui/tabs';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Tabs.Root defaultValue="tab1" class="tabs-default-root">
<Tabs.List aria-label="Account settings" class="tabs-default-list">
<Tabs.Trigger value="tab1" class="tabs-default-trigger">
Account
</Tabs.Trigger>
<Tabs.Trigger value="tab2" class="tabs-default-trigger">
Password
</Tabs.Trigger>
<Tabs.Trigger value="tab3" class="tabs-default-trigger">
Settings
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="tab1" class="tabs-default-content">
Manage your account details and profile information.
</Tabs.Content>
<Tabs.Content value="tab2" class="tabs-default-content">
Change your password and manage authentication settings.
</Tabs.Content>
<Tabs.Content value="tab3" class="tabs-default-content">
Configure application preferences and notification options.
</Tabs.Content>
</Tabs.Root>
);
});.tabs-default-root {
width: 360px;
max-width: 100%;
font-family: inherit;
font-size: 14px;
}
.tabs-default-list {
display: flex;
border-bottom: 1px solid light-dark(#e2e8f0, #ffffff1f);
}
.tabs-default-trigger {
margin-bottom: -1px;
padding: 8px 12px;
background: none;
border: none;
border-bottom: 2px solid transparent;
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.tabs-default-trigger:hover {
color: light-dark(#0f172a, #ffffff);
}
/* Selected tab */
.tabs-default-trigger[data-state="active"] {
color: light-dark(#0f172a, #ffffff);
border-bottom-color: #009dfd;
}
.tabs-default-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}
.tabs-default-trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.tabs-default-content {
padding-top: 14px;
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}
.tabs-default-content:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/tabsnpm install @leve-ui/tabsyarn add @leve-ui/tabsbun add @leve-ui/tabspnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Tabs } from '@leve-ui/tabs'; // or '@leve-ui/core'
<Tabs.Root>
<Tabs.List>
<Tabs.Trigger />
</Tabs.List>
<Tabs.Content />
</Tabs.Root>
| Part | Element | Description |
|---|---|---|
Tabs.Root | <div> | Wraps the parts and holds the selected value. |
Tabs.List | <div> | The list of tabs, with role="tablist". |
Tabs.Trigger | <button> | One tab, with role="tab". Selects the Content with the same value. Use as to render another element or component. |
Tabs.Content | <div> | The panel of a tab, with role="tabpanel". Always rendered, and hidden while its tab is not selected. |
A component passed to as on Tabs.Trigger must spread the props it receives onto a single element, including id, aria-*, data-*, and on*$ event handlers.
Examples#
Controlled#
bind:value controls the selected tab with a signal. The buttons outside the tabs change it.
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Tabs } from '@leve-ui/tabs';
import styles from './controlled.css?inline';
export default component$(() => {
useStyles$(styles);
const selected = useSignal<string | null>('tab1');
return (
<div class="tabs-controlled-wrapper">
<div class="tabs-controlled-controls">
<span class="tabs-controlled-label">External controls:</span>
<button
type="button"
class="tabs-controlled-btn"
onClick$={() => {
selected.value = 'tab1';
}}
>
Select Tab 1
</button>
<button
type="button"
class="tabs-controlled-btn"
onClick$={() => {
selected.value = 'tab2';
}}
>
Select Tab 2
</button>
</div>
<Tabs.Root bind:value={selected} class="tabs-controlled-root">
<Tabs.List aria-label="Controlled tabs" class="tabs-controlled-list">
<Tabs.Trigger value="tab1" class="tabs-controlled-trigger">
First
</Tabs.Trigger>
<Tabs.Trigger value="tab2" class="tabs-controlled-trigger">
Second
</Tabs.Trigger>
<Tabs.Trigger value="tab3" class="tabs-controlled-trigger">
Third
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="tab1" class="tabs-controlled-content">
Panel 1 content (controlled by signal).
</Tabs.Content>
<Tabs.Content value="tab2" class="tabs-controlled-content">
Panel 2 content (controlled by signal).
</Tabs.Content>
<Tabs.Content value="tab3" class="tabs-controlled-content">
Panel 3 content (controlled by signal).
</Tabs.Content>
</Tabs.Root>
</div>
);
});.tabs-controlled-wrapper {
display: flex;
flex-direction: column;
gap: 12px;
font-family: inherit;
font-size: 14px;
}
.tabs-controlled-controls {
display: flex;
align-items: center;
gap: 8px;
}
.tabs-controlled-label {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}
/* Secondary button */
.tabs-controlled-btn {
height: 32px;
padding: 0 12px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.tabs-controlled-btn:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.tabs-controlled-btn:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.tabs-controlled-root {
width: 360px;
max-width: 100%;
}
.tabs-controlled-list {
display: flex;
border-bottom: 1px solid light-dark(#e2e8f0, #ffffff1f);
}
.tabs-controlled-trigger {
margin-bottom: -1px;
padding: 8px 12px;
background: none;
border: none;
border-bottom: 2px solid transparent;
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.tabs-controlled-trigger:hover {
color: light-dark(#0f172a, #ffffff);
}
/* Selected tab */
.tabs-controlled-trigger[data-state="active"] {
color: light-dark(#0f172a, #ffffff);
border-bottom-color: #009dfd;
}
.tabs-controlled-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}
.tabs-controlled-trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.tabs-controlled-content {
padding-top: 14px;
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}
.tabs-controlled-content:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}Disabled Trigger#
disabled on a Trigger disables that tab. Arrow keys skip it.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Tabs } from '@leve-ui/tabs';
import styles from './disabled-trigger.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Tabs.Root defaultValue="tab1" class="tabs-disabled-trigger-root">
<Tabs.List aria-label="Tabs with disabled item" class="tabs-disabled-trigger-list">
<Tabs.Trigger value="tab1" class="tabs-disabled-trigger-trigger">
Available
</Tabs.Trigger>
<Tabs.Trigger value="tab2" disabled class="tabs-disabled-trigger-trigger">
Disabled
</Tabs.Trigger>
<Tabs.Trigger value="tab3" class="tabs-disabled-trigger-trigger">
Also Available
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="tab1" class="tabs-disabled-trigger-content">
Panel 1: Navigating with arrow keys skips the disabled tab.
</Tabs.Content>
<Tabs.Content value="tab2" class="tabs-disabled-trigger-content">
Panel 2: This panel cannot be selected by the user.
</Tabs.Content>
<Tabs.Content value="tab3" class="tabs-disabled-trigger-content">
Panel 3: Reached directly from the first tab.
</Tabs.Content>
</Tabs.Root>
);
});.tabs-disabled-trigger-root {
width: 360px;
max-width: 100%;
font-family: inherit;
font-size: 14px;
}
.tabs-disabled-trigger-list {
display: flex;
border-bottom: 1px solid light-dark(#e2e8f0, #ffffff1f);
}
.tabs-disabled-trigger-trigger {
margin-bottom: -1px;
padding: 8px 12px;
background: none;
border: none;
border-bottom: 2px solid transparent;
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.tabs-disabled-trigger-trigger:hover {
color: light-dark(#0f172a, #ffffff);
}
/* Selected tab */
.tabs-disabled-trigger-trigger[data-state="active"] {
color: light-dark(#0f172a, #ffffff);
border-bottom-color: #009dfd;
}
.tabs-disabled-trigger-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}
.tabs-disabled-trigger-trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.tabs-disabled-trigger-content {
padding-top: 14px;
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}
.tabs-disabled-trigger-content:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}Manual#
With activationMode="manual", arrow keys only move focus. Press Enter or Space to select the focused tab.
import { $, component$, type PropsOf, Slot, sync$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Tabs } from '@leve-ui/tabs';
import styles from './manual.css?inline';
// Example of a router link component (such as Qwik City's <Link>) that spreads received props onto <a>
const ClientTabLink = component$<PropsOf<'a'>>(({ onClick$, ...props }) => {
const visited = useSignal(false);
const preventDefault$ = sync$((event: MouseEvent) => {
event.preventDefault();
});
const handleClick$ = $(() => {
visited.value = true;
});
return (
<a
{...props}
data-visited={visited.value ? 'true' : undefined}
onClick$={[preventDefault$, handleClick$, onClick$]}
>
<Slot />
</a>
);
});
export default component$(() => {
useStyles$(styles);
return (
<Tabs.Root activationMode="manual" defaultValue="tab1" class="tabs-manual-root">
<Tabs.List aria-label="Manual activation tabs" class="tabs-manual-list">
<Tabs.Trigger as={ClientTabLink} href="#first" value="tab1" class="tabs-manual-trigger">
First
</Tabs.Trigger>
<Tabs.Trigger as={ClientTabLink} href="#second" value="tab2" class="tabs-manual-trigger">
Second
</Tabs.Trigger>
<Tabs.Trigger as={ClientTabLink} href="#third" value="tab3" class="tabs-manual-trigger">
Third
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="tab1" class="tabs-manual-content">
Panel 1: In manual mode, arrow keys move focus without changing the active panel.
</Tabs.Content>
<Tabs.Content value="tab2" class="tabs-manual-content">
Panel 2: Press Enter or Space to activate the focused tab.
</Tabs.Content>
<Tabs.Content value="tab3" class="tabs-manual-content">
Panel 3: Active panel updated.
</Tabs.Content>
</Tabs.Root>
);
});.tabs-manual-root {
width: 360px;
max-width: 100%;
font-family: inherit;
font-size: 14px;
}
.tabs-manual-list {
display: flex;
border-bottom: 1px solid light-dark(#e2e8f0, #ffffff1f);
}
.tabs-manual-trigger {
display: inline-block;
margin-bottom: -1px;
padding: 8px 12px;
background: none;
border: none;
border-bottom: 2px solid transparent;
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#475569, #c9ddf0);
text-decoration: none;
cursor: pointer;
}
.tabs-manual-trigger:hover {
color: light-dark(#0f172a, #ffffff);
}
/* Selected tab */
.tabs-manual-trigger[data-state="active"] {
color: light-dark(#0f172a, #ffffff);
border-bottom-color: #009dfd;
}
.tabs-manual-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}
.tabs-manual-trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.tabs-manual-content {
padding-top: 14px;
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}
.tabs-manual-content:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}Vertical#
orientation="vertical" makes ArrowUp / ArrowDown move between tabs instead of ArrowLeft / ArrowRight. It sets aria-orientation and data-orientation.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Tabs } from '@leve-ui/tabs';
import styles from './vertical.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Tabs.Root orientation="vertical" defaultValue="tab1" class="tabs-vertical-root">
<Tabs.List aria-label="Vertical navigation" class="tabs-vertical-list">
<Tabs.Trigger value="tab1" class="tabs-vertical-trigger">
Profile
</Tabs.Trigger>
<Tabs.Trigger value="tab2" class="tabs-vertical-trigger">
Billing
</Tabs.Trigger>
<Tabs.Trigger value="tab3" class="tabs-vertical-trigger">
Notifications
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="tab1" class="tabs-vertical-content">
Profile details and public information.
</Tabs.Content>
<Tabs.Content value="tab2" class="tabs-vertical-content">
Billing history and subscription plan.
</Tabs.Content>
<Tabs.Content value="tab3" class="tabs-vertical-content">
Notification preferences and email alerts.
</Tabs.Content>
</Tabs.Root>
);
});.tabs-vertical-root {
display: flex;
gap: 16px;
width: 360px;
max-width: 100%;
font-family: inherit;
font-size: 14px;
}
.tabs-vertical-list {
display: flex;
flex-direction: column;
border-right: 1px solid light-dark(#e2e8f0, #ffffff1f);
}
.tabs-vertical-trigger {
margin: 0 -1px 0 0;
padding: 8px 12px;
background: none;
border: none;
border-right: 2px solid transparent;
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#475569, #c9ddf0);
text-align: left;
cursor: pointer;
}
.tabs-vertical-trigger:hover {
color: light-dark(#0f172a, #ffffff);
}
/* Selected tab */
.tabs-vertical-trigger[data-state="active"] {
color: light-dark(#0f172a, #ffffff);
border-right-color: #009dfd;
}
.tabs-vertical-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}
.tabs-vertical-trigger[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.tabs-vertical-content {
padding-top: 8px;
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}
.tabs-vertical-content:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
border-radius: 4px;
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| defaultValue | null | string | null The value of the tab selected initially (uncontrolled). |
| bind:value | – | Signal<string | null> | Signal<string> A signal with the value of the selected tab (controlled). |
| onValueChange$ | – | QRL<(value: string) => void> Called when the selected tab changes. |
| orientation | 'horizontal' | 'horizontal' | 'vertical' The orientation of the tab list. |
| activationMode | 'automatic' | 'automatic' | 'manual' How a tab is selected.
- |
| loop | true | boolean Whether arrow keys wrap from the last tab to the first and vice versa. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-tabs | Always present |
data-orientation | "horizontal" | "vertical" |
The selected value is uncontrolled with defaultValue, or controlled with bind:value. onValueChange$ is called when the selected tab changes. Give each Trigger and its Content the same value.
List#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-tabs-list | Always present |
data-orientation | "horizontal" | "vertical" |
Trigger#
| Prop | Default | Type |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any> The element or component to render instead of |
| value (required) | – | string A unique value that identifies the tab. Must match the |
| disabled | false | boolean Disables the tab. |
Also accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-tabs-trigger | Always present |
data-state | "active" | "inactive" |
data-disabled | Present when disabled |
data-orientation | "horizontal" | "vertical" |
Content#
| Prop | Default | Type |
|---|---|---|
| value (required) | – | string The |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-tabs-content | Always present |
data-state | "active" | "inactive" |
data-orientation | "horizontal" | "vertical" |
The Content has the hidden attribute while its tab is not selected.
Accessibility#
Complies with the Tabs WAI-ARIA design pattern.
- Name the tab list with
aria-labeloraria-labelledbyonTabs.List. Tabs.Listrendersrole="tablist"andaria-orientation.- Each
Tabs.Triggeris a<button type="button">withrole="tab",aria-selectedandaria-controlspointing to its Content. Its text content is its accessible name. Disabled tabs use the nativedisabledattribute, so they are not focusable. - Each
Tabs.Contentrendersrole="tabpanel"andaria-labelledbypointing to its Trigger. It hastabIndex={0}, so the panel itself can receive focus. - Only one tab is in the tab sequence: the selected tab, or the first enabled tab when nothing is selected.
Keyboard Interactions#
| Key | Description |
|---|---|
Tab | Moves focus to the selected tab. When focus is on a tab, moves focus to the selected panel. |
ArrowRight / ArrowLeft | Horizontal only. Moves focus to the next / previous enabled tab, and selects it in automatic mode. Wraps around when loop is true. |
ArrowDown / ArrowUp | Vertical only. Moves focus to the next / previous enabled tab, and selects it in automatic mode. Wraps around when loop is true. |
Home | Moves focus to the first enabled tab, and selects it in automatic mode. |
End | Moves focus to the last enabled tab, and selects it in automatic mode. |
Enter / Space | Selects the focused tab (manual mode). |
In right-to-left text, ArrowRight and ArrowLeft are reversed.