Make room transition from multiple directions.
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: {
x: string;
y: string;
rotateZ: number;
scale: number;
originX: string;
originY: number;
};
animate: {
x: number;
y: number;
rotateZ: number;
scale: number;
};
exit: {
originX: number;
originY: number;
x: string;
y: string;
rotateZ: number;
scale: number;
opacity: number;
};
};
const variants: TVariants = {
initial: {
x: "100vw",
y: "-100vh",
rotateZ: -15,
scale: 1.5,
originX: "100vw",
originY: 0,
},
animate: {
x: 0,
y: 0,
rotateZ: 0,
scale: 1,
},
exit: {
originX: 0,
originY: 0,
x: "-100vw",
y: "-50vh",
rotateZ: 15,
scale: 1.5,
opacity: 0,
},
};
const PageTransition = ({ children }: PageProps) => {
return (
<motion.div
{...variants}
transition={{ duration: 0.7, ease: "easeIn" }}
style={{
position: "absolute",
}}
>
{children}
</motion.div>
);
};
export default PageTransition;
page/Home.tsx
const Home = () => {
return (
<PageTransition>
<div>Home</div>
</PageTransition>
);
};