Stack
import { useId, useRef, useState } from 'react';
const TABS = [
{ label: 'Personal', name: 'Personal', last: '4242', kind: 'Debit' },
{ label: 'Work', name: 'Studio Ltd', last: '8810', kind: 'Credit' },
{ label: 'Travel', name: 'Travel', last: '0937', kind: 'Multi-currency' },
];
const css = `
.tabs-stack { width: 250px; }
.tabs-stack__list { display: flex; justify-content: center; gap: 4px; margin-bottom: 30px; }
.tabs-stack__tab { padding: 5px 12px; border: 0; border-radius: 999px; background: none; color: light-dark(#5f626a, #a7abb2); font: inherit; font-size: 12px; font-weight: 600; cursor: pointer; transition: color 0.2s, background-color 0.2s; }
.tabs-stack__tab:hover { color: light-dark(#42454c, #e5e6e9); }
.tabs-stack__tab[aria-selected='true'] { background: light-dark(#ebedf0, #232529); color: light-dark(#373a40, #f1f2f3); }
.tabs-stack__tab:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }
.tabs-stack__deck { position: relative; height: 128px; }
.tabs-stack__card {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 14px 16px;
border-radius: 14px;
background: linear-gradient(135deg, light-dark(#cbced3, #393c41), light-dark(#ebedf0, #232529));
box-shadow: inset 0 1px 0 light-dark(#6d7179, #7d8087), 0 12px 24px -12px light-dark(rgba(0, 0, 0, 0.21), rgba(0, 0, 0, 0.7));
z-index: calc(10 - var(--tabs-stack-k));
opacity: calc(1 - var(--tabs-stack-k) * 0.25);
transform: translateY(calc(var(--tabs-stack-k) * -12px)) scale(calc(1 - var(--tabs-stack-k) * 0.06));
transform-origin: 50% 0;
transition: transform 0.5s cubic-bezier(0.3, 1.4, 0.5, 1), opacity 0.3s;
}
.tabs-stack__top { display: flex; justify-content: space-between; color: light-dark(#42454c, #e5e6e9); font-size: 12px; font-weight: 600; }
.tabs-stack__chip { width: 26px; height: 19px; border-radius: 4px; background: linear-gradient(135deg, light-dark(#50535b, #c6c8cd), light-dark(#6d7179, #7d8087)); }
.tabs-stack__num { color: light-dark(#373a40, #f1f2f3); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; letter-spacing: 0.12em; }
.tabs-stack__kind { color: light-dark(#50535b, #c6c8cd); font-size: 11px; }
@media (prefers-reduced-motion: reduce) {
.tabs-stack__card { transition: none; }
}
`;
export function StackTabs() {
const id = useId();
const [sel, setSel] = useState(0);
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const onKeyDown = (e: React.KeyboardEvent) => {
const n = TABS.length;
const next = { ArrowRight: (sel + 1) % n, ArrowLeft: (sel + n - 1) % n, Home: 0, End: n - 1 }[e.key];
if (next === undefined) return;
e.preventDefault();
setSel(next);
tabs.current[next]?.focus();
};
return (
<>
<style>
{css}
</style>
<div className="tabs-stack">
<div className="tabs-stack__list" role="tablist" aria-label="Cards" onKeyDown={onKeyDown}>
{TABS.map((t, i) => (
<button
key={t.label}
ref={(el) => {
tabs.current[i] = el;
}}
className="tabs-stack__tab"
type="button"
role="tab"
id={`${id}-tab-${i}`}
aria-selected={sel === i}
aria-controls={`${id}-panel-${i}`}
tabIndex={sel === i ? 0 : -1}
onClick={() => setSel(i)}
>
{t.label}
</button>
))}
</div>
<div className="tabs-stack__deck">
{TABS.map((t, i) => (
<div
key={t.label}
className="tabs-stack__card"
role="tabpanel"
id={`${id}-panel-${i}`}
aria-labelledby={`${id}-tab-${i}`}
aria-hidden={sel !== i}
inert={sel !== i}
style={{ '--tabs-stack-k': (i - sel + TABS.length) % TABS.length } as React.CSSProperties}
>
<span className="tabs-stack__top">
{t.name}
<span className="tabs-stack__chip" aria-hidden="true" />
</span>
<span className="tabs-stack__num">•••• {t.last}</span>
<span className="tabs-stack__kind">{t.kind}</span>
</div>
))}
</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTabsStack(el) on the mounted element instead.
<div class="tabs-stack">
<div class="tabs-stack__list" role="tablist" aria-label="Cards">
<button class="tabs-stack__tab" type="button" role="tab" id="tabs-stack-tab-0" aria-selected="true" aria-controls="tabs-stack-panel-0" tabindex="0">Personal</button>
<button class="tabs-stack__tab" type="button" role="tab" id="tabs-stack-tab-1" aria-selected="false" aria-controls="tabs-stack-panel-1" tabindex="-1">Work</button>
<button class="tabs-stack__tab" type="button" role="tab" id="tabs-stack-tab-2" aria-selected="false" aria-controls="tabs-stack-panel-2" tabindex="-1">Travel</button>
</div>
<div class="tabs-stack__deck">
<div class="tabs-stack__card" role="tabpanel" id="tabs-stack-panel-0" aria-labelledby="tabs-stack-tab-0" aria-hidden="false" style="--tabs-stack-k:0">
<span class="tabs-stack__top">Personal<span class="tabs-stack__chip" aria-hidden="true"></span></span>
<span class="tabs-stack__num">•••• 4242</span>
<span class="tabs-stack__kind">Debit</span>
</div>
<div class="tabs-stack__card" role="tabpanel" id="tabs-stack-panel-1" aria-labelledby="tabs-stack-tab-1" aria-hidden="true" inert="" style="--tabs-stack-k:1">
<span class="tabs-stack__top">Studio Ltd<span class="tabs-stack__chip" aria-hidden="true"></span></span>
<span class="tabs-stack__num">•••• 8810</span>
<span class="tabs-stack__kind">Credit</span>
</div>
<div class="tabs-stack__card" role="tabpanel" id="tabs-stack-panel-2" aria-labelledby="tabs-stack-tab-2" aria-hidden="true" inert="" style="--tabs-stack-k:2">
<span class="tabs-stack__top">Travel<span class="tabs-stack__chip" aria-hidden="true"></span></span>
<span class="tabs-stack__num">•••• 0937</span>
<span class="tabs-stack__kind">Multi-currency</span>
</div>
</div>
</div>.tabs-stack { width: 250px; }
.tabs-stack__list { display: flex; justify-content: center; gap: 4px; margin-bottom: 30px; }
.tabs-stack__tab { padding: 5px 12px; border: 0; border-radius: 999px; background: none; color: light-dark(#5f626a, #a7abb2); font: inherit; font-size: 12px; font-weight: 600; cursor: pointer; transition: color 0.2s, background-color 0.2s; }
.tabs-stack__tab:hover { color: light-dark(#42454c, #e5e6e9); }
.tabs-stack__tab[aria-selected='true'] { background: light-dark(#ebedf0, #232529); color: light-dark(#373a40, #f1f2f3); }
.tabs-stack__tab:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }
.tabs-stack__deck { position: relative; height: 128px; }
.tabs-stack__card {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 14px 16px;
border-radius: 14px;
background: linear-gradient(135deg, light-dark(#cbced3, #393c41), light-dark(#ebedf0, #232529));
box-shadow: inset 0 1px 0 light-dark(#6d7179, #7d8087), 0 12px 24px -12px light-dark(rgba(0, 0, 0, 0.21), rgba(0, 0, 0, 0.7));
z-index: calc(10 - var(--tabs-stack-k));
opacity: calc(1 - var(--tabs-stack-k) * 0.25);
transform: translateY(calc(var(--tabs-stack-k) * -12px)) scale(calc(1 - var(--tabs-stack-k) * 0.06));
transform-origin: 50% 0;
transition: transform 0.5s cubic-bezier(0.3, 1.4, 0.5, 1), opacity 0.3s;
}
.tabs-stack__top { display: flex; justify-content: space-between; color: light-dark(#42454c, #e5e6e9); font-size: 12px; font-weight: 600; }
.tabs-stack__chip { width: 26px; height: 19px; border-radius: 4px; background: linear-gradient(135deg, light-dark(#50535b, #c6c8cd), light-dark(#6d7179, #7d8087)); }
.tabs-stack__num { color: light-dark(#373a40, #f1f2f3); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; letter-spacing: 0.12em; }
.tabs-stack__kind { color: light-dark(#50535b, #c6c8cd); font-size: 11px; }
@media (prefers-reduced-motion: reduce) {
.tabs-stack__card { transition: none; }
}function initTabsStack(root) {
var list = root.querySelector('[role="tablist"]');
var tabs = Array.prototype.slice.call(list.querySelectorAll('[role="tab"]'));
var panels = root.querySelectorAll('[role="tabpanel"]');
var sel = tabs.findIndex(function (tab) { return tab.getAttribute('aria-selected') === 'true'; });
function select(i) {
if (i === sel) return;
var prev = sel;
sel = i;
tabs.forEach(function (tab, k) {
tab.setAttribute('aria-selected', String(k === i));
tab.tabIndex = k === i ? 0 : -1;
});
// Each card's place in the deck counts round from the chosen one.
panels.forEach(function (card, k) {
card.setAttribute('aria-hidden', String(k !== i));
card.inert = k !== i;
card.style.setProperty('--tabs-stack-k', (k - i + panels.length) % panels.length);
});
}
function onClick(e) {
var tab = e.target.closest('[role="tab"]');
if (tab) select(tabs.indexOf(tab));
}
function onKeyDown(e) {
var n = tabs.length;
var next = { ArrowRight: (sel + 1) % n, ArrowLeft: (sel + n - 1) % n, Home: 0, End: n - 1 }[e.key];
if (next === undefined) return;
e.preventDefault();
select(next);
tabs[next].focus();
}
list.addEventListener('click', onClick);
list.addEventListener('keydown', onKeyDown);
return function () {
list.removeEventListener('click', onClick);
list.removeEventListener('keydown', onKeyDown);
};
}
document.querySelectorAll('.tabs-stack').forEach(initTabsStack);Goal: Create a "Stack" tab set.
Visual behavior: Payment cards as tabs: the chosen card comes to the front of the deck and the others shuffle back behind it.
Technique:
- The ARIA tabs pattern: a role="tablist" with an aria-label, buttons with role="tab", aria-selected and aria-controls, and role="tabpanel" panels labelled by their tab. Only the selected tab is in the Tab order (roving tabindex); the arrow keys, Home and End move between tabs and select as they go.
- Each card's place in the deck counts round from the chosen one and is set as a custom property; it drives the card's offset, scale, z-index and shade, so one change re-deals the whole stack.
- Cards behind are aria-hidden and inert rather than hidden, since they stay visible.
Accessibility: Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the cards change places without moving.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Stack" tab set.
Visual behavior: Payment cards as tabs: the chosen card comes to the front of the deck and the others shuffle back behind it.
Technique:
- The ARIA tabs pattern: a role="tablist" with an aria-label, buttons with role="tab", aria-selected and aria-controls, and role="tabpanel" panels labelled by their tab. Only the selected tab is in the Tab order (roving tabindex); the arrow keys, Home and End move between tabs and select as they go.
- Each card's place in the deck counts round from the chosen one and is set as a custom property; it drives the card's offset, scale, z-index and shade, so one change re-deals the whole stack.
- Cards behind are aria-hidden and inert rather than hidden, since they stay visible.
Accessibility: Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the cards change places without moving.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
Payment cards as tabs: the chosen card comes to the front of the deck and the others shuffle back behind it.
- Dependencies
- None
- Difficulty
- Beginner
- Triggers
- click, state change, spring
- Techniques
- css variables, css transition, transform only
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the cards change places without moving.