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 = {
|
||||
className?: string;
|
||||
items: Array<{ label: string; action: () => void }>;
|
||||
items: Array<{ label: string; action?: () => void }>;
|
||||
component: string;
|
||||
};
|
||||
|
||||
@@ -13,28 +13,26 @@ const ContextMenu = ({ items, component, className }: ContextMenuProps) => {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const openMenu = () => setOpen(true);
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (ref.current && !ref.current.contains(event.target as Node)) {
|
||||
setOpen(false);
|
||||
}
|
||||
};
|
||||
const wrapper = ref.current;
|
||||
|
||||
const openMenu = () => setOpen(true);
|
||||
const closeMenu = () => setOpen(false);
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
(document.querySelector(`#${component}`) as HTMLElement)?.addEventListener(
|
||||
'mouseenter',
|
||||
openMenu
|
||||
);
|
||||
ref.current?.addEventListener('mouseleave', closeMenu);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
(document.querySelector(
|
||||
`#${component}`
|
||||
) as HTMLElement)?.removeEventListener('mouseenter', openMenu);
|
||||
wrapper?.removeEventListener('mouseleave', closeMenu);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line
|
||||
}, [ref]);
|
||||
}, [ref.current]);
|
||||
|
||||
return (
|
||||
<Wrapper
|
||||
@@ -54,8 +52,10 @@ const ContextMenu = ({ items, component, className }: ContextMenuProps) => {
|
||||
// eslint-disable-next-line
|
||||
<li
|
||||
onClick={() => {
|
||||
setOpen(false);
|
||||
action();
|
||||
if (action) {
|
||||
setOpen(false);
|
||||
action();
|
||||
}
|
||||
}}
|
||||
key={label}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user