Continuity Tabs
One selection surface, moving with the content it reveals.
Your preview settings
{
"motion": "spring",
"disableAccess": false
}These are the exact settings driving this demo, including sample content and simulated states. For the installable component’s public API, see Usage and Props below.
Switch with arrows, Home or End. Disable Access to see a skipped tab.
A real browser viewport. Scroll within the preview when the sample needs more space. Configuration stays in sync between sizes.
01What it does
A tab system with a measured selection indicator, directional panel transitions and a viewport that follows the active panel's real height. Tabs stay mounted, so state survives selection changes.
In this previewA four-tab workspace settings surface. Overview, Activity, Preferences and Access have different heights and densities; Access can be disabled from the preview controls.
Design decisions
The indicator is measured from the live button rather than from equal-width assumptions, so short and long labels both receive the right surface.
Panels remain mounted and only the active and outgoing panels are painted. Internal form state survives switching, and the viewport transitions between measured heights instead of collapsing.
Entering content travels in the direction of the tab change while the outgoing panel leaves the other way, preserving the felt location of the interface.
02Install
Registry install
Run this in a React project with components.json. The component and its stylesheet land in components/lab/continuity-tabs/.
npx shadcn@latest add https://lab.tanishk.me/r/continuity-tabs.jsonFiles included
2- components/lab/continuity-tabs/continuity-tabs.tsxcomponent
- components/lab/continuity-tabs/continuity-tabs.cssstyles
Manual install: copy each file into the same folder and preserve its relative imports. Open the source
03Usage
"use client";
import { useState } from "react";
import { ContinuityTabs } from "@/components/lab/continuity-tabs/continuity-tabs";
export function WorkspaceSettings() {
const [tab, setTab] = useState("overview");
return (
<ContinuityTabs
value={tab}
onValueChange={setTab}
label="Workspace settings"
tabs={[
{ id: "overview", label: "Overview", content: <Overview /> },
{ id: "activity", label: "Activity", content: <Activity /> },
{ id: "access", label: "Access", content: <Access /> },
]}
/>
);
}04Props
| Prop | Type | Default | Notes |
|---|---|---|---|
tabs | { id: string; label: string; content: ReactNode; disabled?: boolean }[] | — | The tab records. Content stays mounted while inactive. |
value | string | — | Controlled active tab id. |
defaultValue | string | — | Initial active tab in uncontrolled use; defaults to the first enabled tab. |
onValueChange | (id: string) => void | — | Called when a different tab is selected. |
label | string | "Tabs" | Accessible name for the tab list. |
motion | "spring" | "fade" | "none" | "spring" | Selection and content transition character. |
className | string | — | Forwarded to the root for layout customization. |
05Accessibility
- Implements the ARIA tabs pattern: tablist, tab and tabpanel roles, aria-selected and aria-controls, with one roving tab stop.
- Arrow keys wrap through enabled tabs; Home and End jump to the bounds; disabled tabs are skipped.
- Inactive panels are hidden and inert, so their controls never enter the tab order or the accessibility tree.
- The measured height transition and panel movement are disabled under prefers-reduced-motion; the instant mode remains fully usable.
06Source
2 files, 532 lines. The combined copy is a reference; save each file separately for use.
components/lab/continuity-tabs/continuity-tabs.tsx— press enter to show or hide the source
"use client";
import {
useCallback,
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
type KeyboardEvent,
type ReactNode,
} from "react";
import "./continuity-tabs.css";
export type ContinuityTab = {
id: string;
label: string;
content: ReactNode;
disabled?: boolean;
};
export type ContinuityTabsProps = {
tabs: ContinuityTab[];
/** Controlled active tab id. */
value?: string;
/** Initial tab id in uncontrolled use. */
defaultValue?: string;
onValueChange?: (id: string) => void;
/** Accessible name for the tab list. */
label?: string;
/**
* spring — a short, damped glide; fade — a calm crossfade;
* none — instant, for low-power surfaces.
*/
motion?: "spring" | "fade" | "none";
className?: string;
};
function reducedMotion() {
return (
typeof window !== "undefined" &&
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches
);
}
/**
* CONTINUITY TABS
*
* The selection surface is one object: it is measured from the live tab
* and moves with a damped spring. Content panels stay mounted, so their
* internal state survives tab changes; only the active and outgoing
* panels are painted. The viewport height follows the measured active
* panel, which means a shorter panel does not yank the page upward.
*/
export function ContinuityTabs({
tabs,
value,
defaultValue,
onValueChange,
label = "Tabs",
motion = "spring",
className,
}: ContinuityTabsProps) {
const uid = useId();
const enabled = tabs.filter((tab) => !tab.disabled);
const firstId = enabled[0]?.id ?? tabs[0]?.id ?? "";
const controlled = value !== undefined;
const [internal, setInternal] = useState(defaultValue ?? firstId);
const requested = controlled ? value : internal;
const activeIndex = Math.max(
0,
tabs.findIndex((tab) => tab.id === requested && !tab.disabled),
);
const active = tabs[activeIndex];
const listRef = useRef<HTMLDivElement>(null);
const tabRefs = useRef(new Map<string, HTMLButtonElement>());
const panelRefs = useRef(new Map<string, HTMLDivElement>());
const [indicator, setIndicator] = useState({ x: 0, width: 0, ready: false });
const [overflow, setOverflow] = useState({ left: false, right: false });
const [height, setHeight] = useState<number | null>(null);
const [outgoing, setOutgoing] = useState<string | null>(null);
const [direction, setDirection] = useState(1);
const outgoingTimer = useRef<number | null>(null);
const panelAnimation = useRef<Animation | null>(null);
const measureOverflow = useCallback(() => {
const list = listRef.current;
if (!list) return;
const next = {
left: list.scrollLeft > 2,
right: list.scrollLeft + list.clientWidth < list.scrollWidth - 2,
};
setOverflow((current) =>
current.left === next.left && current.right === next.right
? current
: next,
);
}, []);
const measure = useCallback(() => {
const list = listRef.current;
if (!list) return;
const button = tabRefs.current.get(active?.id ?? "");
if (!button) return;
setIndicator({
x: button.offsetLeft,
width: button.offsetWidth,
ready: true,
});
measureOverflow();
}, [active?.id, measureOverflow]);
useLayoutEffect(() => {
measure();
const list = listRef.current;
if (!list || typeof ResizeObserver !== "function") return;
const observer = new ResizeObserver(measure);
observer.observe(list);
for (const button of tabRefs.current.values()) observer.observe(button);
return () => observer.disconnect();
}, [measure]);
useEffect(() => {
const onResize = () => measure();
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
}, [measure]);
/* Keep the active tab visible when the list overflows. */
useEffect(() => {
const button = tabRefs.current.get(active?.id ?? "");
button?.scrollIntoView({ block: "nearest", inline: "nearest" });
}, [active?.id]);
/* The viewport height is the measured height of the active panel. */
const measurePanel = useCallback(() => {
const panel = panelRefs.current.get(active?.id ?? "");
if (!panel) return;
const next = panel.scrollHeight;
setHeight((current) => (current === next ? current : next));
}, [active?.id]);
useLayoutEffect(() => {
measurePanel();
const panel = panelRefs.current.get(active?.id ?? "");
if (!panel || typeof ResizeObserver !== "function") return;
const observer = new ResizeObserver(measurePanel);
observer.observe(panel);
return () => observer.disconnect();
}, [measurePanel, active?.id]);
/* Animate the entering panel and retire the outgoing one. */
const previousId = useRef(active?.id);
useLayoutEffect(() => {
if (!active) return;
const previous = previousId.current;
previousId.current = active.id;
if (!previous || previous === active.id) return;
const panel = panelRefs.current.get(active.id);
const prevPanel = panelRefs.current.get(previous);
const dir =
tabs.findIndex((tab) => tab.id === active.id) >
tabs.findIndex((tab) => tab.id === previous)
? 1
: -1;
setDirection(dir);
setOutgoing(previous);
panelAnimation.current?.cancel();
if (panel && motion !== "none") {
if (reducedMotion()) {
panel.style.opacity = "1";
panel.style.transform = "none";
} else {
panelAnimation.current = panel.animate(
[
{
opacity: 0,
transform: `translate3d(${dir * 18}px, 0, 0)`,
},
{ opacity: 1, transform: "translate3d(0, 0, 0)" },
],
{
duration: motion === "spring" ? 300 : 220,
easing:
motion === "spring"
? "cubic-bezier(0.16, 1, 0.3, 1)"
: "cubic-bezier(0.4, 0, 0.2, 1)",
},
);
}
}
if (prevPanel && motion !== "none" && !reducedMotion()) {
prevPanel.animate(
[
{ opacity: 1, transform: "translate3d(0, 0, 0)" },
{
opacity: 0,
transform: `translate3d(${dir * -14}px, 0, 0)`,
},
],
{
duration: motion === "spring" ? 230 : 180,
easing: "cubic-bezier(0.4, 0, 1, 1)",
},
);
}
if (outgoingTimer.current !== null) window.clearTimeout(outgoingTimer.current);
outgoingTimer.current = window.setTimeout(
() => {
setOutgoing(null);
outgoingTimer.current = null;
},
motion === "none" ? 0 : 320,
);
}, [active, motion, tabs]);
useEffect(
() => () => {
if (outgoingTimer.current !== null)
window.clearTimeout(outgoingTimer.current);
panelAnimation.current?.cancel();
},
[],
);
const select = (id: string, focus = false) => {
if (controlled) onValueChange?.(id);
else setInternal(id);
if (focus) tabRefs.current.get(id)?.focus();
};
const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>, index: number) => {
const selectable = tabs
.map((tab, position) => ({ tab, position }))
.filter(({ tab }) => !tab.disabled);
if (selectable.length === 0) return;
const current = selectable.findIndex(({ position }) => position === index);
let next: number | null = null;
if (event.key === "ArrowRight" || event.key === "ArrowDown")
next = (current + 1) % selectable.length;
else if (event.key === "ArrowLeft" || event.key === "ArrowUp")
next = (current - 1 + selectable.length) % selectable.length;
else if (event.key === "Home") next = 0;
else if (event.key === "End") next = selectable.length - 1;
if (next === null) return;
event.preventDefault();
const target = selectable[next];
select(target.tab.id, true);
};
if (!active) return null;
return (
<div
className={`ct-root${className ? ` ${className}` : ""}`}
data-motion={motion}
>
<div className="ct-tablist-wrap" data-fade-left={overflow.left ? "true" : "false"} data-fade-right={overflow.right ? "true" : "false"}>
<div
ref={listRef}
className="ct-tablist"
role="tablist"
aria-label={label}
onScroll={measureOverflow}
>
{tabs.map((tab, index) => {
const selected = tab.id === active.id;
return (
<button
key={tab.id}
ref={(node) => {
if (node) tabRefs.current.set(tab.id, node);
else tabRefs.current.delete(tab.id);
}}
type="button"
role="tab"
id={`${uid}-tab-${tab.id}`}
aria-selected={selected}
aria-controls={`${uid}-panel-${tab.id}`}
aria-disabled={tab.disabled || undefined}
disabled={tab.disabled}
tabIndex={selected ? 0 : -1}
className="ct-tab"
onClick={() => select(tab.id)}
onKeyDown={(event) => onKeyDown(event, index)}
>
{tab.label}
</button>
);
})}
<span
className="ct-indicator"
aria-hidden="true"
data-ready={indicator.ready ? "true" : "false"}
style={{
width: indicator.width,
transform: `translate3d(${indicator.x}px, 0, 0)`,
}}
/>
</div>
</div>
<div
className="ct-viewport"
data-ready={height !== null ? "true" : "false"}
style={height !== null ? { height } : undefined}
>
{tabs.map((tab) => {
const selected = tab.id === active.id;
const leaving = tab.id === outgoing;
return (
<div
key={tab.id}
ref={(node) => {
if (node) panelRefs.current.set(tab.id, node);
else panelRefs.current.delete(tab.id);
}}
className={`ct-panel${leaving ? " ct-panel--leaving" : ""}`}
id={`${uid}-panel-${tab.id}`}
role="tabpanel"
aria-labelledby={`${uid}-tab-${tab.id}`}
aria-hidden={!selected}
inert={!selected}
hidden={!selected && !leaving}
data-direction={direction}
>
{tab.content}
</div>
);
})}
</div>
</div>
);
}components/lab/continuity-tabs/continuity-tabs.css— press enter to show or hide the source
.ct-root {
--ct-ink: #1d2026;
--ct-muted: #6b6f78;
--ct-line: rgba(29, 32, 38, 0.12);
--ct-surface: #ffffff;
--ct-soft: #f4f4f1;
--ct-accent: #3c36c9;
--ct-accent-soft: rgba(60, 54, 201, 0.09);
--ct-focus: #3c36c9;
--ct-ease: cubic-bezier(0.16, 1, 0.3, 1);
width: 100%;
min-width: 0;
color: var(--ct-ink);
font: inherit;
}
.ct-tablist-wrap {
position: relative;
min-width: 0;
}
.ct-tablist-wrap::before,
.ct-tablist-wrap::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: 22px;
z-index: 2;
pointer-events: none;
opacity: 0;
transition: opacity 180ms linear;
}
.ct-tablist-wrap::before {
left: 0;
background: linear-gradient(90deg, var(--ct-soft), transparent);
}
.ct-tablist-wrap::after {
right: 0;
background: linear-gradient(270deg, var(--ct-soft), transparent);
}
.ct-tablist-wrap[data-fade-left="true"]::before,
.ct-tablist-wrap[data-fade-right="true"]::after {
opacity: 1;
}
.ct-tablist {
position: relative;
display: flex;
align-items: stretch;
gap: 2px;
padding: 3px;
border: 1px solid var(--ct-line);
border-radius: 11px;
background: var(--ct-soft);
overflow-x: auto;
overscroll-behavior-x: contain;
scrollbar-width: none;
-ms-overflow-style: none;
}
.ct-tablist::-webkit-scrollbar {
display: none;
}
.ct-tab {
position: relative;
z-index: 1;
flex: 0 0 auto;
min-height: 38px;
padding: 8px 15px;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--ct-muted);
font: inherit;
font-size: 0.875rem;
font-weight: 500;
letter-spacing: -0.006em;
white-space: nowrap;
cursor: pointer;
transition: color 180ms var(--ct-ease);
}
.ct-tab:hover {
color: var(--ct-ink);
}
.ct-tab[aria-selected="true"] {
color: var(--ct-ink);
}
.ct-tab:disabled {
color: var(--ct-muted);
opacity: 0.42;
cursor: not-allowed;
}
.ct-tab:focus-visible {
outline: 2px solid var(--ct-focus);
outline-offset: 1px;
}
.ct-indicator {
position: absolute;
top: 3px;
left: 0;
bottom: 3px;
z-index: 0;
border: 1px solid rgba(29, 32, 38, 0.08);
border-radius: 8px;
background: var(--ct-surface);
box-shadow:
0 1px 2px rgba(29, 32, 38, 0.07),
0 6px 18px -12px rgba(29, 32, 38, 0.3);
opacity: 0;
pointer-events: none;
transition:
transform 360ms var(--ct-ease),
width 360ms var(--ct-ease),
opacity 140ms linear;
will-change: transform, width;
}
.ct-indicator[data-ready="true"] {
opacity: 1;
}
.ct-root[data-motion="fade"] .ct-indicator {
transition:
transform 260ms ease-out,
width 260ms ease-out,
opacity 140ms linear;
}
.ct-root[data-motion="none"] .ct-indicator {
transition: opacity 100ms linear;
}
.ct-viewport {
position: relative;
min-width: 0;
margin-top: 18px;
overflow: hidden;
transition: height 300ms var(--ct-ease);
}
.ct-viewport:not([data-ready="true"]) .ct-panel {
position: static;
}
.ct-panel {
position: absolute;
inset: 0 0 auto;
min-width: 0;
opacity: 1;
}
.ct-panel[hidden] {
display: none;
}
.ct-panel--leaving {
pointer-events: none;
user-select: none;
}
.ct-root[data-motion="none"] .ct-viewport {
transition: none;
}
@media (max-width: 520px) {
.ct-tab {
min-height: 36px;
padding-inline: 12px;
font-size: 0.8125rem;
}
.ct-viewport {
margin-top: 14px;
}
}
@media (prefers-reduced-motion: reduce) {
.ct-indicator,
.ct-viewport,
.ct-tab {
transition-duration: 0.01ms;
}
}