import { useEffect, useRef } from 'react'; import { useStore } from '../lib/store'; import { EMOJI_CATEGORIES, emojiStore, rememberEmoji, } from '../lib/emoji'; import { t } from '../i18n'; // A popover emoji picker: recently-used, curated Unicode categories, and the // server's custom image emojis. `onPick` receives either a unicode char or a // :shortcode: for a custom emoji. export function EmojiPicker(props: { onPick: (token: string) => void; onClose: () => void; }) { const { custom, recent } = useStore(emojiStore); const ref = useRef(null); useEffect(() => { const onDoc = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { props.onClose(); } }; // Defer so the opening click doesn't immediately close it. const id = setTimeout( () => document.addEventListener('mousedown', onDoc), 0 ); return () => { clearTimeout(id); document.removeEventListener('mousedown', onDoc); }; }, [props]); const pick = (token: string) => { rememberEmoji(token); props.onPick(token); }; return (
{recent.length > 0 && (
{t('Recent')}
{recent.map((token, i) => token.startsWith(':') ? ( ) : ( ) )}
)} {custom.length > 0 && (
{t('Custom')}
{custom.map((c) => ( ))}
)} {EMOJI_CATEGORIES.map((cat) => (
{cat.label}
{cat.emoji.map((e) => ( ))}
))}
); }