import React, { useRef, useState } from 'react'; import { Transition } from '@headlessui/react'; import { usePopper } from 'react-popper'; export function HoverPopover({ children, card, }: { children: React.ReactNode; card: React.ReactNode | ((args: { open: boolean; close?: () => void }) => React.ReactNode); }) { const buttonRef = useRef(null); const timeoutDuration = 300; const [open, setOpen] = useState(false); let openTimeout: NodeJS.Timeout | undefined; let closeTimeout: NodeJS.Timeout | undefined; const onMouseEnter = () => { clearTimeout(closeTimeout); if (open) return; setOpen(true); }; const onMouseLeave = () => { clearTimeout(openTimeout); if (!open) return; closeTimeout = setTimeout(() => setOpen(false), timeoutDuration); }; const [popperElement, setPopperElement] = useState(null); const { styles, attributes } = usePopper(buttonRef.current, popperElement, { placement: 'bottom-start', }); return ( {children} {typeof card === 'function' ? card({ open, close: () => setOpen(false) }) : card} ); }