@@ -389,6 +389,7 @@ interface PreviewSession {
389389export function PreviewPopUpContextProvider ( { children } : React . PropsWithChildren < { } > ) : React . ReactNode {
390390 const currentHandle = useRef < IPreviewPopUpSession > ( )
391391 const previewRef = useRef < PreviewPopUpHandle > ( null )
392+ const closeSessionRef = useRef < ( ) => void > ( ( ) => undefined )
392393
393394 const [ previewSession , setPreviewSession ] = useState < PreviewSession | null > ( null )
394395 const [ previewContent , setPreviewContent ] = useState < PreviewContentUI [ ] | null > ( null )
@@ -411,12 +412,39 @@ export function PreviewPopUpContextProvider({ children }: React.PropsWithChildre
411412 setTime ( null )
412413 } , [ ] )
413414
415+ useEffect ( ( ) => {
416+ closeSessionRef . current = closeSession
417+ } , [ closeSession ] )
418+
414419 useEffect ( ( ) => {
415420 return ( ) => {
416421 closeSession ( )
417422 }
418423 } , [ closeSession ] )
419424
425+ useEffect ( ( ) => {
426+ if ( ! previewSession ) return
427+ const anchor = previewSession . anchor
428+ if ( ! ( anchor instanceof HTMLElement ) ) return
429+
430+ let rafHandle : number | undefined
431+ const checkAnchorConnection = ( ) => {
432+ if ( ! anchor . isConnected ) {
433+ closeSessionRef . current ( )
434+ return
435+ }
436+ rafHandle = window . requestAnimationFrame ( checkAnchorConnection )
437+ }
438+
439+ rafHandle = window . requestAnimationFrame ( checkAnchorConnection )
440+
441+ return ( ) => {
442+ if ( rafHandle !== undefined ) {
443+ window . cancelAnimationFrame ( rafHandle )
444+ }
445+ }
446+ } , [ previewSession ] )
447+
420448 const context : IPreviewPopUpContext = {
421449 requestPreview : ( anchor , content , opts ) => {
422450 if ( isDetachedHTMLElement ( anchor ) ) {
0 commit comments