{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tooltip-animated",
  "title": "Animated tooltip",
  "description": "A tooltip springs out of the trigger's side and can lean toward the pointer over avatars; tooltips in a delay group open instantly once one is already showing.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/overlays/tooltip-animated.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useMotionValue, useReducedMotion, useSpring, useTransform } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens, spring, follow } from \"@/lib/goodui-motion\"\n\nexport type TooltipSide = \"top\" | \"bottom\" | \"left\" | \"right\"\n\ntype GroupContextValue = {\n  delay: number\n  /** Opens instantly while another tooltip in the group is open or was closed less than skipDelay ago. */\n  isWarm: () => boolean\n  onOpen: () => void\n  onClose: () => void\n}\n\nconst GroupContext = React.createContext<GroupContextValue | null>(null)\n\nexport type TooltipGroupProps = {\n  /** Delay before the first tooltip in the group opens, in ms. */\n  delay?: number\n  /** How long the group stays \"warm\" after a tooltip closes, in ms. */\n  skipDelay?: number\n  children: React.ReactNode\n}\n\n/** Shares one open delay across tooltips: after the first opens, moving to a neighbour shows its tooltip at once. */\nexport function TooltipGroup({ delay = 500, skipDelay = 300, children }: TooltipGroupProps) {\n  const openCount = React.useRef(0)\n  const warmUntil = React.useRef(0)\n  const value = React.useMemo<GroupContextValue>(\n    () => ({\n      delay,\n      isWarm: () => openCount.current > 0 || performance.now() < warmUntil.current,\n      onOpen: () => {\n        openCount.current += 1\n      },\n      onClose: () => {\n        openCount.current = Math.max(0, openCount.current - 1)\n        warmUntil.current = performance.now() + skipDelay\n      },\n    }),\n    [delay, skipDelay],\n  )\n  return <GroupContext.Provider value={value}>{children}</GroupContext.Provider>\n}\n\nconst PLACEMENT: Record<TooltipSide, string> = {\n  top: \"bottom-full left-1/2 -translate-x-1/2 pb-2\",\n  bottom: \"top-full left-1/2 -translate-x-1/2 pt-2\",\n  left: \"right-full top-1/2 -translate-y-1/2 pr-2\",\n  right: \"left-full top-1/2 -translate-y-1/2 pl-2\",\n}\nconst ORIGIN: Record<TooltipSide, string> = { top: \"50% 100%\", bottom: \"50% 0%\", left: \"100% 50%\", right: \"0% 50%\" }\nconst NUDGE: Record<TooltipSide, { x?: number; y?: number }> = { top: { y: 6 }, bottom: { y: -6 }, left: { x: 6 }, right: { x: -6 } }\nconst ARROW: Record<TooltipSide, string> = {\n  top: \"left-1/2 -bottom-1 -translate-x-1/2\",\n  bottom: \"left-1/2 -top-1 -translate-x-1/2\",\n  left: \"top-1/2 -right-1 -translate-y-1/2\",\n  right: \"top-1/2 -left-1 -translate-y-1/2\",\n}\n\nexport type TooltipAnimatedProps = Omit<React.ComponentProps<\"span\">, \"content\"> & {\n  /** Tooltip text or node. It is also copied into a visually hidden element linked to the trigger with aria-describedby. */\n  content: React.ReactNode\n  /** Side of the trigger the tooltip appears on. There is no automatic flip. */\n  side?: TooltipSide\n  /** Open delay in ms. Inside a TooltipGroup the group's delay wins. */\n  delay?: number\n  /** The tooltip leans and slides toward the pointer (top and bottom sides). Turned off on touch screens. */\n  followPointer?: boolean\n  /** Never open the tooltip. */\n  disabled?: boolean\n  /** Classes for the tooltip bubble. */\n  contentClassName?: string\n  /**\n   * The trigger: one focusable element such as a button, so it gets aria-describedby and keyboard focus.\n   * Plain text (a string or number) is wrapped in a focusable span that carries aria-describedby.\n   */\n  children: React.ReactNode\n}\n\n/**\n * Tooltip that springs out of the trigger's side. Opens on hover after a delay, at once on keyboard focus,\n * on long-press for touch; Esc or a click hides it. Screen readers get the content through aria-describedby.\n */\nexport function TooltipAnimated({\n  content,\n  side = \"top\",\n  delay = 400,\n  followPointer = false,\n  disabled = false,\n  className,\n  contentClassName,\n  children,\n  ...props\n}: TooltipAnimatedProps) {\n  const reduce = useReducedMotion() ?? false\n  const group = React.useContext(GroupContext)\n  const id = React.useId()\n  const [open, setOpen] = React.useState(false)\n  const [instant, setInstant] = React.useState(false)\n  const [canHover, setCanHover] = React.useState(true)\n  const rootRef = React.useRef<HTMLSpanElement>(null)\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null)\n  const openRef = React.useRef(false)\n\n  const pointerX = useMotionValue(0)\n  const x = useSpring(pointerX, follow)\n  const rotate = useTransform(x, [-40, 40], [-10, 10])\n  const follows = followPointer && canHover && !reduce && (side === \"top\" || side === \"bottom\")\n\n  React.useEffect(() => {\n    const mq = window.matchMedia(\"(hover: hover) and (pointer: fine)\")\n    const update = () => setCanHover(mq.matches)\n    update()\n    mq.addEventListener(\"change\", update)\n    return () => mq.removeEventListener(\"change\", update)\n  }, [])\n\n  const clear = () => {\n    if (timer.current) clearTimeout(timer.current)\n    timer.current = null\n  }\n\n  const show = React.useCallback(\n    (now: boolean) => {\n      clear()\n      if (disabled || openRef.current) return\n      const go = (fast: boolean) => {\n        openRef.current = true\n        setInstant(fast)\n        setOpen(true)\n        group?.onOpen()\n      }\n      const warm = group?.isWarm() ?? false\n      if (now || warm) go(warm)\n      else timer.current = setTimeout(() => go(false), group?.delay ?? delay)\n    },\n    [disabled, group, delay],\n  )\n\n  const hide = React.useCallback(() => {\n    clear()\n    if (!openRef.current) return\n    openRef.current = false\n    setOpen(false)\n    group?.onClose()\n    pointerX.set(0)\n  }, [group, pointerX])\n\n  React.useEffect(() => {\n    if (!open) return\n    const onKey = (e: KeyboardEvent) => e.key === \"Escape\" && hide()\n    document.addEventListener(\"keydown\", onKey)\n    return () => document.removeEventListener(\"keydown\", onKey)\n  }, [open, hide])\n\n  React.useEffect(\n    () => () => {\n      clear()\n      if (openRef.current) group?.onClose()\n    },\n    [group],\n  )\n\n  const descId = `${id}-desc`\n  const trigger =\n    React.isValidElement<{ \"aria-describedby\"?: string }>(children) && children.type !== React.Fragment ? (\n      React.cloneElement(children, {\n        \"aria-describedby\": [children.props[\"aria-describedby\"], descId].filter(Boolean).join(\" \"),\n      })\n    ) : (\n      // Text or fragments can't take attributes: wrap them so keyboard users can reach the tooltip and it is announced.\n      <span tabIndex={0} aria-describedby={descId} className=\"rounded-sm outline-none focus-visible:ring-3 focus-visible:ring-ring/50\">\n        {children}\n      </span>\n    )\n\n  const nudge = NUDGE[side]\n  const enter = reduce || instant ? { duration: reduce ? 0 : 0.12 } : spring.snap\n\n  return (\n    <span\n      ref={rootRef}\n      className={cn(\"relative inline-flex\", className)}\n      onPointerEnter={(e) => e.pointerType !== \"touch\" && show(false)}\n      onPointerLeave={(e) => e.pointerType !== \"touch\" && hide()}\n      onPointerMove={(e) => {\n        if (!follows || !rootRef.current) return\n        const r = rootRef.current.getBoundingClientRect()\n        pointerX.set(Math.max(-40, Math.min(40, (e.clientX - (r.left + r.width / 2)) * 0.5)))\n      }}\n      onPointerDown={(e) => {\n        if (e.pointerType !== \"touch\") return hide()\n        clear()\n        timer.current = setTimeout(() => {\n          show(true)\n          timer.current = setTimeout(hide, 1800)\n        }, 450)\n      }}\n      onPointerUp={(e) => {\n        if (e.pointerType === \"touch\" && !openRef.current) clear()\n      }}\n      onFocus={(e) => {\n        if ((e.target as HTMLElement).matches?.(\":focus-visible\")) show(true)\n      }}\n      onBlur={hide}\n      {...props}\n    >\n      <GooduiTokens />\n      {trigger}\n      <span id={descId} role=\"tooltip\" className=\"sr-only\">\n        {content}\n      </span>\n      <AnimatePresence>\n        {open && (\n          <span key=\"tip\" aria-hidden=\"true\" className={cn(\"pointer-events-none absolute z-50\", PLACEMENT[side])}>\n            <motion.span className=\"block\" style={follows ? { x, rotate } : undefined}>\n              <motion.span\n                className={cn(\n                  \"relative block rounded-lg bg-foreground px-2.5 py-1.5 text-xs font-medium whitespace-nowrap text-background shadow-[var(--pc-e2)]\",\n                  contentClassName,\n                )}\n                style={{ transformOrigin: ORIGIN[side] }}\n                initial={reduce ? { opacity: 0 } : { opacity: 0, scale: instant ? 0.96 : 0.6, ...nudge }}\n                animate={{ opacity: 1, scale: 1, x: 0, y: 0 }}\n                exit={reduce ? { opacity: 0, transition: { duration: 0 } } : { opacity: 0, scale: 0.9, transition: { duration: 0.1 } }}\n                transition={enter}\n              >\n                <span aria-hidden=\"true\" className={cn(\"absolute size-2 rotate-45 rounded-[2px] bg-inherit\", ARROW[side])} />\n                <span className=\"relative\">{content}</span>\n              </motion.span>\n            </motion.span>\n          </span>\n        )}\n      </AnimatePresence>\n    </span>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/tooltip-animated.tsx"
    }
  ],
  "categories": [
    "components",
    "overlays"
  ],
  "type": "registry:component"
}