signal-desktop/ts/components/conversation/ReactionPicker.tsx

135 lines
3.8 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-01-23 23:57:37 +00:00
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<Props, 'onClose' | 'style'> &
Pick<EmojiPickerProps, 'onPickEmoji'> & {
ref: React.Ref<HTMLDivElement>;
};
2020-01-23 23:57:37 +00:00
export type OwnProps = {
i18n: LocalizerType;
2020-01-23 23:57:37 +00:00
selected?: string;
onClose?: () => unknown;
onPick: (emoji: string) => unknown;
renderEmojiPicker: (props: RenderEmojiPickerProps) => React.ReactElement;
2020-10-02 20:05:09 +00:00
skinTone: number;
2020-01-23 23:57:37 +00:00
};
export type Props = OwnProps & Pick<React.HTMLProps<HTMLDivElement>, 'style'>;
2020-10-02 20:05:09 +00:00
const DEFAULT_EMOJI_LIST = [
'heart',
'thumbsup',
'thumbsdown',
'joy',
'open_mouth',
'cry',
];
2020-01-23 23:57:37 +00:00
export const ReactionPicker = React.forwardRef<HTMLDivElement, Props>(
2020-10-02 20:05:09 +00:00
(
{ i18n, selected, onClose, skinTone, onPick, renderEmojiPicker, style },
ref
) => {
const [pickingOther, setPickingOther] = React.useState(false);
2020-01-23 23:57:37 +00:00
// 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(
2020-10-02 20:05:09 +00:00
({ shortName, skinTone: pickedSkinTone }) => {
onPick(convertShortName(shortName, pickedSkinTone));
},
[onPick]
);
2020-10-02 20:05:09 +00:00
const emojis = DEFAULT_EMOJI_LIST.map(shortName =>
convertShortName(shortName, skinTone)
);
2020-01-23 23:57:37 +00:00
// Focus first button and restore focus on unmount
const [focusRef] = useRestoreFocus();
2020-01-23 23:57:37 +00:00
2020-06-03 18:36:17 +00:00
const otherSelected = selected && !emojis.includes(selected);
return pickingOther ? (
renderEmojiPicker({ onPickEmoji, onClose, style, ref })
) : (
<div ref={ref} style={style} className="module-reaction-picker">
2020-06-03 18:36:17 +00:00
{emojis.map((emoji, index) => {
2020-01-23 23:57:37 +00:00
const maybeFocusRef = index === 0 ? focusRef : undefined;
return (
<button
2020-09-14 19:51:27 +00:00
type="button"
2020-01-23 23:57:37 +00:00
key={emoji}
ref={maybeFocusRef}
tabIndex={0}
className={classNames(
'module-reaction-picker__emoji-btn',
emoji === selected
? 'module-reaction-picker__emoji-btn--selected'
: null
)}
onClick={e => {
e.stopPropagation();
onPick(emoji);
}}
title={emoji}
2020-01-23 23:57:37 +00:00
>
<div className="module-reaction-picker__emoji-btn__emoji">
<Emoji size={48} emoji={emoji} />
</div>
</button>
);
})}
2020-06-03 18:36:17 +00:00
<button
2020-09-14 19:51:27 +00:00
type="button"
2020-06-03 18:36:17 +00:00
className={classNames(
'module-reaction-picker__emoji-btn',
otherSelected
? 'module-reaction-picker__emoji-btn--selected'
: 'module-reaction-picker__emoji-btn--more'
)}
onClick={e => {
e.stopPropagation();
if (otherSelected && selected) {
onPick(selected);
} else {
setPickingOther(true);
}
}}
title={i18n('ReactionsViewer--more')}
>
{otherSelected ? (
<div className="module-reaction-picker__emoji-btn__emoji">
<Emoji size={48} emoji={selected} />
</div>
) : null}
</button>
2020-01-23 23:57:37 +00:00
</div>
);
}
);