-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathauto-height.tsx
More file actions
33 lines (26 loc) · 797 Bytes
/
auto-height.tsx
File metadata and controls
33 lines (26 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, { useRef, useState, useEffect } from 'react';
import AnimateHeight, { Height } from '../src/index';
const AutoHeight = ({ children, ...props }) => {
const [height, setHeight] = useState<Height>('auto');
const contentDiv = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const element = contentDiv.current as HTMLDivElement;
const resizeObserver = new ResizeObserver(() => {
setHeight(element.clientHeight);
});
resizeObserver.observe(element);
return () => resizeObserver.disconnect();
}, []);
return (
<AnimateHeight
{...props}
height={height}
contentClassName="auto-content"
contentRef={contentDiv}
disableDisplayNone
>
{children}
</AnimateHeight>
);
};
export default AutoHeight;