Skip to content

Commit e497bda

Browse files
committed
feat: simplify size measure and inititaly use default height - prepare resize
1 parent 408596e commit e497bda

File tree

1 file changed

+69
-34
lines changed

1 file changed

+69
-34
lines changed

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

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

7272
const showPlaceholder = !isShowingChildren && !initialShow
73+
//let resizeTimeout: NodeJS.Timeout
7374

7475
// Track the last visibility change to debounce
7576
const lastVisibilityChangeRef = useRef<number>(0)
@@ -120,14 +121,68 @@ export function VirtualElement({
120121
return false
121122
}
122123

124+
// // Handle Viewport width changes:
125+
// useEffect(() => {
126+
// let oldWidth = ref?.clientWidth
127+
128+
// function handleResize() {
129+
// if (ref) {
130+
// // Show children during measurement
131+
// setIsShowingChildren(true)
132+
// if (resizeTimeout) {
133+
// clearTimeout(resizeTimeout)
134+
// }
135+
// resizeTimeout = setTimeout(() => {
136+
// setMeasurements(null)
137+
// requestAnimationFrame(() => {
138+
// const measurements = measureElement(ref, placeholderHeight)
139+
// if (measurements) {
140+
// setMeasurements(measurements)
141+
142+
// // Only hide children again if not in view
143+
// if (!inView) {
144+
// setIsShowingChildren(false)
145+
// }
146+
// }
147+
// })
148+
// }, 50)
149+
// }
150+
// }
151+
152+
// const resizeObserver = new ResizeObserver(() => {
153+
// // Find the segment-timeline-container parent
154+
// const timelineWrapper = ref?.closest('.segment-timeline-wrapper--shelf')?.querySelector('.dashboard-panel')
155+
156+
// // Get width of timeline wrapper
157+
// const containerWidth = timelineWrapper?.clientWidth
158+
159+
// if (containerWidth !== oldWidth) {
160+
// handleResize()
161+
// oldWidth = containerWidth
162+
// }
163+
// })
164+
165+
// if (ref) {
166+
// // Find and observe the segment-timeline-container parent
167+
// const timelineContainer = ref.closest('.segment-timeline-container')
168+
// if (timelineContainer) {
169+
// resizeObserver.observe(timelineContainer)
170+
// }
171+
// }
172+
173+
// return () => {
174+
// resizeObserver.disconnect()
175+
// }
176+
// }, [inView, ref])
177+
123178
useEffect(() => {
124179
if (inView === true) {
125180
setIsShowingChildren(true)
126181

127182
// Schedule a measurement after a short delay
128183
if (waitForInitialLoad && ref) {
129184
const initialMeasurementTimeout = window.setTimeout(() => {
130-
const measurements = measureElement(ref)
185+
const measurements = measureElement(ref, placeholderHeight)
131186
if (measurements) {
132187
setMeasurements(measurements)
133188
setWaitForInitialLoad(false)
@@ -160,7 +215,7 @@ export function VirtualElement({
160215
() => {
161216
// Measure the entire wrapper element instead of just the childRef
162217
if (ref) {
163-
const measurements = measureElement(ref)
218+
const measurements = measureElement(ref, placeholderHeight)
164219
if (measurements) {
165220
setMeasurements(measurements)
166221
}
@@ -218,51 +273,31 @@ export function VirtualElement({
218273
</InView>
219274
)
220275
}
221-
222-
function measureElement(wrapperEl: HTMLDivElement): IElementMeasurements | null {
276+
function measureElement(wrapperEl: HTMLDivElement, placeholderHeight?: number): IElementMeasurements | null {
223277
if (!wrapperEl || !wrapperEl.firstElementChild) {
224278
return null
225279
}
226280

227281
const el = wrapperEl.firstElementChild as HTMLElement
228282
const style = window.getComputedStyle(el)
229283

230-
// Look for the complete wrapper that contains both the timeline and dashboard
231284
const timelineWrapper = el.closest('.segment-timeline-wrapper--shelf')
232285

233286
if (timelineWrapper) {
234-
// Check if the direct child of the wrapper has an explicit height set
235-
const wrapperChild = timelineWrapper.querySelector(':scope > div')
236287
let totalHeight = 0
237288

238-
if (wrapperChild && wrapperChild instanceof HTMLElement) {
239-
// Check for explicit height style
240-
const inlineHeight = wrapperChild.style.height
241-
if (inlineHeight && inlineHeight.length > 0) {
242-
// Use the explicit height if it exists
243-
// Extract the numeric value if it's in pixels
244-
const heightValue = parseInt(inlineHeight, 10)
245-
if (!isNaN(heightValue)) {
246-
totalHeight = heightValue
247-
}
248-
}
289+
// Get the segment timeline height
290+
const segmentTimeline = timelineWrapper.querySelector('.segment-timeline')
291+
if (segmentTimeline) {
292+
const segmentRect = segmentTimeline.getBoundingClientRect()
293+
totalHeight = segmentRect.height
249294
}
250295

251-
// If no explicit height was found or it wasn't parseable, fall back to measuring
252-
if (totalHeight === 0) {
253-
// Get the segment timeline height
254-
const segmentTimeline = timelineWrapper.querySelector('.segment-timeline')
255-
if (segmentTimeline) {
256-
const segmentRect = segmentTimeline.getBoundingClientRect()
257-
totalHeight = segmentRect.height
258-
}
259-
260-
// Add the dashboard panel height if it exists
261-
const dashboardPanel = timelineWrapper.querySelector('.dashboard-panel')
262-
if (dashboardPanel) {
263-
const panelRect = dashboardPanel.getBoundingClientRect()
264-
totalHeight += panelRect.height
265-
}
296+
// Add the dashboard panel height if it exists
297+
const dashboardPanel = timelineWrapper.querySelector('.dashboard-panel')
298+
if (dashboardPanel) {
299+
const panelRect = dashboardPanel.getBoundingClientRect()
300+
totalHeight += panelRect.height
266301
}
267302

268303
return {
@@ -279,7 +314,7 @@ function measureElement(wrapperEl: HTMLDivElement): IElementMeasurements | null
279314
// Fallback to just measuring the element itself if wrapper isn't found
280315
return {
281316
width: style.width || 'auto',
282-
clientHeight: el.clientHeight,
317+
clientHeight: placeholderHeight ?? el.clientHeight,
283318
marginTop: style.marginTop || undefined,
284319
marginBottom: style.marginBottom || undefined,
285320
marginLeft: style.marginLeft || undefined,

0 commit comments

Comments
 (0)