-
Notifications
You must be signed in to change notification settings - Fork 221
fix: panel height not updating when conditionally rendering fields #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
eca5690
27e49df
9e0eccd
e340bd0
bda7da4
108592b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,5 +126,67 @@ export function useToggle(toggled: boolean) { | |
| } | ||
| }, [toggled]) | ||
|
|
||
| // Watch for content size changes when panel is expanded | ||
| useEffect(() => { | ||
| if (!toggled || !contentRef.current || !wrapperRef.current) return | ||
|
|
||
| const wrapper = wrapperRef.current | ||
| let rafId: number | null = null | ||
| let currentTransitionHandler: (() => void) | null = null | ||
|
|
||
| const resizeObserver = new ResizeObserver(() => { | ||
| const content = contentRef.current | ||
| if (!content) return | ||
|
|
||
| // Cancel any pending animation | ||
| if (rafId !== null) { | ||
| cancelAnimationFrame(rafId) | ||
| rafId = null | ||
| } | ||
|
|
||
| // Remove any existing transition handler | ||
| if (currentTransitionHandler) { | ||
| wrapper.removeEventListener('transitionend', currentTransitionHandler) | ||
| currentTransitionHandler = null | ||
| } | ||
|
|
||
| // Get the current and target heights | ||
| const currentHeight = wrapper.getBoundingClientRect().height | ||
| const targetHeight = content.getBoundingClientRect().height | ||
|
|
||
| // Only update if there's a meaningful difference | ||
| if (Math.abs(currentHeight - targetHeight) > 1) { | ||
| // Set explicit height to enable transition | ||
| wrapper.style.height = currentHeight + 'px' | ||
|
|
||
| // Use requestAnimationFrame to ensure the height is set before changing it | ||
| rafId = requestAnimationFrame(() => { | ||
| rafId = null | ||
| wrapper.style.height = targetHeight + 'px' | ||
|
|
||
| // Remove fixed height after transition completes | ||
| const handleTransitionEnd = () => { | ||
| wrapper.style.removeProperty('height') | ||
| currentTransitionHandler = null | ||
| } | ||
| currentTransitionHandler = handleTransitionEnd | ||
| wrapper.addEventListener('transitionend', handleTransitionEnd, { once: true }) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| resizeObserver.observe(contentRef.current) | ||
|
|
||
| return () => { | ||
| resizeObserver.disconnect() | ||
| if (rafId !== null) { | ||
| cancelAnimationFrame(rafId) | ||
| } | ||
| if (currentTransitionHandler) { | ||
| wrapper.removeEventListener('transitionend', currentTransitionHandler) | ||
| } | ||
| } | ||
| }, [toggled]) | ||
|
Comment on lines
+130
to
+189
|
||
|
|
||
| return { wrapperRef, contentRef } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
transitionendevent may not fire in certain edge cases (e.g., if the transition is interrupted, the element is hidden, or the browser skips very small transitions), which would leave the explicit height style set on the wrapper and prevent it from properly auto-sizing.Consider adding a fallback timeout to ensure the height property is eventually removed: