{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"book-cover-tilt-with-spine-and-page-depth","type":"registry:block","title":"Book Cover Tilt With Spine and Page Depth","description":"","dependencies":[],"files":[{"path":"components/we-inc/book-cover-tilt-with-spine-and-page-depth.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst MAX_TILT = 13; // degrees the book turns at the edge of its cover\nconst BASE_TURN = 24; // resting turn so the spine shows\nconst EASE = 0.08; // how quickly the book follows (0.05 slow, 0.15 snappy)\n\nconst css = `\n  .w4bkc { --w4bkc-paper: #efe6d6; --w4bkc-cover: #24433d; --w4bkc-ink: #2a2520; --w4bkc-cream: #f3ead8; --w4bkc-thick: 36px;\n    background: var(--w4bkc-paper); color: var(--w4bkc-ink); padding: 72px 20px; font-family: ui-sans-serif, system-ui, sans-serif; overflow: hidden; }\n  .w4bkc-wrap { max-width: 60rem; margin: 0 auto; display: grid; gap: 56px; align-items: center; }\n  @media (min-width: 768px) { .w4bkc-wrap { grid-template-columns: 1fr 1fr; } }\n  .w4bkc-stage { position: relative; display: flex; justify-content: center; padding: 10px 0 34px; perspective: 1100px; touch-action: pan-y; }\n  .w4bkc-floor { position: absolute; left: 50%; bottom: 14px; width: 260px; height: 34px; margin-left: -130px; border-radius: 50%;\n    background: radial-gradient(closest-side, rgba(60, 40, 20, .32), rgba(60, 40, 20, 0)); }\n  .w4bkc-book { position: relative; display: block; width: 236px; height: 340px; transform-style: preserve-3d; transform: rotateY(24deg);\n    color: inherit; text-decoration: none; outline: none; will-change: transform; }\n  .w4bkc-ring { position: absolute; inset: -10px; border: 3px solid #24433d; border-radius: 10px; opacity: 0; transform: translateZ(calc(var(--w4bkc-thick) / 2 + 12px)); pointer-events: none; }\n  .w4bkc-book:focus-visible .w4bkc-ring { opacity: 1; }\n  .w4bkc-cover, .w4bkc-back { position: absolute; inset: 0; border-radius: 2px 6px 6px 2px; }\n  .w4bkc-cover { transform: translateZ(calc(var(--w4bkc-thick) / 2)); overflow: hidden; background: var(--w4bkc-cover); color: var(--w4bkc-cream);\n    box-shadow: inset 6px 0 10px -4px rgba(0, 0, 0, .45); }\n  .w4bkc-back { transform: translateZ(calc(var(--w4bkc-thick) / -2)); background: #1b322d; box-shadow: 18px 26px 40px -10px rgba(50, 30, 10, .45); }\n  .w4bkc-spine { position: absolute; left: 0; top: 0; width: var(--w4bkc-thick); height: 100%; transform-origin: left center;\n    transform: translateZ(calc(var(--w4bkc-thick) / -2)) rotateY(-90deg); background: linear-gradient(90deg, #1a302b, #2c5049 45%, #1d3631);\n    display: flex; align-items: center; justify-content: center; }\n  .w4bkc-spine span { writing-mode: vertical-rl; transform: rotate(180deg); font-family: Georgia, serif; font-size: 12px; letter-spacing: .14em; color: var(--w4bkc-cream); white-space: nowrap; }\n  .w4bkc-pages { position: absolute; right: 0; top: 3px; width: calc(var(--w4bkc-thick) - 4px); height: calc(100% - 6px); transform-origin: right center;\n    transform: translateZ(calc(var(--w4bkc-thick) / -2 + 2px)) rotateY(90deg); background: repeating-linear-gradient(90deg, #f6f0e2 0 2px, #ddd2bd 2px 3px); }\n  .w4bkc-face { position: absolute; inset: 14px; border: 1px solid rgba(243, 234, 216, .45); display: flex; flex-direction: column; padding: 22px 18px 58px; }\n  .w4bkc-series { font-size: 10px; letter-spacing: .28em; text-transform: uppercase; opacity: .8; }\n  .w4bkc-name { margin-top: 18px; font-family: Georgia, \"Times New Roman\", serif; font-size: 34px; line-height: 1.02; }\n  .w4bkc-art { margin-top: auto; width: 100%; height: 76px; }\n  .w4bkc-author { margin-top: 10px; font-size: 11px; letter-spacing: .24em; text-transform: uppercase; }\n  .w4bkc-hl { position: absolute; inset: -60%; pointer-events: none; opacity: .2;\n    background: radial-gradient(circle at 50% 50%, rgba(255, 248, 230, .5), rgba(255, 248, 230, 0) 38%); }\n  .w4bkc-band { position: absolute; left: -2px; right: -4px; bottom: 30px; height: 40px; transform: translateZ(calc(var(--w4bkc-thick) / 2 + 9px));\n    background: #c9733f; color: #fff7ec; display: flex; align-items: center; justify-content: center; font-size: 11px; letter-spacing: .2em; text-transform: uppercase;\n    box-shadow: 0 10px 14px -8px rgba(0, 0, 0, .5); }\n  .w4bkc-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .24em; text-transform: uppercase; color: #6b5a44; }\n  .w4bkc-title { margin: 14px 0 0; font-family: Georgia, \"Times New Roman\", serif; font-size: clamp(32px, 4.4vw, 46px); font-weight: 400; line-height: 1.08; }\n  .w4bkc-copy { margin: 18px 0 0; max-width: 27rem; font-size: 16px; line-height: 1.65; color: #4a4034; }\n  .w4bkc-facts { margin: 22px 0 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: 8px 22px; font-size: 14px; color: #4a4034; }\n  .w4bkc-facts b { color: var(--w4bkc-ink); font-weight: 600; }\n  .w4bkc-cta { display: inline-flex; align-items: center; min-height: 46px; margin-top: 28px; padding: 0 22px; border-radius: 999px; background: var(--w4bkc-ink);\n    color: var(--w4bkc-paper); font-size: 15px; font-weight: 600; text-decoration: none; }\n  .w4bkc-cta:hover { background: #24433d; }\n  .w4bkc-cta:focus-visible { outline: 3px solid #24433d; outline-offset: 3px; }\n  @media (prefers-reduced-motion: reduce) { .w4bkc-book { transform: rotateX(-4deg) rotateY(26deg) !important; will-change: auto; } .w4bkc-hl { transform: translate(-12%, -16%) !important; } }\n`;\n\nexport default function BookCoverTiltWithSpineAndPageDepth() {\n  const root = useRef<HTMLElement>(null);\n  const book = useRef<HTMLAnchorElement>(null);\n  const hl = useRef<HTMLSpanElement>(null);\n\n  useEffect(() => {\n    const el = root.current, card = book.current, light = hl.current;\n    if (!el || !card || !light) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    let tx = 0, ty = 0, x = 0, y = 0, idle = true, frame = 0, visible = true, timer = 0;\n    const clamp = (v: number) => Math.max(-1, Math.min(1, v));\n    const go = () => { if (!frame && visible) frame = requestAnimationFrame(tick); };\n    const aim = (e: PointerEvent) => {\n      const r = card.getBoundingClientRect();\n      tx = clamp(((e.clientX - r.left) / r.width) * 2 - 1);\n      ty = clamp(((e.clientY - r.top) / r.height) * 2 - 1);\n      idle = false; go();\n    };\n    const flat = () => { tx = 0; ty = 0; go(); };\n    const tick = (now: number) => {\n      frame = 0;\n      // idle demo: a slow sway until a real pointer, finger or focus arrives\n      if (idle) { tx = Math.sin(now / 2300) * 0.7; ty = Math.sin(now / 1700 + 1) * 0.45; }\n      x += (tx - x) * EASE; y += (ty - y) * EASE;\n      card.style.transform = `rotateX(${(-y * MAX_TILT).toFixed(2)}deg) rotateY(${(BASE_TURN + x * MAX_TILT).toFixed(2)}deg)`;\n      light.style.transform = `translate(${(x * 22).toFixed(1)}%, ${(y * 22).toFixed(1)}%)`;\n      light.style.opacity = (0.18 + 0.6 * Math.min(1, Math.hypot(x, y))).toFixed(3);\n      if (idle || Math.abs(tx - x) + Math.abs(ty - y) > 0.002) go();\n    };\n    const onMove = (e: PointerEvent) => { if (e.pointerType === \"mouse\" || e.buttons) aim(e); };\n    const onDown = (e: PointerEvent) => { clearTimeout(timer); aim(e); };\n    const onUp = (e: PointerEvent) => { if (e.pointerType !== \"mouse\") timer = window.setTimeout(flat, 700); };\n    const onLeave = (e: PointerEvent) => { if (e.pointerType === \"mouse\") flat(); };\n    const onFocus = () => { idle = false; tx = 0.5; ty = -0.4; go(); };\n    const pairs: [EventTarget, string, EventListener][] = [\n      [el, \"pointermove\", onMove as EventListener], [el, \"pointerdown\", onDown as EventListener], [el, \"pointerup\", onUp as EventListener],\n      [el, \"pointerleave\", onLeave as EventListener], [el, \"pointercancel\", flat], [card, \"focus\", onFocus], [card, \"blur\", flat],\n    ];\n    pairs.forEach(([t, n, f]) => t.addEventListener(n, f));\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      if (visible) go(); else { cancelAnimationFrame(frame); frame = 0; }\n    });\n    io.observe(el);\n    go();\n    return () => {\n      cancelAnimationFrame(frame); clearTimeout(timer); io.disconnect();\n      pairs.forEach(([t, n, f]) => t.removeEventListener(n, f));\n    };\n  }, []);\n\n  return (\n    <section ref={root} className=\"w4bkc\">\n      <style>{css}</style>\n      <div className=\"w4bkc-wrap\">\n        <div className=\"w4bkc-stage\">\n          <span className=\"w4bkc-floor\" aria-hidden=\"true\" />\n          <a ref={book} href=\"#\" className=\"w4bkc-book\" aria-label=\"The Salt Marsh Year by Edie Calloway, view the book\">\n            <span className=\"w4bkc-back\" aria-hidden=\"true\" />\n            <span className=\"w4bkc-spine\" aria-hidden=\"true\"><span>The Salt Marsh Year · Calloway</span></span>\n            <span className=\"w4bkc-pages\" aria-hidden=\"true\" />\n            <span className=\"w4bkc-cover\" aria-hidden=\"true\">\n              <span className=\"w4bkc-face\">\n                <span className=\"w4bkc-series\">Fernhill Press</span>\n                <span className=\"w4bkc-name\">The Salt Marsh Year</span>\n                <svg className=\"w4bkc-art\" viewBox=\"0 0 180 92\" fill=\"none\" stroke=\"#f3ead8\" strokeWidth=\"1.2\" strokeLinecap=\"round\">\n                  <circle cx=\"132\" cy=\"30\" r=\"16\" />\n                  <path d=\"M0 78 Q45 70 90 78 T180 76\" />\n                  <path d=\"M0 88 Q45 82 90 88 T180 86\" opacity=\".6\" />\n                  <path d=\"M22 78 L18 44 M28 78 L30 50 M34 78 L40 40 M150 76 L146 52 M156 76 L160 46\" />\n                </svg>\n                <span className=\"w4bkc-author\">Edie Calloway</span>\n              </span>\n              <span ref={hl} className=\"w4bkc-hl\" />\n            </span>\n            <span className=\"w4bkc-band\" aria-hidden=\"true\">A year of tides and weather</span>\n            <span className=\"w4bkc-ring\" aria-hidden=\"true\" />\n          </a>\n        </div>\n        <div>\n          <p className=\"w4bkc-kicker\">Fernhill Press · Autumn list</p>\n          <h2 className=\"w4bkc-title\">The Salt Marsh Year</h2>\n          <p className=\"w4bkc-copy\">Edie Calloway walked the same mile of estuary every morning for twelve months and wrote down the birds, the mud and the light. This is that notebook, set in type.</p>\n          <ul className=\"w4bkc-facts\">\n            <li><b>Hardback</b>, cloth spine</li>\n            <li><b>312</b> pages</li>\n            <li><b>£18</b></li>\n          </ul>\n          <a href=\"#\" className=\"w4bkc-cta\">Read the first chapter</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
