import React, { useEffect, useRef, useState } from "react"; import './tooltip.css'; type Props = { message: string, position: 'top' | 'right' | 'bottom' | 'left', children: React.ReactNode, actions: 'hover' | 'click' } const Tooltip = (props:Props) => { const {message, position, actions, children} = props; const [show, setShow] = useState(false); const dropdownRef = useRef(null); const tooltipShow = () => { setShow(!show) } useEffect(() => { const pageClickEvent = (e:any) => { if (dropdownRef.current !== null && !dropdownRef.current.contains(e.target)) { setShow(!show); } }; if (show) { window.addEventListener('click', pageClickEvent); } return () => { window.removeEventListener('click', pageClickEvent); } }, [show]) return ( <>
{actions === 'hover' &&
setShow(true)} onMouseLeave={() => setShow(false)} > {children}
} {actions === 'click' &&
{children}
} {message}
); }; export default Tooltip;