cinny/src/app/organisms/emoji-board/EmojiBoardOpener.jsx

75 lines
1.9 KiB
React
Raw Normal View History

2021-08-14 07:49:29 +03:00
import React, { useEffect, useRef } from 'react';
import cons from '../../../client/state/cons';
import navigation from '../../../client/state/navigation';
import ContextMenu from '../../atoms/context-menu/ContextMenu';
import EmojiBoard from './EmojiBoard';
let requestCallback = null;
2021-08-14 07:59:28 +03:00
let isEmojiBoardVisible = false;
2021-08-14 07:49:29 +03:00
function EmojiBoardOpener() {
const openerRef = useRef(null);
function openEmojiBoard(cords, requestEmojiCallback) {
2021-08-14 07:59:28 +03:00
if (requestCallback !== null || isEmojiBoardVisible) {
2021-08-14 07:49:29 +03:00
requestCallback = null;
if (cords.detail === 0) openerRef.current.click();
return;
}
2021-08-22 15:45:20 +03:00
openerRef.current.style.transform = `translate(${cords.x}px, ${cords.y}px)`;
2021-08-14 07:49:29 +03:00
requestCallback = requestEmojiCallback;
openerRef.current.click();
}
function afterEmojiBoardToggle(isVisible) {
2021-08-14 07:59:28 +03:00
isEmojiBoardVisible = isVisible;
2021-08-14 07:49:29 +03:00
if (!isVisible) {
setTimeout(() => {
2021-08-14 07:59:28 +03:00
if (!isEmojiBoardVisible) requestCallback = null;
2021-08-14 07:49:29 +03:00
}, 500);
}
}
function addEmoji(emoji) {
requestCallback(emoji);
}
useEffect(() => {
navigation.on(cons.events.navigation.EMOJIBOARD_OPENED, openEmojiBoard);
return () => {
navigation.removeListener(cons.events.navigation.EMOJIBOARD_OPENED, openEmojiBoard);
};
}, []);
return (
<ContextMenu
content={(
<EmojiBoard onSelect={addEmoji} />
)}
afterToggle={afterEmojiBoardToggle}
2021-08-14 07:59:28 +03:00
render={(toggleMenu) => (
2021-08-14 07:49:29 +03:00
<input
ref={openerRef}
onClick={toggleMenu}
type="button"
style={{
2021-08-15 11:29:09 +03:00
width: '32px',
height: '32px',
backgroundColor: 'transparent',
2021-08-14 07:49:29 +03:00
position: 'absolute',
top: 0,
left: 0,
padding: 0,
border: 'none',
visibility: 'hidden',
}}
/>
)}
/>
);
}
export default EmojiBoardOpener;