Different Easing Transition

Move transition but with different easing 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;
  };
  animate: {
    x: number;
  };
  exit: {
    x: string;
    transition: {
        duration: number;
    };
  };
};

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

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

Left To Right

Page 1

Top To Bottom

Page 1

Bottom To Top

Page 1