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.0 KiB44 linesraw
1import { tr } from '../lib/i18n';
2import { useLayoutEffect, useRef, useState, type ReactNode } from 'react';
3import { createPortal } from 'react-dom';
4import { IconBtn } from './ui';
5import { measureFrame } from '../lib/overlay-place';
6/** Anchored, non-modal surface. Focus and dismissal stay with its own trigger. */
7export function Popover({ anchor, title, detail, onClose, children, above = false, width = 360 }: {
8    anchor: HTMLElement | null;
9    title: string;
10    /** A short line to the right of the title, on the same row (the Subagents list puts its counts here). */
11    detail?: ReactNode;
12    onClose: () => void;
13    children: ReactNode;
14    above?: boolean;
15    width?: number;
16}) {
17    const ref = useRef<HTMLDivElement>(null);
18    const [position, setPosition] = useState({ left: 12, top: 12 });
19    const [maxWidth, setMaxWidth] = useState<number | string>('calc(100vw - 24px)');
20    useLayoutEffect(() => {
21        // The numbers from the anchor are visual pixels and the numbers written on the panel are the page's own: they differ when the page is scaled (#4653).
22        const place = () => { const { scale, viewport } = measureFrame(); const r = anchor?.getBoundingClientRect(); const h = (ref.current?.offsetHeight ?? 200) * scale; const w = Math.min(width * scale, viewport.width - 24); const y = above ? (r?.top ?? h) - h - 10 : (r?.bottom ?? 12) + 8; setPosition({ left: Math.max(12, Math.min(r?.left ?? 12, viewport.width - w - 12)) / scale, top: Math.max(12, Math.min(y, viewport.height - h - 12)) / scale }); setMaxWidth((viewport.width - 24) / scale); };
23        place();
24        ref.current?.focus({ preventScroll: true });
25        const outside = (e: PointerEvent) => {
26            if (!ref.current?.contains(e.target as Node) && !anchor?.contains(e.target as Node))
27                onClose();
28        };
29        const resize = new ResizeObserver(place);
30        if (ref.current)
31            resize.observe(ref.current);
32        document.addEventListener('pointerdown', outside);
33        window.addEventListener('resize', place);
34        return () => { resize.disconnect(); document.removeEventListener('pointerdown', outside); window.removeEventListener('resize', place); };
35    }, [anchor, width, above]);
36    function close() { onClose(); anchor?.focus({ preventScroll: true }); }
37    return createPortal(<div ref={ref} role="dialog" aria-label={tr(title)} tabIndex={-1} className="telepop anchored-popover" style={{ ...position, zIndex: anchor?.closest('[role="dialog"]') ? 'var(--layer-dialog-popover)' : 'var(--layer-popover)', width, maxWidth }} onKeyDown={e => {
38            if (e.key === 'Escape') {
39                e.preventDefault();
40                e.stopPropagation();
41                close();
42            }
43        }}><div className="telepop__head"><strong>{tr(title)}</strong>{detail ? <span className="telepop__detail">{detail}</span> : null}<IconBtn name="x" title={tr("Close {0}", [title.toLowerCase()])} onClick={close}/></div>{tr(children)}</div>, document.body);
44}