From d9c248e2d1da81fac9fe65f1d07c806a44935bb1 Mon Sep 17 00:00:00 2001 From: expdsn <18111002318@163.com> Date: Fri, 7 Mar 2025 13:44:20 +0800 Subject: [PATCH] save --- app/(main)/page.tsx | 2 ++ app/_lib/atom.ts | 3 +- app/_ui/CustomAffix.tsx | 63 +++++++++++++++++++++++++++++++++++++++++ app/_ui/SiderNav.tsx | 2 +- app/layout.tsx | 2 ++ app/store.tsx | 21 ++++++++++++++ tailwind.config.ts | 1 + 7 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 app/_ui/CustomAffix.tsx create mode 100644 app/store.tsx diff --git a/app/(main)/page.tsx b/app/(main)/page.tsx index 2abf00a..ed597d7 100644 --- a/app/(main)/page.tsx +++ b/app/(main)/page.tsx @@ -5,6 +5,7 @@ import { getLinkTypeList } from "../_lib/data/linkType"; import { getLinkList } from "../_lib/data/link"; import Footer from "../_ui/footer"; import { Suspense } from "react"; +import CustomAffix from "../_ui/CustomAffix"; export default async function Page() { @@ -21,6 +22,7 @@ export default async function Page() { + diff --git a/app/_lib/atom.ts b/app/_lib/atom.ts index 6a4f98f..93c6fdf 100644 --- a/app/_lib/atom.ts +++ b/app/_lib/atom.ts @@ -1,4 +1,5 @@ import { atom } from 'jotai'; +import { atomWithStorage } from "jotai/utils"; -// 定义一个简单的原子 +export const themeAtom = atomWithStorage('theme', 'light') export const linkTypeAtom = atom(''); diff --git a/app/_ui/CustomAffix.tsx b/app/_ui/CustomAffix.tsx new file mode 100644 index 0000000..e877926 --- /dev/null +++ b/app/_ui/CustomAffix.tsx @@ -0,0 +1,63 @@ +"use client"; + +import { faArrowUp, faArrowUp91, faArrowUpAZ, faArrowUpShortWide, faChessKnight, faMoon, faSun, faUpLong } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { useAtom } from "jotai"; +import { useEffect, useState } from "react"; +import { themeAtom } from "../_lib/atom"; +import { Tooltip } from "antd"; + + +export default function CustomAffix() { + const [show, setShow] = useState(false); + const [theme, setTheme] = useAtom(themeAtom) + const handleScroll = () => { + const distance = { + x: document.documentElement.scrollLeft || document.body.scrollLeft, + y: document.documentElement.scrollTop || document.body.scrollTop + }; + console.log(distance); + if (distance.y > 600) { + setShow(true); + } else { + setShow(false) + } + }; + + useEffect(() => { + window.addEventListener('scroll', handleScroll); + return () => { + window.removeEventListener('scroll', handleScroll); + }; + }, []); + return ( +
+ { + show && +
{ + window.scrollTo({ top: 0, behavior: "smooth" }); + }} + className="w-[40px] h-[40px] group bg-[#000]/20 backdrop-blur-md cursor-pointer hover:scale-105 duration-150 rounded-full flex justify-center items-center"> + +
+ + } + +
{ + if (theme === "dark") { + setTheme("light") + } else { + setTheme("dark") + + } + }} + className="w-[40px] h-[40px] group bg-[#000]/20 backdrop-blur-md cursor-pointer hover:scale-105 duration-150 rounded-full flex justify-center items-center"> + +
+
+ +
+ ) +} \ No newline at end of file diff --git a/app/_ui/SiderNav.tsx b/app/_ui/SiderNav.tsx index 932f40e..5b20eec 100644 --- a/app/_ui/SiderNav.tsx +++ b/app/_ui/SiderNav.tsx @@ -13,7 +13,7 @@ export default function SiderNav({ linkList }: { linkList: LinkType[] }) { const [, setSelectType] = useAtom(linkTypeAtom) return ( -
+
diff --git a/app/layout.tsx b/app/layout.tsx index 11bea09..44b357c 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -2,6 +2,7 @@ import type { Metadata } from "next"; import "./globals.css"; import '@fortawesome/fontawesome-svg-core/styles.css' import '@ant-design/v5-patch-for-react-19'; +import Store from "./store"; export const metadata: Metadata = { title: "Create Next App", description: "Generated by create next app", @@ -19,6 +20,7 @@ export default function RootLayout({ > {children} + ); } diff --git a/app/store.tsx b/app/store.tsx new file mode 100644 index 0000000..1410b90 --- /dev/null +++ b/app/store.tsx @@ -0,0 +1,21 @@ +"use client"; + +import { useAtom } from "jotai"; +import { themeAtom } from "./_lib/atom"; +import { useEffect } from "react"; + +export default function Store() { + const [theme] = useAtom(themeAtom) + + useEffect(() => { + const root = document.documentElement + console.log('theme: ' + theme); + + if (theme === 'dark') { + root.classList.add('dark') + } else { + root.classList.remove('dark') + } + }, [theme]) + return '' +} \ No newline at end of file diff --git a/tailwind.config.ts b/tailwind.config.ts index 1362b88..c726b43 100644 --- a/tailwind.config.ts +++ b/tailwind.config.ts @@ -15,4 +15,5 @@ export default { }, }, plugins: [], + darkMode: 'selector' } satisfies Config;