@@ -70,7 +70,7 @@ export function VirtualElement({
70
70
const [ ref , setRef ] = useState < HTMLDivElement | null > ( null )
71
71
72
72
// Store the old dashboard height to detect changes:
73
- let oldDashboardHeight : number | undefined
73
+ let oldElementHeight : number | undefined
74
74
let resizeTimeout : NodeJS . Timeout
75
75
76
76
const showPlaceholder = ! isShowingChildren && ! initialShow
@@ -106,9 +106,18 @@ export function VirtualElement({
106
106
107
107
lastVisibilityChangeRef . current = now
108
108
if ( inView === visible ) {
109
- //Setup resize observer and recalculations
109
+ return
110
+ }
111
+
112
+ if ( visible ) {
113
+ oldElementHeight = findElementHeight ( )
114
+ if ( ref ) {
115
+ console . log ( 'Connecting observer for element' , ref )
116
+ resizeObserver . observe ( ref )
117
+ }
110
118
} else {
111
- // Stop resize observer:
119
+ console . log ( 'Disconnecting observer for element' , ref )
120
+ resizeObserver . disconnect ( )
112
121
}
113
122
114
123
setInView ( visible )
@@ -129,66 +138,62 @@ export function VirtualElement({
129
138
return false
130
139
}
131
140
132
- // // Handle Viewport heigth changes:
133
- useEffect ( ( ) => {
134
- function handleResize ( ) {
135
- if ( ref ) {
136
- // Show children during measurement
137
- setIsShowingChildren ( true )
138
- if ( resizeTimeout ) {
139
- clearTimeout ( resizeTimeout )
140
- }
141
- resizeTimeout = setTimeout ( ( ) => {
142
- requestAnimationFrame ( ( ) => {
143
- const measurements = measureElement ( ref , placeholderHeight )
144
- if ( measurements ) {
145
- setMeasurements ( measurements )
141
+ function handleResize ( ) {
142
+ if ( ref ) {
143
+ // Show children during measurement
144
+ setIsShowingChildren ( true )
145
+ if ( resizeTimeout ) {
146
+ clearTimeout ( resizeTimeout )
147
+ }
148
+ resizeTimeout = setTimeout ( ( ) => {
149
+ requestAnimationFrame ( ( ) => {
150
+ const measurements = measureElement ( ref , placeholderHeight )
151
+ if ( measurements ) {
152
+ setMeasurements ( measurements )
146
153
147
- // Only hide children again if not in view
148
- if ( ! inView && measurements . clientHeight > 0 ) {
149
- setIsShowingChildren ( false )
150
- } else {
151
- setIsShowingChildren ( true )
152
- }
154
+ // Only hide children again if not in view
155
+ if ( ! inView && measurements . clientHeight > 0 ) {
156
+ setIsShowingChildren ( false )
157
+ } else {
158
+ setIsShowingChildren ( true )
153
159
}
154
- } )
155
- } , 200 )
156
- }
160
+ }
161
+ } )
162
+ } , 50 )
157
163
}
164
+ }
158
165
159
- const findDashboardPanel = ( ) : HTMLElement | null => {
160
- const timelineWrapper = ref ?. closest ( '.segment-timeline-wrapper--shelf' )
161
- const dashboardPanel = timelineWrapper ?. querySelector ( '.dashboard-panel' )
162
- if ( dashboardPanel ) {
163
- return dashboardPanel as HTMLElement
164
- }
165
- return null
166
+ const findDashboardPanel = ( ) : HTMLElement | null => {
167
+ const timelineWrapper = ref ?. closest ( '.segment-timeline-wrapper--shelf' )
168
+ const dashboardPanel = timelineWrapper ?. querySelector ( '.dashboard-panel' )
169
+ if ( dashboardPanel ) {
170
+ return dashboardPanel as HTMLElement
166
171
}
172
+ return null
173
+ }
167
174
168
- const resizeObserver = new ResizeObserver ( ( ) => {
169
- const dashboardElement = findDashboardPanel ( )
170
- // Get heigth of timeline wrapper
171
- const containerHeight = dashboardElement ?. clientHeight
175
+ function findElementHeight ( ) : number {
176
+ const dashboardElement = findDashboardPanel ( )
177
+ // Get heigth of timeline wrapper
178
+ const dashboardHeight = dashboardElement ?. clientHeight || 0
179
+ const totalHeight = ref ?. clientHeight || placeholderHeight || 160 + dashboardHeight
172
180
173
- if ( containerHeight && containerHeight !== oldDashboardHeight ) {
174
- console . log ( 'dashboard containerHeigth changed to ' , containerHeight , 'from' , oldDashboardHeight )
175
- oldDashboardHeight = containerHeight
176
- handleResize ( )
177
- }
178
- } )
181
+ return totalHeight
182
+ }
179
183
180
- if ( ref ) {
181
- const dashboardElement = findDashboardPanel ( )
182
- if ( dashboardElement ) {
183
- oldDashboardHeight = dashboardElement ?. clientHeight
184
- resizeObserver . observe ( ref )
185
- }
186
- }
184
+ // Handle Viewport heigth changes:
185
+ const resizeObserver = new ResizeObserver ( ( ) => {
186
+ console . log ( 'Observing resize' )
187
187
188
- return ( ) => {
189
- resizeObserver . disconnect ( )
188
+ const elementHeight = findElementHeight ( )
189
+ console . log ( 'elementHeight' , elementHeight , 'oldElementHeight' , oldElementHeight )
190
+
191
+ if ( elementHeight && elementHeight !== oldElementHeight ) {
192
+ console . log ( 'dashboard containerHeigth changed to ' , elementHeight , 'from' , oldElementHeight )
193
+ oldElementHeight = elementHeight
194
+ handleResize ( )
190
195
}
191
- } , [ ref , placeholderHeight ] )
196
+ } )
192
197
193
198
useEffect ( ( ) => {
194
199
if ( inView === true ) {
@@ -202,7 +207,12 @@ export function VirtualElement({
202
207
setMeasurements ( measurements )
203
208
setWaitForInitialLoad ( false )
204
209
}
205
- } , 1000 )
210
+ // Setup initial resize observers
211
+ oldElementHeight = findElementHeight ( )
212
+ if ( ref ) {
213
+ resizeObserver . observe ( ref )
214
+ }
215
+ } , 2000 )
206
216
207
217
return ( ) => {
208
218
window . clearTimeout ( initialMeasurementTimeout )
0 commit comments