import { useState, useRef, useEffect } from 'react' import { Info, X } from 'lucide-react' const FeatureTooltip = ({ feature, definition }) => { const [isOpen, setIsOpen] = useState(false) const [position, setPosition] = useState({ top: 0, left: 0 }) const iconRef = useRef(null) const tooltipRef = useRef(null) // Handle tooltip positioning to stay visible useEffect(() => { if (!isOpen || !iconRef.current || !tooltipRef.current) return const calculatePosition = () => { const iconRect = iconRef.current.getBoundingClientRect() const tooltipRect = tooltipRef.current.getBoundingClientRect() let top = iconRect.top - 8 let left = iconRect.right + 12 // Adjust if tooltip goes off-screen to the right if (left + tooltipRect.width > window.innerWidth - 10) { left = iconRect.left - tooltipRect.width - 12 } // Adjust if tooltip goes off-screen at the bottom if (top + tooltipRect.height > window.innerHeight - 10) { top = window.innerHeight - tooltipRect.height - 10 } // Adjust if tooltip goes off-screen at the top if (top < 10) { top = 10 } setPosition({ top, left }) } calculatePosition() // Recalculate on window resize window.addEventListener('resize', calculatePosition) return () => window.removeEventListener('resize', calculatePosition) }, [isOpen]) // Close tooltip when clicking outside useEffect(() => { if (!isOpen) return const handleClickOutside = (e) => { if ( tooltipRef.current && !tooltipRef.current.contains(e.target) && iconRef.current && !iconRef.current.contains(e.target) ) { setIsOpen(false) } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, [isOpen]) const handleIconClick = (e) => { e.stopPropagation() setIsOpen(!isOpen) } const handleIconHover = (open) => { // Only auto-close on hover leave for desktop, not on click if (open) { setIsOpen(true) } } return (
{definition}
{/* Arrow pointer (subtle) */}