okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
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 } :{ 6anchor :HTMLElement | null ; 7title :string ; 8onClose :() => void ; 9children :React . ReactNode ; 10width ?:number ; 11}) { 12const ref = useRef < HTMLDivElement >( null ); 13const [ placed , setPlaced ] = useState <{ left :number ; top :number ; maxWidth ?:number ; maxHeight ?:number }>({ left :12 , top :12 }); 14useLayoutEffect (() => { 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). 16const place = () => { 17const el = ref . current ; 18const { scale, viewport} = measureFrame (); 19const rect = anchor ?. getBoundingClientRect (); 20const natural = el ?Math . max ( el . getBoundingClientRect (). height , el . scrollHeight * scale ) :200 * scale ; 21const home = { left :12 , top :12 , right :12 , bottom :12 }; 22const p = placeAnchored ({ anchor :rect ?? home , size :{ width :width * scale , height :natural }, viewport, scale}); 23setPlaced ({ left :p . left , top :p . top , maxWidth :p . maxWidth , maxHeight :p . maxHeight }); 24}; 25place (); 26ref . current ?. querySelector < HTMLElement >( '[role="menuitem"]:not(:disabled), [role="menuitemradio"]:not(:disabled)' )?. focus (); 27const outside = ( e :PointerEvent ) => { if ( ! ref . current ?. contains ( e . target as Node ) && ! anchor ?. contains ( e . target as Node )) 28onClose (); }; 29document . addEventListener ( 'pointerdown' , outside ); 30window . addEventListener ( 'resize' , place ); 31return () => { document . removeEventListener ( 'pointerdown' , outside ); window . removeEventListener ( 'resize' , place ); if ( anchor ?. isConnected ) 32anchor . focus ({ preventScroll :true }); }; 33}, [ anchor , width ]); 34return 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 => { 35const nodes = Array . from ( ref . current ?. querySelectorAll < HTMLElement >( '[role="menuitem"]:not(:disabled), [role="menuitemradio"]:not(:disabled)' ) ?? []); 36const at = nodes . indexOf ( document . activeElement as HTMLElement ); 37if ([ 'Escape' , 'Tab' ]. includes ( e . key )) { 38e . preventDefault (); 39e . stopPropagation (); 40onClose (); 41} 42if ([ 'ArrowUp' , 'ArrowDown' , 'Home' , 'End' ]. includes ( e . key )) { 43e . preventDefault (); 44nodes [ 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}