Recorded demo
Source
/**
* Feature tabs - original UI Stacks free component, MIT licensed.
* Source: public page https://uistacks.dev/components/feature-tabs (code block reproduced verbatim, unmodified).
* Upstream project: https://github.com/nazmijavier/uistacks
*/
"use client";
import { useEffect, useId, useRef, useState, useSyncExternalStore, type KeyboardEvent, type ReactNode } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
export interface FeatureTab {
id: string;
title: string;
/** One or two lines, shown under the title while the tab is open. */
description?: ReactNode;
/** A 20 px icon before the title. */
icon?: ReactNode;
/** What the panel shows for this tab: a screenshot, a small live UI, a video. */
preview: ReactNode;
}
export interface FeatureTabsProps {
items: FeatureTab[];
/** The open tab's id, controlled. */
value?: string;
defaultValue?: string;
onValueChange?: (id: string) => void;
/** Time each tab stays open before the next one, in ms. Default 6000. */
interval?: number;
/** Moves through the tabs on its own until someone picks one. Default true (never under reduced motion). */
autoPlay?: boolean;
/** Holds the current tab with no progress (the finished frame for previews). */
paused?: boolean;
/** Panel height in px, kept fixed so the tabs never jump. Default 380. */
panelHeight?: number;
/** An accessible name for the tab list. Default "Features". */
"aria-label"?: string;
className?: string;
}
const noSubscribe = () => () => {};
// The indicator springs past the new tab a touch and settles (the kit's fluid feel).
const glide = { type: "spring", stiffness: 600, damping: 30 } as const;
const spring = { type: "spring", stiffness: 420, damping: 34 } as const;
const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-[var(--select)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--surface)]";
/**
* Feature tabs for a landing page: a list of features on one side and a large preview on the other. The open tab
* shows its line and a thin progress bar, and the list moves on by itself until someone picks a tab. Pointing at it
* or focusing inside holds the progress. The preview rises into the panel on a spring each time it changes.
*/
export function FeatureTabs({
items,
value,
defaultValue,
onValueChange,
interval = 6000,
autoPlay = true,
paused = false,
panelHeight = 380,
"aria-label": label = "Features",
className = "",
}: FeatureTabsProps) {
const prefers = useReducedMotion();
const mounted = useSyncExternalStore(noSubscribe, () => true, () => false);
const reduce = mounted && Boolean(prefers);
const id = useId();
const [inner, setInner] = useState(defaultValue ?? items[0]?.id);
const [picked, setPicked] = useState(false);
const current = value ?? inner;
const index = Math.max(0, items.findIndex((t) => t.id === current));
const active = items[index];
const running = autoPlay && !paused && !reduce && !picked && mounted && items.length > 1;
const hold = useRef(false);
const bar = useRef<HTMLSpanElement>(null);
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const latest = useRef({ items, onValueChange, value });
useEffect(() => {
latest.current = { items, onValueChange, value };
});
// Progress for the open tab: a rAF loop that writes the bar straight to the DOM and moves on at the end. It starts
// and stops with the effect, so Strict Mode never leaves two loops running.
useEffect(() => {
if (!running) return;
let elapsed = 0;
let last = performance.now();
let raf = 0;
const tick = (now: number) => {
const dt = Math.min(200, now - last);
last = now;
if (!hold.current && !document.hidden) elapsed += dt;
if (bar.current) bar.current.style.transform = `scaleX(${Math.min(1, elapsed / interval)})`;
if (elapsed >= interval) {
const { items: list, onValueChange: cb, value: v } = latest.current;
if (v === undefined) {
setInner((prev) => {
const i = list.findIndex((t) => t.id === prev);
return list[(i + 1) % list.length].id;
});
}
const i = list.findIndex((t) => t.id === (v ?? current));
cb?.(list[(Math.max(0, i) + 1) % list.length].id);
return;
}
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, [running, interval, current]);
const choose = (i: number, focus = false) => {
const next = items[(i + items.length) % items.length];
setPicked(true);
if (value === undefined) setInner(next.id);
if (next.id !== current) onValueChange?.(next.id);
if (focus) tabs.current[(i + items.length) % items.length]?.focus();
};
const onKey = (e: KeyboardEvent) => {
const keys: Record<string, number> = { ArrowDown: index + 1, ArrowRight: index + 1, ArrowUp: index - 1, ArrowLeft: index - 1, Home: 0, End: items.length - 1 };
if (!(e.key in keys)) return;
e.preventDefault();
choose(keys[e.key], true);
};
return (
<div
onPointerEnter={() => (hold.current = true)}
onPointerLeave={() => (hold.current = false)}
onFocus={() => (hold.current = true)}
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) hold.current = false;
}}
className={`@container w-full rounded-[20px] bg-surface p-3 shadow-[var(--shadow-card)] ${className}`}
>
<div className="flex flex-col gap-3 @3xl:flex-row">
<div role="tablist" aria-label={label} aria-orientation="vertical" onKeyDown={onKey} className="flex shrink-0 flex-col gap-1 @3xl:w-[300px] @3xl:justify-center">
{items.map((t, i) => {
const on = i === index;
return (
<button
key={t.id}
ref={(el) => {
tabs.current[i] = el;
}}
id={`${id}-tab-${t.id}`}
role="tab"
type="button"
aria-selected={on}
aria-controls={`${id}-panel`}
tabIndex={on ? 0 : -1}
onClick={() => choose(i)}
className={`relative isolate flex w-full flex-col rounded-[14px] px-4 py-3 text-left transition-colors ${on ? "text-fg" : "text-fg-muted hover:text-fg"} ${focusRing}`}
>
{on && (
<motion.span
layoutId={`${id}-indicator`}
aria-hidden
transition={reduce ? { duration: 0 } : glide}
className="absolute inset-0 -z-10 rounded-[14px] bg-subtle dark:bg-[var(--raised)]"
/>
)}
<span className="flex items-center gap-3">
{t.icon && <span className="grid size-5 shrink-0 place-items-center [&_svg]:size-5">{t.icon}</span>}
<span className="text-[15px] font-medium leading-6 tracking-[-0.01em]">{t.title}</span>
</span>
<AnimatePresence initial={false}>
{on && t.description && (
<motion.span
initial={reduce ? { opacity: 0 } : { height: 0, opacity: 0 }}
animate={reduce ? { opacity: 1 } : { height: "auto", opacity: 1 }}
exit={reduce ? { opacity: 0 } : { height: 0, opacity: 0 }}
transition={spring}
className="block overflow-hidden"
>
<span className={`block pt-1 text-pretty text-[14px] leading-5 text-fg-muted ${t.icon ? "pl-8" : ""}`}>{t.description}</span>
</motion.span>
)}
</AnimatePresence>
{on && running && (
<span aria-hidden className={`mt-3 block h-[3px] overflow-hidden rounded-full bg-[color-mix(in_srgb,var(--fg)_10%,transparent)] ${t.icon ? "ml-8" : ""}`}>
<span ref={bar} className="block h-full origin-left scale-x-0 rounded-full bg-fg" />
</span>
)}
</button>
);
})}
</div>
<div
id={`${id}-panel`}
role="tabpanel"
aria-labelledby={active ? `${id}-tab-${active.id}` : undefined}
className="relative min-w-0 flex-auto overflow-hidden rounded-[14px] bg-subtle"
style={{ height: panelHeight }}
>
<AnimatePresence initial={false} mode="popLayout">
{active && (
<motion.div
key={active.id}
initial={reduce ? { opacity: 0 } : { opacity: 0, y: 24, scale: 0.97, filter: "blur(4px)" }}
animate={{ opacity: 1, y: 0, scale: 1, filter: "blur(0px)" }}
exit={reduce ? { opacity: 0 } : { opacity: 0, y: -12, scale: 0.99, filter: "blur(4px)" }}
transition={reduce ? { duration: 0.15 } : { type: "spring", stiffness: 380, damping: 28 }}
className="absolute inset-0 grid place-items-center p-4 @lg:p-6"
>
{active.preview}
</motion.div>
)}
</AnimatePresence>
</div>
</div>
</div>
);
}
Usage
Add the TSX to your React project and install the required dependencies. Styles use Tailwind CSS and theme tokens。
Package dependencies · motion、react
Props
| Name | Type | Description |
|---|---|---|
`items` | `{ id; title; description?; icon?; preview }[]` | preview is any node: a screenshot, a small live UI, a video. |
`value / defaultValue / onValueChange` | `string / string / (id) => void` | The open tab, controlled or not. |
`interval` | `number` | Time per tab in ms. Default 6000. Pointing at it or focusing inside holds the progress. |
`autoPlay` | `boolean` | Moves on by itself until someone picks a tab. Default true; off under reduced motion. |
`panelHeight` | `number` | Fixed panel height in px, so the tabs never jump. Default 380. |
`paused` | `boolean` | Holds the open tab with no progress. |