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

79 lines
2.0 KiB
React
Raw Normal View History

2021-08-14 10:19:29 +05:30
import React, { useEffect, useRef } from 'react';
import cons from '../../../client/state/cons';
import navigation from '../../../client/state/navigation';
2022-02-08 12:43:59 +01:00
import settings from '../../../client/state/settings';
2021-08-14 10:19:29 +05:30
import ContextMenu from '../../atoms/context-menu/ContextMenu';
import EmojiBoard from './EmojiBoard';
let requestCallback = null;
2021-08-14 10:29:28 +05:30
let isEmojiBoardVisible = false;
2021-08-14 10:19:29 +05:30
function EmojiBoardOpener() {
const openerRef = useRef(null);
2022-02-08 12:43:59 +01:00
const searchRef = useRef(null);
2021-08-14 10:19:29 +05:30
function openEmojiBoard(cords, requestEmojiCallback) {
2021-08-14 10:29:28 +05:30
if (requestCallback !== null || isEmojiBoardVisible) {
2021-08-14 10:19:29 +05:30
requestCallback = null;
if (cords.detail === 0) openerRef.current.click();
return;
}
2021-08-22 18:15:20 +05:30
openerRef.current.style.transform = `translate(${cords.x}px, ${cords.y}px)`;
2021-08-14 10:19:29 +05:30
requestCallback = requestEmojiCallback;
openerRef.current.click();
}
function afterEmojiBoardToggle(isVisible) {
2021-08-14 10:29:28 +05:30
isEmojiBoardVisible = isVisible;
2022-02-08 12:43:59 +01:00
if (isVisible) {
if (!settings.isTouchScreenDevice) searchRef.current.focus();
} else {
2021-08-14 10:19:29 +05:30
setTimeout(() => {
2021-08-14 10:29:28 +05:30
if (!isEmojiBoardVisible) requestCallback = null;
2021-08-14 10:19:29 +05:30
}, 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={(
2022-02-08 12:43:59 +01:00
<EmojiBoard onSelect={addEmoji} searchRef={searchRef} />
2021-08-14 10:19:29 +05:30
)}
afterToggle={afterEmojiBoardToggle}
2021-08-14 10:29:28 +05:30
render={(toggleMenu) => (
2021-08-14 10:19:29 +05:30
<input
ref={openerRef}
onClick={toggleMenu}
type="button"
style={{
2021-08-15 13:59:09 +05:30
width: '32px',
height: '32px',
backgroundColor: 'transparent',
2021-08-14 10:19:29 +05:30
position: 'absolute',
top: 0,
left: 0,
padding: 0,
border: 'none',
visibility: 'hidden',
}}
/>
)}
/>
);
}
export default EmojiBoardOpener;