import { useEffect, useRef, useState, type ReactNode } from "react"; interface PageTransitionProps { viewKey: string; children: ReactNode; } const EXIT_DURATION_MS = 180; export default function PageTransition({ viewKey, children }: PageTransitionProps) { const [displayedChildren, setDisplayedChildren] = useState(children); const [phase, setPhase] = useState<"idle" | "exit">("idle"); const prevKeyRef = useRef(viewKey); const timerRef = useRef>(); useEffect(() => { if (viewKey === prevKeyRef.current) { setDisplayedChildren(children); return; } prevKeyRef.current = viewKey; setPhase("exit"); timerRef.current = setTimeout(() => { setDisplayedChildren(children); setPhase("idle"); }, EXIT_DURATION_MS); return () => clearTimeout(timerRef.current); }, [viewKey, children]); return (
{displayedChildren}
); }