{
  "schemaVersion": 1,
  "target": "react",
  "name": "reactions",
  "version": "0.1.0",
  "category": "data",
  "title": "Reactions",
  "description": "Emoji reaction chips with counts under a message: press a chip to toggle your reaction, or pick from a tray and watch the emoji fly into its chip while the count rolls.",
  "tags": [
    "reactions",
    "emoji",
    "chat",
    "message",
    "counter",
    "toggle",
    "base-ui"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0",
    "motion": "^14.14.0"
  },
  "registryDependencies": [],
  "docsPath": "/components/reactions",
  "props": [
    {
      "name": "value / defaultValue",
      "type": "{ emoji, label, count, reacted? }[]",
      "description": "The reactions. label is the spoken name, such as Thumbs up; reacted marks the current user's own."
    },
    {
      "name": "onValueChange",
      "type": "(value) => void",
      "description": "Called with the next reactions when you add, toggle, or remove one. Chips that reach zero are removed."
    },
    {
      "name": "options",
      "type": "{ emoji, label }[]",
      "default": "defaultReactionOptions",
      "description": "Emoji offered by the add button's tray."
    },
    {
      "name": "readOnly",
      "type": "boolean",
      "default": "false",
      "description": "Hides the add button and disables the chips."
    },
    {
      "name": "addLabel",
      "type": "string",
      "default": "\"Add reaction\"",
      "description": "Accessible name of the add button."
    }
  ],
  "accessibility": [
    "The row is a group labeled Reactions; each chip is a toggle button with aria-pressed and a name like \"Party popper, 4 reactions\". Emoji glyphs are hidden from screen readers.",
    "The add button opens a Base UI Popover tray; focus starts on the first emoji, Left and Right Arrow, Home and End move between them, and Escape closes it and returns focus.",
    "Adding or removing a reaction is announced in a polite live region with the new total.",
    "Under reduced motion the emoji does not fly and counts change in place without rolling."
  ],
  "states": [
    "empty",
    "reacted",
    "not reacted",
    "picker open",
    "read only"
  ],
  "files": [
    {
      "path": "ui/reactions.tsx",
      "content": "\"use client\";\n\nimport { Popover as BasePopover } from \"@base-ui/react/popover\";\nimport { AnimatePresence, MotionConfig, animate, motion, useReducedMotion } from \"motion/react\";\nimport { useEffect, useLayoutEffect, useRef, useState } from \"react\";\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nexport interface Reaction {\n  emoji: string;\n  /** Spoken name, such as \"Thumbs up\". */\n  label: string;\n  count: number;\n  /** Whether the current user is one of the reactors. */\n  reacted?: boolean;\n}\n\nexport interface ReactionOption {\n  emoji: string;\n  label: string;\n}\n\nexport const defaultReactionOptions: ReactionOption[] = [\n  { emoji: \"👍\", label: \"Thumbs up\" },\n  { emoji: \"❤️\", label: \"Heart\" },\n  { emoji: \"😂\", label: \"Laughing\" },\n  { emoji: \"🎉\", label: \"Party popper\" },\n  { emoji: \"👀\", label: \"Eyes\" },\n  { emoji: \"🚀\", label: \"Rocket\" },\n];\n\nexport interface ReactionsProps {\n  /** Controlled reactions. */\n  value?: Reaction[];\n  defaultValue?: Reaction[];\n  onValueChange?: (value: Reaction[]) => void;\n  /** Emoji offered by the add button. */\n  options?: ReactionOption[];\n  /** Hides the add button, for read-only messages. */\n  readOnly?: boolean;\n  /** Label of the add button. */\n  addLabel?: string;\n  className?: string;\n}\n\ninterface Flight {\n  id: number;\n  emoji: string;\n  from: DOMRect;\n}\n\n/**\n * Emoji reaction chips with counts. Pressing a chip toggles your reaction; the add button opens a\n * tray of emoji, and the picked one flies along a short arc into its chip before the count rolls.\n * Under reduced motion the chip updates in place.\n */\nexport function Reactions({\n  value: valueProp,\n  defaultValue = [],\n  onValueChange,\n  options = defaultReactionOptions,\n  readOnly = false,\n  addLabel = \"Add reaction\",\n  className,\n}: ReactionsProps) {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLDivElement>(null);\n  const [valueState, setValueState] = useState(defaultValue);\n  const value = valueProp ?? valueState;\n  const [open, setOpen] = useState(false);\n  const [flights, setFlights] = useState<Flight[]>([]);\n  // Emoji whose increment is still in the air: their chip shows the old count until it lands.\n  const [inAir, setInAir] = useState<string[]>([]);\n  // Emoji whose chip just received a reaction, for the landing pop.\n  const [popKey, setPopKey] = useState<Record<string, number>>({});\n  const [message, setMessage] = useState(\"\");\n  const flightId = useRef(0);\n\n  const commit = (next: Reaction[]) => {\n    if (valueProp === undefined) setValueState(next);\n    onValueChange?.(next);\n  };\n\n  const pop = (emoji: string) => setPopKey((keys) => ({ ...keys, [emoji]: (keys[emoji] ?? 0) + 1 }));\n\n  const toggle = (emoji: string, from?: DOMRect) => {\n    const existing = value.find((r) => r.emoji === emoji);\n    const label = existing?.label ?? options.find((o) => o.emoji === emoji)?.label ?? emoji;\n    if (existing?.reacted) {\n      commit(\n        value\n          .map((r) => (r.emoji === emoji ? { ...r, count: r.count - 1, reacted: false } : r))\n          .filter((r) => r.count > 0),\n      );\n      setMessage(`Removed your ${label} reaction.`);\n      return;\n    }\n    const next = existing\n      ? value.map((r) => (r.emoji === emoji ? { ...r, count: r.count + 1, reacted: true } : r))\n      : [...value, { emoji, label, count: 1, reacted: true }];\n    commit(next);\n    setMessage(`You reacted with ${label}. ${(existing?.count ?? 0) + 1} in total.`);\n    if (from && !reduce) {\n      setInAir((list) => [...list, emoji]);\n      setFlights((list) => [...list, { id: ++flightId.current, emoji, from }]);\n    } else {\n      pop(emoji);\n    }\n  };\n\n  const land = (flight: Flight) => {\n    setFlights((list) => list.filter((f) => f.id !== flight.id));\n    setInAir((list) => {\n      const index = list.indexOf(flight.emoji);\n      return index === -1 ? list : [...list.slice(0, index), ...list.slice(index + 1)];\n    });\n    pop(flight.emoji);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div ref={rootRef} role=\"group\" aria-label=\"Reactions\" className={cx(\"relative flex flex-wrap items-center gap-1.5\", className)}>\n        <AnimatePresence initial={false} mode=\"popLayout\">\n          {value.map((reaction) => {\n            const pending = inAir.filter((e) => e === reaction.emoji).length;\n            const shownCount = reaction.count - pending;\n            const arriving = shownCount <= 0;\n            return (\n              <motion.button\n                key={reaction.emoji}\n                type=\"button\"\n                layout=\"position\"\n                aria-pressed={!!reaction.reacted}\n                aria-label={`${reaction.label}, ${reaction.count} ${reaction.count === 1 ? \"reaction\" : \"reactions\"}`}\n                disabled={readOnly}\n                onClick={() => toggle(reaction.emoji)}\n                initial={{ opacity: 0, scale: 0.8 }}\n                animate={{ opacity: arriving ? 0.001 : 1, scale: arriving ? 0.85 : 1 }}\n                exit={{ opacity: 0, scale: 0.85, transition: { duration: reduce ? 0 : 0.14, ease: \"easeOut\" } }}\n                transition={\n                  reduce\n                    ? { duration: 0 }\n                    : { layout: { type: \"spring\", bounce: 0, duration: 0.3 }, default: { type: \"spring\", bounce: 0.35, duration: 0.4 } }\n                }\n                className={cx(\n                  \"inline-flex h-7 items-center gap-1.5 rounded-lg border pr-2 pl-1.5 select-none\",\n                  \"transition-[background-color,border-color,color] duration-150 ease-out motion-reduce:transition-none\",\n                  \"outline-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n                  \"disabled:cursor-default\",\n                  reaction.reacted\n                    ? \"border-zinc-950/25 bg-zinc-950/[0.05] text-zinc-950 dark:border-white/30 dark:bg-white/[0.1] dark:text-white\"\n                    : \"border-zinc-950/10 bg-white text-zinc-600 enabled:hover:border-zinc-950/20 enabled:hover:bg-zinc-950/[0.04] dark:border-white/10 dark:bg-black dark:text-zinc-400 dark:enabled:hover:border-white/20 dark:enabled:hover:bg-white/[0.06]\",\n                )}\n              >\n                <motion.span\n                  key={popKey[reaction.emoji] ?? 0}\n                  data-reaction-emoji={reaction.emoji}\n                  aria-hidden=\"true\"\n                  initial={popKey[reaction.emoji] ? { scale: reduce ? 1 : 1.35 } : false}\n                  animate={{ scale: 1 }}\n                  transition={{ type: \"spring\", bounce: 0.45, duration: 0.45 }}\n                  className=\"text-[15px] leading-none\"\n                >\n                  {reaction.emoji}\n                </motion.span>\n                <RollingCount value={Math.max(shownCount, 0)} reduce={!!reduce} />\n              </motion.button>\n            );\n          })}\n          {readOnly ? null : (\n            <motion.div key=\"__add\" layout=\"position\" transition={reduce ? { duration: 0 } : { type: \"spring\", bounce: 0, duration: 0.3 }}>\n              <BasePopover.Root open={open} onOpenChange={setOpen}>\n                <BasePopover.Trigger\n                  aria-label={addLabel}\n                  className={cx(\n                    \"inline-flex size-7 items-center justify-center rounded-lg border select-none\",\n                    \"border-zinc-950/10 bg-white text-zinc-500 hover:border-zinc-950/20 hover:bg-zinc-950/[0.04] hover:text-zinc-950\",\n                    \"dark:border-white/10 dark:bg-black dark:hover:border-white/20 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n                    \"data-[popup-open]:border-zinc-950/20 data-[popup-open]:text-zinc-950 dark:data-[popup-open]:border-white/20 dark:data-[popup-open]:text-white\",\n                    \"transition-[background-color,border-color,color,scale] duration-150 ease-out motion-safe:active:scale-[0.94] motion-reduce:transition-none\",\n                    \"outline-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n                  )}\n                >\n                  <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" className=\"size-4\">\n                    <path d=\"M13.9 8.6A6 6 0 1 1 7.4 2.1\" />\n                    <path d=\"M5.5 9.75c.6.8 1.45 1.25 2.5 1.25s1.9-.45 2.5-1.25\" />\n                    <path d=\"M6 6.25v.5M10 6.25v.5\" />\n                    <path d=\"M12.5 1.75v4M10.5 3.75h4\" />\n                  </svg>\n                </BasePopover.Trigger>\n                <BasePopover.Portal>\n                  <BasePopover.Positioner side=\"top\" align=\"start\" sideOffset={8} className=\"z-50\">\n                    <BasePopover.Popup\n                      aria-label=\"Pick a reaction\"\n                      className={cx(\n                        \"flex origin-(--transform-origin) gap-0.5 rounded-xl border p-1\",\n                        \"border-zinc-950/10 bg-white shadow-[0_12px_32px_-8px_rgb(0_0_0/0.18)]\",\n                        \"dark:border-white/15 dark:bg-black dark:shadow-[0_16px_48px_-12px_rgb(0_0_0/1),inset_0_1px_0_rgb(255_255_255/0.08)]\",\n                        \"transition-[opacity,scale,translate] duration-160 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none\",\n                        \"data-[starting-style]:opacity-0 motion-safe:data-[starting-style]:translate-y-1 motion-safe:data-[starting-style]:scale-95\",\n                        \"data-[ending-style]:opacity-0 data-[ending-style]:duration-110 data-[ending-style]:ease-out\",\n                        \"outline-none\",\n                      )}\n                      onKeyDown={(event) => {\n                        const buttons = Array.from(event.currentTarget.querySelectorAll<HTMLButtonElement>(\"button\"));\n                        const index = buttons.indexOf(document.activeElement as HTMLButtonElement);\n                        const step = event.key === \"ArrowRight\" ? 1 : event.key === \"ArrowLeft\" ? -1 : 0;\n                        if (step) {\n                          event.preventDefault();\n                          buttons[(index + step + buttons.length) % buttons.length]?.focus();\n                        } else if (event.key === \"Home\" || event.key === \"End\") {\n                          event.preventDefault();\n                          buttons[event.key === \"Home\" ? 0 : buttons.length - 1]?.focus();\n                        }\n                      }}\n                    >\n                      {options.map((option) => {\n                        const mine = value.some((r) => r.emoji === option.emoji && r.reacted);\n                        return (\n                          <button\n                            key={option.emoji}\n                            type=\"button\"\n                            aria-label={option.label}\n                            aria-pressed={mine}\n                            onClick={(event) => {\n                              const from = event.currentTarget.querySelector(\"span\")!.getBoundingClientRect();\n                              setOpen(false);\n                              toggle(option.emoji, mine ? undefined : from);\n                            }}\n                            className={cx(\n                              \"group/opt inline-flex size-9 items-center justify-center rounded-lg\",\n                              \"transition-[background-color] duration-150 ease-out hover:bg-zinc-950/[0.05] dark:hover:bg-white/[0.08] motion-reduce:transition-none\",\n                              mine && \"bg-zinc-950/[0.06] dark:bg-white/[0.1]\",\n                              \"outline-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-sky-500\",\n                            )}\n                          >\n                            <span\n                              aria-hidden=\"true\"\n                              className={cx(\n                                \"text-xl leading-none\",\n                                \"transition-[scale,translate] duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none\",\n                                \"motion-safe:group-hover/opt:-translate-y-0.5 motion-safe:group-hover/opt:scale-[1.18]\",\n                                \"motion-safe:group-active/opt:scale-95\",\n                              )}\n                            >\n                              {option.emoji}\n                            </span>\n                          </button>\n                        );\n                      })}\n                    </BasePopover.Popup>\n                  </BasePopover.Positioner>\n                </BasePopover.Portal>\n              </BasePopover.Root>\n            </motion.div>\n          )}\n        </AnimatePresence>\n        <span role=\"status\" aria-live=\"polite\" className=\"sr-only\">\n          {message}\n        </span>\n        {flights.map((flight) => (\n          <FlyingEmoji key={flight.id} flight={flight} root={rootRef} onLand={() => land(flight)} />\n        ))}\n      </div>\n    </MotionConfig>\n  );\n}\n\n/**\n * The picked emoji, tossed along a quadratic arc from the tray to its chip. Positioned inside the\n * reactions row and re-aimed every frame, so a chip that shifts while it flies still catches it.\n */\nfunction FlyingEmoji({ flight, root, onLand }: { flight: Flight; root: React.RefObject<HTMLDivElement | null>; onLand: () => void }) {\n  const ref = useRef<HTMLSpanElement>(null);\n  const landRef = useRef(onLand);\n  landRef.current = onLand;\n\n  useLayoutEffect(() => {\n    const el = ref.current;\n    const host = root.current;\n    if (!el || !host) return;\n    const origin = host.getBoundingClientRect();\n    // The start stays fixed in the row's coordinates; the end is measured each frame.\n    const start = { x: flight.from.left + flight.from.width / 2 - origin.left, y: flight.from.top + flight.from.height / 2 - origin.top };\n    const target = () => {\n      const base = host.getBoundingClientRect();\n      const r = host.querySelector<HTMLElement>(`[data-reaction-emoji=\"${flight.emoji}\"]`)?.getBoundingClientRect();\n      return r ? { x: r.left + r.width / 2 - base.left, y: r.top + r.height / 2 - base.top } : start;\n    };\n    const first = target();\n    const lift = 28 + Math.hypot(first.x - start.x, first.y - start.y) * 0.35;\n    const place = (t: number) => {\n      const end = target();\n      const control = { x: (start.x + end.x) / 2, y: Math.min(start.y, end.y) - lift };\n      const u = 1 - t;\n      const x = u * u * start.x + 2 * u * t * control.x + t * t * end.x;\n      const y = u * u * start.y + 2 * u * t * control.y + t * t * end.y;\n      el.style.transform = `translate(${x}px, ${y}px) translate(-50%, -50%) scale(${1.35 - 0.35 * t}) rotate(${-14 * u}deg)`;\n    };\n    place(0);\n    const controls = animate(0, 1, {\n      duration: 0.56,\n      ease: [0.35, 0, 0.25, 1],\n      onUpdate: place,\n      onComplete: () => landRef.current(),\n    });\n    return () => controls.stop();\n  }, [flight, root]);\n\n  return (\n    <span ref={ref} aria-hidden=\"true\" className=\"pointer-events-none absolute top-0 left-0 z-10 text-[15px] leading-none will-change-transform\">\n      {flight.emoji}\n    </span>\n  );\n}\n\n/** A count whose changed digits roll: up when it grows, down when it shrinks. */\nfunction RollingCount({ value, reduce }: { value: number; reduce: boolean }) {\n  const previous = useRef(value);\n  const direction = value >= previous.current ? 1 : -1;\n  useEffect(() => {\n    previous.current = value;\n  }, [value]);\n  const digits = String(value).split(\"\");\n\n  return (\n    <span className=\"inline-flex font-mono text-xs leading-4 tabular-nums\">\n      {digits.map((digit, i) => (\n        <span key={digits.length - i} className=\"relative inline-flex h-4 overflow-hidden\">\n          <AnimatePresence initial={false} mode=\"popLayout\" custom={direction}>\n            <motion.span\n              key={digit}\n              custom={direction}\n              variants={{\n                enter: (d: number) => ({ y: reduce ? 0 : `${d * 100}%`, opacity: reduce ? 1 : 0 }),\n                center: { y: 0, opacity: 1 },\n                exit: (d: number) => ({ y: reduce ? 0 : `${-d * 100}%`, opacity: 0 }),\n              }}\n              initial=\"enter\"\n              animate=\"center\"\n              exit=\"exit\"\n              transition={reduce ? { duration: 0 } : { type: \"spring\", bounce: 0.2, duration: 0.32 }}\n              className=\"inline-block\"\n            >\n              {digit}\n            </motion.span>\n          </AnimatePresence>\n        </span>\n      ))}\n    </span>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { Reactions } from \"@/components/shaken/ui/reactions\";\n\nexport default function ReactionsExample() {\n  return (\n    <article className=\"grid w-full max-w-md grid-cols-[auto_1fr] gap-x-3 gap-y-2\">\n      <span\n        aria-hidden=\"true\"\n        className=\"row-span-3 flex size-8 items-center justify-center rounded-full bg-zinc-950/[0.06] text-xs font-medium text-zinc-700 dark:bg-white/[0.1] dark:text-zinc-300\"\n      >\n        MO\n      </span>\n      <p className=\"flex items-baseline gap-2 text-sm\">\n        <span className=\"font-semibold text-zinc-950 dark:text-white\">Mara Okafor</span>\n        <time className=\"text-xs text-zinc-500\">10:42</time>\n      </p>\n      <p className=\"text-sm leading-relaxed text-zinc-700 dark:text-zinc-300\">\n        The eval suite is green on the new retrieval prompt: answer accuracy went from 81% to 88% with no extra latency. Shipping it\n        behind the flag this afternoon.\n      </p>\n      <Reactions\n        className=\"pt-1\"\n        defaultValue={[\n          { emoji: \"🎉\", label: \"Party popper\", count: 4 },\n          { emoji: \"🚀\", label: \"Rocket\", count: 2, reacted: true },\n          { emoji: \"👀\", label: \"Eyes\", count: 1 },\n        ]}\n      />\n    </article>\n  );\n}\n"
  }
}
