Metro
import { useId, useRef, useState } from 'react';
const ITEMS = [
{ label: 'Harbour', body: 'Ferries every 20 minutes. Fish market on the left.' },
{ label: 'Market', body: 'Covered stalls, open until two. Try the figs.' },
{ label: 'Museum', body: 'Free on Thursdays after five. Lockers inside.' },
{ label: 'Park', body: 'Bandstand concerts on Sunday afternoons.' },
];
const css = `
@keyframes tabs-metro-doors-a { 0%, 55% { transform: scaleX(1); } 75%, 100% { transform: scaleX(0.15); } }
@keyframes tabs-metro-doors-b { 0%, 55% { transform: scaleX(1); } 75%, 100% { transform: scaleX(0.15); } }
@keyframes tabs-metro-arrive { 0%, 60% { opacity: 0; transform: translateY(4px); } 100% { opacity: 1; transform: none; } }
.tabs-metro { display: grid; gap: 14px; width: 260px; }
.tabs-metro__list { position: relative; display: flex; justify-content: space-between; padding-top: 34px; }
.tabs-metro__list::before { content: ''; position: absolute; left: 16px; right: 16px; top: 44px; height: 3px; border-radius: 2px; background: light-dark(#cbced3, #393c41); }
.tabs-metro__tab { position: relative; padding: 0; border: 0; background: none; font: inherit; cursor: pointer; }
.tabs-metro__tab:focus-visible { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; border-radius: 8px; }
.tabs-metro__tab { display: grid; justify-items: center; gap: 8px; width: 60px; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; transition: color 0.3s; }
.tabs-metro__tab::before { content: ''; position: relative; width: 11px; height: 11px; border-radius: 50%; background: light-dark(#fbfcfd, #121417); box-shadow: inset 0 0 0 2.5px light-dark(#6d7179, #7d8087); transition: box-shadow 0.3s; }
.tabs-metro__tab[aria-selected='true'] { color: light-dark(#373a40, #f1f2f3); font-weight: 500; }
.tabs-metro__tab[aria-selected='true']::before { box-shadow: inset 0 0 0 2.5px light-dark(#373a40, #f1f2f3); }
/* The train rides the line to the chosen stop, easing in and braking; its doors open once it stops. */
.tabs-metro__train { position: absolute; top: 4px; left: calc(30px + var(--i) * ((100% - 60px) / 3) - 26px); display: flex; gap: 4px; align-items: center; justify-content: center; width: 52px; height: 26px; border-radius: 8px 8px 4px 4px; background: light-dark(#42454c, #e5e6e9); box-shadow: 0 4px 10px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); pointer-events: none; transition: left 0.9s cubic-bezier(0.65, 0, 0.35, 1); }
.tabs-metro__door { width: 10px; height: 15px; border-radius: 2px; background: light-dark(#dbdde1, #2e3035); transform-origin: 50% 50%; }
.tabs-metro[data-flip='a'] .tabs-metro__door { animation: tabs-metro-doors-a 1.3s ease-in-out; }
.tabs-metro[data-flip='b'] .tabs-metro__door { animation: tabs-metro-doors-b 1.3s ease-in-out; }
.tabs-metro__door { transform: scaleX(0.15); }
.tabs-metro__panel { padding: 10px 12px; border-radius: 10px; background: light-dark(#f3f4f7, #1a1c1f); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035); color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; line-height: 1.45; }
.tabs-metro[data-armed] .tabs-metro__panel { animation: tabs-metro-arrive 1.2s ease-out both; }
@media (prefers-reduced-motion: reduce) {
.tabs-metro__train { transition: none; }
.tabs-metro__door, .tabs-metro__panel { animation: none !important; }
}
`;
export function MetroTabs() {
const id = useId();
const [state, setState] = useState({ sel: 0, prev: -1, flip: 0 });
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const { sel, prev, flip } = state;
const select = (i: number) => {
if (i !== sel) setState((s) => ({ sel: i, prev: s.sel, flip: s.flip + 1 }));
};
const onKeyDown = (e: React.KeyboardEvent) => {
const n = ITEMS.length;
const next = ({ ArrowRight: (sel + 1) % n, ArrowLeft: (sel + n - 1) % n, Home: 0, End: n - 1 } as Record<string, number>)[e.key];
if (next === undefined) return;
e.preventDefault();
select(next);
tabs.current[next]?.focus();
};
return (
<>
<style>
{css}
</style>
<div
className="tabs-metro"
data-armed={flip ? '' : undefined}
data-flip={flip ? (flip % 2 ? 'a' : 'b') : undefined}
style={{ '--i': sel, '--p': prev < 0 ? sel : prev } as React.CSSProperties}
>
<div className="tabs-metro__list" role="tablist" aria-label="Stops" onKeyDown={onKeyDown}>
{ITEMS.map((t, i) => (
<button
key={t.label}
ref={(el) => {
tabs.current[i] = el;
}}
className="tabs-metro__tab"
type="button"
role="tab"
id={`${id}-tab-${i}`}
aria-selected={sel === i}
aria-controls={`${id}-panel-${i}`}
tabIndex={sel === i ? 0 : -1}
onClick={() => select(i)}
>
{t.label}
</button>
))}
<span className="tabs-metro__train" aria-hidden="true">
<span className="tabs-metro__door" />
<span className="tabs-metro__door" />
</span>
</div>
{ITEMS.map((t, i) => (
<div key={t.label} className="tabs-metro__panel" role="tabpanel" id={`${id}-panel-${i}`} aria-labelledby={`${id}-tab-${i}`} hidden={sel !== i}>
{t.body}
</div>
))}
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTabsMetro(el) on the mounted element instead.
<div class="tabs-metro" style="--i:0;--p:0">
<div class="tabs-metro__list" role="tablist" aria-label="Stops">
<button class="tabs-metro__tab" type="button" role="tab" id="tabs-metro-tab-0" aria-selected="true" aria-controls="tabs-metro-panel-0" tabindex="0">Harbour</button>
<button class="tabs-metro__tab" type="button" role="tab" id="tabs-metro-tab-1" aria-selected="false" aria-controls="tabs-metro-panel-1" tabindex="-1">Market</button>
<button class="tabs-metro__tab" type="button" role="tab" id="tabs-metro-tab-2" aria-selected="false" aria-controls="tabs-metro-panel-2" tabindex="-1">Museum</button>
<button class="tabs-metro__tab" type="button" role="tab" id="tabs-metro-tab-3" aria-selected="false" aria-controls="tabs-metro-panel-3" tabindex="-1">Park</button>
<span class="tabs-metro__train" aria-hidden="true">
<span class="tabs-metro__door"></span>
<span class="tabs-metro__door"></span>
</span>
</div>
<div class="tabs-metro__panel" role="tabpanel" id="tabs-metro-panel-0" aria-labelledby="tabs-metro-tab-0">Ferries every 20 minutes. Fish market on the left.</div>
<div class="tabs-metro__panel" role="tabpanel" id="tabs-metro-panel-1" aria-labelledby="tabs-metro-tab-1" hidden="">Covered stalls, open until two. Try the figs.</div>
<div class="tabs-metro__panel" role="tabpanel" id="tabs-metro-panel-2" aria-labelledby="tabs-metro-tab-2" hidden="">Free on Thursdays after five. Lockers inside.</div>
<div class="tabs-metro__panel" role="tabpanel" id="tabs-metro-panel-3" aria-labelledby="tabs-metro-tab-3" hidden="">Bandstand concerts on Sunday afternoons.</div>
</div>@keyframes tabs-metro-doors-a { 0%, 55% { transform: scaleX(1); } 75%, 100% { transform: scaleX(0.15); } }
@keyframes tabs-metro-doors-b { 0%, 55% { transform: scaleX(1); } 75%, 100% { transform: scaleX(0.15); } }
@keyframes tabs-metro-arrive { 0%, 60% { opacity: 0; transform: translateY(4px); } 100% { opacity: 1; transform: none; } }
.tabs-metro { display: grid; gap: 14px; width: 260px; }
.tabs-metro__list { position: relative; display: flex; justify-content: space-between; padding-top: 34px; }
.tabs-metro__list::before { content: ''; position: absolute; left: 16px; right: 16px; top: 44px; height: 3px; border-radius: 2px; background: light-dark(#cbced3, #393c41); }
.tabs-metro__tab { position: relative; padding: 0; border: 0; background: none; font: inherit; cursor: pointer; }
.tabs-metro__tab:focus-visible { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; border-radius: 8px; }
.tabs-metro__tab { display: grid; justify-items: center; gap: 8px; width: 60px; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; transition: color 0.3s; }
.tabs-metro__tab::before { content: ''; position: relative; width: 11px; height: 11px; border-radius: 50%; background: light-dark(#fbfcfd, #121417); box-shadow: inset 0 0 0 2.5px light-dark(#6d7179, #7d8087); transition: box-shadow 0.3s; }
.tabs-metro__tab[aria-selected='true'] { color: light-dark(#373a40, #f1f2f3); font-weight: 500; }
.tabs-metro__tab[aria-selected='true']::before { box-shadow: inset 0 0 0 2.5px light-dark(#373a40, #f1f2f3); }
/* The train rides the line to the chosen stop, easing in and braking; its doors open once it stops. */
.tabs-metro__train { position: absolute; top: 4px; left: calc(30px + var(--i) * ((100% - 60px) / 3) - 26px); display: flex; gap: 4px; align-items: center; justify-content: center; width: 52px; height: 26px; border-radius: 8px 8px 4px 4px; background: light-dark(#42454c, #e5e6e9); box-shadow: 0 4px 10px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); pointer-events: none; transition: left 0.9s cubic-bezier(0.65, 0, 0.35, 1); }
.tabs-metro__door { width: 10px; height: 15px; border-radius: 2px; background: light-dark(#dbdde1, #2e3035); transform-origin: 50% 50%; }
.tabs-metro[data-flip='a'] .tabs-metro__door { animation: tabs-metro-doors-a 1.3s ease-in-out; }
.tabs-metro[data-flip='b'] .tabs-metro__door { animation: tabs-metro-doors-b 1.3s ease-in-out; }
.tabs-metro__door { transform: scaleX(0.15); }
.tabs-metro__panel { padding: 10px 12px; border-radius: 10px; background: light-dark(#f3f4f7, #1a1c1f); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035); color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; line-height: 1.45; }
.tabs-metro[data-armed] .tabs-metro__panel { animation: tabs-metro-arrive 1.2s ease-out both; }
@media (prefers-reduced-motion: reduce) {
.tabs-metro__train { transition: none; }
.tabs-metro__door, .tabs-metro__panel { animation: none !important; }
}function initTabsMetro(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'; });
var flip = 0;
function select(i) {
if (i === sel) return;
var prev = sel;
sel = i;
// Armed on the first change, so nothing animates on load; each change flips data-flip
// between a and b, so the keyframes restart every time.
flip += 1;
root.setAttribute('data-armed', '');
root.setAttribute('data-flip', flip % 2 ? 'a' : 'b');
root.style.setProperty('--i', i);
root.style.setProperty('--p', prev);
tabs.forEach(function (tab, k) {
tab.setAttribute('aria-selected', String(k === i));
tab.tabIndex = k === i ? 0 : -1;
});
panels.forEach(function (panel, k) { panel.hidden = k !== i; });
}
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-metro').forEach(initTabsMetro);Goal: Create a "Metro" tab set.
Visual behavior: Stop tabs on a metro line: the train rides the line to the chosen stop, easing in and braking, its doors open once it stops and the stop's notes appear.
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.
- The selected index is a CSS custom property (--i) on the root, so the moving parts are positioned with calc() and transitions alone.
- The train is positioned along the line from the index on a long ease-in-out transition; its doors part with a scale keyframe timed for after it arrives, and the panel fades in last.
- Effects that should replay on every change alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
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 train jumps to the stop and the doors stay still.
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 "Metro" tab set.
Visual behavior: Stop tabs on a metro line: the train rides the line to the chosen stop, easing in and braking, its doors open once it stops and the stop's notes appear.
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.
- The selected index is a CSS custom property (--i) on the root, so the moving parts are positioned with calc() and transitions alone.
- The train is positioned along the line from the index on a long ease-in-out transition; its doors part with a scale keyframe timed for after it arrives, and the panel fades in last.
- Effects that should replay on every change alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
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 train jumps to the stop and the doors stay still.
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
Stop tabs on a metro line: the train rides the line to the chosen stop, easing in and braking, its doors open once it stops and the stop's notes appear.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change
- Techniques
- css variables, css transition, keyframes
- 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 train jumps to the stop and the doors stay still.