Tabs
Flexible navigation tool with various modes and features.
You can explore the tabs component in the following curated examples.
Select Tabs
Render a Tabs component within a Select.
Tabs with links
Render a Tabs component with links as the Tab Trigger.
Anatomy
<Tabs.Root>
<Tabs.List>
<Tabs.Trigger />
<Tabs.Indicator />
</Tabs.List>
<Tabs.Content />
</Tabs.Root>
Examples
Controlled
To create a controlled Tabs component, manage the current selected tab using the value prop and update it when the
onValueChange event handler is called:
Root Provider
An alternative way to control the tabs is to use the RootProvider component and the useTabs hook. This way you can
access the state and methods from outside the component.
Indicator
To provide a visual cue for the selected tab, use the Tabs.Indicator component:
Disabled
To disable a tab, simply pass the disabled prop to the Tabs.Trigger component:
Vertical
The default orientation of the tabs is horizontal. To change the orientation, set the orientation prop to
vertical.
Lazy Mount
Lazy mounting is a feature that allows the content of a tab to be rendered only when the tab is first activated. This is
useful for performance optimization, especially when tab content is large or complex. To enable lazy mounting, use the
lazyMount prop on the Tabs.Content component.
In addition, the unmountOnExit prop can be used in conjunction with lazyMount to unmount the tab content when the
tab is deactivated, freeing up resources. The next time the tab is activated, its content will be re-rendered.
Manual Activation
By default, the tab can be selected when it receives focus from either the keyboard or pointer interaction. This is called automatic tab activation.
In contrast, manual tab activation means the tab is selected with the
Enter key or by clicking on the tab.
Links
Use the asChild prop to render tab triggers as anchor links. This is useful for SEO and allows tabs to work with
browser navigation.
Guides
Router Integration
When using frameworks like Next.js, Remix, or React Router, controlling the active tabs based on the URL can be useful.
To achieve this, you need to do two things:
- Set the
valueprop to the current URL path. - Listen to the
onValueChangeevent and update the URL path.
Here's an example using Remix Router
import { Tabs } from '@ark-ui/<framework>/tabs'
import { useLocation, useNavigate, Link } from '@remix-run/react'
export default function App() {
const { pathname } = useLocation()
const navigate = useNavigate()
const lastPathFragment = pathname.substring(pathname.lastIndexOf('/') + 1)
const activeTab = lastPathFragment.length > 0 ? lastPathFragment : 'homepage'
return (
<Tabs.Root
value={activeTab}
onValueChange={({ value }) => {
navigate(`/${value === 'home' ? '' : value}`)
}}
>
<Tabs.List>
<Tabs.Trigger asChild value="home">
<Link to="">Home</Link>
</Tabs.Trigger>
<Tabs.Trigger asChild value="page-1">
<Link to="page-1">Page 1</Link>
</Tabs.Trigger>
<Tabs.Trigger asChild value="page-2">
<Link to="page-2">Page 2</Link>
</Tabs.Trigger>
</Tabs.List>
</Tabs.Root>
)
}
API Reference
Props
Root
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
activationMode | 'automatic' | 'manual' | 'automatic'The activation mode of the tabs. Can be `manual` or `automatic` - `manual`: Tabs are activated when clicked or press `enter` key. - `automatic`: Tabs are activated when receiving focus |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
composite | booleanWhether the tab is composite | |
defaultValue | stringThe initial selected tab value when rendered. Use when you don't need to control the selected tab value. | |
deselectable | booleanWhether the active tab can be deselected when clicking on it. | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
trigger: (value: string) => string
list: string
content: (value: string) => string
indicator: string
}>The ids of the elements in the tabs. Useful for composition. | |
lazyMount | false | booleanWhether to enable lazy mounting |
loopFocus | true | booleanWhether the keyboard navigation will loop from last tab to first, and vice versa. |
navigate | (details: NavigateDetails) => voidFunction to navigate to the selected tab when clicking on it. Useful if tab triggers are anchor elements. | |
onFocusChange | (details: FocusChangeDetails) => voidCallback to be called when the focused tab changes | |
onValueChange | (details: ValueChangeDetails) => voidCallback to be called when the selected/active tab changes | |
orientation | 'horizontal' | 'horizontal' | 'vertical'The orientation of the tabs. Can be `horizontal` or `vertical` - `horizontal`: only left and right arrow key navigation will work. - `vertical`: only up and down arrow key navigation will work. |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
unmountOnExit | false | booleanWhether to unmount on exit. |
value | stringThe controlled selected tab value |
| Attribute | Description |
|---|---|
[data-scope] | tabs |
[data-part] | root |
[data-orientation] | The orientation of the tabs |
[data-focus] | Present when focused |
TabContent
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
value | stringThe value of the tab | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
TabIndicator
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
TabList
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
TabTrigger
Renders a <button> element.
| Prop | Default | Type |
|---|---|---|
value | stringThe value of the tab | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
disabled | booleanWhether the tab is disabled |
RootProvider
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
value | UseTabsReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
lazyMount | false | booleanWhether to enable lazy mounting |
unmountOnExit | false | booleanWhether to unmount on exit. |
Context
API
| Property | Type |
|---|---|
value | stringThe current value of the tabs. |
focusedValue | stringThe value of the tab that is currently focused. |
setValue | (value: string) => voidSets the value of the tabs. |
clearValue | VoidFunctionClears the value of the tabs. |
setIndicatorRect | (value: string) => voidSets the indicator rect to the tab with the given value |
syncTabIndex | VoidFunctionSynchronizes the tab index of the content element. Useful when rendering tabs within a select or combobox |
focus | VoidFunctionSet focus on the selected tab trigger |
selectNext | (fromValue?: string) => voidSelects the next tab |
selectPrev | (fromValue?: string) => voidSelects the previous tab |
getTriggerState | (props: TriggerProps) => TriggerStateReturns the state of the trigger with the given props |
Accessibility
Complies with the Tabs WAI-ARIA design pattern.
Keyboard Support
| 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 in vertical orientation and activates its associated content. |
ArrowRight | Moves focus to the next trigger in horizontal orientation and activates its associated content. |
ArrowUp | Moves focus to the previous trigger in vertical orientation and activates its associated content. |
ArrowLeft | Moves focus to the previous trigger in horizontal 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. |
EnterSpace | In manual mode, when a trigger is focused, moves focus to its associated content. |