Tabs Primitive
Use Tabs to show one panel at a time in the same space. Each tab names a panel, and the arrow keys move between tabs.
Anatomy
Use the Astro primitive adapter to render Tabs anatomy with the Runtime wiring included.
---import { Tabs } from "@starwind-ui/astro/tabs";---
<Tabs.Root defaultValue="account"> <Tabs.List> <Tabs.Tab value="account">Account</Tabs.Tab> </Tabs.List> <Tabs.Panel value="account">Account settings</Tabs.Panel></Tabs.Root>Use the React primitive adapter when Tabs state participates in React rendering.
import { Tabs } from "@starwind-ui/react/tabs";
export function Example() { return ( <Tabs.Root defaultValue="account"> <Tabs.List> <Tabs.Tab value="account">Account</Tabs.Tab> </Tabs.List> <Tabs.Panel value="account">Account settings</Tabs.Panel> </Tabs.Root> );}Use the Vue components to build Tabs.
<script setup lang="ts">import Tabs from "@starwind-ui/vue/tabs";</script>
<template> <Tabs.Root defaultValue="account"> <Tabs.List> <Tabs.Tab value="account">Account</Tabs.Tab> </Tabs.List> <Tabs.Panel value="account">Account settings</Tabs.Panel> </Tabs.Root></template>Use the Svelte components to build Tabs.
<script lang="ts"> import { Tabs } from "@starwind-ui/svelte/tabs";</script>
<Tabs.Root defaultValue="account"> <Tabs.List> <Tabs.Tab value="account">Account</Tabs.Tab> </Tabs.List> <Tabs.Panel value="account">Account settings</Tabs.Panel></Tabs.Root>Render the Tabs data-sw-* contract yourself, then initialize createTabs.
<div data-sw-tabs data-default-value="account"> <div data-sw-tabs-list role="tablist"> <button data-sw-tabs-tab role="tab" type="button" data-value="account">Account</button> </div> <div data-sw-tabs-panel role="tabpanel" hidden data-value="account">Account settings</div></div>
<script type="module"> import { createTabs } from "@starwind-ui/runtime/tabs";
const root = document.querySelector("[data-sw-tabs]"); if (root) { createTabs(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
Usage Guidelines
- Animate panel changes with CSS. Apply transitions or keyframe animations to the panel. Use
data-starting-stylefor its entrance anddata-ending-stylefor its exit. Initial panels appear without an entrance animation. The animated Styled Tabs example shows a crossfade with reduced-motion support. - Keep visibility under Runtime control. An outgoing panel becomes
inertimmediately, then receiveshiddenwhen its own finite motion finishes. Its controls cannot receive input during the exit. Descendant motion and infinite animations do not delay hiding. Panel DOM and local state remain mounted;keepMountedpreserves its existing behavior. - Keep keyboard entry aligned with selection. When code changes the selected tab while focus is outside the tab list, the selected enabled trigger becomes the next keyboard entry target. Browser focus stays where it is. While focus is inside the list, Tabs preserves the userโs keyboard position.
API Reference
Root
The main element that owns the Tabs Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-tabs
- Role
- -
Props
value TabsValue -
- Description
- Controls the current Tabs value.
- Kind
- control
- Targets
- root
- Full type
- TabsValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
defaultValue TabsValue -
- Description
- Sets the initial Tabs value for uncontrolled usage.
- Kind
- control
- Targets
- -
- Full type
- TabsValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
orientation TabsOrientation "horizontal"
- Description
- Sets the Tabs orientation.
- Kind
- option
- Targets
- -
- Full type
- TabsOrientation
syncKey string -
- Description
- Configures the sync key option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- string
onValueChange (value: TabsValue, details: TabsValueChangeDetails) => void -
- Description
- Runs when the Tabs value changes.
- Kind
- callback
- Targets
- -
- Full type
- (value: TabsValue, details: TabsValueChangeDetails) => void
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Events
valueChange onValueChange value: TabsValue
- Description
- Fires when the value changes for Tabs.
- DOM event
- starwind:value-change
- Details type
- TabsValueChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
value TabsValue value / defaultValue
- Description
- Tracks the current Tabs value.
- Initial attribute
- data-default-value
- Runtime getter
- getValue
- Runtime setter
- setValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
List
The list of selectable Tabs items.
- Default element
- div
- Discovery hook
- data-sw-tabs-list
- Role
- tablist
Props
activateOnFocus boolean false
- Description
- Configures the activate on focus option for the List part.
- Kind
- option
- Targets
- list
- Full type
- boolean
loopFocus boolean true
- Description
- Lets keyboard focus wrap around the Tabs items.
- Kind
- option
- Targets
- list
- Full type
- boolean
Data Attributes
Runtime hooks
Metadata
Tab
A selectable tab inside Tabs.
- Default element
- button
- Discovery hook
- data-sw-tabs-tab
- Role
- tab
Props
disabled boolean false
- Description
- Disables the Tab part.
- Kind
- option
- Targets
- tab
- Full type
- boolean
value string -
- Description
- Controls the current Tabs value.
- Kind
- option
- Targets
- tab
- Full type
- string
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
Panel
The content panel controlled by Tabs.
- Default element
- div
- Discovery hook
- data-sw-tabs-panel
- Role
- tabpanel
Props
keepMounted boolean false
- Description
- Keeps the Panel part in the DOM when hidden.
- Kind
- rendering
- Targets
- panel
- Full type
- boolean
value string -
- Description
- Controls the current Tabs value.
- Kind
- option
- Targets
- panel
- Full type
- string
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
Indicator
Visual state indicator rendered by Tabs.
- Default element
- span
- Discovery hook
- data-sw-tabs-indicator
- Role
- presentation
Data Attributes
Runtime hooks
State
Runtime API
- Factory
createTabs- Import
@starwind-ui/runtime/tabs- Root hook
- root
data-sw-tabs - Option props
- defaultValue, orientation, syncKey, value
Option Lifecycles
| Option | Lifecycle |
|---|---|
| syncKey | constructor-only |
Runtime Setters
Related Styled Components
Changelog
View version history v1.3.0 7 releases
v1.3.0
- Tabs horizontal arrow keys follow visible positions in RTL. The indicator moves to the new position after the `starwind:direction-change` event.
- In Vue, the default slot of `Tabs.Root` no longer receives `value` and `orientation`. If you read `value`, use `v-model` on the root. If you read `orientation`, use the `orientation` prop that you pass, or the root's `data-orientation` attribute.
- In Svelte, the `children` snippet of `Tabs.Root` no longer receives arguments. If you read the value from a snippet argument, use `bind:value` on the root instead.
- `Tabs.Tab` and `Tabs.Panel` no longer pass `active` to their default slot in Vue or to their `children` snippet in Svelte. Style with the `data-state="active"` attribute, or compare the Tabs value with the item value.
- In Vue, `TabsContextValue` no longer has `refresh()`. The parts get it from their Root.
- In Vue, this component now needs Vue 3.5.21 or newer.
- Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.
v1.2.0
- In Astro, an attribute that you set on a Tabs part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `value` that you pass when Tabs mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Tabs keeps its own state.
v1.1.0
- Vue Tabs keeps the selected tab when a selection change is canceled.
- Tabs keeps its initial `syncKey` while mounted. Remount it to change that key.
- Added CSS entrance and exit animations using `data-starting-style` and `data-ending-style`. Panels stay visible until their exit animation finishes, while inactive content stops accepting keyboard focus and clicks.
- When your code selects a tab, pressing Tab to enter the tab list now focuses that tab. Focus stays in place while someone is already navigating the list.
- Reduced repeated JavaScript when Astro Tabs is used with other Starwind components.
v1.0.1
- Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.
- Fixed the active tab indicator position when its container is scrolled or transformed.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.1.0
- First release. Tabs handles selection, arrow-key focus, showing and hiding panels, and orientation. Groups of tabs can stay in sync.