phanpy/src/components/menu2.jsx

34 lines
951 B
React
Raw Normal View History

2023-06-13 12:46:37 +03:00
import { Menu } from '@szhsin/react-menu';
import { useWindowSize } from '@uidotdev/usehooks';
import { useRef } from 'preact/hooks';
import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding';
// It's like Menu but with sensible defaults, bug fixes and improvements.
function Menu2(props) {
const { containerProps, instanceRef: _instanceRef } = props;
2023-06-13 12:46:37 +03:00
const size = useWindowSize();
const instanceRef = _instanceRef?.current ? _instanceRef : useRef();
2023-06-13 12:46:37 +03:00
return (
<Menu
boundingBoxPadding={safeBoundingBoxPadding()}
repositionFlag={`${size.width}x${size.height}`}
unmountOnClose
2023-06-13 12:46:37 +03:00
{...props}
instanceRef={instanceRef}
containerProps={{
onClick: (e) => {
if (e.target === e.currentTarget) {
instanceRef.current?.closeMenu?.();
}
containerProps?.onClick?.(e);
},
...containerProps,
}}
/>
);
}
export default Menu2;