Scale Transition

Pages scale up/down 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;
    zIndex: number;
  };
  animate: {
    x: number;
    zIndex: number;
  };
  exit: {
    scale: number;
    zIndex: number;
  };
};

// values for right to left transition
const variants: TVariants = {
  initial: {
    x: "100vw",
    zIndex: 1,
  },
  animate: {
    x: 0,
    zIndex: 1,
  },
  exit: {
    scale: 0.5,
    zIndex: 0,
  },
};

const PageTransition = ({ children }: PageProps) => {
  return (
    <motion.div
      {...variants}
      transition={{ duration: 0.5 }}
      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

Scale Down / From Left

Page 1

Scale Down / From Top

Page 1

Scale Down / From Bottom

Page 1

Scale Down / Scale Down

Page 1

Scale Up / Scale Up

Page 1

Move To Left / Scale Up

Page 1

Move To Right / Scale Up

Page 1

Move To Top / Scale Up

Page 1

Move To Bottom / Scale Up

Page 1

Scale Down / Scale Up

Page 1