import { SubMenu, type MenuInstance, type SubMenuProps, } from '@szhsin/react-menu'; import { useLayoutEffect, useRef, useState } from 'preact/hooks'; export default function SubMenu2(props: SubMenuProps) { const menuRef = useRef(null); const itemRef = useRef(null); const { label, direction, shift, ...restProps } = props; const [computedDirection, setComputedDirection] = useState(direction); const [computedShift, setComputedShift] = useState(shift); // If menu item width is >50% of viewport, use bottom direction useLayoutEffect(() => { if (itemRef.current) { const width = itemRef.current.offsetWidth; const viewportWidth = window.innerWidth; if (width > viewportWidth * 0.5) { setComputedDirection('bottom'); setComputedShift(shift || 8); } else { setComputedDirection(direction); setComputedShift(shift); } } }, [direction, shift]); return ( { if (e.pointerType === 'touch') { menuRef.current?.openMenu?.(); } }, onPointerLeave: (e: PointerEvent) => { if (e.pointerType === 'touch') { menuRef.current?.openMenu?.(); } }, ...props.itemProps, }} /> ); }