Room Transition

Make room transition from multiple directions.

Page 1

Installation

Install dependencies

npm i framer-motion

Add the following code in App.tsx file


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;

Copy the source code

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;

Finally wrap your page with <PageTransition> component

page/Home.tsx


const Home = () => {
  return (
    <PageTransition>
      <div>Home</div>
    </PageTransition>
  );
};

Replace variants object with one of the examples below according to your need

Left To Right

Page 1