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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
'use client';
import { motion } from 'framer-motion';
import { usePathname } from 'next/navigation';
import { ReactNode, useEffect, useState } from 'react';
interface PageTransitionProps {
children: ReactNode;
}
export function PageTransition({ children }: PageTransitionProps) {
const pathname = usePathname();
const [isFirstRender, setIsFirstRender] = useState(true);
useEffect(() => {
const timeout = setTimeout(() => {
setIsFirstRender(false);
}, 500);
return () => clearTimeout(timeout);
}, []);
const variants = {
hidden: { opacity: 0, y: 20 },
enter: { opacity: 1, y: 0 },
exit: { opacity: 0, y: -20 },
};
// Only apply softer animation on initial render
const initialAnimation = isFirstRender ? {
initial: { opacity: 0 },
animate: { opacity: 1 },
transition: { duration: 0.5 }
} : {
initial: "hidden",
animate: "enter",
exit: "exit",
variants,
transition: {
type: "tween",
ease: "easeInOut",
duration: 0.3
}
};
return (
<motion.div
key={pathname}
{...initialAnimation}
className="w-full h-full"
>
{children}
</motion.div>
);
}
|