{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cloth-bound-book-spine-chapter-menu-where-the-chosen-volume-leaves-the-shelf-and-opens-at-its-chapter","type":"registry:block","title":"Cloth Bound Book Spine Chapter Menu Where the Chosen Volume Leaves the Shelf and Opens at Its Chapter","description":"Chapter menu drawn as a shelf of cloth bound books: pick a spine and that volume is pulled out and opened. Copy the React and Tailwind code or HTML free.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/cloth-bound-book-spine-chapter-menu-where-the-chosen-volume-leaves-the-shelf-and-opens-at-its-chapter.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useSpring, useTransform } from \"framer-motion\";\nimport type { MotionValue } from \"framer-motion\";\n\n/** The set on the shelf. One volume per chapter: change words, cloth, foil and plate here. */\nconst series = \"The Studio Handbook\";\nconst volumes = [\n  { no: \"01\", title: \"Red Ground\", line: \"Choosing one colour and letting it fill the frame.\", cloth: \"#1f5759\", foil: \"gold\", end: \"#d8452c\", t: 52, h: 372, d: 244, photo: \"https://images.pexels.com/photos/13023364/pexels-photo-13023364.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"A pale flower in a blue glass bottle against a red wall\", tone: \"#c9483b\", focus: \"50% 40%\" },\n  { no: \"02\", title: \"Tall Grasses\", line: \"Dried stems, a low table and a long afternoon.\", cloth: \"#d8cdb3\", foil: \"ink\", end: \"#a9502a\", t: 42, h: 340, d: 232, photo: \"https://images.pexels.com/photos/9956158/pexels-photo-9956158.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Dried foxtail lilies standing on a white table in front of a burnt orange wall\", tone: \"#7a3513\", focus: \"50% 50%\" },\n  { no: \"03\", title: \"Yellow Room\", line: \"Still life with glass, oranges and one hard light.\", cloth: \"#1e2947\", foil: \"gold\", end: \"#e2b621\", t: 62, h: 392, d: 250, photo: \"https://images.pexels.com/photos/39780460/pexels-photo-39780460.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Glass vase of dried flowers and orange balls on a mirrored table against a yellow wall\", tone: \"#c9a514\", focus: \"50% 55%\" },\n  { no: \"04\", title: \"Hard Shadow\", line: \"Buildings at noon, when the sun draws the lines.\", cloth: \"#c7932a\", foil: \"ink\", end: \"#2c55a6\", t: 46, h: 356, d: 238, photo: \"https://images.pexels.com/photos/3068581/pexels-photo-3068581.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Yellow building wall cut by a deep black diagonal shadow\", tone: \"#e0a01c\", focus: \"40% 50%\" },\n  { no: \"05\", title: \"Blue Door\", line: \"White walls, a strip of sky and a painted door.\", cloth: \"#b5502e\", foil: \"pearl\", end: \"#3c7ccb\", t: 68, h: 384, d: 250, photo: \"https://images.pexels.com/photos/16615603/pexels-photo-16615603.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"White and blue modern facade with an orange door under a clear sky\", tone: \"#7fb2e6\", focus: \"60% 50%\" },\n  { no: \"06\", title: \"Night Vase\", line: \"One lamp, a dark room and a green glass vase.\", cloth: \"#27503a\", foil: \"gold\", end: \"#ae1630\", t: 44, h: 332, d: 228, photo: \"https://images.pexels.com/photos/36174582/pexels-photo-36174582.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Wavy green glass vase holding red flowers against a black background\", tone: \"#0c1410\", focus: \"50% 45%\" },\n  { no: \"07\", title: \"Red Veil\", line: \"Portraits through tulle, lit from one side.\", cloth: \"#2b2927\", foil: \"copper\", end: \"#c0181d\", t: 58, h: 366, d: 244, photo: \"https://images.pexels.com/photos/31167809/pexels-photo-31167809.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in dramatic make-up wrapped in red tulle against a black backdrop\", tone: \"#6d0f10\", focus: \"50% 25%\" },\n  { no: \"08\", title: \"One Stem\", line: \"A single flower and nothing else on the table.\", cloth: \"#c58c84\", foil: \"pearl\", end: \"#e2761c\", t: 48, h: 348, d: 236, photo: \"https://images.pexels.com/photos/8801127/pexels-photo-8801127.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Orange gerbera daisy in a matching orange vase on a pale grey background\", tone: \"#cfcfcd\", focus: \"50% 40%\" },\n];\ntype Volume = (typeof volumes)[number];\ntype Pose = \"in\" | \"out\" | \"open\";\n\nconst FOIL: Record<string, string> = {\n  gold: \"linear-gradient(100deg, #8e6c22 0%, #f6e29a 22%, #b98f30 45%, #fff3bd 62%, #a37c27 80%, #e9cf7c 100%)\",\n  copper: \"linear-gradient(100deg, #7d4426 0%, #f0b48c 24%, #a85f38 46%, #ffd3b3 64%, #8c4d2c 82%, #dd9a72 100%)\",\n  pearl: \"linear-gradient(100deg, #d9d2c2 0%, #fffdf6 30%, #e2dccd 55%, #ffffff 72%, #d6cfbe 100%)\",\n  ink: \"linear-gradient(100deg, #1c1916 0%, #33302b 50%, #1c1916 100%)\",\n};\n\n// Two layouts in the scene's own pixels: one long shelf, or (on narrow screens) a display shelf above the row.\nconst WIDE = { W: 1000, H: 650, shelfY: 520, rowX: 84, dispY: 520, closedX: 706, openX: 742, eye: \"50% -12%\" };\nconst TALL = { W: 500, H: 1066, shelfY: 930, rowX: 22, dispY: 462, closedX: 128, openX: 224, eye: \"50% 26%\" };\ntype Layout = typeof WIDE;\nconst LANE = 292; // how far a volume is pulled forward before it travels along the shelf\nconst SHELF_BACK = 284; // distance from the spines to the wall\nconst RY_OUT = -63;\nconst RY_OPEN = -77;\nconst AJAR = 87; // cover angle while standing closed (90 is shut)\nconst COVER_OPEN = -56;\nconst WALL = \"#e6e0d4\";\nconst PAPER = \"#f3ecda\";\nconst weave = \"repeating-linear-gradient(0deg, rgba(255,255,255,0.07) 0 1px, rgba(0,0,0,0) 1px 2px), repeating-linear-gradient(90deg, rgba(0,0,0,0.1) 0 1px, rgba(0,0,0,0) 1px 2px)\";\nconst grain = \"url(\\\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5 -0.08'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E\\\")\";\nconst fibre = grain.replace(\"0.5 -0.08\", \"0.16 -0.02\");\nconst foilText = (kind: string) => ({ backgroundImage: FOIL[kind], WebkitBackgroundClip: \"text\", backgroundClip: \"text\", color: \"transparent\" }) as const;\n\nfunction rest(pose: Pose, L: Layout, x0: number) {\n  if (pose === \"in\") return { x: 0, y: 0, z: 0, rx: 0, ry: 0, cv: 90 };\n  const open = pose === \"open\";\n  return { x: (open ? L.openX : L.closedX) - x0, y: L.dispY - L.shelfY, z: -22, rx: 0, ry: open ? RY_OPEN : RY_OUT, cv: open ? COVER_OPEN : AJAR };\n}\n\nfunction Book({ v, x0, L, pose, lean, wait, reduce, onPick, onCover }: { v: Volume; x0: number; L: Layout; pose: Pose; lean: boolean; wait: number; reduce: boolean; onPick: () => void; onCover: () => void }) {\n  const start = rest(pose, L, x0);\n  const x = useMotionValue(start.x);\n  const y = useMotionValue(start.y);\n  const z = useMotionValue(start.z);\n  const rx = useMotionValue(start.rx);\n  const ry = useMotionValue(start.ry);\n  const cv = useMotionValue(start.cv);\n  const [hover, setHover] = useState(false);\n  const prev = useRef({ pose, L });\n  const running = useRef<{ stop: () => void }[]>([]);\n\n  useEffect(() => {\n    const was = prev.current;\n    prev.current = { pose, L };\n    const to = rest(pose, L, x0);\n    const all: [MotionValue<number>, number][] = [[x, to.x], [y, to.y], [z, to.z], [rx, to.rx], [ry, to.ry], [cv, to.cv]];\n    running.current.forEach((a) => a.stop());\n    running.current = [];\n    if (reduce || was.L !== L || was.pose === pose) {\n      all.forEach(([m, val]) => m.set(val));\n      return;\n    }\n    const run = (m: MotionValue<number>, keys: number[], opts: object) => running.current.push(animate(m, [m.get(), ...keys], opts));\n    if (was.pose === \"in\") {\n      // tip the top of the spine forward, pull the volume clear of the row, carry it along, stand it down turned\n      const o = { duration: 1.15, times: [0, 0.14, 0.42, 0.8, 1], ease: \"easeInOut\", delay: wait } as const;\n      run(rx, [-9, 0, 0, 0], o);\n      run(z, [28, LANE, LANE, to.z], o);\n      run(x, [0, 0, to.x, to.x], o);\n      run(y, [0, 0, to.y, to.y], o);\n      run(ry, [0, 0, -36, to.ry], o);\n      running.current.push(animate(cv, to.cv, { type: \"spring\", stiffness: 70, damping: 9, delay: wait + 1.05 }));\n    } else if (pose === \"in\") {\n      const o = { duration: 0.82, times: [0, 0.24, 0.62, 0.9, 1], ease: \"easeInOut\" } as const;\n      running.current.push(animate(cv, 90, { duration: 0.22, ease: \"easeIn\" }));\n      run(z, [LANE, LANE, 22, 0], o);\n      run(x, [x.get(), 0, 0, 0], o);\n      run(y, [y.get(), 0, 0, 0], o);\n      run(ry, [-30, 0, 0, 0], o);\n      run(rx, [0, 0, -6, 0], o);\n    } else {\n      const s = { type: \"spring\", stiffness: 60, damping: 12, mass: 1 } as const;\n      all.forEach(([m, val]) => running.current.push(animate(m, val, m === cv ? { type: \"spring\", stiffness: 46, damping: 8.5 } : s)));\n    }\n  }, [pose, L, reduce, x0, wait, x, y, z, rx, ry, cv]);\n  useEffect(() => () => running.current.forEach((a) => a.stop()), []);\n\n  // a finger on the head of the spine: the volume rocks forward a little before it is pulled\n  useEffect(() => {\n    if (reduce || pose !== \"in\" || prev.current.pose !== \"in\") return;\n    const a = animate(rx, hover ? -7 : 0, { type: \"spring\", stiffness: 220, damping: 16 });\n    const b = animate(z, hover ? 18 : 0, { type: \"spring\", stiffness: 220, damping: 18 });\n    return () => {\n      a.stop();\n      b.stop();\n    };\n  }, [hover, pose, reduce, rx, z]);\n\n  const dark = v.foil === \"ink\";\n  const out = pose !== \"in\";\n  const round = \"linear-gradient(90deg, rgba(0,0,0,0.42) 0%, rgba(0,0,0,0.08) 14%, rgba(255,255,255,0.2) 32%, rgba(255,255,255,0.04) 52%, rgba(0,0,0,0.1) 78%, rgba(0,0,0,0.4) 100%)\";\n\n  return (\n    <motion.div className=\"pointer-events-none absolute\" style={{ left: x0, top: L.shelfY - v.h, width: v.t, height: v.h, transformStyle: \"preserve-3d\", transformOrigin: `${v.t / 2}px ${v.h}px`, x, y, z, rotateX: rx, rotateY: ry }}>\n      <motion.div className=\"absolute inset-0\" style={{ transformStyle: \"preserve-3d\", transformOrigin: \"0% 100%\" }} animate={{ rotateZ: lean && !reduce ? -3.1 : 0 }} transition={{ type: \"spring\", stiffness: 120, damping: 9 }} initial={false}>\n        {/* back board */}\n        <div aria-hidden=\"true\" className=\"absolute left-0 top-0\" style={{ width: v.d, height: v.h, transformOrigin: \"0 50%\", transform: \"rotateY(90deg)\", backgroundColor: v.cloth, backgroundImage: `linear-gradient(90deg, rgba(0,0,0,0.25), rgba(0,0,0,0.5)), ${weave}` }} />\n        {/* the page block seen from above, with its headband, set down inside the boards */}\n        <div aria-hidden=\"true\" className=\"absolute\" style={{ left: 3, top: 5, width: v.t - 6, height: v.d - 7, transformOrigin: \"50% 0\", transform: \"rotateX(-90deg)\", backgroundColor: \"#ece3cd\", backgroundImage: \"linear-gradient(180deg, rgba(0,0,0,0.5) 0, rgba(0,0,0,0) 3px), linear-gradient(180deg, rgba(255,255,255,0.3), rgba(0,0,0,0.05) 40%, rgba(60,40,10,0.38)), repeating-linear-gradient(90deg, rgba(90,70,40,0.34) 0 0.6px, rgba(0,0,0,0) 0.6px 2px)\" }}>\n          <div className=\"absolute inset-x-0 top-0 h-[7px]\" style={{ backgroundColor: v.end, backgroundImage: \"repeating-linear-gradient(90deg, rgba(255,255,255,0.75) 0 2px, rgba(0,0,0,0) 2px 4px), linear-gradient(180deg, rgba(0,0,0,0.3), rgba(0,0,0,0) 60%)\" }} />\n        </div>\n        {/* cloth over the head of the spine and the edge of the back board */}\n        <div aria-hidden=\"true\" className=\"absolute left-0 top-0\" style={{ width: v.t - 3, height: v.d, transformOrigin: \"50% 0\", transform: \"rotateX(-90deg)\", backgroundImage: `linear-gradient(180deg, ${v.cloth} 0 5px, rgba(0,0,0,0) 5px), linear-gradient(90deg, ${v.cloth} 0 3px, rgba(0,0,0,0) 3px)` }} />\n        {/* first leaf, printed with the chapter opening */}\n        <div className=\"absolute overflow-hidden\" style={{ left: v.t - 4, top: 5, width: v.d - 7, height: v.h - 10, transformOrigin: \"0 50%\", transform: \"rotateY(90deg)\", backgroundColor: PAPER, backgroundImage: `linear-gradient(90deg, rgba(60,40,10,0.42) 0, rgba(60,40,10,0.08) 16px, rgba(0,0,0,0) 44px), linear-gradient(100deg, rgba(255,255,255,0.3), rgba(255,255,255,0) 50%, rgba(80,60,20,0.1)), ${fibre}` }}>\n          <div className=\"absolute inset-x-[22px] top-[20px] overflow-hidden\" style={{ height: Math.round(v.h * 0.4), backgroundColor: v.tone }}>\n            <img src={v.photo} alt=\"\" draggable={false} className=\"h-full w-full object-cover\" style={{ objectPosition: v.focus }} />\n          </div>\n          <div className=\"absolute inset-x-[22px] text-[#2a2118]\" style={{ top: Math.round(v.h * 0.4) + 32 }}>\n            <p className=\"font-mono text-[8.5px] uppercase tracking-[0.26em] text-[#6b5a44]\">Chapter {v.no}</p>\n            <p className=\"mt-[6px] font-serif text-[27px] leading-[1] tracking-[-0.01em]\">{v.title}</p>\n            <div className=\"mt-[11px] h-px w-[34px] bg-[#2a2118]\" />\n            <p className=\"mt-[10px] font-serif text-[12px] italic leading-[1.42] text-[#4a3d2e]\">{v.line}</p>\n          </div>\n          <a href=\"#\" tabIndex={pose === \"open\" ? 0 : -1} className=\"pointer-events-auto absolute bottom-[16px] left-[22px] inline-flex min-h-[40px] items-center gap-2 font-mono text-[9px] font-semibold uppercase tracking-[0.22em] text-[#2a2118] underline decoration-[#2a2118]/40 underline-offset-4 outline-none focus-visible:ring-2 focus-visible:ring-[#2a2118]\">\n            Read chapter {v.no}\n            <svg aria-hidden=\"true\" width=\"14\" height=\"8\" viewBox=\"0 0 14 8\" fill=\"none\"><path d=\"M0 4h12M9 1l3 3-3 3\" stroke=\"currentColor\" strokeWidth=\"1.2\" /></svg>\n          </a>\n          <span className=\"absolute bottom-[18px] right-[18px] font-serif text-[10px] text-[#6b5a44]\">{Number(v.no) * 24 - 17}</span>\n        </div>\n        {/* spine */}\n        <button\n          type=\"button\"\n          onClick={onPick}\n          onPointerEnter={() => setHover(true)}\n          onPointerLeave={() => setHover(false)}\n          aria-current={out ? \"true\" : undefined}\n          aria-label={`Chapter ${v.no}, ${v.title}`}\n          className=\"pointer-events-auto absolute inset-0 block cursor-pointer overflow-hidden rounded-[1.5px] outline-none focus-visible:ring-[3px] focus-visible:ring-inset focus-visible:ring-sky-400\"\n          style={{ backgroundColor: v.cloth, backgroundImage: `${round}, ${weave}` }}\n        >\n          <span aria-hidden=\"true\" className=\"absolute inset-0 opacity-60 mix-blend-multiply\" style={{ backgroundImage: grain }} />\n          {/* foil rules at head and tail */}\n          {[18, 23, v.h - 76, v.h - 24].map((top) => (\n            <span key={top} aria-hidden=\"true\" className=\"absolute inset-x-[5px] h-[1.5px]\" style={{ top, backgroundImage: FOIL[v.foil] }} />\n          ))}\n          <span aria-hidden=\"true\" className=\"absolute left-1/2 top-[38px] whitespace-nowrap font-serif text-[17px] uppercase leading-none tracking-[0.2em]\" style={{ ...foilText(v.foil), writingMode: \"vertical-rl\", transform: \"translateX(-50%)\", fontSize: v.t < 46 ? 15 : 18, filter: dark ? \"none\" : \"drop-shadow(0 0.5px 0 rgba(0,0,0,0.45))\" }}>\n            {v.title}\n          </span>\n          <span aria-hidden=\"true\" className=\"absolute inset-x-0 text-center font-serif text-[15px] leading-none\" style={{ ...foilText(v.foil), top: v.h - 59 }}>\n            {v.no}\n          </span>\n          <span aria-hidden=\"true\" className=\"absolute left-1/2 h-[9px] w-[9px] -translate-x-1/2 rotate-45\" style={{ top: v.h - 16, backgroundImage: FOIL[v.foil], opacity: 0.9 }} />\n          {/* the groove where the cloth turns onto each board */}\n          <span aria-hidden=\"true\" className=\"absolute inset-y-0 left-0 w-[3px] bg-black/25\" />\n          <span aria-hidden=\"true\" className=\"absolute inset-y-0 right-0 w-[3px] bg-black/30\" />\n          <span aria-hidden=\"true\" className=\"absolute inset-x-0 top-0 h-[5px]\" style={{ background: \"linear-gradient(180deg, rgba(255,255,255,0.35), rgba(255,255,255,0))\" }} />\n          <span aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-0 h-[26px]\" style={{ background: \"linear-gradient(0deg, rgba(0,0,0,0.42), rgba(0,0,0,0))\" }} />\n        </button>\n        {/* front board, hinged on the spine */}\n        <motion.div className=\"absolute top-0\" style={{ left: v.t - 3, width: v.d, height: v.h, transformStyle: \"preserve-3d\", transformOrigin: \"0 50%\", rotateY: cv }}>\n          <button\n            type=\"button\"\n            onClick={onCover}\n            tabIndex={out ? 0 : -1}\n            aria-expanded={pose === \"open\"}\n            aria-label={pose === \"open\" ? `Close ${v.title}` : `Open ${v.title} at chapter ${v.no}`}\n            className=\"pointer-events-auto absolute inset-0 block cursor-pointer overflow-hidden rounded-r-[2px] outline-none focus-visible:ring-[3px] focus-visible:ring-inset focus-visible:ring-sky-400\"\n            style={{ transform: \"translateZ(3px)\", backfaceVisibility: \"hidden\", WebkitBackfaceVisibility: \"hidden\", backgroundColor: v.cloth, backgroundImage: `linear-gradient(90deg, rgba(0,0,0,0.3) 0, rgba(0,0,0,0.3) 9px, rgba(255,255,255,0.16) 11px, rgba(0,0,0,0) 26px), ${weave}` }}\n          >\n            <span aria-hidden=\"true\" className=\"absolute inset-0 opacity-60 mix-blend-multiply\" style={{ backgroundImage: grain }} />\n            {/* the plate, pasted into a pressed panel */}\n            <span className=\"absolute block\" style={{ left: 38, right: 30, top: 34, height: Math.round(v.h * 0.56), backgroundColor: \"rgba(0,0,0,0.2)\", boxShadow: \"inset 0 2px 3px rgba(0,0,0,0.55), inset 0 -1px 0 rgba(255,255,255,0.22), 0 1px 0 rgba(255,255,255,0.14)\" }}>\n              <span className=\"absolute inset-[5px] block overflow-hidden\" style={{ backgroundColor: v.tone, boxShadow: \"0 1px 2px rgba(0,0,0,0.5)\" }}>\n                <img src={v.photo} alt={v.alt} draggable={false} className=\"h-full w-full object-cover\" style={{ objectPosition: v.focus }} />\n                <span aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: \"linear-gradient(125deg, rgba(255,255,255,0.22), rgba(255,255,255,0) 34%, rgba(0,0,0,0) 70%, rgba(0,0,0,0.14))\" }} />\n              </span>\n            </span>\n            <span aria-hidden=\"true\" className=\"absolute block text-left\" style={{ left: 38, right: 30, top: Math.round(v.h * 0.56) + 52 }}>\n              <span className=\"block font-serif text-[22px] uppercase leading-[1.02] tracking-[0.14em]\" style={{ ...foilText(v.foil), filter: dark ? \"none\" : \"drop-shadow(0 0.6px 0 rgba(0,0,0,0.5))\" }}>\n                {v.title}\n              </span>\n              <span className=\"mt-[9px] block h-[1.5px] w-[30px]\" style={{ backgroundImage: FOIL[v.foil] }} />\n              <span className=\"mt-[9px] block font-mono text-[8px] uppercase tracking-[0.3em]\" style={foilText(v.foil)}>\n                {series} / {v.no}\n              </span>\n            </span>\n            <span aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: \"linear-gradient(112deg, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0) 30%, rgba(0,0,0,0) 62%, rgba(0,0,0,0.26) 100%)\", boxShadow: \"inset 0 0 0 1px rgba(255,255,255,0.1), inset -2px 0 3px rgba(0,0,0,0.25)\" }} />\n          </button>\n          {/* inside of the board: coloured endpaper inside the cloth turn-ins */}\n          <div aria-hidden=\"true\" className=\"absolute inset-0\" style={{ transform: \"rotateY(180deg)\", backfaceVisibility: \"hidden\", WebkitBackfaceVisibility: \"hidden\", backgroundColor: v.cloth, backgroundImage: weave }}>\n            <div className=\"absolute\" style={{ left: 9, right: 0, top: 9, bottom: 9, backgroundColor: v.end, backgroundImage: `linear-gradient(270deg, rgba(0,0,0,0.42) 0, rgba(0,0,0,0.06) 22px, rgba(0,0,0,0) 60px), linear-gradient(120deg, rgba(255,255,255,0.2), rgba(255,255,255,0) 40%, rgba(0,0,0,0.12)), ${fibre}` }} />\n          </div>\n          {/* thickness of the board along its head and its long edge */}\n          <div aria-hidden=\"true\" className=\"absolute left-0 top-0 h-[3px]\" style={{ width: v.d, transformOrigin: \"50% 0\", transform: \"rotateX(90deg)\", backgroundColor: v.cloth, backgroundImage: \"linear-gradient(rgba(255,255,255,0.22), rgba(255,255,255,0.22))\" }} />\n          <div aria-hidden=\"true\" className=\"absolute top-0 w-[3px]\" style={{ left: v.d, height: v.h, transformOrigin: \"0 50%\", transform: \"rotateY(-90deg)\", backgroundColor: v.cloth, backgroundImage: \"linear-gradient(rgba(0,0,0,0.2), rgba(0,0,0,0.2))\" }} />\n        </motion.div>\n      </motion.div>\n    </motion.div>\n  );\n}\n\nexport default function ClothBoundChapterShelf() {\n  const reduce = !!useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const sceneRef = useRef<HTMLDivElement>(null);\n  const [tall, setTall] = useState(false);\n  const [pick, setPick] = useState(2);\n  const [open, setOpen] = useState(false);\n  const [wait, setWait] = useState(0);\n  const idle = useRef({ paused: false, touched: false });\n  const L = tall ? TALL : WIDE;\n  const landed = useRef(false);\n  useEffect(() => {\n    landed.current = !reduce;\n  }, [reduce]);\n  const tilt = useSpring(0, { stiffness: 50, damping: 14 });\n  const drift = useMotionValue(0);\n  const turn = useTransform([tilt, drift], ([a, b]: number[]) => a + b);\n  // the camera never quite stands still\n  useEffect(() => {\n    if (reduce) return;\n    const a = animate(drift, [-1.5, 1.5], { duration: 5.5, ease: \"easeInOut\", repeat: Infinity, repeatType: \"mirror\" });\n    return () => a.stop();\n  }, [drift, reduce]);\n\n  const choose = useCallback(\n    (i: number, byHand: boolean) => {\n      if (byHand) idle.current.touched = true;\n      setPick((was) => {\n        if (was === i) return was;\n        setWait(0.36);\n        setOpen(false);\n        return i;\n      });\n    },\n    [],\n  );\n  const toggle = useCallback((byHand: boolean) => {\n    if (byHand) idle.current.touched = true;\n    setOpen((o) => !o);\n  }, []);\n\n  // self-playing demo: open the standing volume, hold, then fetch the next one\n  useEffect(() => {\n    if (reduce) {\n      setOpen(true);\n      return;\n    }\n    let step = 0;\n    let timer = 0;\n    const tick = () => {\n      const s = idle.current;\n      if (s.touched) return;\n      if (!s.paused && !document.hidden) {\n        step += 1;\n        if (step % 2 === 1) setOpen(true);\n        else {\n          setWait(0.36);\n          setOpen(false);\n          setPick((p) => (p + 3) % volumes.length);\n        }\n      }\n      timer = window.setTimeout(tick, step % 2 === 1 ? 3000 : 3300);\n    };\n    timer = window.setTimeout(tick, 3400);\n    return () => window.clearTimeout(timer);\n  }, [reduce]);\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const scene = sceneRef.current;\n    if (!wrap || !scene) return;\n    const measure = () => {\n      const isTall = wrap.clientWidth < 720;\n      const lay = isTall ? TALL : WIDE;\n      setTall(isTall);\n      const sc = Math.min(1.12, wrap.clientWidth / (lay.W + (isTall ? 0 : 60)));\n      wrap.style.height = `${Math.round(lay.H * sc)}px`;\n      scene.style.transform = `translateX(-50%) scale(${sc.toFixed(4)})`;\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(wrap);\n    return () => ro.disconnect();\n  }, []);\n\n  let run = L.rowX;\n  const slots = volumes.map((v) => {\n    const at = run;\n    run += v.t + 1;\n    return at;\n  });\n  const endX = run + 2;\n  const shelves = tall ? [L.dispY, L.shelfY] : [L.shelfY];\n  const oak = \"linear-gradient(180deg, #e9d3ad 0%, #d9bd8f 38%, #c4a373 100%)\";\n  const figure = \"repeating-linear-gradient(91deg, rgba(120,84,40,0.16) 0 1px, rgba(0,0,0,0) 1px 7px), repeating-linear-gradient(89deg, rgba(255,255,255,0.14) 0 2px, rgba(0,0,0,0) 2px 23px)\";\n  const v = volumes[pick];\n\n  return (\n    <section className=\"relative overflow-hidden\" style={{ backgroundColor: WALL }}>\n      <div\n        ref={wrapRef}\n        onPointerMove={(e) => {\n          if (reduce || e.pointerType === \"touch\") return;\n          const r = e.currentTarget.getBoundingClientRect();\n          tilt.set((((e.clientX - r.left) / r.width) * 2 - 1) * -2.2);\n        }}\n        onPointerEnter={() => (idle.current.paused = true)}\n        onPointerLeave={() => {\n          idle.current.paused = false;\n          tilt.set(0);\n        }}\n        onFocus={() => (idle.current.paused = true)}\n        onBlur={() => (idle.current.paused = false)}\n        className=\"relative mx-auto w-full overflow-hidden\"\n        style={{ height: WIDE.H }}\n      >\n        <div ref={sceneRef} className=\"absolute left-1/2 top-0 select-none\" style={{ width: L.W, height: L.H, transformOrigin: \"50% 0\", transform: \"translateX(-50%)\", perspective: \"2200px\", perspectiveOrigin: L.eye }}>\n          <motion.nav aria-label={`${series}: chapters`} className=\"pointer-events-none absolute inset-0\" style={{ transformStyle: \"preserve-3d\", transformOrigin: `50% 60% -${SHELF_BACK / 2}px`, rotateY: turn }}>\n            {/* the wall behind, with the light pooling on it and the shadows the books throw */}\n            <div aria-hidden=\"true\" className=\"pointer-events-none absolute\" style={{ left: -700, top: -400, width: L.W + 1400, height: L.H + 800, transform: `translateZ(-${SHELF_BACK}px)`, backgroundColor: WALL, backgroundImage: `radial-gradient(60% 55% at 46% 36%, #f6f2e9 0%, #ebe5da 45%, #d6cfc1 100%), ${grain}`, backgroundBlendMode: \"normal\" }}>\n              {shelves.map((sy) => (\n                <div key={sy} className=\"absolute\" style={{ left: 0, right: 0, top: 400 + sy + 22, height: 120, background: \"linear-gradient(180deg, rgba(60,45,25,0.42), rgba(60,45,25,0.12) 42%, rgba(60,45,25,0))\" }} />\n              ))}\n              {volumes.map((b, i) => (\n                <motion.div key={b.no} className=\"absolute\" style={{ left: 700 + slots[i] + 20, top: 400 + L.shelfY - b.h + 16, width: b.t + 8, height: b.h, background: \"rgba(48,36,20,0.5)\", filter: \"blur(16px)\" }} initial={false} animate={{ opacity: i === pick ? 0 : 1 }} transition={{ duration: 0.5, delay: i === pick ? 0.2 : 0.5 }} />\n              ))}\n              <div className=\"absolute\" style={{ left: 700 + endX + 16, top: 400 + L.shelfY - 150, width: 60, height: 160, background: \"rgba(48,36,20,0.42)\", filter: \"blur(16px)\" }} />\n              <motion.div key={pick} className=\"absolute\" style={{ left: 700 + L.closedX + 40, top: 400 + L.dispY - v.h + 20, width: 250, height: v.h, background: \"rgba(48,36,20,0.34)\", filter: \"blur(30px)\" }} initial={{ opacity: landed.current ? 0 : 1 }} animate={{ opacity: 1 }} transition={{ duration: 0.4, delay: wait + 0.95 }} />\n            </div>\n\n            {shelves.map((sy) => (\n              <div key={sy} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ transformStyle: \"preserve-3d\" }}>\n                <div className=\"absolute\" style={{ left: -700, top: sy, width: L.W + 1400, height: SHELF_BACK + 18, transformOrigin: \"50% 0\", transform: \"translateZ(18px) rotateX(-90deg)\", backgroundColor: \"#dcc195\", backgroundImage: `linear-gradient(180deg, rgba(255,255,255,0.3) 0, rgba(255,255,255,0) 30px, rgba(70,48,20,0) 40%, rgba(70,48,20,0.5) 100%), ${figure}` }}>\n                  {sy === L.dispY && <motion.div key={pick} className=\"absolute\" style={{ left: 700 + L.closedX - 10, top: 26, width: 300, height: 150, background: \"rgba(40,28,12,0.5)\", filter: \"blur(18px)\", borderRadius: \"40%\" }} initial={{ opacity: landed.current ? 0 : 1 }} animate={{ opacity: 1 }} transition={{ duration: 0.4, delay: wait + 0.95 }} />}\n                </div>\n                <div className=\"absolute\" style={{ left: -700, top: sy, width: L.W + 1400, height: 30, transform: \"translateZ(18px)\", backgroundColor: \"#d2b585\", backgroundImage: `linear-gradient(180deg, rgba(255,255,255,0.55) 0, rgba(255,255,255,0) 2px, rgba(0,0,0,0) 70%, rgba(60,40,15,0.35) 100%), ${figure}, ${oak}` }} />\n                <div className=\"absolute\" style={{ left: -700, top: sy + 30, width: L.W + 1400, height: 20, transform: \"translateZ(18px)\", background: \"linear-gradient(180deg, rgba(50,36,16,0.34), rgba(50,36,16,0))\" }} />\n              </div>\n            ))}\n\n            {/* a block of dark green marble holds the row up */}\n            <div aria-hidden=\"true\" className=\"pointer-events-none absolute\" style={{ left: endX, top: L.shelfY - 150, width: 44, height: 150 }}>\n              {/* its top is drawn flat, as the camera sees it, so the block stays one plane */}\n              <div className=\"absolute inset-x-0 bottom-full h-[30px]\" style={{ clipPath: tall ? \"polygon(2% 0, 94% 0, 100% 100%, 0 100%)\" : \"polygon(0 0, 91% 0, 100% 100%, 0 100%)\", backgroundColor: \"#2f6551\", backgroundImage: \"linear-gradient(170deg, rgba(255,255,255,0.5), rgba(255,255,255,0.1) 55%, rgba(0,0,0,0.12)), linear-gradient(64deg, rgba(0,0,0,0) 44%, rgba(220,240,225,0.6) 47%, rgba(0,0,0,0) 51%)\" }} />\n              <div className=\"absolute inset-0\" style={{ backgroundColor: \"#17382d\", backgroundImage: \"linear-gradient(90deg, rgba(255,255,255,0.22) 0, rgba(255,255,255,0) 4px, rgba(0,0,0,0) 80%, rgba(0,0,0,0.4)), linear-gradient(118deg, rgba(0,0,0,0) 30%, rgba(214,236,222,0.42) 31.5%, rgba(0,0,0,0) 34%), linear-gradient(72deg, rgba(0,0,0,0) 58%, rgba(214,236,222,0.3) 59%, rgba(0,0,0,0) 62%), linear-gradient(140deg, rgba(0,0,0,0) 70%, rgba(214,236,222,0.2) 71%, rgba(0,0,0,0) 73%), radial-gradient(80% 60% at 30% 20%, #2f6a54, #143328)\", boxShadow: \"inset 0 1px 0 rgba(255,255,255,0.5), inset 1px 0 0 rgba(255,255,255,0.25), inset 0 -18px 18px -10px rgba(0,0,0,0.5)\" }}>\n                <div className=\"absolute inset-y-0 left-[7px] w-[9px]\" style={{ background: \"linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,0.3), rgba(255,255,255,0))\" }} />\n              </div>\n            </div>\n\n            {volumes.map((b, i) => (\n              <Book key={b.no} v={b} x0={slots[i]} L={L} pose={i === pick ? (open ? \"open\" : \"out\") : \"in\"} lean={i === pick + 1} wait={wait} reduce={reduce} onPick={() => (i === pick ? toggle(true) : choose(i, true))} onCover={() => toggle(true)} />\n            ))}\n          </motion.nav>\n        </div>\n\n        <div className=\"absolute inset-x-5 bottom-4 flex items-end justify-between gap-4 sm:inset-x-8 sm:bottom-7\">\n          <p aria-live=\"polite\" className=\"font-mono text-[11px] uppercase leading-relaxed tracking-[0.18em] text-stone-600\">\n            <span className=\"text-stone-900\">\n              {v.no} / {v.title}\n            </span>\n            <span className=\"block\">{open ? \"Press the cover to close it\" : \"Pick a spine, then open the cover\"}</span>\n          </p>\n          <div className=\"flex gap-2\">\n            {[-1, 1].map((d) => (\n              <button key={d} type=\"button\" onClick={() => choose((pick + d + volumes.length) % volumes.length, true)} aria-label={d < 0 ? \"Previous chapter\" : \"Next chapter\"} className=\"grid h-10 w-10 place-items-center rounded-full border border-stone-900/25 bg-white/50 text-stone-900 outline-none transition-colors hover:bg-white focus-visible:ring-2 focus-visible:ring-stone-900\">\n                <svg aria-hidden=\"true\" width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\"><path d={d < 0 ? \"M9 2 4 7l5 5\" : \"M5 2l5 5-5 5\"} stroke=\"currentColor\" strokeWidth=\"1.5\" /></svg>\n              </button>\n            ))}\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
