2022-08-31 02:35:06 -07:00
|
|
|
import classNames from 'clsx';
|
2023-02-06 09:18:49 -08:00
|
|
|
import React, { useRef } from 'react';
|
2022-03-31 19:17:29 -07:00
|
|
|
|
2022-04-01 09:42:07 -07:00
|
|
|
import { Emoji, HStack } from 'soapbox/components/ui';
|
2022-03-31 19:17:29 -07:00
|
|
|
|
|
|
|
interface IEmojiButton {
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Unicode emoji character. */
|
2022-03-31 19:17:29 -07:00
|
|
|
emoji: string,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Event handler when the emoji is clicked. */
|
2022-03-31 19:17:29 -07:00
|
|
|
onClick: React.EventHandler<React.MouseEvent>,
|
2023-02-06 09:18:49 -08:00
|
|
|
/** Keyboard event handler. */
|
|
|
|
onKeyDown?: React.EventHandler<React.KeyboardEvent>,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Extra class name on the <button> element. */
|
2022-03-31 19:17:29 -07:00
|
|
|
className?: string,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Tab order of the button. */
|
2022-03-31 19:17:29 -07:00
|
|
|
tabIndex?: number,
|
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Clickable emoji button that scales when hovered. */
|
2023-02-06 09:18:49 -08:00
|
|
|
const EmojiButton: React.FC<IEmojiButton> = ({ emoji, className, onClick, onKeyDown, tabIndex }): JSX.Element => {
|
2022-03-31 19:17:29 -07:00
|
|
|
return (
|
2023-02-06 09:18:49 -08:00
|
|
|
<button className={classNames(className)} onClick={onClick} onKeyDown={onKeyDown} tabIndex={tabIndex}>
|
|
|
|
<Emoji className='h-8 w-8 duration-100' emoji={emoji} />
|
2022-03-31 19:17:29 -07:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IEmojiSelector {
|
2022-04-30 21:39:58 -07:00
|
|
|
/** List of Unicode emoji characters. */
|
2022-04-10 18:31:24 -07:00
|
|
|
emojis: Iterable<string>,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Event handler when an emoji is clicked. */
|
2022-03-31 19:17:29 -07:00
|
|
|
onReact: (emoji: string) => void,
|
2023-02-06 09:18:49 -08:00
|
|
|
/** Event handler when selector is escaped. */
|
|
|
|
onUnfocus: React.KeyboardEventHandler<HTMLDivElement>,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Whether the selector should be visible. */
|
2022-03-31 19:17:29 -07:00
|
|
|
visible?: boolean,
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Whether the selector should be focused. */
|
2022-03-31 19:17:29 -07:00
|
|
|
focused?: boolean,
|
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Panel with a row of emoji buttons. */
|
2023-02-06 09:18:49 -08:00
|
|
|
const EmojiSelector: React.FC<IEmojiSelector> = ({ emojis, onReact, onUnfocus, visible = false, focused = false }): JSX.Element => {
|
|
|
|
const emojiList = Array.from(emojis);
|
|
|
|
const node = useRef<HTMLDivElement>(null);
|
2022-03-31 19:17:29 -07:00
|
|
|
|
|
|
|
const handleReact = (emoji: string): React.EventHandler<React.MouseEvent> => {
|
|
|
|
return (e) => {
|
|
|
|
onReact(emoji);
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-06 09:18:49 -08:00
|
|
|
const selectPreviousEmoji = (i: number): void => {
|
|
|
|
if (!node.current) return;
|
|
|
|
|
|
|
|
if (i !== 0) {
|
|
|
|
const button: HTMLButtonElement | null = node.current.querySelector(`.emoji-react-selector__emoji:nth-child(${i})`);
|
|
|
|
button?.focus();
|
|
|
|
} else {
|
|
|
|
const button: HTMLButtonElement | null = node.current.querySelector('.emoji-react-selector__emoji:last-child');
|
|
|
|
button?.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectNextEmoji = (i: number) => {
|
|
|
|
if (!node.current) return;
|
|
|
|
|
|
|
|
if (i !== emojiList.length - 1) {
|
|
|
|
const button: HTMLButtonElement | null = node.current.querySelector(`.emoji-react-selector__emoji:nth-child(${i + 2})`);
|
|
|
|
button?.focus();
|
|
|
|
} else {
|
|
|
|
const button: HTMLButtonElement | null = node.current.querySelector('.emoji-react-selector__emoji:first-child');
|
|
|
|
button?.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = (i: number): React.KeyboardEventHandler<HTMLDivElement> => e => {
|
|
|
|
switch (e.key) {
|
|
|
|
case 'Enter':
|
|
|
|
handleReact(emojiList[i])(e as any);
|
|
|
|
break;
|
|
|
|
case 'Tab':
|
|
|
|
e.preventDefault();
|
|
|
|
if (e.shiftKey) selectPreviousEmoji(i);
|
|
|
|
else selectNextEmoji(i);
|
|
|
|
break;
|
|
|
|
case 'Left':
|
|
|
|
case 'ArrowLeft':
|
|
|
|
selectPreviousEmoji(i);
|
|
|
|
break;
|
|
|
|
case 'Right':
|
|
|
|
case 'ArrowRight':
|
|
|
|
selectNextEmoji(i);
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
onUnfocus(e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-31 19:17:29 -07:00
|
|
|
return (
|
2022-04-01 09:42:07 -07:00
|
|
|
<HStack
|
2023-02-06 09:18:49 -08:00
|
|
|
className={classNames('emoji-react-selector z-[999] w-max max-w-[100vw] flex-wrap gap-2 rounded-full bg-white p-3 shadow-md dark:bg-gray-900')}
|
|
|
|
ref={node}
|
2022-03-31 19:17:29 -07:00
|
|
|
>
|
2023-02-06 09:18:49 -08:00
|
|
|
{emojiList.map((emoji, i) => (
|
2022-03-31 19:17:29 -07:00
|
|
|
<EmojiButton
|
2023-02-06 09:18:49 -08:00
|
|
|
className='emoji-react-selector__emoji hover:scale-125 focus:scale-125'
|
2022-03-31 19:17:29 -07:00
|
|
|
key={i}
|
|
|
|
emoji={emoji}
|
|
|
|
onClick={handleReact(emoji)}
|
2023-02-06 09:18:49 -08:00
|
|
|
onKeyDown={handleKeyDown(i)}
|
2022-03-31 19:17:29 -07:00
|
|
|
tabIndex={(visible || focused) ? 0 : -1}
|
|
|
|
/>
|
|
|
|
))}
|
2022-04-01 09:42:07 -07:00
|
|
|
</HStack>
|
2022-03-31 19:17:29 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EmojiSelector;
|