import { Popover, Transition } from '@headlessui/react' import { Fragment, cloneElement, useRef } from 'react' import s from './style.module.css' import cn from '@/utils/classnames' export type HtmlContentProps = { onClose?: () => void onClick?: () => void } type IPopover = { className?: string htmlContent: React.ReactElement popupClassName?: string trigger?: 'click' | 'hover' position?: 'bottom' | 'br' | 'bl' btnElement?: string | React.ReactNode btnClassName?: string | ((open: boolean) => string) manualClose?: boolean disabled?: boolean } const timeoutDuration = 100 export default function CustomPopover({ trigger = 'hover', position = 'bottom', htmlContent, popupClassName, btnElement, className, btnClassName, manualClose, disabled = false, }: IPopover) { const buttonRef = useRef(null) const timeOutRef = useRef(null) const onMouseEnter = (isOpen: boolean) => { timeOutRef.current && clearTimeout(timeOutRef.current) !isOpen && buttonRef.current?.click() } const onMouseLeave = (isOpen: boolean) => { timeOutRef.current = setTimeout(() => { isOpen && buttonRef.current?.click() }, timeoutDuration) } return ( {({ open }: { open: boolean }) => { return ( <>
onMouseLeave(open), onMouseEnter: () => onMouseEnter(open), })} > {btnElement} onMouseLeave(open), onMouseEnter: () => onMouseEnter(open), }) } > {({ close }) => (
onMouseLeave(open), onMouseEnter: () => onMouseEnter(open), }) } > {cloneElement(htmlContent as React.ReactElement, { onClose: () => onMouseLeave(open), ...(manualClose ? { onClick: close, } : {}), })}
)}
) }}
) }