{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"animated-tooltip","type":"registry:ui","title":"Animated Tooltip","description":"A tooltip that springs out of its trigger, leans toward the pointer and shows on focus too.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/animated-tooltip.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { AnimatePresence, motion, useMotionValue, useReducedMotion, useSpring, useTransform } from \"motion/react\";\nimport {\n  cloneElement,\n  isValidElement,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n  type FocusEvent,\n  type PointerEvent,\n  type ReactElement,\n  type ReactNode,\n} from \"react\";\n\ntype Side = \"top\" | \"bottom\" | \"left\" | \"right\";\n\ntype AnimatedTooltipProps = {\n  content: ReactNode;\n  side?: Side;\n  /** Hover delay before showing, in ms. Keyboard focus shows it at once. */\n  delay?: number;\n  /** Force the tooltip open or closed. */\n  open?: boolean;\n  /** The trigger: a single focusable element, such as a button. */\n  children: ReactNode;\n  /** Extra classes for the bubble. */\n  className?: string;\n};\n\nconst PLACEMENT: Record<Side, string> = {\n  top: \"bottom-full left-1/2 mb-2.5 -translate-x-1/2\",\n  bottom: \"top-full left-1/2 mt-2.5 -translate-x-1/2\",\n  left: \"right-full top-1/2 mr-2.5 -translate-y-1/2\",\n  right: \"left-full top-1/2 ml-2.5 -translate-y-1/2\",\n};\n\nconst ARROW: Record<Side, string> = {\n  top: \"left-1/2 top-full -mt-1 -translate-x-1/2 border-b border-r\",\n  bottom: \"left-1/2 bottom-full -mb-1 -translate-x-1/2 border-l border-t\",\n  left: \"top-1/2 left-full -ml-1 -translate-y-1/2 border-r border-t\",\n  right: \"top-1/2 right-full -mr-1 -translate-y-1/2 border-b border-l\",\n};\n\nconst ORIGIN: Record<Side, string> = { top: \"50% 100%\", bottom: \"50% 0%\", left: \"100% 50%\", right: \"0% 50%\" };\n\n/** A tooltip that springs out of its trigger and leans toward the pointer. Shows on hover and focus. */\nexport function AnimatedTooltip({ content, side = \"top\", delay = 300, open: forced, children, className = \"\" }: AnimatedTooltipProps) {\n  const id = useId();\n  const reduce = useReducedMotion();\n  const [shown, setShown] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  const horizontal = side === \"top\" || side === \"bottom\";\n\n  // The bubble follows the pointer along the trigger by a few pixels, with a slight tilt.\n  const follow = useMotionValue(0);\n  const drift = useSpring(follow, { stiffness: 260, damping: 18 });\n  const tilt = useTransform(drift, [-14, 14], [-5, 5]);\n\n  const open = forced ?? shown;\n\n  useEffect(() => {\n    const t = timer;\n    return () => clearTimeout(t.current);\n  }, []);\n\n  function show(now: boolean) {\n    clearTimeout(timer.current);\n    if (now || delay <= 0) setShown(true);\n    else timer.current = setTimeout(() => setShown(true), delay);\n  }\n\n  function hide() {\n    clearTimeout(timer.current);\n    setShown(false);\n    follow.set(0);\n  }\n\n  function onPointerMove(e: PointerEvent<HTMLSpanElement>) {\n    if (reduce || e.pointerType !== \"mouse\") return;\n    const rect = e.currentTarget.getBoundingClientRect();\n    const d = horizontal ? e.clientX - (rect.left + rect.width / 2) : e.clientY - (rect.top + rect.height / 2);\n    follow.set(Math.max(-14, Math.min(14, d * 0.35)));\n  }\n\n  function onFocus(e: FocusEvent<HTMLSpanElement>) {\n    // Only keyboard focus: a mouse click already has hover handling it.\n    if (e.target instanceof HTMLElement && e.target.matches(\":focus-visible\")) show(true);\n  }\n\n  const trigger = isValidElement(children)\n    ? cloneElement(children as ReactElement<{ \"aria-describedby\"?: string }>, { \"aria-describedby\": open ? id : undefined })\n    : children;\n\n  // Enter from the trigger: along y for top and bottom, along x for left and right.\n  const enterDistance = side === \"top\" || side === \"left\" ? 6 : -6;\n  const offset = (d: number) => (horizontal ? { y: d } : { x: d });\n\n  return (\n    <span\n      className=\"relative inline-flex\"\n      onPointerEnter={(e) => e.pointerType === \"mouse\" && show(false)}\n      onPointerLeave={hide}\n      onPointerMove={onPointerMove}\n      onPointerDown={hide}\n      onFocus={onFocus}\n      onBlur={hide}\n      onKeyDown={(e) => {\n        if (e.key === \"Escape\" && shown) {\n          e.stopPropagation();\n          hide();\n        }\n      }}\n    >\n      {trigger}\n      <AnimatePresence>\n        {open ? (\n          <motion.span\n            key=\"tooltip\"\n            id={id}\n            role=\"tooltip\"\n            initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.85, ...offset(enterDistance) }}\n            animate={reduce ? { opacity: 1 } : { opacity: 1, scale: 1, ...offset(0) }}\n            exit={\n              reduce\n                ? { opacity: 0 }\n                : { opacity: 0, scale: 0.92, ...offset(enterDistance / 2), transition: { duration: 0.12 } }\n            }\n            transition={reduce ? { duration: 0.12 } : { type: \"spring\", stiffness: 520, damping: 26 }}\n            style={\n              horizontal\n                ? { x: drift, rotate: tilt, transformOrigin: ORIGIN[side] }\n                : { y: drift, transformOrigin: ORIGIN[side] }\n            }\n            className={`pointer-events-none absolute z-50 w-max max-w-60 rounded-xl border border-ink-500 bg-ink-700 px-2.5 py-1.5 text-xs font-semibold text-bone shadow-[0_12px_30px_-10px_rgb(0_0_0/0.9)] ${PLACEMENT[side]} ${className}`}\n          >\n            <span aria-hidden=\"true\" className={`absolute size-2 rotate-45 border-ink-500 bg-ink-700 ${ARROW[side]}`} />\n            <span className=\"relative\">{content}</span>\n          </motion.span>\n        ) : null}\n      </AnimatePresence>\n    </span>\n  );\n}\n"}]}