2023-07-17 16:01:00 +03:00
|
|
|
import { Menu, MenuItem, SubMenu } from '@szhsin/react-menu';
|
|
|
|
import { cloneElement } from 'preact';
|
2023-08-29 10:23:58 +03:00
|
|
|
import { useRef } from 'preact/hooks';
|
2023-07-17 16:01:00 +03:00
|
|
|
|
|
|
|
function MenuConfirm({
|
|
|
|
subMenu = false,
|
|
|
|
confirm = true,
|
|
|
|
confirmLabel,
|
|
|
|
menuItemClassName,
|
|
|
|
menuFooter,
|
|
|
|
...props
|
|
|
|
}) {
|
|
|
|
const { children, onClick, ...restProps } = props;
|
|
|
|
if (!confirm) {
|
|
|
|
if (subMenu) return <MenuItem {...props} />;
|
|
|
|
if (onClick) {
|
|
|
|
return cloneElement(children, {
|
|
|
|
onClick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
const Parent = subMenu ? SubMenu : Menu;
|
2023-08-29 10:23:58 +03:00
|
|
|
const menuRef = useRef();
|
2023-07-17 16:01:00 +03:00
|
|
|
return (
|
|
|
|
<Parent
|
2023-08-29 10:23:58 +03:00
|
|
|
instanceRef={menuRef}
|
2023-07-17 16:01:00 +03:00
|
|
|
openTrigger="clickOnly"
|
|
|
|
direction="bottom"
|
|
|
|
overflow="auto"
|
|
|
|
gap={-8}
|
|
|
|
shift={8}
|
|
|
|
menuClassName="menu-emphasized"
|
|
|
|
{...restProps}
|
|
|
|
menuButton={subMenu ? undefined : children}
|
|
|
|
label={subMenu ? children : undefined}
|
2023-08-29 10:23:58 +03:00
|
|
|
// Test fix for bug; submenus not opening on Android
|
|
|
|
itemProps={{
|
|
|
|
onPointerMove: (e) => {
|
|
|
|
if (e.pointerType === 'touch') {
|
|
|
|
menuRef.current?.openMenu?.();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onPointerLeave: (e) => {
|
|
|
|
if (e.pointerType === 'touch') {
|
|
|
|
menuRef.current?.openMenu?.();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}}
|
2023-07-17 16:01:00 +03:00
|
|
|
>
|
|
|
|
<MenuItem className={menuItemClassName} onClick={onClick}>
|
|
|
|
{confirmLabel}
|
|
|
|
</MenuItem>
|
|
|
|
{menuFooter}
|
|
|
|
</Parent>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MenuConfirm;
|