{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "liquid-tooltip",
  "type": "registry:ui",
  "title": "Liquid Tooltip",
  "description": "A fluid liquid-expanding tooltip with smooth spring physics.",
  "dependencies": [
    "react",
    "react-dom",
    "motion"
  ],
  "devDependencies": [
    "@types/react",
    "@types/react-dom"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "ui/liquid-tooltip.tsx",
      "content": "\"use client\";\n\nimport React, { useState, useRef, useEffect, useCallback } from 'react';\nimport { createPortal } from 'react-dom';\nimport { motion, AnimatePresence, useSpring, useMotionValue, useTransform } from 'motion/react';\nimport { cn } from '@/lib/utils';\n\ntype Placement = \"top\" | \"bottom\" | \"left\" | \"right\";\n\ninterface LiquidTooltipProps {\n  text: string;\n  children: React.ReactNode;\n  className?: string;\n  popupClassName?: string;\n  placement?: Placement;\n}\n\nexport const LiquidTooltip = ({\n  text,\n  children,\n  className,\n  popupClassName,\n  placement = \"top\",\n}: LiquidTooltipProps) => {\n  const [isHovered, setIsHovered] = useState(false);\n  const [mounted, setMounted] = useState(false);\n  const [effectivePlacement, setEffectivePlacement] = useState<Placement>(placement);\n  const [coords, setCoords] = useState<{\n    left: number;\n    top: number;\n    width: number;\n    height: number;\n  }>({ left: 0, top: 0, width: 0, height: 0 });\n\n  const triggerRef = useRef<HTMLSpanElement>(null);\n\n  useEffect(() => {\n    setMounted(true);\n  }, []);\n\n  useEffect(() => {\n    setEffectivePlacement(placement);\n  }, [placement]);\n\n  // Spring physics for the \"liquid\" feel\n  const springConfig = { stiffness: 100, damping: 15, mass: 0.1 };\n\n  // Mouse position tracking for subtle parallax\n  const value = useMotionValue(0);\n\n  // Rotate based on movement value\n  const rotate = useSpring(\n    useTransform(value, [-100, 100], [-45, 45]),\n    springConfig\n  );\n\n  // Springy movement value\n  const springValue = useSpring(value, springConfig);\n\n  // Update position & auto-flip based on viewport boundaries\n  const updatePosition = useCallback(() => {\n    if (!triggerRef.current || typeof window === 'undefined') return;\n    const rect = triggerRef.current.getBoundingClientRect();\n\n    setCoords({\n      left: rect.left,\n      top: rect.top,\n      width: rect.width,\n      height: rect.height,\n    });\n\n    // Space available around the trigger relative to viewport\n    const spaceLeft = rect.left;\n    const spaceRight = window.innerWidth - rect.right;\n    const spaceTop = rect.top;\n    const spaceBottom = window.innerHeight - rect.bottom;\n\n    const HORIZONTAL_THRESHOLD = 150;\n    const VERTICAL_THRESHOLD = 60;\n\n    let nextPlacement = placement;\n\n    if (placement === 'left') {\n      if (spaceLeft < HORIZONTAL_THRESHOLD && spaceRight > spaceLeft) {\n        nextPlacement = 'right';\n      }\n    } else if (placement === 'right') {\n      if (spaceRight < HORIZONTAL_THRESHOLD && spaceLeft > spaceRight) {\n        nextPlacement = 'left';\n      }\n    } else if (placement === 'top') {\n      if (spaceTop < VERTICAL_THRESHOLD && spaceBottom > spaceTop) {\n        nextPlacement = 'bottom';\n      }\n    } else if (placement === 'bottom') {\n      if (spaceBottom < VERTICAL_THRESHOLD && spaceTop > spaceBottom) {\n        nextPlacement = 'top';\n      }\n    }\n\n    setEffectivePlacement(nextPlacement);\n  }, [placement]);\n\n  // Track scroll and resize across the document so fixed portal tooltip stays perfectly pinned\n  useEffect(() => {\n    if (!isHovered) return;\n    updatePosition();\n\n    window.addEventListener('scroll', updatePosition, true);\n    window.addEventListener('resize', updatePosition);\n    return () => {\n      window.removeEventListener('scroll', updatePosition, true);\n      window.removeEventListener('resize', updatePosition);\n    };\n  }, [isHovered, updatePosition]);\n\n  const handleMouseEnter = () => {\n    updatePosition();\n    setIsHovered(true);\n  };\n\n  const handleMouseMove = (event: React.MouseEvent<HTMLSpanElement>) => {\n    const isVertical = effectivePlacement === \"top\" || effectivePlacement === \"bottom\";\n    if (isVertical) {\n      const halfWidth = event.currentTarget.offsetWidth / 2;\n      value.set(event.nativeEvent.offsetX - halfWidth);\n    } else {\n      const halfHeight = event.currentTarget.offsetHeight / 2;\n      value.set(event.nativeEvent.offsetY - halfHeight);\n    }\n  };\n\n  const handleMouseLeave = () => {\n    setIsHovered(false);\n    value.set(0);\n  };\n\n  // Determine fixed anchor coordinates and CSS translation for the portal wrapper\n  const getAnchorStyle = (): React.CSSProperties => {\n    let top = 0;\n    let left = 0;\n    let transform = '';\n\n    switch (effectivePlacement) {\n      case 'top':\n        left = coords.left + coords.width / 2;\n        top = coords.top - 8;\n        transform = 'translate(-50%, -100%)';\n        break;\n      case 'bottom':\n        left = coords.left + coords.width / 2;\n        top = coords.top + coords.height + 8;\n        transform = 'translate(-50%, 0)';\n        break;\n      case 'left':\n        left = coords.left - 8;\n        top = coords.top + coords.height / 2;\n        transform = 'translate(-100%, -50%)';\n        break;\n      case 'right':\n        left = coords.left + coords.width + 8;\n        top = coords.top + coords.height / 2;\n        transform = 'translate(0, -50%)';\n        break;\n      default:\n        left = coords.left + coords.width / 2;\n        top = coords.top - 8;\n        transform = 'translate(-50%, -100%)';\n    }\n\n    return {\n      position: 'fixed',\n      left: `${left}px`,\n      top: `${top}px`,\n      transform,\n      zIndex: 99999,\n      pointerEvents: 'none',\n    };\n  };\n\n  const getArrowClasses = () => {\n    switch (effectivePlacement) {\n      case 'top':\n        return '-bottom-0.5 left-1/2 -translate-x-1/2';\n      case 'bottom':\n        return '-top-0.5 left-1/2 -translate-x-1/2';\n      case 'left':\n        return '-right-0.5 top-1/2 -translate-y-1/2';\n      case 'right':\n        return '-left-0.5 top-1/2 -translate-y-1/2';\n      default:\n        return '-bottom-0.5 left-1/2 -translate-x-1/2';\n    }\n  };\n\n  const getAnimation = () => {\n    const distance = 8;\n    switch (effectivePlacement) {\n      case 'top':\n        return {\n          initial: { opacity: 0, scale: 0.6, y: distance },\n          animate: { opacity: 1, scale: 1, y: 0 },\n          exit: { opacity: 0, scale: 0.6, y: distance },\n        };\n      case 'bottom':\n        return {\n          initial: { opacity: 0, scale: 0.6, y: -distance },\n          animate: { opacity: 1, scale: 1, y: 0 },\n          exit: { opacity: 0, scale: 0.6, y: -distance },\n        };\n      case 'left':\n        return {\n          initial: { opacity: 0, scale: 0.6, x: distance },\n          animate: { opacity: 1, scale: 1, x: 0 },\n          exit: { opacity: 0, scale: 0.6, x: distance },\n        };\n      case 'right':\n        return {\n          initial: { opacity: 0, scale: 0.6, x: -distance },\n          animate: { opacity: 1, scale: 1, x: 0 },\n          exit: { opacity: 0, scale: 0.6, x: -distance },\n        };\n      default:\n        return {\n          initial: { opacity: 0, scale: 0.6, y: distance },\n          animate: { opacity: 1, scale: 1, y: 0 },\n          exit: { opacity: 0, scale: 0.6, y: distance },\n        };\n    }\n  };\n\n  const animationProps = getAnimation();\n  const isVertical = effectivePlacement === 'top' || effectivePlacement === 'bottom';\n\n  const motionStyle = isVertical\n    ? ({ x: springValue, rotate: rotate } as any)\n    : ({ y: springValue, rotate: rotate } as any);\n\n  return (\n    <>\n      <span\n        ref={triggerRef}\n        className={cn('relative inline-flex', className)}\n        onMouseEnter={handleMouseEnter}\n        onMouseLeave={handleMouseLeave}\n        onMouseMove={handleMouseMove}\n      >\n        {children}\n      </span>\n\n      {mounted &&\n        typeof document !== 'undefined' &&\n        createPortal(\n          <AnimatePresence>\n            {isHovered && (\n              <div style={getAnchorStyle()}>\n                <motion.div\n                  initial={animationProps.initial}\n                  animate={{\n                    ...animationProps.animate,\n                    transition: {\n                      type: 'spring',\n                      stiffness: 260,\n                      damping: 20,\n                    },\n                  }}\n                  exit={animationProps.exit}\n                  style={motionStyle}\n                  className={cn(\n                    'relative flex flex-col items-center justify-center rounded-xl bg-neutral-900 dark:bg-neutral-100 px-3 py-1.5 text-xs font-medium text-neutral-100 dark:text-neutral-900 shadow-xl select-none whitespace-nowrap',\n                    popupClassName\n                  )}\n                >\n                  <span className=\"relative z-10 font-mono tracking-tight\">\n                    {text}\n                  </span>\n\n                  <div\n                    className={cn(\n                      'absolute h-2 w-2 rotate-45 transform bg-neutral-900 dark:bg-neutral-100',\n                      getArrowClasses()\n                    )}\n                  />\n                </motion.div>\n              </div>\n            )}\n          </AnimatePresence>,\n          document.body\n        )}\n    </>\n  );\n};\n\nexport default LiquidTooltip;\n",
      "type": "registry:ui"
    }
  ]
}