Open History to see the last commit
1import { type ReactNode, useEffect, useLayoutEffect, useRef, useState } from "react";
2import { observeResize } from "../lib/observeResize";
3
4const HEIGHT_TRANSITION_FALLBACK_MS = 250;
5
6export function AnimatedHeight({
7 children,
8 holdHeight = false,
9}: {
10 readonly children: ReactNode;
11 readonly holdHeight?: boolean;
12}) {
13 const contentRef = useRef<HTMLDivElement>(null);
14 const [heightState, setHeightState] = useState<{
15 readonly height: number | null;
16 readonly isClipping: boolean;
17 }>({ height: null, isClipping: false });
18
19 useEffect(() => {
20 if (!heightState.isClipping) return;
21 const timeoutId = window.setTimeout(() => {
22 setHeightState((currentState) =>
23 currentState.isClipping ? { ...currentState, isClipping: false } : currentState,
24 );
25 }, HEIGHT_TRANSITION_FALLBACK_MS);
26 return () => window.clearTimeout(timeoutId);
27 }, [
28 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A height change mid-transition starts a new transition, so the fallback timer restarts.
29 heightState.height,
30 heightState.isClipping,
31 ]);
32
33 useLayoutEffect(() => {
34 if (holdHeight) return;
35 const element = contentRef.current;
36 if (!element) return;
37 let firstFrameId: number | null = null;
38 let secondFrameId: number | null = null;
39
40 const updateHeight = () => {
41 const nextHeight = Math.ceil(element.scrollHeight || element.getBoundingClientRect().height);
42 setHeightState((currentState) => {
43 if (currentState.height === nextHeight) return currentState;
44 return {
45 height: nextHeight,
46 isClipping: currentState.height !== null,
47 };
48 });
49 };
50 const cancelPendingFrames = () => {
51 if (firstFrameId !== null) {
52 window.cancelAnimationFrame(firstFrameId);
53 firstFrameId = null;
54 }
55 if (secondFrameId !== null) {
56 window.cancelAnimationFrame(secondFrameId);
57 secondFrameId = null;
58 }
59 };
60 const updateHeightAfterPaint = () => {
61 cancelPendingFrames();
62 updateHeight();
63 firstFrameId = window.requestAnimationFrame(() => {
64 firstFrameId = null;
65 updateHeight();
66 secondFrameId = window.requestAnimationFrame(() => {
67 secondFrameId = null;
68 updateHeight();
69 });
70 });
71 };
72
73 updateHeightAfterPaint();
74 const stopObserving = observeResize(element, updateHeightAfterPaint);
75 return () => {
76 stopObserving();
77 cancelPendingFrames();
78 };
79 }, [holdHeight]);
80
81 return (
82 <div
83 data-slot="animated-height"
84 className="transition-[height] duration-200 ease-out motion-reduce:transition-none [overflow-clip-margin:2px]"
85 style={
86 heightState.height === null
87 ? undefined
88 : {
89 height: heightState.height,
90 overflow: holdHeight || heightState.isClipping ? "clip" : "visible",
91 }
92 }
93 onTransitionEnd={(event) => {
94 if (event.target !== event.currentTarget || event.propertyName !== "height") return;
95 setHeightState((currentState) =>
96 currentState.isClipping ? { ...currentState, isClipping: false } : currentState,
97 );
98 }}
99 >
100 <div ref={contentRef} style={holdHeight ? { height: "100%" } : undefined}>
101 {children}
102 </div>
103 </div>
104 );
105}