Cómo funciona
The indicator fills the whole tab bar and a clip-path: inset() cuts out the current tab, so transitioning clip-path animates position and width together without stretching the pill's corners. The pill variant carries a copy of the tabs in inverted text inside the indicator, clipped by the same path, so text colour and pill always match mid-slide. The pill is ink on a filled track by default; set --ds-tabs-pill and --ds-tabs-on-pill to use the accent or a brand colour. JS only measures the tab's left and right distances into CSS variables.
Úsalo para
- Lists such as orders, messages or favourites filtered by status (All, Unpaid, Shipping, Delivered).
- Switching between 2 to 5 sibling views on a page, such as a product's Overview, Specs and Reviews.
- A segmented choice for view mode or time range instead of radios (tab bar only, no content).
Busca otra opción cuando
- More than 6 tabs or long labels: they get squeezed, use a scrollable tab bar or a dropdown.
- Navigation to different page addresses: use link navigation, not role=tab.
- A single-choice question in a form: the semantics differ, use a radio group.
Uso
import { useState } from 'react'
import { SlidingTabs } from './SlidingTabs.jsx'
export function Orders() {
const [tab, setTab] = useState('all')
return (
<SlidingTabs
label="Order status"
value={tab}
onChange={setTab}
fullWidth
items={[
{ value: 'all', label: 'All', content: <OrderList /> },
{ value: 'unpaid', label: 'Unpaid', badge: 2, content: <OrderList status="unpaid" /> },
{ value: 'shipping', label: 'Shipping', content: <OrderList status="shipping" /> },
{ value: 'delivered', label: 'Delivered', content: <OrderList status="delivered" /> },
]}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
items | { value, label, badge?, content?, disabled? }[] | — | The tabs. If no item has content, only the tab bar is rendered, which suits a filter. |
value | string | — | Controlled: the selected value. |
defaultValue | string | first enabled item | Uncontrolled: the initial value. If the selected item is later removed from items, it falls back to the first enabled one. |
onChange | (value: string) => void | — | Called when the selection changes. |
variant | 'pill' | 'underline' | 'pill' | Look: an ink pill on a filled track, or a thick underline. |
duration | number | 240 | Indicator glide time in ms; panels enter in 1.25 times that. Follows --ds-duration-base when omitted. |
fullWidth | boolean | false | Stretches the tab bar and splits the width equally, common on mobile. |
label | string | — | Accessible name of the tab bar (aria-label), such as “Order status”. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
Accesibilidad
- The full WAI-ARIA Tabs pattern: role tablist, tab and tabpanel linked by aria-selected, aria-controls and aria-labelledby.
- Left and Right switch tabs with wrap-around, skipping disabled ones; Home and End jump to the ends; focus selects the tab.
- Roving tabindex: only the selected tab is in the Tab order, and one more Tab enters the panel (tabIndex=0).
- The indicator and its inverted-text copy are aria-hidden, so screen readers do not read them twice.
- Pill tabs get an invisible click area above and below for a 44px touch height, and a :focus-visible ring.
- With reduced motion the indicator jumps into place and panels do not slide.
Movimiento
Indicator clip-path 240ms ease-out; panel entrance 300ms ease-out moving 16px; text colour 160ms; a pressed tab label scales to 0.96 for 100ms.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
SlidingTabs.jsxsliding-tabs.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.