Tabs
Make changes to your account here. Click save when you're done.
Features
- Can be controlled or uncontrolled.
- Supports horizontal/vertical orientation.
- Supports automatic/manual activation.
- Full keyboard navigation.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import { TabsContent, TabsIndicator, TabsList, TabsRoot, TabsTrigger } from 'reka-ui'
</script>
<template>
<TabsRoot>
<TabsList>
<TabsIndicator />
<TabsTrigger />
</TabsList>
<TabsContent />
</TabsRoot>
</template>API Reference
Root
Contains all the tabs component parts.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
List
Contains the triggers that are aligned along the edge of the active content.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Trigger
The button that activates its associated content.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
Indicator
The indicator that highlights the current active tab.
| CSS Variable | Description |
|---|---|
--reka-tabs-indicator-size | The size of the indicator. |
--reka-tabs-indicator-thickness | The thickness of the indicator. |
--reka-tabs-indicator-position | The position of the indicator |
Content
Contains the content associated with each trigger.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-orientation] | "vertical" | "horizontal" |
Examples
Vertical
You can create vertical tabs by using the orientation prop.
<script setup>
import { TabsContent, TabsList, TabsRoot, TabsTrigger } from 'reka-ui'
</script>
<template>
<TabsRoot
default-value="tab1"
orientation="vertical"
>
<TabsList aria-label="tabs example">
<TabsTrigger value="tab1">
One
</TabsTrigger>
<TabsTrigger value="tab2">
Two
</TabsTrigger>
<TabsTrigger value="tab3">
Three
</TabsTrigger>
</TabsList>
<TabsContent value="tab1">
Tab one content
</TabsContent>
<TabsContent value="tab2">
Tab two content
</TabsContent>
<TabsContent value="tab3">
Tab three content
</TabsContent>
</TabsRoot>
</template>Manual activation
By default a tab activates as soon as its trigger gets focus, so arrow keys switch panels immediately. When a panel is expensive to render or fetches data, set activation-mode="manual": the arrow keys only move focus, and Enter, Space or a click activates the focused tab.
<template>
<TabsRoot default-value="overview" activation-mode="manual">
<!-- ... -->
</TabsRoot>
</template>Syncing the tab with the URL
Control the value with v-model and read and write it from the route, so a reload or a shared link opens the same tab.
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
const tab = computed({
get: () => {
const tab = route.query.tab
return (Array.isArray(tab) ? tab[0] : tab) ?? 'overview'
},
set: value => router.replace({ query: { ...route.query, tab: value } }),
})
</script>
<template>
<TabsRoot v-model="tab">
<!-- ... -->
</TabsRoot>
</template>Preventing a tab change
beforeUpdate:modelValue fires before every change, with a details object whose reason is 'trigger-press', 'trigger-focus' (automatic activation), 'trigger-keydown' or 'imperative-action'. Call details.cancel() to keep the current tab, for example while a form in it has unsaved changes.
Keeping inactive panels mounted
The panel elements of inactive tabs stay in the DOM with hidden, but their content unmounts. Set :unmount-on-hide="false" on TabsRoot to keep the content mounted too, so its state (form input, a playing video, a loaded iframe) survives switching tabs.
Animated indicator
TabsIndicator renders inside TabsList and exposes the size and position of the active trigger as the --reka-tabs-indicator-size and --reka-tabs-indicator-position CSS variables. Transition them to slide an underline between tabs, as in the demo at the top of this page.
<template>
<TabsList class="relative">
<TabsIndicator class="absolute bottom-0 left-0 h-0.5 w-[--reka-tabs-indicator-size] translate-x-[--reka-tabs-indicator-position] bg-black transition-[width,transform]" />
<!-- triggers -->
</TabsList>
</template>Real-world examples
- Command Menu with Tabs: A command palette whose results are scoped by Tabs, with Tab cycling scopes without leaving the search field.
Accessibility
Adheres to the Tabs WAI-ARIA design pattern.
Keyboard Interactions
| Key | Description |
|---|---|
Tab | When focus moves onto the tabs, focuses the active trigger. When a trigger is focused, moves focus to the active content. |
ArrowDown | Moves focus to the next trigger depending on orientation and activates its associated content. |
ArrowRight | Moves focus to the next trigger depending on orientation and activates its associated content. |
ArrowUp | Moves focus to the previous trigger depending on orientation and activates its associated content. |
ArrowLeft | Moves focus to the previous trigger depending on orientation and activates its associated content. |
Home | Moves focus to the first trigger and activates its associated content. |
End | Moves focus to the last trigger and activates its associated content. |
In a right-to-left layout (dir="rtl" on TabsRoot, or on a ConfigProvider), ArrowLeft moves to the next trigger and ArrowRight to the previous one, in a horizontal list. See Internationalization.
Headless composable
useTabs holds the selected value (modelValue, selectTab()), the ids and the ARIA wiring: the root and list part surfaces, and getTriggerSurface(value, disabled?) and getContentSurface(value) for each tab. It doesn't move focus between the triggers with the arrow keys, or mount and unmount the panels: wrap the triggers in a RovingFocusGroup and render the panels with v-if or Presence, or use the components. Call the surface getters in setup(), once per tab, not in the template. Pass a stable baseId when rendering on the server. The composable is experimental; see Headless composables.
