|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useEffect, |
| 4 | + useLayoutEffect, |
| 5 | + useRef, |
| 6 | + useState, |
| 7 | +} from "react"; |
| 8 | + |
| 9 | +import { cn } from "@/lib/utils"; |
| 10 | + |
| 11 | +interface AnimatedHeightProps { |
| 12 | + children: React.ReactNode; |
| 13 | + className?: string; |
| 14 | + /** Duration of the height transition in milliseconds */ |
| 15 | + duration?: number; |
| 16 | + /** Easing function for the transition */ |
| 17 | + easing?: string; |
| 18 | + /** Key that changes when content changes - triggers re-measurement */ |
| 19 | + contentKey?: string | number; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * A component that smoothly animates its height based on content changes. |
| 24 | + * Uses ResizeObserver to detect content size changes and applies CSS transitions. |
| 25 | + * During shrink transitions, overflow is visible to prevent content clipping. |
| 26 | + */ |
| 27 | +export function AnimatedHeight({ |
| 28 | + children, |
| 29 | + className, |
| 30 | + duration = 200, |
| 31 | + easing = "ease-out", |
| 32 | + contentKey, |
| 33 | +}: AnimatedHeightProps) { |
| 34 | + const contentRef = useRef<HTMLDivElement>(null); |
| 35 | + const [height, setHeight] = useState<number | null>(null); |
| 36 | + const [enableTransition, setEnableTransition] = useState(false); |
| 37 | + const [isShrinking, setIsShrinking] = useState(false); |
| 38 | + const prevHeightRef = useRef<number | null>(null); |
| 39 | + const shrinkTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 40 | + const isFirstMeasurementRef = useRef(true); |
| 41 | + const isTransitioningRef = useRef(false); |
| 42 | + |
| 43 | + const measureHeight = useCallback( |
| 44 | + (force = false) => { |
| 45 | + if (isTransitioningRef.current && !force) return; |
| 46 | + |
| 47 | + const contentEl = contentRef.current; |
| 48 | + if (!contentEl) return; |
| 49 | + |
| 50 | + const newHeight = contentEl.offsetHeight; |
| 51 | + const prevHeight = prevHeightRef.current; |
| 52 | + |
| 53 | + if (newHeight !== prevHeight && newHeight > 0) { |
| 54 | + const shrinking = prevHeight !== null && newHeight < prevHeight; |
| 55 | + |
| 56 | + if (shrinkTimeoutRef.current) { |
| 57 | + clearTimeout(shrinkTimeoutRef.current); |
| 58 | + shrinkTimeoutRef.current = null; |
| 59 | + } |
| 60 | + |
| 61 | + if (shrinking) { |
| 62 | + setIsShrinking(true); |
| 63 | + shrinkTimeoutRef.current = setTimeout(() => { |
| 64 | + setIsShrinking(false); |
| 65 | + }, duration); |
| 66 | + } |
| 67 | + |
| 68 | + prevHeightRef.current = newHeight; |
| 69 | + setHeight(newHeight); |
| 70 | + |
| 71 | + if (isFirstMeasurementRef.current) { |
| 72 | + isFirstMeasurementRef.current = false; |
| 73 | + requestAnimationFrame(() => { |
| 74 | + setEnableTransition(true); |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + [duration], |
| 80 | + ); |
| 81 | + |
| 82 | + // Handle contentKey changes synchronously before paint |
| 83 | + useLayoutEffect(() => { |
| 84 | + isTransitioningRef.current = true; |
| 85 | + |
| 86 | + // Measure synchronously - useLayoutEffect runs after DOM update but before paint |
| 87 | + measureHeight(true); |
| 88 | + |
| 89 | + isTransitioningRef.current = false; |
| 90 | + }, [contentKey, measureHeight]); |
| 91 | + |
| 92 | + // ResizeObserver for dynamic content changes (not during key transitions) |
| 93 | + useEffect(() => { |
| 94 | + const contentEl = contentRef.current; |
| 95 | + if (!contentEl) return; |
| 96 | + |
| 97 | + const resizeObserver = new ResizeObserver(() => { |
| 98 | + if (!isTransitioningRef.current) { |
| 99 | + measureHeight(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + resizeObserver.observe(contentEl); |
| 104 | + |
| 105 | + return () => { |
| 106 | + resizeObserver.disconnect(); |
| 107 | + if (shrinkTimeoutRef.current) { |
| 108 | + clearTimeout(shrinkTimeoutRef.current); |
| 109 | + } |
| 110 | + }; |
| 111 | + }, [measureHeight]); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div |
| 115 | + className={cn( |
| 116 | + isShrinking ? "overflow-visible" : "overflow-hidden", |
| 117 | + className, |
| 118 | + )} |
| 119 | + style={{ |
| 120 | + height: height !== null ? `${height}px` : "auto", |
| 121 | + transition: enableTransition |
| 122 | + ? `height ${duration}ms ${easing}` |
| 123 | + : undefined, |
| 124 | + }} |
| 125 | + > |
| 126 | + <div ref={contentRef}>{children}</div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments