okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1import { tr } from '../lib/i18n' ; 2import { useEffect , useLayoutEffect , useRef , useState } from 'react' ; 3import { createPortal } from 'react-dom' ; 4import { measureFrame } from '../lib/overlay-place' ; 5export function VersionMemoryTooltip ({ id, anchor, text, onClose, onEnter, onLeave} :{ 6id :string ; 7anchor :HTMLElement ; 8text :string ; 9onClose :() => void ; 10onEnter :() => void ; 11onLeave :() => void ; 12}) { 13const ref = useRef < HTMLDivElement >( null ); 14const [ position , setPosition ] = useState ({ left :12 , top :12 }); 15useLayoutEffect (() => { 16const { scale, viewport} = measureFrame (); 17const rect = anchor . getBoundingClientRect (), width = Math . min ( 260 * scale , viewport . width - 24 ); 18const height = ref . current ?. getBoundingClientRect (). height ?? 80 * scale ; 19const left = Math . max ( 12 , Math . min ( rect . right - width , viewport . width - width - 12 )); 20const top = rect . bottom + height + 20 <= viewport . height ?rect . bottom + 8 :Math . max ( 12 , rect . top - height - 8 ); 21setPosition ({ left :left / scale , top :top / scale }); 22}, [ anchor , text ]); 23useEffect (() => { 24const escape = ( event :KeyboardEvent ) => { if ( event . key === 'Escape' ) { 25event . preventDefault (); 26event . stopImmediatePropagation (); 27onClose (); 28} }; 29document . addEventListener ( 'keydown' , escape , true ); 30window . addEventListener ( 'scroll' , onClose , true ); 31window . addEventListener ( 'resize' , onClose ); 32return () => { document . removeEventListener ( 'keydown' , escape , true ); window . removeEventListener ( 'scroll' , onClose , true ); window . removeEventListener ( 'resize' , onClose ); }; 33}, [ onClose ]); 34return createPortal (< div ref = { ref } id = { id } role = "tooltip" className = "version-memory-tooltip" style = { position } onPointerEnter = { onEnter } onPointerLeave = { onLeave }>{ tr ( text )}</ div >, document . body ); 35}