import { supportsPassiveEvents } from 'detect-passive-events'; import React, { useCallback, useEffect, useRef } from 'react'; import { Emoji } from './emoji-picker-dropdown'; const listenerOptions = supportsPassiveEvents ? { passive: true } : false; const backgroundImageFn = () => require('emoji-datasource/img/twitter/sheets/32.png'); interface IModifierPickerMenu { active: boolean, onSelect: (modifier: number) => void, onClose: () => void, } const ModifierPickerMenu: React.FC = ({ active, onSelect, onClose }) => { const node = useRef(null); const handleClick: React.MouseEventHandler = e => { onSelect(+e.currentTarget.getAttribute('data-index')! * 1); }; const handleDocumentClick = useCallback((e => { if (node.current && !node.current.contains(e.target)) { onClose(); } }), []); const attachListeners = () => { document.addEventListener('click', handleDocumentClick, false); document.addEventListener('touchend', handleDocumentClick, listenerOptions); }; const removeListeners = () => { document.removeEventListener('click', handleDocumentClick, false); document.removeEventListener('touchend', handleDocumentClick, listenerOptions as any); }; useEffect(() => { return () => { removeListeners(); }; }, []); useEffect(() => { if (active) attachListeners(); else removeListeners(); }, [active]); return (
); }; export default ModifierPickerMenu;