{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bookmark-button","type":"registry:component","title":"Bookmark button","description":"Fills from the bottom up and lands with a small squash.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/bookmark-button.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useState } from \"react\";\nimport { AnimatePresence, motion, useAnimate } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst BOOKMARK =\n  \"M6.5 4.75A1.75 1.75 0 0 1 8.25 3h7.5a1.75 1.75 0 0 1 1.75 1.75v15.07a.5.5 0 0 1-.79.41L12 16.75l-4.71 3.48a.5.5 0 0 1-.79-.41Z\";\n\n// Swaps ride the same no-bounce spring as the lab's icon swaps.\nconst SWAP = { type: \"spring\", duration: 0.3, bounce: 0 } as const;\n\nexport function BookmarkButton({\n  saved,\n  onSavedChange,\n  count,\n  label = \"Save\",\n  savedLabel = \"Saved\",\n  className,\n}: {\n  saved: boolean;\n  onSavedChange: (saved: boolean) => void;\n  /** Shows a running count instead of the Save / Saved label. */\n  count?: number;\n  label?: string;\n  savedLabel?: string;\n  className?: string;\n}) {\n  const reduceMotion = useReducedMotion();\n  const [scope, animate] = useAnimate<HTMLSpanElement>();\n  // Empty until the first toggle, so nothing is announced on load.\n  const [note, setNote] = useState(\"\");\n  const countId = useId();\n\n  const toggle = () => {\n    const next = !saved;\n    onSavedChange(next);\n    setNote(next ? \"Saved\" : \"Removed from saved\");\n    if (reduceMotion) return;\n    if (next) {\n      // The ribbon lifts and stretches a touch, then lands with a squash as\n      // the fill tops out (0.65 x 400ms is about when the fill finishes).\n      // Kept within 0.95 to 1.05 so it reads as weight, not rubber.\n      animate(\n        scope.current,\n        {\n          y: [0, -2, 1, 0],\n          scaleX: [1, 0.97, 1.04, 1],\n          scaleY: [1, 1.04, 0.95, 1],\n        },\n        {\n          duration: 0.4,\n          times: [0, 0.35, 0.65, 1],\n          ease: [\"easeOut\", \"easeIn\", \"easeOut\"],\n        },\n      );\n    } else {\n      // Unsaving cancels any bounce still in flight and settles fast.\n      animate(\n        scope.current,\n        { y: 0, scaleX: 1, scaleY: 1 },\n        { duration: 0.15, ease: [0.23, 1, 0.32, 1] },\n      );\n    }\n  };\n\n  const hasCount = count !== undefined;\n\n  return (\n    <>\n      <button\n        type=\"button\"\n        aria-pressed={saved}\n        aria-label={label}\n        aria-describedby={hasCount ? countId : undefined}\n        onClick={toggle}\n        className={cn(\n          \"inline-flex h-10 touch-manipulation items-center gap-2 rounded-full bg-muted text-sm font-medium text-foreground shadow-raised outline-hidden select-none\",\n          \"transition-[scale,background-color] duration-150 ease-out hover:bg-background active:scale-[0.96] motion-reduce:transition-[background-color]\",\n          \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground\",\n          // 4px less on the icon side: the glyph's own bearings fill it.\n          \"pr-4 pl-3\",\n          className,\n        )}\n      >\n        <span\n          ref={scope}\n          aria-hidden\n          // Squashes from its base, like something landing.\n          className=\"grid origin-bottom\"\n        >\n          <svg\n            viewBox=\"0 0 24 24\"\n            className=\"col-start-1 row-start-1 size-5\"\n            fill=\"none\"\n            stroke=\"currentColor\"\n            strokeWidth={1.75}\n            strokeLinejoin=\"round\"\n          >\n            <path d={BOOKMARK} />\n          </svg>\n          {/* The shape spans 12.5% to 84% of the 24px box, so the clip runs\n              between those edges; the full 0 to 100% would waste the first\n              and last parts of every transition on empty space. Fills in\n              260ms, empties in 150ms: saving is the moment worth showing. */}\n          <svg\n            viewBox=\"0 0 24 24\"\n            className={cn(\n              \"col-start-1 row-start-1 size-5 transition-[clip-path]\",\n              saved\n                ? \"[clip-path:inset(12%_0_0_0)] duration-[260ms] ease-[cubic-bezier(0.23,1,0.32,1)]\"\n                : \"[clip-path:inset(85%_0_0_0)] duration-150 ease-out\",\n              \"motion-reduce:duration-0\",\n            )}\n            fill=\"currentColor\"\n          >\n            <path d={BOOKMARK} />\n          </svg>\n        </span>\n\n        {hasCount ? (\n          <Count value={count} reduceMotion={reduceMotion} id={countId} />\n        ) : (\n          // One grid cell, so the button holds the longer label's width and\n          // never jumps when they swap.\n          <span aria-hidden className=\"grid\">\n            <Label visible={!saved}>{label}</Label>\n            <Label visible={saved}>{savedLabel}</Label>\n          </span>\n        )}\n      </button>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {note}\n      </span>\n    </>\n  );\n}\n\n// Enters over 200ms, leaves in 100ms: the old word is gone before the new\n// one is legible, so the two never read as overlapping text.\nfunction Label({\n  visible,\n  children,\n}: {\n  visible: boolean;\n  children: React.ReactNode;\n}) {\n  return (\n    <span\n      className={cn(\n        \"col-start-1 row-start-1 transition-[opacity,filter,translate] ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-[opacity]\",\n        visible\n          ? \"translate-y-0 opacity-100 blur-[0px] duration-200\"\n          : \"translate-y-0.5 opacity-0 blur-[4px] duration-100 motion-reduce:translate-y-0 motion-reduce:blur-[0px]\",\n      )}\n    >\n      {children}\n    </span>\n  );\n}\n\nfunction Count({\n  value,\n  reduceMotion,\n  id,\n}: {\n  value: number;\n  reduceMotion: boolean | null;\n  id: string;\n}) {\n  // Holds the last value between renders to tell up from down.\n  const [prev, setPrev] = useState(value);\n  const [dir, setDir] = useState(1);\n  if (prev !== value) {\n    setDir(value > prev ? 1 : -1);\n    setPrev(value);\n  }\n  // Up comes in from below and leaves upward, like a counter rolling.\n  const shift = reduceMotion ? 0 : 6;\n\n  return (\n    <span className=\"relative inline-flex justify-end tabular-nums\">\n      <span id={id} className=\"sr-only\">\n        {value} saves\n      </span>\n      <AnimatePresence mode=\"popLayout\" initial={false} custom={dir}>\n        <motion.span\n          key={value}\n          aria-hidden\n          custom={dir}\n          variants={{\n            enter: (d: number) => ({\n              y: d * shift,\n              opacity: 0,\n              filter: reduceMotion ? \"blur(0px)\" : \"blur(4px)\",\n            }),\n            center: { y: 0, opacity: 1, filter: \"blur(0px)\" },\n            // Exits travel half as far and finish sooner than entries.\n            exit: (d: number) => ({\n              y: (-d * shift) / 2,\n              opacity: 0,\n              filter: reduceMotion ? \"blur(0px)\" : \"blur(4px)\",\n              transition: { duration: 0.12, ease: [0.23, 1, 0.32, 1] },\n            }),\n          }}\n          initial=\"enter\"\n          animate=\"center\"\n          exit=\"exit\"\n          transition={SWAP}\n        >\n          {value}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\nexport default function BookmarkButtonDemo() {\n  const [articleSaved, setArticleSaved] = useState(false);\n  const [labelSaved, setLabelSaved] = useState(false);\n  // Someone else's saves; yours adds one on top.\n  const base = 127;\n\n  return (\n    <div className=\"flex w-[min(440px,100%)] flex-col items-center gap-6\">\n      <article className=\"w-full rounded-2xl bg-muted p-5 shadow-raised\">\n        <p className=\"text-xs text-muted-foreground\">Essay</p>\n        <h3 className=\"mt-1 text-base font-medium text-balance text-foreground\">\n          Notes on interruptible motion\n        </h3>\n        <p className=\"mt-1.5 text-sm text-pretty text-muted-foreground\">\n          Why a transition that can change its mind halfway feels faster than\n          one that always runs to the end.\n        </p>\n        <div className=\"mt-4 flex items-center justify-between gap-4\">\n          <span className=\"text-sm text-muted-foreground\">6 min read</span>\n          <BookmarkButton\n            saved={articleSaved}\n            onSavedChange={setArticleSaved}\n            count={base + (articleSaved ? 1 : 0)}\n            className=\"bg-background hover:bg-background/60\"\n          />\n        </div>\n      </article>\n      <BookmarkButton saved={labelSaved} onSavedChange={setLabelSaved} />\n    </div>\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["buttons"],"docs":"From ui lab: https://lab.xevrion.dev/lab/bookmark-button"}