Specification
- Catalogue
- No. 91 of 110
- Section
- Tabs
- Interaction
- Layout Animation
- Element
- Tabs
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
One indicator element sits behind the tab labels and moves to the selected tab, instead of each tab drawing its own background. Its position and width are read from the tab with offsetLeft and offsetWidth, which ignore transforms, so it stays right inside a scaled preview.
The curve overshoots slightly (cubic-bezier(0.3, 1.25, 0.4, 1)), enough to feel springy without wobbling. The panel content re-mounts on change so its bars can rise in with a short stagger.
Arrow keys move between tabs and only the selected tab is in the tab order, following the usual tablist pattern.
Code
React and plain CSS"use client";
import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./sliding-tabs.module.css";
const TABS = [
{ id: "overview", label: "Overview", bars: [92, 70, 84] },
{ id: "activity", label: "Activity", bars: [60, 88, 46, 74] },
{ id: "settings", label: "Settings", bars: [78, 52] },
];
export default function SlidingTabs({ active, context }: LivePreviewProps) {
const [selected, setSelected] = useState(0);
const [step, setStep] = useState(0);
const listRef = useRef<HTMLDivElement>(null);
const indicatorRef = useRef<HTMLSpanElement>(null);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
const id = window.setInterval(() => setStep((s) => s + 1), 1100);
return () => window.clearInterval(id);
}, [demo]);
const current = context === "card" ? (demo ? (step + 1) % TABS.length : 0) : selected;
// Move the indicator under the selected tab. offsetLeft ignores the preview's scale.
useLayoutEffect(() => {
const tab = listRef.current?.children[current + 1] as HTMLElement | undefined;
const indicator = indicatorRef.current;
if (!tab || !indicator) return;
indicator.style.transform = `translateX(${tab.offsetLeft}px)`;
indicator.style.width = `${tab.offsetWidth}px`;
}, [current]);
function onKeyDown(event: KeyboardEvent<HTMLButtonElement>) {
if (event.key !== "ArrowRight" && event.key !== "ArrowLeft") return;
event.preventDefault();
const next = (current + (event.key === "ArrowRight" ? 1 : -1) + TABS.length) % TABS.length;
setSelected(next);
(listRef.current?.children[next + 1] as HTMLElement | undefined)?.focus();
}
return (
<div className={styles.root}>
<div ref={listRef} className={styles.list} role="tablist" aria-label="Project views">
<span ref={indicatorRef} className={styles.indicator} aria-hidden="true" />
{TABS.map((tab, i) => (
<button
key={tab.id}
type="button"
role="tab"
id={`tabs-demo-${tab.id}`}
aria-selected={i === current}
aria-controls="tabs-demo-panel"
tabIndex={context === "card" ? -1 : i === current ? 0 : -1}
className={styles.tab}
onClick={() => setSelected(i)}
onKeyDown={onKeyDown}
>
{tab.label}
</button>
))}
</div>
<div id="tabs-demo-panel" role="tabpanel" aria-labelledby={`tabs-demo-${TABS[current]?.id}`} className={styles.panel}>
<div key={current} className={styles.content}>
{TABS[current]?.bars.map((width, i) => (
<span key={i} className={styles.bar} style={{ width: `${width}%`, animationDelay: `${i * 40}ms` }} />
))}
</div>
</div>
</div>
);
}