import React, { useRef, useState, ReactElement } from 'react'; import { CSSTransition } from 'react-transition-group'; const DropFade = ({ children, show }: { children: ReactElement; show: boolean }) => { const reference = useRef(null); const [height, setHeight] = useState(0); const clonedChild = React.cloneElement(children); return ( { // Set height so we can animate to the correct height setHeight(reference.current?.scrollHeight || 0); }} // Un-set height, as we don't want to presume it should be that height forever onEntered={() => setHeight(undefined)} onExit={() => { setHeight(reference.current?.scrollHeight || 0); }} onExiting={() => { setHeight(0); }} >
{clonedChild}
); }; export default DropFade;