Shelf
import { useId, useRef, useState } from 'react';
const BOOKS = [
{ label: 'Dune', author: 'Frank Herbert', note: 'Bookmark at the desert crossing.', w: 30, h: 96, c: 'light-dark(#6d7179, #7d8087)' },
{ label: 'Emma', author: 'Jane Austen', note: 'Finished in May. Loved the ending.', w: 24, h: 84, c: 'light-dark(#50535b, #c6c8cd)' },
{ label: 'Beloved', author: 'Toni Morrison', note: 'Next up for the book club.', w: 34, h: 102, c: 'light-dark(#cbced3, #393c41)' },
{ label: 'Kindred', author: 'Octavia E. Butler', note: 'Borrowed from Lena, due Friday.', w: 28, h: 90, c: 'light-dark(#5f626a, #a7abb2)' },
{ label: 'Ulysses', author: 'James Joyce', note: 'Started three times. Maybe this year.', w: 38, h: 98, c: 'light-dark(#dbdde1, #2e3035)' },
];
const css = `
.tabs-shelf { width: 250px; }
.tabs-shelf__list { display: flex; align-items: flex-end; justify-content: center; gap: 3px; height: 116px; padding: 0 14px; border-bottom: 6px solid light-dark(#cbced3, #393c41); border-radius: 0 0 3px 3px; box-shadow: 0 8px 12px -8px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); }
.tabs-shelf__tab {
position: relative;
display: grid;
place-items: center;
flex: none;
padding: 0;
border: 0;
border-radius: 3px 3px 1px 1px;
background: var(--tabs-shelf-c);
box-shadow: inset 2px 0 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), inset -2px 0 0 light-dark(rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.18));
cursor: pointer;
transform-origin: 0 100%;
transition: transform 0.45s cubic-bezier(0.3, 1.5, 0.5, 1), box-shadow 0.3s;
}
.tabs-shelf__tab::before, .tabs-shelf__tab::after { content: ''; position: absolute; left: 4px; right: 4px; height: 2px; background: color-mix(in oklab, light-dark(#373a40, #f1f2f3) 35%, transparent); }
.tabs-shelf__tab::before { top: 10px; }
.tabs-shelf__tab::after { bottom: 10px; }
.tabs-shelf__title { color: light-dark(#373a40, #f1f2f3); font-size: 11px; font-weight: 600; letter-spacing: 0.04em; writing-mode: vertical-rl; transform: rotate(180deg); }
.tabs-shelf__tab:hover { transform: translateY(-4px); }
.tabs-shelf__tab[aria-selected='true'] { transform: translate(4px, -14px) rotate(-9deg); box-shadow: inset 2px 0 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), 6px 10px 14px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); }
.tabs-shelf__tab:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 2px; }
.tabs-shelf__panel { min-height: 56px; padding: 12px 4px 0; text-align: center; }
.tabs-shelf__name { display: block; color: light-dark(#373a40, #f1f2f3); font-size: 14px; font-weight: 600; }
.tabs-shelf__author { display: block; margin-bottom: 4px; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; }
.tabs-shelf__note { color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; }
@media (prefers-reduced-motion: reduce) {
.tabs-shelf__tab { transition: none; }
}
`;
export function ShelfTabs() {
const id = useId();
const [sel, setSel] = useState(2);
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const onKeyDown = (e: React.KeyboardEvent) => {
const n = BOOKS.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-shelf">
<div className="tabs-shelf__list" role="tablist" aria-label="Books" onKeyDown={onKeyDown}>
{BOOKS.map((b, i) => (
<button
key={b.label}
ref={(el) => {
tabs.current[i] = el;
}}
className="tabs-shelf__tab"
type="button"
role="tab"
id={`${id}-tab-${i}`}
aria-selected={sel === i}
aria-controls={`${id}-panel-${i}`}
tabIndex={sel === i ? 0 : -1}
style={{ width: b.w, height: b.h, '--tabs-shelf-c': b.c } as React.CSSProperties}
onClick={() => setSel(i)}
>
<span className="tabs-shelf__title">{b.label}</span>
</button>
))}
</div>
{BOOKS.map((b, i) => (
<div key={b.label} className="tabs-shelf__panel" role="tabpanel" id={`${id}-panel-${i}`} aria-labelledby={`${id}-tab-${i}`} hidden={sel !== i}>
<span className="tabs-shelf__name">{b.label}</span>
<span className="tabs-shelf__author">{b.author}</span>
<span className="tabs-shelf__note">{b.note}</span>
</div>
))}
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTabsShelf(el) on the mounted element instead.
<div class="tabs-shelf">
<div class="tabs-shelf__list" role="tablist" aria-label="Books">
<button class="tabs-shelf__tab" type="button" role="tab" id="tabs-shelf-tab-0" aria-selected="false" aria-controls="tabs-shelf-panel-0" tabindex="-1" style="width:30px;height:96px;--tabs-shelf-c:light-dark(#6d7179, #7d8087)">
<span class="tabs-shelf__title">Dune</span>
</button>
<button class="tabs-shelf__tab" type="button" role="tab" id="tabs-shelf-tab-1" aria-selected="false" aria-controls="tabs-shelf-panel-1" tabindex="-1" style="width:24px;height:84px;--tabs-shelf-c:light-dark(#50535b, #c6c8cd)">
<span class="tabs-shelf__title">Emma</span>
</button>
<button class="tabs-shelf__tab" type="button" role="tab" id="tabs-shelf-tab-2" aria-selected="true" aria-controls="tabs-shelf-panel-2" tabindex="0" style="width:34px;height:102px;--tabs-shelf-c:light-dark(#cbced3, #393c41)">
<span class="tabs-shelf__title">Beloved</span>
</button>
<button class="tabs-shelf__tab" type="button" role="tab" id="tabs-shelf-tab-3" aria-selected="false" aria-controls="tabs-shelf-panel-3" tabindex="-1" style="width:28px;height:90px;--tabs-shelf-c:light-dark(#5f626a, #a7abb2)">
<span class="tabs-shelf__title">Kindred</span>
</button>
<button class="tabs-shelf__tab" type="button" role="tab" id="tabs-shelf-tab-4" aria-selected="false" aria-controls="tabs-shelf-panel-4" tabindex="-1" style="width:38px;height:98px;--tabs-shelf-c:light-dark(#dbdde1, #2e3035)">
<span class="tabs-shelf__title">Ulysses</span>
</button>
</div>
<div class="tabs-shelf__panel" role="tabpanel" id="tabs-shelf-panel-0" aria-labelledby="tabs-shelf-tab-0" hidden="">
<span class="tabs-shelf__name">Dune</span>
<span class="tabs-shelf__author">Frank Herbert</span>
<span class="tabs-shelf__note">Bookmark at the desert crossing.</span>
</div>
<div class="tabs-shelf__panel" role="tabpanel" id="tabs-shelf-panel-1" aria-labelledby="tabs-shelf-tab-1" hidden="">
<span class="tabs-shelf__name">Emma</span>
<span class="tabs-shelf__author">Jane Austen</span>
<span class="tabs-shelf__note">Finished in May. Loved the ending.</span>
</div>
<div class="tabs-shelf__panel" role="tabpanel" id="tabs-shelf-panel-2" aria-labelledby="tabs-shelf-tab-2">
<span class="tabs-shelf__name">Beloved</span>
<span class="tabs-shelf__author">Toni Morrison</span>
<span class="tabs-shelf__note">Next up for the book club.</span>
</div>
<div class="tabs-shelf__panel" role="tabpanel" id="tabs-shelf-panel-3" aria-labelledby="tabs-shelf-tab-3" hidden="">
<span class="tabs-shelf__name">Kindred</span>
<span class="tabs-shelf__author">Octavia E. Butler</span>
<span class="tabs-shelf__note">Borrowed from Lena, due Friday.</span>
</div>
<div class="tabs-shelf__panel" role="tabpanel" id="tabs-shelf-panel-4" aria-labelledby="tabs-shelf-tab-4" hidden="">
<span class="tabs-shelf__name">Ulysses</span>
<span class="tabs-shelf__author">James Joyce</span>
<span class="tabs-shelf__note">Started three times. Maybe this year.</span>
</div>
</div>.tabs-shelf { width: 250px; }
.tabs-shelf__list { display: flex; align-items: flex-end; justify-content: center; gap: 3px; height: 116px; padding: 0 14px; border-bottom: 6px solid light-dark(#cbced3, #393c41); border-radius: 0 0 3px 3px; box-shadow: 0 8px 12px -8px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); }
.tabs-shelf__tab {
position: relative;
display: grid;
place-items: center;
flex: none;
padding: 0;
border: 0;
border-radius: 3px 3px 1px 1px;
background: var(--tabs-shelf-c);
box-shadow: inset 2px 0 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), inset -2px 0 0 light-dark(rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.18));
cursor: pointer;
transform-origin: 0 100%;
transition: transform 0.45s cubic-bezier(0.3, 1.5, 0.5, 1), box-shadow 0.3s;
}
.tabs-shelf__tab::before, .tabs-shelf__tab::after { content: ''; position: absolute; left: 4px; right: 4px; height: 2px; background: color-mix(in oklab, light-dark(#373a40, #f1f2f3) 35%, transparent); }
.tabs-shelf__tab::before { top: 10px; }
.tabs-shelf__tab::after { bottom: 10px; }
.tabs-shelf__title { color: light-dark(#373a40, #f1f2f3); font-size: 11px; font-weight: 600; letter-spacing: 0.04em; writing-mode: vertical-rl; transform: rotate(180deg); }
.tabs-shelf__tab:hover { transform: translateY(-4px); }
.tabs-shelf__tab[aria-selected='true'] { transform: translate(4px, -14px) rotate(-9deg); box-shadow: inset 2px 0 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), 6px 10px 14px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6)); }
.tabs-shelf__tab:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 2px; }
.tabs-shelf__panel { min-height: 56px; padding: 12px 4px 0; text-align: center; }
.tabs-shelf__name { display: block; color: light-dark(#373a40, #f1f2f3); font-size: 14px; font-weight: 600; }
.tabs-shelf__author { display: block; margin-bottom: 4px; color: light-dark(#5f626a, #a7abb2); font-size: 11.5px; }
.tabs-shelf__note { color: light-dark(#50535b, #c6c8cd); font-size: 12.5px; }
@media (prefers-reduced-motion: reduce) {
.tabs-shelf__tab { transition: none; }
}function initTabsShelf(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;
});
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-shelf').forEach(initTabsShelf);Goal: Create a "Shelf" tab set.
Visual behavior: Book spines as tabs: the book you pick tips forward off the shelf and the others settle back, and its title, author and a note appear below.
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 spine is a tab with its own width, height and shade as inline styles and its title set in vertical writing; the selected one is lifted and rotated forward with an overshooting transition.
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 books move without easing.
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 "Shelf" tab set.
Visual behavior: Book spines as tabs: the book you pick tips forward off the shelf and the others settle back, and its title, author and a note appear below.
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 spine is a tab with its own width, height and shade as inline styles and its title set in vertical writing; the selected one is lifted and rotated forward with an overshooting transition.
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 books move without easing.
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
Book spines as tabs: the book you pick tips forward off the shelf and the others settle back, and its title, author and a note appear below.
- Dependencies
- None
- Difficulty
- Beginner
- Triggers
- click, state change, spring
- Techniques
- css transition, transform only, css variables
- 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 books move without easing.