-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref: #355 ## Goal: To animate the content rendered by React Router - now it slides in and out in y-axis as the route changes. ## Pros: - better user experience - looks better ofc π π» ## Misc: Refactored router creation strategy to use components instead of factory function.
- Loading branch information
Showing
3 changed files
with
72 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState } from "react" | ||
import { AnimatePresence, motion, Variants } from "framer-motion" | ||
import { useLocation, useOutlet } from "react-router-dom" | ||
import Header from "../Header" | ||
import DocsDrawer from "../DocsDrawer" | ||
import Sidebar from "../Sidebar" | ||
|
||
const wrapperVariants: Variants = { | ||
in: { opacity: 0, y: 48 }, | ||
out: { opacity: 0, y: -48 }, | ||
visible: { opacity: 1, y: 0 }, | ||
} | ||
|
||
// This tricky component makes Outlet persistent so React and Framer Motion can | ||
// distinguish wheather it should be rerendered between routes. | ||
// Ref: https://github.com/remix-run/react-router/discussions/8008#discussioncomment-1280897 | ||
function PersistentOutlet() { | ||
const [outlet] = useState(useOutlet()) | ||
return outlet | ||
} | ||
|
||
function Layout() { | ||
const location = useLocation() | ||
return ( | ||
<> | ||
<Header /> | ||
<AnimatePresence mode="popLayout"> | ||
<motion.main | ||
key={location.pathname} | ||
variants={wrapperVariants} | ||
transition={{ type: "spring", damping: 12, stiffness: 120 }} | ||
initial="in" | ||
animate="visible" | ||
exit="out" | ||
> | ||
<PersistentOutlet /> | ||
</motion.main> | ||
</AnimatePresence> | ||
<Sidebar /> | ||
<DocsDrawer /> | ||
</> | ||
) | ||
} | ||
|
||
export default Layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import React from "react" | ||
import { createBrowserRouter } from "react-router-dom" | ||
import { BrowserRouter, Route, Routes } from "react-router-dom" | ||
import LandingPage from "#/pages/LandingPage" | ||
import ActivityPage from "#/pages/ActivityPage" | ||
import DashboardPage from "#/pages/DashboardPage" | ||
import Layout from "#/components/shared/Layout" | ||
import { routerPath } from "./path" | ||
|
||
export const router = createBrowserRouter([ | ||
{ | ||
path: routerPath.home, | ||
element: <LandingPage />, | ||
index: true, | ||
}, | ||
{ | ||
path: routerPath.dashboard, | ||
element: <DashboardPage />, | ||
}, | ||
{ | ||
path: `${routerPath.activity}/:activityId`, | ||
element: <ActivityPage />, | ||
}, | ||
]) | ||
export function Router() { | ||
return ( | ||
<BrowserRouter> | ||
<Routes> | ||
<Route path="/" element={<Layout />}> | ||
<Route index path={routerPath.home} element={<LandingPage />} /> | ||
<Route path={routerPath.dashboard} element={<DashboardPage />} /> | ||
<Route | ||
path={`${routerPath.activity}/:activityId`} | ||
element={<ActivityPage />} | ||
/> | ||
</Route> | ||
</Routes> | ||
</BrowserRouter> | ||
) | ||
} |