Try 'magnetic button'
View code

Specification

Catalogue
No. 81 of 110
Interaction
Press, Layout Animation
Element
Media, Carousel
Preview
Live component
Imports
react 19.3.1
Added

Notes

A shelf of book spines. Choose one and it slides off the shelf and turns to show its cover while the others make room. The arrow keys move along the shelf and Escape puts the book back.

Authors, coaches with a book, publishers and bookshops can show a catalogue in the shape it has on a shelf, and the 3D has a job: it turns a spine, which says little, into a cover, which says a lot.

The pull (40px toward the viewer) takes 420ms and the turn to -90 degrees 500ms, both cubic-bezier(.16, 1, .3, 1), with the turn 80ms behind the pull; the neighbours slide aside on the turn's timing with the same lag. Perspective 1600px. Each face darkens as it turns side-on. A pointed-at spine lifts 8px and tips back 5 degrees. Each book is a button named by its title and author, laid flat over the book's footprint, because a turned 3D face is hard to click. Arrows, Home and End browse; the caption is announced. Instant under reduced motion.

The shelf fits the stage here, so there is no drag to scroll a long shelf, no sound and no cover tilt toward the pointer. The books, authors and covers are sample content.

Code

React and plain CSS
"use client";

import { useRef, useState, type CSSProperties, type KeyboardEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./bookshelf.css";

type Book = { id: string; title: string; author: string; year: number; pages: number; color: string; ink: string; thickness: number; motif: "sun" | "lines" | "arch" | "dots" };

const BOOKS: Book[] = [
  { id: "clay", title: "Clay and Fire", author: "M. Okafor", year: 2023, pages: 212, color: "#b4583a", ink: "#fff6ef", thickness: 38, motif: "sun" },
  { id: "garden", title: "The Patient Garden", author: "R. Lindqvist", year: 2021, pages: 288, color: "#3f5d4a", ink: "#eef4ee", thickness: 44, motif: "arch" },
  { id: "rooms", title: "Small Rooms", author: "A. Benali", year: 2024, pages: 164, color: "#d9c9a8", ink: "#2c2618", thickness: 30, motif: "lines" },
  { id: "salt", title: "Salt Year", author: "J. Moreau", year: 2022, pages: 240, color: "#26384f", ink: "#eaf0f7", thickness: 40, motif: "dots" },
  { id: "light", title: "Notes on Light", author: "S. Ito", year: 2020, pages: 196, color: "#e3b04b", ink: "#2b1f05", thickness: 34, motif: "sun" },
  { id: "kiln", title: "Kiln Logbook", author: "P. Walsh", year: 2025, pages: 320, color: "#6d4a6b", ink: "#f6eef6", thickness: 46, motif: "lines" },
];

const HEIGHT = 200;
const COVER = 140;
const GAP = 4;
const OPEN_GAP = 14;

/** Where each book sits along the shelf: spines side by side, the open book as wide as its cover with room either side. */
function layout(open: number): { slots: number[]; width: number } {
  const slots: number[] = [];
  let x = 0;
  BOOKS.forEach((b, i) => {
    if (i > 0) x += i === open || i - 1 === open ? OPEN_GAP : GAP;
    slots.push(x);
    x += i === open ? COVER : b.thickness;
  });
  return { slots, width: x };
}

function Motif({ kind }: { kind: Book["motif"] }) {
  if (kind === "sun") return <circle cx="70" cy="92" r="26" fill="currentColor" opacity="0.85" />;
  if (kind === "arch") return <path d="M40 130V96a30 30 0 0 1 60 0v34" fill="none" stroke="currentColor" strokeWidth="8" opacity="0.85" />;
  if (kind === "dots") return <g fill="currentColor" opacity="0.85">{[0, 1, 2].flatMap((r) => [0, 1, 2].map((c) => <circle key={`${r}${c}`} cx={46 + c * 24} cy={74 + r * 24} r="5" />))}</g>;
  return <g stroke="currentColor" strokeWidth="5" opacity="0.85">{[0, 1, 2, 3].map((i) => <line key={i} x1="36" x2="104" y1={78 + i * 14} y2={78 + i * 14} />)}</g>;
}

function BookshelfDemo() {
  const [open, setOpen] = useState(1);
  const buttons = useRef<(HTMLButtonElement | null)[]>([]);
  const { slots, width } = layout(open);
  const book = open >= 0 ? BOOKS[open] : null;

  const onKey = (e: KeyboardEvent<HTMLUListElement>) => {
    const at = buttons.current.indexOf(e.target as HTMLButtonElement);
    const from = at >= 0 ? at : Math.max(0, open);
    let next: number | null = null;
    if (e.key === "ArrowRight" || e.key === "ArrowDown") next = Math.min(BOOKS.length - 1, from + 1);
    else if (e.key === "ArrowLeft" || e.key === "ArrowUp") next = Math.max(0, from - 1);
    else if (e.key === "Home") next = 0;
    else if (e.key === "End") next = BOOKS.length - 1;
    else if (e.key === "Escape" && open >= 0) {
      e.preventDefault();
      setOpen(-1);
      return;
    }
    if (next === null) return;
    e.preventDefault();
    setOpen(next);
    buttons.current[next]?.focus({ preventScroll: true });
  };

  return (
    <div className="dm bs">
      <div className="bs-frame">
        <ul className="bs-shelf" aria-label="Books" style={{ width, height: HEIGHT + 2 }} onKeyDown={onKey}>
          {BOOKS.map((b, i) => {
            const isOpen = i === open;
            const turn = (COVER - b.thickness) / 2;
            return (
              <li key={b.id} className="bs-book" data-open={isOpen || undefined} style={{ width: b.thickness, height: HEIGHT, translate: `${slots[i]}px 0` }}>
                <span className="bs-pull" aria-hidden="true">
                  <span
                    className="bs-turn"
                    style={{ width: b.thickness, height: HEIGHT, "--turn": `${turn}px`, transformOrigin: `${b.thickness / 2}px 50% ${-COVER / 2}px`, "--c": b.color, "--ink": b.ink } as CSSProperties}
                  >
                    <span className="bs-spine">
                      <span className="bs-spine-title">{b.title}</span>
                      <span className="bs-spine-author">{b.author.split(" ").pop()}</span>
                    </span>
                    <span className="bs-cover" style={{ width: COVER, left: b.thickness }}>
                      <span className="bs-cover-title">{b.title}</span>
                      <svg viewBox="0 0 140 200" className="bs-motif" aria-hidden="true">
                        <Motif kind={b.motif} />
                      </svg>
                      <span className="bs-cover-author">{b.author}</span>
                    </span>
                  </span>
                </span>
                <button
                  ref={(el) => {
                    buttons.current[i] = el;
                  }}
                  type="button"
                  className="bs-hit"
                  style={{ width: isOpen ? COVER : b.thickness }}
                  aria-label={`${b.title} by ${b.author}`}
                  aria-pressed={isOpen}
                  tabIndex={isOpen || (open < 0 && i === 0) ? 0 : -1}
                  onClick={() => setOpen(isOpen ? -1 : i)}
                />
              </li>
            );
          })}
        </ul>
        <span className="bs-plank" aria-hidden="true" />
      </div>
      <p className="bs-caption" aria-live="polite">
        {book ? (
          <>
            <strong>{book.title}</strong> {book.author}, {book.year}, {book.pages} pages
          </>
        ) : (
          "Choose a book, or use the arrow keys"
        )}
      </p>
    </div>
  );
}

// What a hovered card shows: another book taken out, then two steps along the shelf with the arrow key.
const TOUR: Tour = [
  { click: ".bs-hit", nth: 3, wait: 1400 },
  { key: "ArrowRight", on: ".bs-shelf", wait: 600 },
  { key: "ArrowRight", on: ".bs-shelf", wait: 600 },
];

export default function BookshelfEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Bookshelf" tour={TOUR} sample>
      <BookshelfDemo />
    </PieceStage>
  );
}

More like this

Card stack that deals the next picture

Press, Layout AnimationMedia and galleries

Image panels that widen

Hover, Layout AnimationMedia and galleries

Photo grid that opens into the photo

Layout AnimationMedia and galleries

A photo for each option

PressTabs

Menu that opens into link cards

Press, Layout AnimationNavigation

Before and after slider

DragMedia and galleries