114 lines
2.7 KiB
TypeScript
114 lines
2.7 KiB
TypeScript
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<HTMLDivElement>(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 (
|
|
<div className="emoji-picker" ref={ref}>
|
|
{recent.length > 0 && (
|
|
<div className="emoji-section">
|
|
<div className="emoji-section-label">{t('Recent')}</div>
|
|
<div className="emoji-grid">
|
|
{recent.map((token, i) =>
|
|
token.startsWith(':') ? (
|
|
<button
|
|
key={i}
|
|
className="emoji-btn"
|
|
onClick={() => pick(token)}
|
|
>
|
|
<img
|
|
src={`/emoji/${token.slice(1, -1)}`}
|
|
alt={token}
|
|
className="emoji-img"
|
|
/>
|
|
</button>
|
|
) : (
|
|
<button
|
|
key={i}
|
|
className="emoji-btn"
|
|
onClick={() => pick(token)}
|
|
>
|
|
{token}
|
|
</button>
|
|
)
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
{custom.length > 0 && (
|
|
<div className="emoji-section">
|
|
<div className="emoji-section-label">{t('Custom')}</div>
|
|
<div className="emoji-grid">
|
|
{custom.map((c) => (
|
|
<button
|
|
key={c.name}
|
|
className="emoji-btn"
|
|
title={`:${c.name}:`}
|
|
onClick={() => pick(`:${c.name}:`)}
|
|
>
|
|
<img
|
|
src={`/emoji/${c.name}`}
|
|
alt={c.name}
|
|
className="emoji-img"
|
|
/>
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
{EMOJI_CATEGORIES.map((cat) => (
|
|
<div key={cat.label} className="emoji-section">
|
|
<div className="emoji-section-label">{cat.label}</div>
|
|
<div className="emoji-grid">
|
|
{cat.emoji.map((e) => (
|
|
<button
|
|
key={e}
|
|
className="emoji-btn"
|
|
onClick={() => pick(e)}
|
|
>
|
|
{e}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|