import { Dialog, DialogTitle } from '@headlessui/react'; import { Icon } from '@wordpress/components'; import { useEffect, useLayoutEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { close } from '@wordpress/icons'; export const ToolTip = ({ children, title, onClose, anchor }) => { const [open, setOpen] = useState(false); const [left, setLeft] = useState(0); const [top, setTop] = useState(0); const onCloseModal = () => { setOpen(false); onClose?.(); }; useLayoutEffect(() => { if (!anchor || !open) return; // set the anchor's outline to something thick const prevOutline = anchor.style.outline; anchor.style.outline = '99999px solid rgba(0, 0, 0, 0.4)'; // find the anchor's center (now only support bottom center) const rect = anchor.getBoundingClientRect(); const x = rect.left + rect.width / 2; const y = rect.top + rect.height; setLeft(x); setTop(y); return () => { anchor.style.outline = prevOutline; }; }, [anchor, open]); useEffect(() => { setOpen(true); }, [left, top]); if (!open) return; return (
{/* overlay to prevent click through */}
{title}
{children}
); };