Last active
March 22, 2024 06:28
-
-
Save yyyyaaa/185d3c2a7e15aec0946f1d6a3c56b556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useEffect, useState } from "react"; | |
import { animateLayout } from "./animate-layout.helper"; | |
import type { AnimateLayoutProps } from "./animate-layout.types"; | |
useMetadata({ | |
rsc: { | |
componentType: "client", | |
}, | |
}); | |
export default function AnimateLayout(props: AnimateLayoutProps) { | |
const [isShown, setIsShown] = useState<boolean>(false); | |
let parentRef = useRef<HTMLDivElement>(null); | |
function hide(el) { | |
setIsShown(false); | |
el.style.display = "none"; | |
} | |
useEffect(() => { | |
if (parentRef.current) { | |
animateLayout(parentRef.current); | |
} | |
}, []); | |
useEffect(() => { | |
return () => { | |
if (parentRef.current) { | |
hide(parentRef.current); | |
} | |
}; | |
}); | |
useEffect(() => { | |
if (parentRef) { | |
setIsShown(true); | |
animateLayout(parentRef); | |
} | |
}, [parentRef.current, props.children]); | |
return ( | |
<div | |
ref={parentRef} | |
className={props.className} | |
style={{ | |
display: isShown ? "block" : "none", | |
backfaceVisibility: "hidden", | |
}} | |
> | |
{props.children} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment