Skip to content

Commit 8955053

Browse files
committed
feat: dynamic resize handler
1 parent 6a9a40e commit 8955053

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

packages/webui/src/client/lib/VirtualElement.tsx

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function VirtualElement({
7070
const [ref, setRef] = useState<HTMLDivElement | null>(null)
7171

7272
// Store the old dashboard height to detect changes:
73-
let oldDashboardHeight: number | undefined
73+
let oldElementHeight: number | undefined
7474
let resizeTimeout: NodeJS.Timeout
7575

7676
const showPlaceholder = !isShowingChildren && !initialShow
@@ -106,9 +106,18 @@ export function VirtualElement({
106106

107107
lastVisibilityChangeRef.current = now
108108
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+
}
110118
} else {
111-
// Stop resize observer:
119+
console.log('Disconnecting observer for element', ref)
120+
resizeObserver.disconnect()
112121
}
113122

114123
setInView(visible)
@@ -129,66 +138,62 @@ export function VirtualElement({
129138
return false
130139
}
131140

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)
146153

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)
153159
}
154-
})
155-
}, 200)
156-
}
160+
}
161+
})
162+
}, 50)
157163
}
164+
}
158165

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
166171
}
172+
return null
173+
}
167174

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
172180

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+
}
179183

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')
187187

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()
190195
}
191-
}, [ref, placeholderHeight])
196+
})
192197

193198
useEffect(() => {
194199
if (inView === true) {
@@ -202,7 +207,12 @@ export function VirtualElement({
202207
setMeasurements(measurements)
203208
setWaitForInitialLoad(false)
204209
}
205-
}, 1000)
210+
// Setup initial resize observers
211+
oldElementHeight = findElementHeight()
212+
if (ref) {
213+
resizeObserver.observe(ref)
214+
}
215+
}, 2000)
206216

207217
return () => {
208218
window.clearTimeout(initialMeasurementTimeout)

0 commit comments

Comments
 (0)