okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
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 } :{ 8anchor :HTMLElement | null ; 9title :string ; 10/** A short line to the right of the title, on the same row (the Subagents list puts its counts here). */ 11detail ?:ReactNode ; 12onClose :() => void ; 13children :ReactNode ; 14above ?:boolean ; 15width ?:number ; 16}) { 17const ref = useRef < HTMLDivElement >( null ); 18const [ position , setPosition ] = useState ({ left :12 , top :12 }); 19const [ maxWidth , setMaxWidth ] = useState < number | string >( 'calc(100vw - 24px)' ); 20useLayoutEffect (() => { 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). 22const 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 ); }; 23place (); 24ref . current ?. focus ({ preventScroll :true }); 25const outside = ( e :PointerEvent ) => { 26if ( ! ref . current ?. contains ( e . target as Node ) && ! anchor ?. contains ( e . target as Node )) 27onClose (); 28}; 29const resize = new ResizeObserver ( place ); 30if ( ref . current ) 31resize . observe ( ref . current ); 32document . addEventListener ( 'pointerdown' , outside ); 33window . addEventListener ( 'resize' , place ); 34return () => { resize . disconnect (); document . removeEventListener ( 'pointerdown' , outside ); window . removeEventListener ( 'resize' , place ); }; 35}, [ anchor , width , above ]); 36function close () { onClose (); anchor ?. focus ({ preventScroll :true }); } 37return 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 => { 38if ( e . key === 'Escape' ) { 39e . preventDefault (); 40e . stopPropagation (); 41close (); 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}