{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tooltip",
  "type": "registry:component",
  "title": "Tooltip",
  "description": "A tooltip that trails the pointer with a soft lag, and settles in a fixed spot for keyboard focus.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "tooltip",
    "hover",
    "pointer",
    "follow",
    "a11y"
  ],
  "docs": "https://hyperblur.dev/effects/tooltip",
  "files": [
    {
      "path": "registry/hyperblur/tooltip.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/tooltip.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TooltipProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, \"content\"> {\n  content: React.ReactNode;\n  children: React.ReactNode;\n  placement?: \"top\" | \"bottom\";\n  delay?: number;\n  follow?: boolean;\n}\n\nconst css = `\n.hb-tt {\n  position: relative;\n  display: inline-flex;\n}\n.hb-tt > .hb-tt-t {\n  position: absolute;\n  left: 0;\n  top: 0;\n  width: 0;\n  height: 0;\n  z-index: 50;\n  pointer-events: none;\n  will-change: transform;\n}\n.hb-tt-b {\n  display: block;\n  width: max-content;\n  max-width: 240px;\n  box-sizing: border-box;\n  padding: 6px 10px;\n  border-radius: 8px;\n  border: 1px solid rgba(255, 255, 255, .14);\n  background: rgba(17, 17, 17, .96);\n  color: #f5f5f5;\n  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);\n  font: 500 12px/1.4 system-ui, -apple-system, \"Segoe UI\", sans-serif;\n  letter-spacing: .01em;\n  text-align: center;\n  opacity: 0;\n  visibility: hidden;\n  transform: translate(-50%, calc(-100% - 14px)) scale(.94);\n  transform-origin: 50% 100%;\n  transition: opacity .16s ease, transform .16s cubic-bezier(.2, .9, .2, 1), visibility 0s linear .16s;\n}\n.hb-tt-t[data-p=\"bottom\"] > .hb-tt-b {\n  transform: translate(-50%, 18px) scale(.94);\n  transform-origin: 50% 0;\n}\n.hb-tt-t[data-open=\"true\"] > .hb-tt-b {\n  opacity: 1;\n  visibility: visible;\n  transform: translate(-50%, calc(-100% - 14px)) scale(1);\n  transition-delay: 0s;\n}\n.hb-tt-t[data-open=\"true\"][data-p=\"bottom\"] > .hb-tt-b {\n  transform: translate(-50%, 18px) scale(1);\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-tt-b,\n  .hb-tt-t[data-p=\"bottom\"] > .hb-tt-b {\n    transition: none;\n    transform: translate(-50%, calc(-100% - 14px));\n  }\n  .hb-tt-t[data-p=\"bottom\"] > .hb-tt-b,\n  .hb-tt-t[data-open=\"true\"][data-p=\"bottom\"] > .hb-tt-b {\n    transform: translate(-50%, 18px);\n  }\n  .hb-tt-t[data-open=\"true\"] > .hb-tt-b {\n    transform: translate(-50%, calc(-100% - 14px));\n  }\n}\n`;\n\nexport function Tooltip({\n  content,\n  children,\n  placement = \"top\",\n  delay = 120,\n  follow = true,\n  className = \"\",\n  onPointerEnter,\n  onPointerMove,\n  onPointerLeave,\n  onFocus,\n  onBlur,\n  ...rest\n}: TooltipProps) {\n  const id = React.useId();\n  const [open, setOpen] = React.useState(false);\n  const wrap = React.useRef<HTMLSpanElement>(null);\n  const tip = React.useRef<HTMLSpanElement>(null);\n  const target = React.useRef({ x: 0, y: 0 });\n  const current = React.useRef({ x: 0, y: 0 });\n  const following = React.useRef(false);\n  const hovering = React.useRef(false);\n  const focused = React.useRef(false);\n  const timer = React.useRef<number | undefined>(undefined);\n\n  function reduced() {\n    return window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  }\n\n  function anchor() {\n    const el = wrap.current;\n    if (!el) return;\n    target.current = { x: el.offsetWidth / 2, y: placement === \"top\" ? 0 : el.offsetHeight };\n    following.current = false;\n  }\n\n  function aim(e: React.PointerEvent<HTMLSpanElement>) {\n    const el = wrap.current;\n    if (!el) return;\n    const r = el.getBoundingClientRect();\n    target.current = { x: e.clientX - r.left, y: e.clientY - r.top };\n    following.current = true;\n  }\n\n  function reveal() {\n    window.clearTimeout(timer.current);\n    timer.current = window.setTimeout(() => setOpen(true), delay);\n  }\n\n  function hide() {\n    window.clearTimeout(timer.current);\n    setOpen(false);\n  }\n\n  function handleEnter(e: React.PointerEvent<HTMLSpanElement>) {\n    onPointerEnter?.(e);\n    if (e.pointerType === \"touch\") return;\n    hovering.current = true;\n    if (follow && !reduced()) aim(e);\n    else anchor();\n    reveal();\n  }\n\n  function handleMove(e: React.PointerEvent<HTMLSpanElement>) {\n    onPointerMove?.(e);\n    if (!hovering.current || !follow || reduced()) return;\n    aim(e);\n  }\n\n  function handleLeave(e: React.PointerEvent<HTMLSpanElement>) {\n    onPointerLeave?.(e);\n    hovering.current = false;\n    if (focused.current) anchor();\n    else hide();\n  }\n\n  function handleFocus(e: React.FocusEvent<HTMLSpanElement>) {\n    onFocus?.(e);\n    if (!(e.target as HTMLElement).matches(\":focus-visible\")) return;\n    focused.current = true;\n    if (hovering.current) return;\n    anchor();\n    reveal();\n  }\n\n  function handleBlur(e: React.FocusEvent<HTMLSpanElement>) {\n    onBlur?.(e);\n    focused.current = false;\n    if (!hovering.current) hide();\n  }\n\n  React.useEffect(() => {\n    const el = tip.current;\n    if (!el || !open) return;\n    current.current = { ...target.current };\n    let raf = 0;\n\n    function apply() {\n      if (!el) return;\n      el.style.transform = `translate3d(${current.current.x.toFixed(2)}px, ${current.current.y.toFixed(2)}px, 0)`;\n    }\n\n    function tick() {\n      const c = current.current;\n      const t = target.current;\n      const k = following.current ? 0.16 : 0.3;\n      c.x += (t.x - c.x) * k;\n      c.y += (t.y - c.y) * k;\n      apply();\n      raf = requestAnimationFrame(tick);\n    }\n\n    function key(e: KeyboardEvent) {\n      if (e.key !== \"Escape\") return;\n      window.clearTimeout(timer.current);\n      setOpen(false);\n    }\n\n    apply();\n    raf = requestAnimationFrame(tick);\n    window.addEventListener(\"keydown\", key);\n    return () => {\n      cancelAnimationFrame(raf);\n      window.removeEventListener(\"keydown\", key);\n    };\n  }, [open]);\n\n  React.useEffect(() => {\n    return () => window.clearTimeout(timer.current);\n  }, []);\n\n  return (\n    <span\n      {...rest}\n      ref={wrap}\n      className={`hb-tt ${className}`}\n      aria-describedby={id}\n      onPointerEnter={handleEnter}\n      onPointerMove={handleMove}\n      onPointerLeave={handleLeave}\n      onFocus={handleFocus}\n      onBlur={handleBlur}\n    >\n      <style href=\"hb-tooltip\" precedence=\"default\">{css}</style>\n      {children}\n      <span ref={tip} id={id} role=\"tooltip\" className=\"hb-tt-t\" data-open={open} data-p={placement}>\n        <span className=\"hb-tt-b\">{content}</span>\n      </span>\n    </span>\n  );\n}\n"
    }
  ]
}