Flip
import { useId, useRef, useState } from 'react';
const TABS = [
{ label: 'Front', title: 'Ada Park', body: 'Product designer, Seoul. Says yes to coffee.' },
{ label: 'Back', title: 'Contact', body: 'ada@example.com, +82 10 5555 0199' },
{ label: 'Notes', title: 'Met at', body: 'Config, the session on motion systems.' },
];
const css = `
@keyframes tabs-flip-out { to { transform: rotateY(calc(var(--tabs-flip-d) * 90deg)); } }
@keyframes tabs-flip-in { from { transform: rotateY(calc(var(--tabs-flip-d) * -90deg)); } }
.tabs-flip { width: 250px; }
.tabs-flip__list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-bottom: 10px; padding: 3px; border-radius: 10px; background: light-dark(#f3f4f7, #1a1c1f); }
.tabs-flip__tab { padding: 6px 0; border: 0; border-radius: 7px; 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-flip__tab:hover { color: light-dark(#42454c, #e5e6e9); }
.tabs-flip__tab[aria-selected='true'] { background: light-dark(#dbdde1, #2e3035); color: light-dark(#373a40, #f1f2f3); }
.tabs-flip__tab:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }
.tabs-flip__stage { display: grid; perspective: 700px; }
.tabs-flip__panel { grid-area: 1 / 1; height: 96px; padding: 16px; border-radius: 14px; background: linear-gradient(150deg, light-dark(#dbdde1, #2e3035), light-dark(#ebedf0, #232529)); box-shadow: inset 0 1px 0 light-dark(#cbced3, #393c41), 0 14px 26px -14px light-dark(rgba(0, 0, 0, 0.21), rgba(0, 0, 0, 0.7)); backface-visibility: hidden; }
.tabs-flip__panel[data-prev] { animation: tabs-flip-out 0.22s ease-in forwards; }
.tabs-flip[data-armed] .tabs-flip__panel[data-on] { animation: tabs-flip-in 0.32s cubic-bezier(0.3, 1.4, 0.5, 1) 0.22s backwards; }
.tabs-flip__title { display: block; margin-bottom: 4px; color: light-dark(#373a40, #f1f2f3); font-size: 16px; font-weight: 600; }
.tabs-flip__body { color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; line-height: 1.5; }
@media (prefers-reduced-motion: reduce) {
.tabs-flip__panel { animation: none !important; }
.tabs-flip__panel[data-prev] { display: none; }
}
`;
export function FlipTabs() {
const id = useId();
const [state, setState] = useState({ sel: 0, prev: -1, d: 1 });
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const { sel, prev } = state;
const select = (i: number) => {
if (i !== sel) setState({ sel: i, prev: sel, d: i > sel ? -1 : 1 });
};
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();
select(next);
tabs.current[next]?.focus();
};
return (
<>
<style>
{css}
</style>
<div className="tabs-flip" data-armed={prev >= 0 ? '' : undefined} style={{ '--tabs-flip-d': state.d } as React.CSSProperties}>
<div className="tabs-flip__list" role="tablist" aria-label="Contact card" onKeyDown={onKeyDown}>
{TABS.map((t, i) => (
<button
key={t.label}
ref={(el) => {
tabs.current[i] = el;
}}
className="tabs-flip__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>
))}
</div>
<div className="tabs-flip__stage">
{TABS.map((t, i) => (
<div
key={t.label}
className="tabs-flip__panel"
role="tabpanel"
id={`${id}-panel-${i}`}
aria-labelledby={`${id}-tab-${i}`}
hidden={i !== sel && i !== prev}
aria-hidden={i === prev || undefined}
inert={i === prev}
data-on={i === sel ? '' : undefined}
data-prev={i === prev ? '' : undefined}
>
<span className="tabs-flip__title">{t.title}</span>
<span className="tabs-flip__body">{t.body}</span>
</div>
))}
</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTabsFlip(el) on the mounted element instead.
<div class="tabs-flip" style="--tabs-flip-d:1">
<div class="tabs-flip__list" role="tablist" aria-label="Contact card">
<button class="tabs-flip__tab" type="button" role="tab" id="tabs-flip-tab-0" aria-selected="true" aria-controls="tabs-flip-panel-0" tabindex="0">Front</button>
<button class="tabs-flip__tab" type="button" role="tab" id="tabs-flip-tab-1" aria-selected="false" aria-controls="tabs-flip-panel-1" tabindex="-1">Back</button>
<button class="tabs-flip__tab" type="button" role="tab" id="tabs-flip-tab-2" aria-selected="false" aria-controls="tabs-flip-panel-2" tabindex="-1">Notes</button>
</div>
<div class="tabs-flip__stage">
<div class="tabs-flip__panel" role="tabpanel" id="tabs-flip-panel-0" aria-labelledby="tabs-flip-tab-0" data-on="">
<span class="tabs-flip__title">Ada Park</span>
<span class="tabs-flip__body">Product designer, Seoul. Says yes to coffee.</span>
</div>
<div class="tabs-flip__panel" role="tabpanel" id="tabs-flip-panel-1" aria-labelledby="tabs-flip-tab-1" hidden="">
<span class="tabs-flip__title">Contact</span>
<span class="tabs-flip__body">ada@example.com, +82 10 5555 0199</span>
</div>
<div class="tabs-flip__panel" role="tabpanel" id="tabs-flip-panel-2" aria-labelledby="tabs-flip-tab-2" hidden="">
<span class="tabs-flip__title">Met at</span>
<span class="tabs-flip__body">Config, the session on motion systems.</span>
</div>
</div>
</div>@keyframes tabs-flip-out { to { transform: rotateY(calc(var(--tabs-flip-d) * 90deg)); } }
@keyframes tabs-flip-in { from { transform: rotateY(calc(var(--tabs-flip-d) * -90deg)); } }
.tabs-flip { width: 250px; }
.tabs-flip__list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-bottom: 10px; padding: 3px; border-radius: 10px; background: light-dark(#f3f4f7, #1a1c1f); }
.tabs-flip__tab { padding: 6px 0; border: 0; border-radius: 7px; 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-flip__tab:hover { color: light-dark(#42454c, #e5e6e9); }
.tabs-flip__tab[aria-selected='true'] { background: light-dark(#dbdde1, #2e3035); color: light-dark(#373a40, #f1f2f3); }
.tabs-flip__tab:focus-visible { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 2px; }
.tabs-flip__stage { display: grid; perspective: 700px; }
.tabs-flip__panel { grid-area: 1 / 1; height: 96px; padding: 16px; border-radius: 14px; background: linear-gradient(150deg, light-dark(#dbdde1, #2e3035), light-dark(#ebedf0, #232529)); box-shadow: inset 0 1px 0 light-dark(#cbced3, #393c41), 0 14px 26px -14px light-dark(rgba(0, 0, 0, 0.21), rgba(0, 0, 0, 0.7)); backface-visibility: hidden; }
.tabs-flip__panel[data-prev] { animation: tabs-flip-out 0.22s ease-in forwards; }
.tabs-flip[data-armed] .tabs-flip__panel[data-on] { animation: tabs-flip-in 0.32s cubic-bezier(0.3, 1.4, 0.5, 1) 0.22s backwards; }
.tabs-flip__title { display: block; margin-bottom: 4px; color: light-dark(#373a40, #f1f2f3); font-size: 16px; font-weight: 600; }
.tabs-flip__body { color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; line-height: 1.5; }
@media (prefers-reduced-motion: reduce) {
.tabs-flip__panel { animation: none !important; }
.tabs-flip__panel[data-prev] { display: none; }
}function initTabsFlip(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;
});
// The panel you left stays visible, out of reach, while the new one comes in over it.
panels.forEach(function (panel, k) {
panel.hidden = k !== i && k !== prev;
panel.toggleAttribute('data-on', k === i);
panel.toggleAttribute('data-prev', k === prev);
panel.inert = k === prev;
if (k === prev) panel.setAttribute('aria-hidden', 'true');
else panel.removeAttribute('aria-hidden');
});
root.setAttribute('data-armed', '');
// The card turns one way going right and the other going left.
root.style.setProperty('--tabs-flip-d', i > prev ? -1 : 1);
}
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-flip').forEach(initTabsFlip);Goal: Create a "Flip" tab set.
Visual behavior: A contact card as tabs: the card flips over on its vertical axis to show the new side, turning left or right with the direction you moved.
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 old side turns away and the new one turns in with rotateY keyframes in perspective; a direction property (1 or -1) mirrors the turn.
- The panel you left stays in place, marked data-prev, inert and aria-hidden, so it can animate out under the new one while only the new one is reachable.
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 card switches sides without turning.
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 "Flip" tab set.
Visual behavior: A contact card as tabs: the card flips over on its vertical axis to show the new side, turning left or right with the direction you moved.
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 old side turns away and the new one turns in with rotateY keyframes in perspective; a direction property (1 or -1) mirrors the turn.
- The panel you left stays in place, marked data-prev, inert and aria-hidden, so it can animate out under the new one while only the new one is reachable.
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 card switches sides without turning.
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
A contact card as tabs: the card flips over on its vertical axis to show the new side, turning left or right with the direction you moved.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change, 3d
- Techniques
- keyframes, 3d transform, perspective
- 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 card switches sides without turning.