From 483c1d5782000b4092c6ac7846473691aeac84b6 Mon Sep 17 00:00:00 2001 From: boojack Date: Wed, 8 Mar 2023 09:00:10 +0800 Subject: [PATCH] feat: update responsible layout (#1306) * feat: update responsible layout * chore: update --- web/package.json | 1 + web/src/components/Header.tsx | 60 ++++---- web/src/components/HomeSidebar.tsx | 62 +++++---- web/src/components/MobileHeader.tsx | 11 +- web/src/components/SearchBar.tsx | 13 +- web/src/components/UserBanner.tsx | 4 +- web/src/layouts/Root.tsx | 12 +- web/src/less/header.less | 15 -- web/src/less/home-sidebar.less | 15 -- web/src/pages/Home.tsx | 2 +- web/src/store/index.ts | 2 + web/src/store/module/index.ts | 1 + web/src/store/module/layout.ts | 18 +++ web/src/store/reducer/layout.ts | 32 +++++ web/src/utils/layout.ts | 7 + web/yarn.lock | 207 ++++++++++++++++++++++++++-- 16 files changed, 349 insertions(+), 113 deletions(-) delete mode 100644 web/src/less/header.less delete mode 100644 web/src/less/home-sidebar.less create mode 100644 web/src/store/module/layout.ts create mode 100644 web/src/store/reducer/layout.ts create mode 100644 web/src/utils/layout.ts diff --git a/web/package.json b/web/package.json index 57cf0038..f6adfb67 100644 --- a/web/package.json +++ b/web/package.json @@ -26,6 +26,7 @@ "react-i18next": "^11.18.6", "react-redux": "^8.0.1", "react-router-dom": "^6.8.2", + "react-use": "^17.4.0", "semver": "^7.3.8", "tailwindcss": "^3.2.4" }, diff --git a/web/src/components/Header.tsx b/web/src/components/Header.tsx index 57fd477a..bcdc03a9 100644 --- a/web/src/components/Header.tsx +++ b/web/src/components/Header.tsx @@ -1,8 +1,8 @@ -import { isUndefined } from "lodash-es"; import { useEffect } from "react"; import { Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; -import { useLocationStore, useUserStore } from "../store/module"; +import { useLayoutStore, useLocationStore, useUserStore } from "../store/module"; +import { resolution } from "../utils/layout"; import Icon from "./Icon"; import showDailyReviewDialog from "./DailyReviewDialog"; import showResourcesDialog from "./ResourcesDialog"; @@ -10,24 +10,45 @@ import showSettingDialog from "./SettingDialog"; import showAskAIDialog from "./AskAIDialog"; import showArchivedMemoDialog from "./ArchivedMemoDialog"; import UserBanner from "./UserBanner"; -import "../less/header.less"; const Header = () => { const { t } = useTranslation(); const userStore = useUserStore(); const locationStore = useLocationStore(); - const query = locationStore.state.query; + const layoutStore = useLayoutStore(); + const showHeader = layoutStore.state.showHeader; useEffect(() => { - toggleHeader(false); - }, [query]); + const handleWindowResize = () => { + if (window.innerWidth < resolution.sm) { + layoutStore.setHeaderStatus(false); + } else { + layoutStore.setHeaderStatus(true); + } + }; + window.addEventListener("resize", handleWindowResize); + handleWindowResize(); + }, []); return ( - <> -
toggleHeader(false)}>
-
+
+
layoutStore.setHeaderStatus(false)} + >
+
-
+
{ )}
- +
); }; -export const toggleHeader = (show?: boolean) => { - const headerEl = document.body.querySelector(".header-wrapper") as HTMLDivElement; - const maskEl = headerEl.previousSibling as HTMLDivElement; - - if (isUndefined(show)) { - show = !headerEl.classList.contains("show"); - } - - if (show) { - headerEl.classList.add("show"); - maskEl.classList.add("show"); - } else { - headerEl.classList.remove("show"); - maskEl.classList.remove("show"); - } -}; - export default Header; diff --git a/web/src/components/HomeSidebar.tsx b/web/src/components/HomeSidebar.tsx index 1b655edb..20297986 100644 --- a/web/src/components/HomeSidebar.tsx +++ b/web/src/components/HomeSidebar.tsx @@ -1,15 +1,44 @@ -import { isUndefined } from "lodash-es"; +import { useEffect } from "react"; +import { resolution } from "../utils/layout"; +import { useLayoutStore } from "../store/module"; import ShortcutList from "./ShortcutList"; import TagList from "./TagList"; import SearchBar from "./SearchBar"; import UsageHeatMap from "./UsageHeatMap"; -import "../less/home-sidebar.less"; const HomeSidebar = () => { + const layoutStore = useLayoutStore(); + const showHomeSidebar = layoutStore.state.showHomeSidebar; + + useEffect(() => { + const handleWindowResize = () => { + if (window.innerWidth < resolution.sm) { + layoutStore.setHomeSidebarStatus(false); + } else { + layoutStore.setHomeSidebarStatus(true); + } + }; + window.addEventListener("resize", handleWindowResize); + handleWindowResize(); + }, []); + return ( - <> -
toggleHomeSidebar(false)}>
-