mirror of
https://github.com/hazemKrimi/crimson-quirks-ui.git
synced 2026-05-02 02:30:29 +00:00
Update context menu component
This commit is contained in:
@@ -4,7 +4,7 @@ import { Text } from '..';
|
|||||||
|
|
||||||
type ContextMenuProps = {
|
type ContextMenuProps = {
|
||||||
className?: string;
|
className?: string;
|
||||||
items: Array<{ label: string; action: () => void }>;
|
items: Array<{ label: string; action?: () => void }>;
|
||||||
component: string;
|
component: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -13,28 +13,26 @@ const ContextMenu = ({ items, component, className }: ContextMenuProps) => {
|
|||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const openMenu = () => setOpen(true);
|
const wrapper = ref.current;
|
||||||
const handleClickOutside = (event: MouseEvent) => {
|
|
||||||
if (ref.current && !ref.current.contains(event.target as Node)) {
|
const openMenu = () => setOpen(true);
|
||||||
setOpen(false);
|
const closeMenu = () => setOpen(false);
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
document.addEventListener('mousedown', handleClickOutside);
|
|
||||||
(document.querySelector(`#${component}`) as HTMLElement)?.addEventListener(
|
(document.querySelector(`#${component}`) as HTMLElement)?.addEventListener(
|
||||||
'mouseenter',
|
'mouseenter',
|
||||||
openMenu
|
openMenu
|
||||||
);
|
);
|
||||||
|
ref.current?.addEventListener('mouseleave', closeMenu);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
document.removeEventListener('mousedown', handleClickOutside);
|
|
||||||
(document.querySelector(
|
(document.querySelector(
|
||||||
`#${component}`
|
`#${component}`
|
||||||
) as HTMLElement)?.removeEventListener('mouseenter', openMenu);
|
) as HTMLElement)?.removeEventListener('mouseenter', openMenu);
|
||||||
|
wrapper?.removeEventListener('mouseleave', closeMenu);
|
||||||
};
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
}, [ref]);
|
}, [ref.current]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Wrapper
|
<Wrapper
|
||||||
@@ -54,8 +52,10 @@ const ContextMenu = ({ items, component, className }: ContextMenuProps) => {
|
|||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
<li
|
<li
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setOpen(false);
|
if (action) {
|
||||||
action();
|
setOpen(false);
|
||||||
|
action();
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
key={label}
|
key={label}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user