{
  "name": "bookshelf",
  "type": "registry:ui",
  "title": "Bookshelf",
  "description": "A single-row bookshelf with spring hover lift, flat cover reveals, keyboard selection, and horizontal scrolling on narrow screens.",
  "author": "Aditya Ojha <https://akoder.xyz>",
  "categories": [
    "books",
    "animation",
    "interactive",
    "accessibility"
  ],
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "meta": {
    "docs": "https://akoder.xyz/components/bookshelf",
    "source": "https://github.com/AdityaKodez/adityaojha/blob/main/components/ui/bookshelf.tsx"
  },
  "files": [
    {
      "path": "components/ui/bookshelf.tsx",
      "content": "\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { useEffect, useRef, useState, type CSSProperties } from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { reveal, revealDelay } from \"@/lib/motion\";\nimport styles from \"./bookshelf.module.css\";\n\nconst TRANSITION = { duration: 0.38, ease: [0.22, 1, 0.36, 1] as const };\nconst LIFT_TRANSITION = {\n  type: \"spring\",\n  stiffness: 340,\n  damping: 25,\n  mass: 0.7,\n} as const;\n\nexport interface BookshelfBook {\n  id: string;\n  title: string;\n  author: string;\n  cover: { src: string; width: number; height: number; alt: string };\n  spine: { src: string; height: number; width: number };\n}\n\nexport interface BookshelfProps {\n  /** Books render in the supplied order. IDs must be unique and stable. */\n  books: readonly BookshelfBook[];\n  /** Initially open book. Missing or unknown IDs start with every book closed. */\n  defaultBookId?: string | null;\n  label?: string;\n  emptyMessage?: string;\n  coverUnavailableMessage?: string;\n  /** Closed-book hover lift in pixels, clamped to 0 through 20. */\n  liftAmount?: number;\n  /** Optional once-only entrance for shelves below the fold. */\n  revealOnScroll?: boolean;\n  className?: string;\n}\n\nfunction BookCover({\n  book,\n  unavailableMessage,\n}: {\n  book: BookshelfBook;\n  unavailableMessage: string;\n}) {\n  const [failed, setFailed] = useState(false);\n\n  return (\n    <span className={styles.cover}>\n      {failed ? (\n        <span className={styles.fallback}>\n          <span>{book.title}</span>\n          <span>{book.author}</span>\n          <span>{unavailableMessage}</span>\n        </span>\n      ) : (\n        // Consumer artwork needs no Next.js loader or domain configuration.\n        // eslint-disable-next-line @next/next/no-img-element\n        <img\n          src={book.cover.src}\n          width={book.cover.width}\n          height={book.cover.height}\n          alt={book.cover.alt}\n          loading=\"eager\"\n          decoding=\"sync\"\n          className={styles.coverImage}\n          onError={() => setFailed(true)}\n        />\n      )}\n    </span>\n  );\n}\n\nexport function Bookshelf({\n  books,\n  defaultBookId = null,\n  label = \"Bookshelf\",\n  emptyMessage = \"More books will find their way here.\",\n  coverUnavailableMessage = \"Cover unavailable\",\n  liftAmount = 10,\n  revealOnScroll = false,\n  className,\n}: BookshelfProps) {\n  const [selectedId, setSelectedId] = useState<string | null>(() =>\n    books.some((book) => book.id === defaultBookId) ? defaultBookId : null,\n  );\n  const activeId = books.some((book) => book.id === selectedId)\n    ? selectedId\n    : null;\n  const [hoveredId, setHoveredId] = useState<string | null>(null);\n  const root = useRef<HTMLDivElement>(null);\n  const rail = useRef<HTMLDivElement>(null);\n  const reduceMotion = useReducedMotion();\n  const instant = reduceMotion !== false;\n  const transition = instant ? { duration: 0 } : TRANSITION;\n  const lift = Number.isFinite(liftAmount)\n    ? Math.min(20, Math.max(0, liftAmount))\n    : 10;\n\n  useEffect(() => {\n    function dismissOutside(event: PointerEvent) {\n      const target = event.target;\n      if (\n        target instanceof Element &&\n        root.current?.contains(target) &&\n        target.closest(\"[data-bookshelf-book]\")\n      ) {\n        return;\n      }\n      // The header floats over the page; pressing its controls (theme,\n      // search, GitHub, menu) must not close an open book.\n      if (target instanceof Element && target.closest(\"header\")) {\n        return;\n      }\n      setSelectedId(null);\n    }\n\n    function dismissOnEscape(event: KeyboardEvent) {\n      if (event.key !== \"Escape\") return;\n      setSelectedId(null);\n      setHoveredId(null);\n    }\n\n    document.addEventListener(\"pointerdown\", dismissOutside);\n    document.addEventListener(\"keydown\", dismissOnEscape);\n    return () => {\n      document.removeEventListener(\"pointerdown\", dismissOutside);\n      document.removeEventListener(\"keydown\", dismissOnEscape);\n    };\n  }, []);\n\n  useEffect(() => {\n    if (!activeId) return;\n    const book = root.current?.querySelector<HTMLElement>(\n      `[data-bookshelf-book=\"${CSS.escape(activeId)}\"]`,\n    );\n    const position = book?.parentElement;\n    const railElement = rail.current;\n    if (!position || !railElement) return;\n    const max = railElement.scrollWidth - railElement.clientWidth;\n    const centered =\n      position.offsetLeft -\n      (railElement.clientWidth - position.offsetWidth) / 2;\n    const next = Math.max(0, Math.min(max, centered));\n    if (Math.abs(next - railElement.scrollLeft) > 1) {\n      railElement.scrollLeft = next;\n    }\n  }, [activeId, books]);\n\n  if (books.length === 0) {\n    return (\n      <div\n        role=\"group\"\n        aria-label={label}\n        className={cn(styles.shelf, className)}\n      >\n        <p className=\"px-6 py-16 text-sm text-muted-foreground\">\n          {emptyMessage}\n        </p>\n      </div>\n    );\n  }\n\n  return (\n    <div\n      ref={root}\n      role=\"group\"\n      aria-label={label}\n      className={cn(styles.shelf, className)}\n    >\n      <noscript>\n        <style>{`.${styles.reveal} { opacity: 1 !important; transform: none !important; }`}</style>\n      </noscript>\n      <div ref={rail} className={styles.rail}>\n        <div className={styles.books}>\n          {books.map((book, index) => {\n            const active = activeId === book.id;\n            const bookStyle = {\n              \"--book-height\": `${book.spine.height}px`,\n              \"--spine-width\": `${book.spine.width}px`,\n              \"--cover-ratio\": book.cover.width / book.cover.height,\n            } as CSSProperties;\n\n            return (\n              <motion.div\n                key={book.id}\n                className={styles.reveal}\n                {...(revealOnScroll && !reduceMotion\n                  ? reveal({\n                      y: 28,\n                      delay: revealDelay(index, 0.06, 0.36),\n                      margin: \"0px 0px -120px 0px\",\n                      amount: 0.3,\n                    })\n                  : { initial: false })}\n              >\n                <motion.button\n                  type=\"button\"\n                  layout=\"position\"\n                  transition={transition}\n                  data-bookshelf-book={book.id}\n                  data-open={active}\n                  className={styles.book}\n                  style={bookStyle}\n                  aria-label={`${book.title}, by ${book.author}`}\n                  aria-pressed={active}\n                  onPointerEnter={(event) => {\n                    if (\n                      event.pointerType === \"mouse\" &&\n                      window.matchMedia(\"(hover: hover) and (pointer: fine)\")\n                        .matches\n                    ) {\n                      setHoveredId(book.id);\n                    }\n                  }}\n                  onPointerLeave={() => setHoveredId(null)}\n                  onClick={() =>\n                    setSelectedId((current) =>\n                      current === book.id ? null : book.id,\n                    )\n                  }\n                >\n                  <motion.span\n                    className={styles.artwork}\n                    initial={false}\n                    animate={{\n                      y:\n                        !instant && !active && hoveredId === book.id\n                          ? -lift\n                          : 0,\n                    }}\n                    transition={\n                      instant\n                        ? { duration: 0 }\n                        : active\n                          ? TRANSITION\n                          : LIFT_TRANSITION\n                    }\n                  >\n                    <motion.span\n                      className={styles.volume}\n                      initial={false}\n                      animate={{ opacity: active ? 1 : 0 }}\n                      transition={{ duration: instant ? 0 : 0.18 }}\n                      aria-hidden=\"true\"\n                    >\n                      <BookCover\n                        key={book.cover.src}\n                        book={book}\n                        unavailableMessage={coverUnavailableMessage}\n                      />\n                    </motion.span>\n                    <motion.span\n                      className={styles.spine}\n                      initial={false}\n                      animate={{ opacity: active ? 0 : 1 }}\n                      transition={{ duration: instant ? 0 : 0.18 }}\n                      aria-hidden=\"true\"\n                    >\n                      {/* Artwork uses the supplied URL without a framework image loader. */}\n                      {/* eslint-disable-next-line @next/next/no-img-element */}\n                      <img\n                        src={book.spine.src}\n                        alt=\"\"\n                        width={book.spine.width}\n                        height={book.spine.height}\n                        loading=\"eager\"\n                        className={styles.spineImage}\n                      />\n                    </motion.span>\n                  </motion.span>\n                </motion.button>\n              </motion.div>\n            );\n          })}\n        </div>\n      </div>\n      <div className={styles.baseline} aria-hidden=\"true\" />\n    </div>\n  );\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/bookshelf.module.css",
      "content": ".shelf {\n  --book-scale: 1;\n  margin: 0 auto;\n  padding: 8px 0 24px;\n}\n\n.rail {\n  position: relative;\n  overflow-x: auto;\n  overscroll-behavior-x: contain;\n  /* A cover opening changes the row width, so the bar would flicker for the\n     length of the animation. The shelf still scrolls by wheel, trackpad, touch,\n     and keyboard focus. */\n  scrollbar-width: none;\n}\n\n.rail::-webkit-scrollbar {\n  display: none;\n}\n\n.books {\n  display: flex;\n  align-items: flex-end;\n  gap: 3px;\n  width: max-content;\n  min-height: calc(320px * var(--book-scale));\n  margin: 0 auto;\n  padding: 24px 32px 0;\n}\n\n.book {\n  --depth: calc(var(--spine-width) * var(--book-scale));\n  position: relative;\n  display: block;\n  flex: 0 0 auto;\n  width: calc(var(--depth) + 4px);\n  height: calc(var(--book-height) * var(--book-scale));\n  padding: 0;\n  cursor: pointer;\n  -webkit-tap-highlight-color: transparent;\n  transition-property: none;\n  outline: none;\n}\n\n.reveal {\n  flex: 0 0 auto;\n}\n\n.book[data-open=\"true\"] {\n  width: calc(\n    var(--book-height) * var(--book-scale) * var(--cover-ratio) + 5px\n  );\n  z-index: 1;\n}\n\n.book:focus-visible {\n  outline: 2px solid var(--ring, currentColor);\n  outline-offset: 5px;\n  border-radius: var(--radius-sm, 0.25rem);\n}\n\n.artwork {\n  position: absolute;\n  inset: 0;\n  display: block;\n  overflow: clip;\n  pointer-events: none;\n}\n\n.volume {\n  position: absolute;\n  inset: 0 auto 0 2px;\n  display: block;\n  width: calc(var(--book-height) * var(--book-scale) * var(--cover-ratio));\n  pointer-events: none;\n}\n\n.cover,\n.spine {\n  position: absolute;\n  display: block;\n}\n\n.cover {\n  inset: 0;\n  overflow: hidden;\n  background: var(--card, #fafafa);\n  border-radius: 0 calc(var(--radius-sm, 0.25rem) / 2)\n    calc(var(--radius-sm, 0.25rem) / 2) 0;\n}\n\n.coverImage,\n.spineImage {\n  display: block;\n  width: 100%;\n  height: 100%;\n}\n\n.coverImage {\n  object-fit: cover;\n}\n\n.spine {\n  inset: 0 auto 0 2px;\n  width: var(--depth);\n  overflow: hidden;\n  border-radius: calc(var(--radius-sm, 0.25rem) / 2);\n  pointer-events: none;\n}\n\n.cover::after,\n.spine::after {\n  content: \"\";\n  position: absolute;\n  inset: 0;\n  border-radius: inherit;\n  box-shadow: inset 0 0 0 1px var(--border, #d4d4d4);\n  pointer-events: none;\n}\n\n.baseline {\n  margin: 0 24px;\n  height: 8px;\n  border-radius: var(--radius-lg, 0.5rem);\n  background: var(--border, #d4d4d4);\n}\n\n.fallback {\n  display: flex;\n  height: 100%;\n  flex-direction: column;\n  justify-content: center;\n  gap: 10px;\n  padding: 10px;\n  background: var(--secondary, #f5f5f5);\n  color: var(--secondary-foreground, #171717);\n  font-size: 12px;\n  line-height: 1.4;\n  text-align: center;\n}\n\n.fallback span:last-child {\n  color: var(--muted-foreground, #737373);\n  font-size: 10px;\n}\n\n@media (prefers-reduced-motion: reduce) {\n  .reveal {\n    opacity: 1 !important;\n    transform: none !important;\n  }\n}\n\n@media (prefers-reduced-motion: reduce), (hover: none), (pointer: coarse) {\n  .artwork {\n    transform: none !important;\n  }\n}\n\n@media (width < 480px) {\n  .shelf {\n    --book-scale: 0.78;\n    padding-top: 28px;\n  }\n\n  .books {\n    gap: 2px;\n    padding-inline: 24px;\n  }\n\n  .baseline {\n    margin-inline: 0;\n    border-radius: var(--radius, 0.45rem);\n  }\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "lib/motion.ts",
      "content": "import type { MotionProps, Transition, Variants } from \"motion/react\";\n\n/**\n * Shared entrance motion for the site.\n *\n * The hero greeting sets the reference: a short fade with a small rise.\n * Everything that enters on scroll speaks the same language so the page reads\n * as one system rather than a dozen separate reveals.\n *\n * Timing is deliberately split per value, which is what makes it feel snappy\n * rather than slow:\n *   - opacity lands first (fast), so nothing lingers as a ghost\n *   - y settles on a critically damped spring, so it stops without wobble\n *\n * Only `opacity` and `y` are animated. Filter-based reveals were removed:\n * animating `filter: blur()` cannot be composited and forces the browser to\n * re-rasterize the whole subtree on every frame, which shows up as forced\n * reflow and scroll jank on section-sized content.\n *\n * Reduced motion: site-internal usage relies on the `[data-reveal]` rule in\n * `app/globals.css`, which pins the resolved state with `!important` and so\n * beats Motion's inline styles. That keeps these helpers hook-free and avoids\n * hydration mismatches. Registry components cannot depend on this file or that\n * stylesheet, so they gate on `useReducedMotion()` locally instead.\n */\n\nexport const MOTION_EASE = [0.22, 1, 0.36, 1] as const;\n\n/** Damping ratio is ~1.03, so `y` never overshoots into a bounce. */\nconst REVEAL_SPRING = {\n  type: \"spring\",\n  stiffness: 420,\n  damping: 34,\n  mass: 0.7,\n} satisfies Transition;\n\nconst OPACITY_DURATION = 0.18;\n\nexport type RevealOptions = {\n  /** Rise distance in px. Keep it small: this is a settle, not a slide. */\n  y?: number;\n  /** Seconds to hold before starting. Use `revealDelay()` for lists. */\n  delay?: number;\n  /** Intersection root margin for scroll-triggered reveals. */\n  margin?: string;\n  /** How much of the element must be visible before it fires. */\n  amount?: \"some\" | \"all\" | number;\n};\n\ntype RevealProps = Pick<\n  MotionProps,\n  \"initial\" | \"animate\" | \"whileInView\" | \"viewport\" | \"transition\"\n> & {\n  \"data-reveal\": \"\";\n};\n\nfunction hiddenState(y: number) {\n  return { opacity: 0, y };\n}\n\nfunction resolvedState() {\n  return { opacity: 1, y: 0 };\n}\n\nfunction revealTransition(delay: number): Transition {\n  return {\n    default: { ...REVEAL_SPRING, delay },\n    opacity: { duration: OPACITY_DURATION, ease: MOTION_EASE, delay },\n  };\n}\n\n/**\n * Scroll-triggered fade + rise. Fires once.\n *\n * Put the reveal on whatever the eye actually reads: text, rows, cards. When a\n * section animates its own children, let the section wrapper rise without its\n * own delay so entrances never stack.\n */\nexport function reveal({\n  y = 8,\n  delay = 0,\n  margin = \"-40px\",\n  amount,\n}: RevealOptions = {}): RevealProps {\n  return {\n    \"data-reveal\": \"\",\n    initial: hiddenState(y),\n    whileInView: resolvedState(),\n    viewport: { once: true, margin, amount },\n    transition: revealTransition(delay),\n  };\n}\n\n/**\n * Same reveal, driven by mount instead of scroll. For content that appears in\n * response to an interaction, such as rows inside a section that just expanded.\n */\nexport function revealOnMount({\n  y = 8,\n  delay = 0,\n}: Omit<RevealOptions, \"margin\" | \"amount\"> = {}): RevealProps {\n  return {\n    \"data-reveal\": \"\",\n    initial: hiddenState(y),\n    animate: resolvedState(),\n    transition: revealTransition(delay),\n  };\n}\n\n/**\n * Variant pair for parent-driven staggers, where the container owns the\n * sequencing via `staggerChildren`.\n */\nexport function revealVariants({\n  y = 8,\n}: Pick<RevealOptions, \"y\"> = {}): Variants {\n  return {\n    hidden: hiddenState(y),\n    visible: {\n      ...resolvedState(),\n      transition: revealTransition(0),\n    },\n  };\n}\n\n/**\n * Capped stagger for lists. The cap matters: without it a long list leaves the\n * last item waiting a second to appear, which reads as sluggish.\n */\nexport function revealDelay(index: number, step = 0.04, max = 0.2) {\n  return Math.min(index * step, max);\n}\n",
      "type": "registry:lib"
    }
  ],
  "docs": "## Installation\n\n```bash\nnpx shadcn add @akoder/bookshelf\n```\n\nPass your own book data and cover/spine image URLs to Bookshelf. No portfolio config or artwork is required. The CSS module and motion helper are installed together. See https://akoder.xyz/components/bookshelf for a complete usage example and props reference."
}