@@ -5,6 +5,10 @@ import type { Padding, Placement, VirtualElement } from '@popperjs/core'
55
66import './PreviewPopUp.scss'
77
8+ function isDetachedHTMLElementAnchor ( anchor : HTMLElement | VirtualElement | null ) : anchor is HTMLElement {
9+ return anchor instanceof HTMLElement && ! anchor . isConnected
10+ }
11+
812export const PreviewPopUp = React . forwardRef <
913 PreviewPopUpHandle ,
1014 React . PropsWithChildren < {
@@ -62,6 +66,7 @@ export const PreviewPopUp = React.forwardRef<
6266 anchor ?. getBoundingClientRect ( ) . y ?? 0
6367 ) ,
6468 } )
69+ const anchorRef = useRef ( anchor )
6570 const anchorYRef = useRef ( anchor ?. getBoundingClientRect ( ) . y ?? 0 )
6671 const { styles, attributes, update } = usePopper (
6772 trackMouse ? virtualElement . current : anchor ,
@@ -76,12 +81,14 @@ export const PreviewPopUp = React.forwardRef<
7681 } , [ update ] )
7782
7883 useEffect ( ( ) => {
84+ anchorRef . current = anchor
7985 anchorYRef . current = anchor ?. getBoundingClientRect ( ) . y ?? 0
8086 } , [ anchor ] )
8187
8288 useEffect ( ( ) => {
8389 if ( trackMouse ) {
8490 const listener = ( { clientX : x } : MouseEvent ) => {
91+ if ( isDetachedHTMLElementAnchor ( anchorRef . current ) ) return
8592 virtualElement . current . getBoundingClientRect = generateGetBoundingClientRect ( x , anchorYRef . current )
8693 // If update is available, call it to reposition the popper:
8794 if ( updateRef . current ) {
@@ -107,6 +114,7 @@ export const PreviewPopUp = React.forwardRef<
107114 useImperativeHandle ( ref , ( ) => {
108115 return {
109116 update : ( ) => {
117+ if ( isDetachedHTMLElementAnchor ( anchorRef . current ) ) return
110118 if ( ! updateRef . current ) return
111119 updateRef . current ( ) . catch ( console . error )
112120 } ,
0 commit comments