Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AnimatedHeight.tsx

101 lines3.1 KB

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