{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tooltip",
  "title": "Tooltip",
  "description": "An accessible tooltip with 4 placements, auto-flip near the screen edge, a hover delay, keyboard-focus support, an arrow and an optional shortcut badge.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/tooltip/tooltip.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TooltipPlacement = \"top\" | \"bottom\" | \"left\" | \"right\";\n\nexport interface TooltipProps {\n  content: React.ReactNode;\n  children: React.ReactNode;\n  placement?: TooltipPlacement;\n  delay?: number;\n  offset?: number;\n  arrow?: boolean;\n  shortcut?: string;\n  theme?: \"dark\" | \"light\";\n  maxWidth?: number;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#161616\", border: \"rgba(255,255,255,0.12)\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.55)\", kbdBg: \"rgba(255,255,255,0.08)\", shadow: \"0 12px 32px rgba(0,0,0,0.5)\" },\n  light: { bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.1)\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", kbdBg: \"rgba(10,10,10,0.05)\", shadow: \"0 12px 32px rgba(0,0,0,0.16)\" },\n};\n\ninterface Position {\n  left: number;\n  top: number;\n  placement: TooltipPlacement;\n}\n\nexport function Tooltip({ content, children, placement = \"top\", delay = 20, offset = 10, arrow = true, shortcut, theme = \"dark\", maxWidth = 240, disabled = false, className }: TooltipProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const triggerRef = React.useRef<HTMLSpanElement>(null);\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const [open, setOpen] = React.useState(false);\n  const [mounted, setMounted] = React.useState(false);\n  const [pos, setPos] = React.useState<Position | null>(null);\n\n  const measure = React.useCallback(() => {\n    const el = triggerRef.current;\n    if (!el) return;\n    const r = el.getBoundingClientRect();\n    let place = placement;\n    if (place === \"top\" && r.top < 80) place = \"bottom\";\n    else if (place === \"bottom\" && window.innerHeight - r.bottom < 80) place = \"top\";\n    else if (place === \"left\" && r.left < 200) place = \"right\";\n    else if (place === \"right\" && window.innerWidth - r.right < 200) place = \"left\";\n    const cx = r.left + r.width / 2;\n    const cy = r.top + r.height / 2;\n    const map: Record<TooltipPlacement, Position> = {\n      top: { left: cx, top: r.top - offset, placement: \"top\" },\n      bottom: { left: cx, top: r.bottom + offset, placement: \"bottom\" },\n      left: { left: r.left - offset, top: cy, placement: \"left\" },\n      right: { left: r.right + offset, top: cy, placement: \"right\" },\n    };\n    setPos(map[place]);\n  }, [placement, offset]);\n\n  const show = (immediate = false) => {\n    if (disabled) return;\n    if (timer.current) clearTimeout(timer.current);\n    timer.current = setTimeout(\n      () => {\n        measure();\n        setMounted(true);\n        setOpen(true);\n      },\n      immediate ? 0 : delay,\n    );\n  };\n\n  const hide = () => {\n    if (timer.current) clearTimeout(timer.current);\n    setOpen(false);\n  };\n\n  React.useEffect(() => {\n    if (!open) return;\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") setOpen(false);\n    };\n    window.addEventListener(\"keydown\", onKey);\n    window.addEventListener(\"scroll\", measure, true);\n    window.addEventListener(\"resize\", measure);\n    return () => {\n      window.removeEventListener(\"keydown\", onKey);\n      window.removeEventListener(\"scroll\", measure, true);\n      window.removeEventListener(\"resize\", measure);\n    };\n  }, [open, measure]);\n\n  React.useEffect(\n    () => () => {\n      if (timer.current) clearTimeout(timer.current);\n    },\n    [],\n  );\n\n  const place = pos?.placement ?? placement;\n  const translate: Record<TooltipPlacement, { x: string; y: string }> = {\n    top: { x: \"-50%\", y: \"-100%\" },\n    bottom: { x: \"-50%\", y: \"0%\" },\n    left: { x: \"-100%\", y: \"-50%\" },\n    right: { x: \"0%\", y: \"-50%\" },\n  };\n  const origin: Record<TooltipPlacement, string> = { top: \"bottom center\", bottom: \"top center\", left: \"center right\", right: \"center left\" };\n  const arrowStyle: Record<TooltipPlacement, React.CSSProperties> = {\n    top: { bottom: -5, left: \"50%\", marginLeft: -4.5, borderRight: `1px solid ${p.border}`, borderBottom: `1px solid ${p.border}` },\n    bottom: { top: -5, left: \"50%\", marginLeft: -4.5, borderLeft: `1px solid ${p.border}`, borderTop: `1px solid ${p.border}` },\n    left: { right: -5, top: \"50%\", marginTop: -4.5, borderTop: `1px solid ${p.border}`, borderRight: `1px solid ${p.border}` },\n    right: { left: -5, top: \"50%\", marginTop: -4.5, borderBottom: `1px solid ${p.border}`, borderLeft: `1px solid ${p.border}` },\n  };\n\n  return (\n    <>\n      <span\n        ref={triggerRef}\n        className={cn(\"inline-flex\", className)}\n        aria-describedby={open ? `${uid}-tip` : undefined}\n        onMouseEnter={() => show()}\n        onMouseLeave={hide}\n        onFocus={(e) => {\n          if ((e.target as HTMLElement).matches?.(\":focus-visible\")) show(true);\n        }}\n        onBlur={hide}\n        onMouseDown={hide}\n      >\n        {children}\n      </span>\n      {mounted &&\n        createPortal(\n          <AnimatePresence>\n            {open && pos && (\n              <motion.div\n                key=\"tooltip\"\n                id={`${uid}-tip`}\n                role=\"tooltip\"\n                className=\"pointer-events-none fixed z-[9999] flex items-center gap-2\"\n                style={{\n                  left: pos.left,\n                  top: pos.top,\n                  x: translate[place].x,\n                  y: translate[place].y,\n                  maxWidth,\n                  padding: \"7px 11px\",\n                  borderRadius: 10,\n                  background: p.bg,\n                  border: `1px solid ${p.border}`,\n                  boxShadow: p.shadow,\n                  color: p.text,\n                  fontFamily: \"Inter, sans-serif\",\n                  fontSize: 12.5,\n                  fontWeight: 500,\n                  lineHeight: 1.4,\n                  transformOrigin: origin[place],\n                }}\n                initial={{ opacity: 0, scale: 0.94 }}\n                animate={{ opacity: 1, scale: 1 }}\n                exit={{ opacity: 0, scale: 0.94 }}\n                transition={{ duration: 0.14, ease: [0.16, 1, 0.3, 1] }}\n              >\n                <span>{content}</span>\n                {shortcut && (\n                  <span className=\"shrink-0 rounded-md font-semibold\" style={{ padding: \"1px 6px\", background: p.kbdBg, color: p.muted, fontSize: 11 }}>\n                    {shortcut}\n                  </span>\n                )}\n                {arrow && <span aria-hidden=\"true\" className=\"absolute block\" style={{ width: 9, height: 9, background: p.bg, transform: \"rotate(45deg)\", ...arrowStyle[place] }} />}\n              </motion.div>\n            )}\n          </AnimatePresence>,\n          document.body,\n        )}\n    </>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}