import React, { useState, useRef, useEffect } from 'react'; import { createPortal } from 'react-dom'; import { useFloatingPosition } from '../../hooks/useFloatingPosition'; interface TooltipProps { content: string; children: React.ReactNode; position?: 'top' | 'right' | 'bottom' | 'left'; delay?: number; } export function Tooltip({ content, children, position = 'right', delay = 200 }: TooltipProps) { const [isVisible, setIsVisible] = useState(false); const timeoutRef = useRef>(); const anchorRef = useRef(null); const floatingRef = useRef(null); const { style } = useFloatingPosition(anchorRef, floatingRef, { placement: position, offset: 8, enabled: isVisible, }); const show = () => { timeoutRef.current = setTimeout(() => setIsVisible(true), delay); }; const hide = () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); setIsVisible(false); }; useEffect(() => { return () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); }; }, []); return (
{children} {isVisible && createPortal(
{content}
, document.body, )}
); }