import { useEffect, useRef, useState } from 'react'; import conditionalClass from '../../../lib/utils/conditional-class'; import styles from './Dropdown.module.scss'; export const Dropdown = ({ children, opens = 'left', }: { children: React.ReactNode; opens?: 'right' | 'left'; }) => { const [visible, setVisible] = useState(false); const ref = useRef(null); // Off click listener useEffect(() => { if (!visible || !ref) return; function handleClickOutside(event: MouseEvent | TouchEvent) { if (ref.current && !ref.current.contains(event.target as HTMLElement)) { setVisible(false); } } document.addEventListener('mousedown', handleClickOutside); document.addEventListener('touchstart', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('touchstart', handleClickOutside); }; }, [ref, visible]); return (
{visible &&
{children}
}
); };