okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
3.1 KiB47 linesraw
1import { tr } from '../lib/i18n';
2import { useLayoutEffect, useRef, useState } from 'react';
3import { createPortal } from 'react-dom';
4import { measureFrame, placeAnchored } from '../lib/overlay-place';
5export function FloatingMenu({ anchor, title, onClose, children, width = 232 }: {
6    anchor: HTMLElement | null;
7    title: string;
8    onClose: () => void;
9    children: React.ReactNode;
10    width?: number;
11}) {
12    const ref = useRef<HTMLDivElement>(null);
13    const [placed, setPlaced] = useState<{ left: number; top: number; maxWidth?: number; maxHeight?: number }>({ left: 12, top: 12 });
14    useLayoutEffect(() => {
15        // Placed in visual pixels and written in the page's own pixels (lib/overlay-place.ts): the page may be scaled by "Interface size" (#4653).
16        const place = () => {
17            const el = ref.current;
18            const { scale, viewport } = measureFrame();
19            const rect = anchor?.getBoundingClientRect();
20            const natural = el ? Math.max(el.getBoundingClientRect().height, el.scrollHeight * scale) : 200 * scale;
21            const home = { left: 12, top: 12, right: 12, bottom: 12 };
22            const p = placeAnchored({ anchor: rect ?? home, size: { width: width * scale, height: natural }, viewport, scale });
23            setPlaced({ left: p.left, top: p.top, maxWidth: p.maxWidth, maxHeight: p.maxHeight });
24        };
25        place();
26        ref.current?.querySelector<HTMLElement>('[role="menuitem"]:not(:disabled), [role="menuitemradio"]:not(:disabled)')?.focus();
27        const outside = (e: PointerEvent) => { if (!ref.current?.contains(e.target as Node) && !anchor?.contains(e.target as Node))
28            onClose(); };
29        document.addEventListener('pointerdown', outside);
30        window.addEventListener('resize', place);
31        return () => { document.removeEventListener('pointerdown', outside); window.removeEventListener('resize', place); if (anchor?.isConnected)
32            anchor.focus({ preventScroll: true }); };
33    }, [anchor, width]);
34    return createPortal(<div className="menu" role="menu" aria-label={tr(title)} ref={ref} style={{ ...placed, zIndex: anchor?.closest('[role="dialog"]') ? 'var(--layer-dialog-popover)' : 'var(--layer-popover)', width, maxWidth: placed.maxWidth ?? 'calc(100dvw - 24px)', maxHeight: placed.maxHeight ?? 'calc(100dvh - 24px)', overflowY: 'auto' }} onKeyDown={e => {
35            const nodes = Array.from(ref.current?.querySelectorAll<HTMLElement>('[role="menuitem"]:not(:disabled), [role="menuitemradio"]:not(:disabled)') ?? []);
36            const at = nodes.indexOf(document.activeElement as HTMLElement);
37            if (['Escape', 'Tab'].includes(e.key)) {
38                e.preventDefault();
39                e.stopPropagation();
40                onClose();
41            }
42            if (['ArrowUp', 'ArrowDown', 'Home', 'End'].includes(e.key)) {
43                e.preventDefault();
44                nodes[e.key === 'Home' ? 0 : e.key === 'End' ? nodes.length - 1 : (at + (e.key === 'ArrowDown' ? 1 : -1) + nodes.length) % nodes.length]?.focus();
45            }
46        }}>{tr(children)}</div>, document.body);
47}