Skip to content

Commit

Permalink
feat: add route transition
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko233 committed Nov 11, 2023
1 parent 01d6310 commit 1eb34e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"react-hook-form": "^7.39.5",
"react-i18next": "^12.0.0",
"react-router-dom": "^6.4.3",
"react-transition-group": "^4.4.5",
"react-virtuoso": "^3.1.3",
"recoil": "^0.7.6",
"snarkdown": "^2.0.0",
Expand Down
21 changes: 21 additions & 0 deletions src/assets/styles/anime.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.page-enter {
opacity: 0;
transform: scale(0.9);
}

.page-enter-active {
opacity: 1;
transform: scale(1);
transition: opacity 300ms, transform 300ms;
}

.page-exit {
opacity: 1;
transform: scale(0);
}

.page-exit-active {
opacity: 0;
transform: scale(0.9);
transition: opacity 300ms, transform 300ms;
}
1 change: 1 addition & 0 deletions src/assets/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ body {

@import "./layout.scss";
@import "./page.scss";
@import "./anime.scss";

@media (prefers-color-scheme: dark) {
:root {
Expand Down
41 changes: 25 additions & 16 deletions src/pages/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import relativeTime from "dayjs/plugin/relativeTime";
import { SWRConfig, mutate } from "swr";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Route, Routes } from "react-router-dom";
import { Route, Routes, useLocation } from "react-router-dom";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import { alpha, List, Paper, ThemeProvider } from "@mui/material";
import { listen } from "@tauri-apps/api/event";
import { appWindow } from "@tauri-apps/api/window";
Expand Down Expand Up @@ -34,6 +35,8 @@ const Layout = () => {
const { verge } = useVerge();
const { theme_blur, language } = verge || {};

const location = useLocation();

useEffect(() => {
window.addEventListener("keydown", (e) => {
// macOS有cmd+w
Expand Down Expand Up @@ -136,21 +139,27 @@ const Layout = () => {
</div>
)}

<div className="the-content">
<Routes>
{routers.map(({ label, link, ele: Ele }) => (
<Route
key={label}
path={link}
element={
<BaseErrorBoundary key={label}>
<Ele />
</BaseErrorBoundary>
}
/>
))}
</Routes>
</div>
<TransitionGroup className="the-content">
<CSSTransition
key={location.pathname}
timeout={300}
classNames="page"
>
<Routes>
{routers.map(({ label, link, ele: Ele }) => (
<Route
key={label}
path={link}
element={
<BaseErrorBoundary key={label}>
<Ele />
</BaseErrorBoundary>
}
/>
))}
</Routes>
</CSSTransition>
</TransitionGroup>
</div>
</Paper>
</ThemeProvider>
Expand Down

0 comments on commit 1eb34e0

Please sign in to comment.