@@ -92,18 +92,13 @@ export function Popup<TDataItem>({
9292 const graphComponent = useGraphComponent ( ) !
9393 const [ location , setLocation ] = useState < { x : number ; y : number } > ( { x : 0 , y : 0 } )
9494 const [ currentItem , setCurrentItem ] = useState < IModelItem | null > ( null )
95+ const [ visibility , setVisibility ] = useState ( false )
9596 const popupContainerRef = useRef < HTMLDivElement > ( null )
96- const [ mounted , setMounted ] = useState ( false )
97-
98- useEffect ( ( ) => {
99- // needed to run the updateLocation twice the first time that the popup appears to make sure
100- // that the content is already in the dom, and it has the correct width and height
101- setMounted ( ! ! currentItem )
102- } , [ currentItem ] )
10397
10498 useEffect ( ( ) => {
99+ setVisibility ( false )
105100 updateLocation ( graphComponent , currentItem , popupContainerRef , position )
106- } , [ currentItem , graphComponent , mounted , position ] )
101+ } , [ currentItem , graphComponent , position ] )
107102
108103 /**
109104 * Registers the popup listeners.
@@ -117,6 +112,11 @@ export function Popup<TDataItem>({
117112 }
118113 inputMode . addCanvasClickedListener ( canvasClickedListener )
119114
115+ const viewportChangedListener = ( ) => {
116+ updateLocation ( graphComponent , graphComponent . currentItem , popupContainerRef , position )
117+ }
118+ graphComponent . addViewportChangedListener ( viewportChangedListener )
119+
120120 const itemClickedListener = ( _ : GraphInputMode , evt : ItemClickedEventArgs < IModelItem > ) => {
121121 setCurrentItem ( evt . item instanceof INode || evt . item instanceof IEdge ? evt . item : null )
122122 }
@@ -139,6 +139,7 @@ export function Popup<TDataItem>({
139139 return ( ) => {
140140 // clean up
141141 inputMode . removeCanvasClickedListener ( canvasClickedListener )
142+ graphComponent . removeViewportChangedListener ( viewportChangedListener )
142143
143144 switch ( clickMode ) {
144145 case 'double' :
@@ -171,7 +172,8 @@ export function Popup<TDataItem>({
171172 style = { {
172173 position : 'absolute' ,
173174 left : location . x ,
174- top : location . y
175+ top : location . y ,
176+ visibility : visibility ? 'visible' : 'hidden'
175177 } }
176178 ref = { popupContainerRef }
177179 >
@@ -218,6 +220,7 @@ export function Popup<TDataItem>({
218220 new Point ( newLayout . anchorX , newLayout . anchorY - ( height + 10 ) / zoom )
219221 )
220222 setLocation ( location )
223+ setVisibility ( true )
221224 }
222225 }
223226}
0 commit comments