2023-03-20 11:57:29 -07:00
|
|
|
import { shift, useFloating, Placement, offset, OffsetOptions } from '@floating-ui/react';
|
2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2023-02-08 09:58:01 -08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-03-31 19:17:29 -07:00
|
|
|
|
2023-03-01 11:27:16 -08:00
|
|
|
import { Emoji as EmojiComponent, HStack, IconButton } from 'soapbox/components/ui';
|
2023-02-28 09:59:32 -08:00
|
|
|
import EmojiPickerDropdown from 'soapbox/features/emoji/components/emoji-picker-dropdown';
|
2023-03-19 15:52:45 -07:00
|
|
|
import { useClickOutside, useFeatures, useSoapboxConfig } from 'soapbox/hooks';
|
2022-03-31 19:17:29 -07:00
|
|
|
|
2023-03-17 16:07:18 -07:00
|
|
|
import type { Emoji } from 'soapbox/features/emoji';
|
2023-03-01 11:27:16 -08:00
|
|
|
|
2022-03-31 19:17:29 -07:00
|
|
|
interface IEmojiButton {
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Unicode emoji character. */
|
2023-02-15 13:26:27 -08:00
|
|
|
emoji: string
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Event handler when the emoji is clicked. */
|
2023-02-08 09:58:01 -08:00
|
|
|
onClick(emoji: string): void
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Extra class name on the <button> element. */
|
2023-02-15 13:26:27 -08:00
|
|
|
className?: string
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Tab order of the button. */
|
2023-02-15 13:26:27 -08:00
|
|
|
tabIndex?: number
|
2022-03-31 19:17:29 -07:00
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Clickable emoji button that scales when hovered. */
|
2022-03-31 19:17:29 -07:00
|
|
|
const EmojiButton: React.FC<IEmojiButton> = ({ emoji, className, onClick, tabIndex }): JSX.Element => {
|
2023-02-08 09:58:01 -08:00
|
|
|
const handleClick: React.EventHandler<React.MouseEvent> = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
onClick(emoji);
|
|
|
|
};
|
|
|
|
|
2022-03-31 19:17:29 -07:00
|
|
|
return (
|
2023-02-08 09:58:01 -08:00
|
|
|
<button className={clsx(className)} onClick={handleClick} tabIndex={tabIndex}>
|
2023-03-01 11:27:16 -08:00
|
|
|
<EmojiComponent className='h-6 w-6 duration-100 hover:scale-110' emoji={emoji} />
|
2022-03-31 19:17:29 -07:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IEmojiSelector {
|
2023-02-08 09:58:01 -08:00
|
|
|
onClose?(): void
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Event handler when an emoji is clicked. */
|
2023-03-17 16:07:18 -07:00
|
|
|
onReact(emoji: string, custom?: string): void
|
2023-02-08 09:58:01 -08:00
|
|
|
/** Element that triggers the EmojiSelector Popper */
|
|
|
|
referenceElement: HTMLElement | null
|
|
|
|
placement?: Placement
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Whether the selector should be visible. */
|
2023-02-08 09:58:01 -08:00
|
|
|
visible?: boolean
|
2023-03-20 11:57:29 -07:00
|
|
|
offsetOptions?: OffsetOptions
|
2023-02-08 17:20:17 -08:00
|
|
|
/** Whether to allow any emoji to be chosen. */
|
|
|
|
all?: boolean
|
2022-03-31 19:17:29 -07:00
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Panel with a row of emoji buttons. */
|
2023-02-08 09:58:01 -08:00
|
|
|
const EmojiSelector: React.FC<IEmojiSelector> = ({
|
|
|
|
referenceElement,
|
|
|
|
onClose,
|
|
|
|
onReact,
|
|
|
|
placement = 'top',
|
|
|
|
visible = false,
|
2023-03-20 11:57:29 -07:00
|
|
|
offsetOptions,
|
2023-02-08 17:20:17 -08:00
|
|
|
all = true,
|
2023-02-08 09:58:01 -08:00
|
|
|
}): JSX.Element => {
|
|
|
|
const soapboxConfig = useSoapboxConfig();
|
2023-03-17 16:07:18 -07:00
|
|
|
const { customEmojiReacts } = useFeatures();
|
2022-03-31 19:17:29 -07:00
|
|
|
|
2023-02-08 17:20:17 -08:00
|
|
|
const [expanded, setExpanded] = useState(false);
|
|
|
|
|
2023-03-19 15:52:45 -07:00
|
|
|
const { x, y, strategy, refs, update } = useFloating<HTMLElement>({
|
2023-02-08 09:58:01 -08:00
|
|
|
placement,
|
2023-03-20 11:57:29 -07:00
|
|
|
middleware: [offset(offsetOptions), shift()],
|
2023-02-08 09:58:01 -08:00
|
|
|
});
|
2022-03-31 19:17:29 -07:00
|
|
|
|
2023-02-08 17:20:17 -08:00
|
|
|
const handleExpand: React.MouseEventHandler = () => {
|
|
|
|
setExpanded(true);
|
|
|
|
};
|
|
|
|
|
2023-03-01 11:27:16 -08:00
|
|
|
const handlePickEmoji = (emoji: Emoji) => {
|
2023-03-17 16:07:18 -07:00
|
|
|
onReact(emoji.custom ? emoji.id : emoji.native, emoji.custom ? emoji.imageUrl : undefined);
|
2023-03-01 11:27:16 -08:00
|
|
|
};
|
|
|
|
|
2023-03-19 15:52:45 -07:00
|
|
|
useEffect(() => {
|
|
|
|
refs.setReference(referenceElement);
|
|
|
|
}, [referenceElement]);
|
|
|
|
|
2023-02-25 14:30:24 -08:00
|
|
|
useEffect(() => () => {
|
|
|
|
document.body.style.overflow = '';
|
|
|
|
}, []);
|
|
|
|
|
2023-02-08 17:20:17 -08:00
|
|
|
useEffect(() => {
|
|
|
|
setExpanded(false);
|
|
|
|
}, [visible]);
|
|
|
|
|
2023-03-19 15:52:45 -07:00
|
|
|
useClickOutside(refs, () => {
|
|
|
|
if (onClose) {
|
|
|
|
onClose();
|
2023-02-14 13:18:56 -08:00
|
|
|
}
|
2023-03-19 15:52:45 -07:00
|
|
|
});
|
2023-02-23 08:42:31 -08:00
|
|
|
|
2022-03-31 19:17:29 -07:00
|
|
|
return (
|
2023-02-08 09:58:01 -08:00
|
|
|
<div
|
2023-02-14 09:18:20 -08:00
|
|
|
className={clsx('z-[101] transition-opacity duration-100', {
|
2023-02-08 09:58:01 -08:00
|
|
|
'opacity-0 pointer-events-none': !visible,
|
|
|
|
})}
|
2023-03-19 15:52:45 -07:00
|
|
|
ref={refs.setFloating}
|
|
|
|
style={{
|
|
|
|
position: strategy,
|
|
|
|
top: y ?? 0,
|
|
|
|
left: x ?? 0,
|
|
|
|
width: 'max-content',
|
|
|
|
}}
|
2022-03-31 19:17:29 -07:00
|
|
|
>
|
2023-02-08 17:20:17 -08:00
|
|
|
{expanded ? (
|
2023-02-28 09:59:32 -08:00
|
|
|
<EmojiPickerDropdown
|
|
|
|
visible={expanded}
|
|
|
|
setVisible={setExpanded}
|
|
|
|
update={update}
|
2023-03-17 16:07:18 -07:00
|
|
|
withCustom={customEmojiReacts}
|
2023-03-01 11:27:16 -08:00
|
|
|
onPickEmoji={handlePickEmoji}
|
2022-03-31 19:17:29 -07:00
|
|
|
/>
|
2023-02-08 17:20:17 -08:00
|
|
|
) : (
|
|
|
|
<HStack
|
|
|
|
className={clsx('z-[999] flex w-max max-w-[100vw] flex-wrap space-x-3 rounded-full bg-white px-3 py-2.5 shadow-lg focus:outline-none dark:bg-gray-900 dark:ring-2 dark:ring-primary-700')}
|
|
|
|
>
|
|
|
|
{Array.from(soapboxConfig.allowedEmoji).map((emoji, i) => (
|
|
|
|
<EmojiButton
|
|
|
|
key={i}
|
|
|
|
emoji={emoji}
|
|
|
|
onClick={onReact}
|
|
|
|
tabIndex={visible ? 0 : -1}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
|
|
|
{all && (
|
|
|
|
<IconButton
|
2023-02-12 18:29:33 -08:00
|
|
|
className='text-gray-600 hover:text-gray-600 dark:hover:text-white'
|
2023-02-08 17:20:17 -08:00
|
|
|
src={require('@tabler/icons/dots.svg')}
|
|
|
|
onClick={handleExpand}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</HStack>
|
|
|
|
)}
|
2023-02-08 09:58:01 -08:00
|
|
|
</div>
|
2022-03-31 19:17:29 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EmojiSelector;
|