import clsx from 'clsx'; import { supportsPassiveEvents } from 'detect-passive-events'; import React, { KeyboardEvent, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { defineMessages, useIntl } from 'react-intl'; import { usePopper } from 'react-popper'; import { IconButton } from 'soapbox/components/ui'; import { isMobile } from 'soapbox/is-mobile'; import EmojiPickerDropdown, { IEmojiPickerDropdown } from '../components/emoji-picker-dropdown'; const listenerOptions = supportsPassiveEvents ? { passive: true } : false; export const messages = defineMessages({ emoji: { id: 'emoji_button.label', defaultMessage: 'Insert emoji' }, }); const EmojiPickerDropdownContainer = ( props: Pick, ) => { const intl = useIntl(); const title = intl.formatMessage(messages.emoji); const [popperElement, setPopperElement] = useState(null); const [popperReference, setPopperReference] = useState(null); const [containerElement, setContainerElement] = useState(null); const [visible, setVisible] = useState(false); const placement = props.condensed ? 'bottom-start' : 'top-start'; const { styles, attributes, update } = usePopper(popperReference, popperElement, { placement: isMobile(window.innerWidth) ? 'auto' : placement, }); const handleDocClick = (e: any) => { if (!containerElement?.contains(e.target) && !popperElement?.contains(e.target)) { setVisible(false); } }; const handleToggle = (e: MouseEvent | KeyboardEvent) => { e.stopPropagation(); setVisible(!visible); }; // TODO: move to class const style: React.CSSProperties = !isMobile(window.innerWidth) ? styles.popper : { ...styles.popper, width: '100%', }; useEffect(() => { document.addEventListener('click', handleDocClick, false); document.addEventListener('touchend', handleDocClick, listenerOptions); return () => { document.removeEventListener('click', handleDocClick, false); // @ts-ignore document.removeEventListener('touchend', handleDocClick, listenerOptions); }; }); return (
} tabIndex={0} /> {createPortal(
, document.body, )}
); }; export default EmojiPickerDropdownContainer;