Skip to content

Instantly share code, notes, and snippets.

@yyyyaaa
Last active March 22, 2024 06:28
Show Gist options
  • Save yyyyaaa/185d3c2a7e15aec0946f1d6a3c56b556 to your computer and use it in GitHub Desktop.
Save yyyyaaa/185d3c2a7e15aec0946f1d6a3c56b556 to your computer and use it in GitHub Desktop.
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