Skip to content

Commit

Permalink
Merge pull request #332 from BinaryStudioAcademy/fix/OV-326-close-vid…
Browse files Browse the repository at this point in the history
…eo-menu-on-studio-page

OV-326: Close Video Menu Studio Page When Click Outside
  • Loading branch information
nikita-remeslov authored Sep 17, 2024
2 parents ceb2a6d + 8029bfe commit 9be4314
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions frontend/src/bundles/common/enums/dom-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const DOM_EVENT = {
MOUSE_DOWN: 'mousedown',
} as const;

export { DOM_EVENT };
1 change: 1 addition & 0 deletions frontend/src/bundles/common/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { AppRoute } from './app-route.enum.js';
export { DataStatus } from './data-status.enum.js';
export { DOM_EVENT } from './dom-event.js';
export { VideoPreview } from './video-preview.enum.js';
export {
ApiPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
Icon,
IconButton,
} from '~/bundles/common/components/components.js';
import { DOM_EVENT } from '~/bundles/common/enums/enums.js';
import { useEffect, useRef } from '~/bundles/common/hooks/hooks.js';
import { IconName } from '~/bundles/common/icons/icons.js';

import styles from './styles.module.css';
Expand All @@ -22,8 +24,34 @@ const MenuBody: React.FC<React.PropsWithChildren<Properties>> = ({
onClose,
onChatOpen,
}) => {
const menuReference = useRef<HTMLDivElement | null>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent): void => {
if (
menuReference.current &&
!menuReference.current.contains(event.target as Node)
) {
onClose();
}
};

document.addEventListener(DOM_EVENT.MOUSE_DOWN, handleClickOutside);

return () => {
document.removeEventListener(
DOM_EVENT.MOUSE_DOWN,
handleClickOutside,
);
};
}, [onClose]);

return (
<Box bg="background.900" className={styles['menu-body']}>
<Box
ref={menuReference}
bg="background.900"
className={styles['menu-body']}
>
<Flex
justifyContent="space-between"
alignItems="center"
Expand Down

0 comments on commit 9be4314

Please sign in to comment.