// Copyright 2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import * as React from 'react'; import classNames from 'classnames'; import { Emoji } from '../emoji/Emoji'; import { convertShortName } from '../emoji/lib'; import { Props as EmojiPickerProps } from '../emoji/EmojiPicker'; import { useRestoreFocus } from '../../util/hooks'; import { LocalizerType } from '../../types/Util'; export type RenderEmojiPickerProps = Pick & Pick & { ref: React.Ref; }; export type OwnProps = { i18n: LocalizerType; selected?: string; onClose?: () => unknown; onPick: (emoji: string) => unknown; renderEmojiPicker: (props: RenderEmojiPickerProps) => React.ReactElement; skinTone: number; }; export type Props = OwnProps & Pick, 'style'>; const DEFAULT_EMOJI_LIST = [ 'heart', 'thumbsup', 'thumbsdown', 'joy', 'open_mouth', 'cry', ]; export const ReactionPicker = React.forwardRef( ( { i18n, selected, onClose, skinTone, onPick, renderEmojiPicker, style }, ref ) => { const [pickingOther, setPickingOther] = React.useState(false); const focusRef = React.useRef(null); // Handle escape key React.useEffect(() => { const handler = (e: KeyboardEvent) => { if (onClose && e.key === 'Escape') { onClose(); } }; document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [onClose]); // Handle EmojiPicker::onPickEmoji const onPickEmoji: EmojiPickerProps['onPickEmoji'] = React.useCallback( ({ shortName, skinTone: pickedSkinTone }) => { onPick(convertShortName(shortName, pickedSkinTone)); }, [onPick] ); const emojis = DEFAULT_EMOJI_LIST.map(shortName => convertShortName(shortName, skinTone) ); // Focus first button and restore focus on unmount useRestoreFocus(focusRef); const otherSelected = selected && !emojis.includes(selected); return pickingOther ? ( renderEmojiPicker({ onPickEmoji, onClose, style, ref }) ) : (
{emojis.map((emoji, index) => { const maybeFocusRef = index === 0 ? focusRef : undefined; return ( ); })}
); } );