Smooth falling transition effect
npm i framer-motion
import { BrowserRouter, Route, Routes, useLocation } from "react-router-dom";
import { AnimatePresence } from "framer-motion";
function App() {
const location = useLocation();
return (
<BrowserRouter>
<AnimatePresence initial={false}>
<Routes location={location} key={location.pathname}>
<Route path="/" element={<Home />} />
// more routes here
</Routes>
</AnimatePresence>
</BrowserRouter>
);
};
export default App;
components/PageTransition.tsx
import { motion } from "framer-motion";
type PageProps = {
children: React.ReactNode;
};
type TVariants = {
initial: {
scale: number;
opacity: number;
};
animate: {
scale: number;
opacity: number;
};
exit: {
originX: number;
originY: number;
rotateZ: number;
opacity: [number, number, string];
zIndex: number;
};
};
const variants: TVariants = {
initial: {
scale: 0.5,
opacity: 0,
},
animate: {
scale: 1,
opacity: 1,
},
exit: {
originX: 0,
originY: 0,
rotateZ: 15,
y: [0, 0, "100vh"],
zIndex: 1,
},
};
const PageTransition = ({ children }: PageProps) => {
return (
<motion.div
{...variants}
transition={{ duration: 1 }}
style={{
position: "absolute"
}}
>
{children}
</motion.div>
);
};
export default PageTransition;
page/Home.tsx
const Home = () => {
return (
<PageTransition>
<div>Home</div>
</PageTransition>
);
};