import { useState, useRef, useEffect } from 'react' /** * Tooltip компонент в стиле Tabler UI * Использует position: absolute относительно trigger элемента */ function Tooltip({ children, content, position = 'top', // top, bottom, left, right shortcut, // keyboard shortcut to display delay = 200, className = '' }) { const [isVisible, setIsVisible] = useState(false) const timeoutRef = useRef(null) const triggerRef = useRef(null) const showTooltip = () => { if (timeoutRef.current) clearTimeout(timeoutRef.current) timeoutRef.current = setTimeout(() => { setIsVisible(true) }, delay) } const hideTooltip = () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current) } setIsVisible(false) } useEffect(() => { return () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current) } } }, []) return ( {children} {isVisible && (
{ const styles = { position: 'absolute', width: '8px', height: '8px', background: 'inherit' } if (position === 'top') { styles.bottom = '0' styles.left = '50%' styles.transform = 'translateX(-50%) translateY(50%) rotate(45deg)' } else if (position === 'bottom') { styles.top = '0' styles.left = '50%' styles.transform = 'translateX(-50%) translateY(-50%) rotate(45deg)' } else if (position === 'left') { styles.right = '0' styles.top = '50%' styles.transform = 'translateX(50%) translateY(-50%) rotate(45deg)' } else if (position === 'right') { styles.left = '0' styles.top = '50%' styles.transform = 'translateX(-50%) translateY(-50%) rotate(45deg)' } return styles })()}>
{content} {shortcut && ( {shortcut} )}
)}
) } export default Tooltip