{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"reaction-bar","type":"registry:ui","title":"Reaction Bar","description":"A reaction button whose emoji tray springs open on hover or click. The emoji under the pointer lifts and names itself, and the one you pick pops onto the button. Arrow keys and Escape work.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/reaction-bar.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { useEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface Reaction {\n  emoji: string;\n  label: string;\n}\n\nexport interface ReactionBarProps {\n  reactions?: Reaction[];\n  /** Controlled selection (the reaction's label), or null for none. */\n  value?: string | null;\n  defaultValue?: string | null;\n  onChange?: (label: string | null) => void;\n  /** Button text while nothing is picked. */\n  label?: string;\n  /** Open the tray on hover as well as on click. */\n  hover?: boolean;\n  className?: string;\n}\n\nconst DEFAULTS: Reaction[] = [\n  { emoji: \"👍\", label: \"Like\" },\n  { emoji: \"❤️\", label: \"Love\" },\n  { emoji: \"😂\", label: \"Haha\" },\n  { emoji: \"😮\", label: \"Wow\" },\n  { emoji: \"😢\", label: \"Sad\" },\n  { emoji: \"🎉\", label: \"Party\" },\n];\n\n/**\n * A reaction button with a tray of emoji that springs open above it. The emoji\n * under the pointer lifts and grows, and the one you pick pops onto the button.\n */\nexport function ReactionBar({\n  reactions = DEFAULTS,\n  value,\n  defaultValue = null,\n  onChange,\n  label = \"React\",\n  hover = true,\n  className,\n}: ReactionBarProps) {\n  const [inner, setInner] = useState<string | null>(defaultValue);\n  const [open, setOpen] = useState(false);\n  const [hot, setHot] = useState<number | null>(null);\n  const timer = useRef<number | undefined>(undefined);\n  const tray = useRef<HTMLDivElement>(null);\n  const trigger = useRef<HTMLButtonElement>(null);\n  // Only a click or key press moves focus into the tray; hovering just shows it.\n  const focusTray = useRef(false);\n  const current = value === undefined ? inner : value;\n  const picked = reactions.find((r) => r.label === current);\n\n  useEffect(() => () => window.clearTimeout(timer.current), []);\n  useEffect(() => {\n    if (open && focusTray.current) tray.current?.querySelector<HTMLElement>(\"button\")?.focus({ preventScroll: true });\n    focusTray.current = false;\n  }, [open]);\n\n  const choose = (next: string | null) => {\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n    setOpen(false);\n    setHot(null);\n  };\n  const later = (fn: () => void, ms: number) => {\n    window.clearTimeout(timer.current);\n    timer.current = window.setTimeout(fn, ms);\n  };\n\n  const onTrayKey = (e: KeyboardEvent) => {\n    const els = [...(tray.current?.querySelectorAll<HTMLElement>(\"button\") ?? [])];\n    const i = els.indexOf(document.activeElement as HTMLElement);\n    if (e.key === \"ArrowRight\") els[(i + 1) % els.length]?.focus();\n    else if (e.key === \"ArrowLeft\") els[(i - 1 + els.length) % els.length]?.focus();\n    else if (e.key === \"Escape\") {\n      setOpen(false);\n      trigger.current?.focus();\n    } else return;\n    e.preventDefault();\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div\n        className={cn(\"relative inline-block\", className)}\n        onPointerEnter={(e) => hover && e.pointerType === \"mouse\" && later(() => setOpen(true), 350)}\n        onPointerLeave={(e) => hover && e.pointerType === \"mouse\" && later(() => setOpen(false), 300)}\n        onBlur={(e) => !e.currentTarget.contains(e.relatedTarget as Node) && setOpen(false)}\n      >\n        <AnimatePresence>\n          {open && (\n            <motion.div\n              ref={tray}\n              role=\"toolbar\"\n              aria-label=\"Reactions\"\n              onKeyDown={onTrayKey}\n              className=\"absolute bottom-full left-0 z-50 mb-2 flex items-end gap-0.5 rounded-full border border-border bg-card px-1.5 py-1 shadow-2xl\"\n              style={{ transformOrigin: \"20px 100%\" }}\n              initial={{ opacity: 0, scale: 0.6, y: 8 }}\n              animate={{ opacity: 1, scale: 1, y: 0 }}\n              exit={{ opacity: 0, scale: 0.8, y: 6, transition: { duration: 0.15 } }}\n              transition={{ type: \"spring\", bounce: 0.35, duration: 0.4 }}\n            >\n              {reactions.map((r, i) => (\n                <motion.button\n                  key={r.label}\n                  type=\"button\"\n                  aria-label={r.label}\n                  aria-pressed={current === r.label}\n                  onClick={() => choose(current === r.label ? null : r.label)}\n                  onPointerEnter={() => setHot(i)}\n                  onPointerLeave={() => setHot(null)}\n                  onFocus={() => setHot(i)}\n                  className=\"relative grid size-10 place-items-center rounded-full text-2xl leading-none outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                  initial={{ opacity: 0, y: 12, scale: 0.4 }}\n                  animate={{ opacity: 1, y: hot === i ? -6 : 0, scale: hot === i ? 1.35 : 1 }}\n                  transition={{ type: \"spring\", bounce: 0.45, duration: 0.35, delay: hot === null ? i * 0.035 : 0 }}\n                >\n                  <span aria-hidden>{r.emoji}</span>\n                  <AnimatePresence>\n                    {hot === i && (\n                      <motion.span\n                        aria-hidden\n                        className=\"pointer-events-none absolute -top-7 whitespace-nowrap rounded-full bg-foreground px-2 py-0.5 text-[10px] font-semibold text-background\"\n                        initial={{ opacity: 0, y: 4, scale: 0.8 }}\n                        animate={{ opacity: 1, y: 0, scale: 0.75 }}\n                        exit={{ opacity: 0, scale: 0.6 }}\n                        transition={{ duration: 0.15 }}\n                      >\n                        {r.label}\n                      </motion.span>\n                    )}\n                  </AnimatePresence>\n                </motion.button>\n              ))}\n            </motion.div>\n          )}\n        </AnimatePresence>\n\n        <button\n          ref={trigger}\n          type=\"button\"\n          aria-haspopup=\"true\"\n          aria-expanded={open}\n          onClick={() => {\n            window.clearTimeout(timer.current);\n            focusTray.current = !open;\n            setOpen((o) => !o);\n          }}\n          className={cn(\n            \"inline-flex h-9 items-center gap-1.5 rounded-full border px-3.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n            picked ? \"border-primary/40 bg-primary/10 text-foreground\" : \"border-border bg-card text-muted-foreground hover:text-foreground\",\n          )}\n        >\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.span\n              key={picked?.label ?? \"none\"}\n              aria-hidden\n              className=\"text-base leading-none\"\n              initial={{ scale: 0, rotate: -40 }}\n              animate={{ scale: 1, rotate: 0 }}\n              exit={{ scale: 0, opacity: 0 }}\n              transition={{ type: \"spring\", bounce: 0.55, duration: 0.45 }}\n            >\n              {picked?.emoji ?? \"☺\"}\n            </motion.span>\n          </AnimatePresence>\n          {picked?.label ?? label}\n        </button>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}