Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: antd语言和主题色随系统
gaoSiraa24eff719a9
diff --git a/src/app/[lng]/layout.tsx b/src/app/[lng]/layout.tsxindex 0f2959da..de02bf89 100644--- a/src/app/[lng]/layout.tsx+++ b/src/app/[lng]/layout.tsx@@ -34,7 +34,7 @@ export default async function RootLayout({return (<html lang={lng} dir={dir(lng)}><body>- <Providers>+ <Providers lng={lng}><LyaoutHeader />{children}<LyaoutFooter lng={lng} />diff --git a/src/app/hooks/useCssVariables.ts b/src/app/hooks/useCssVariables.tsnew file mode 100644index 00000000..41a85529--- /dev/null+++ b/src/app/hooks/useCssVariables.ts@@ -0,0 +1,63 @@+import { useState, useEffect } from "react";++/**+ * 自定义 Hook,用于获取多个 CSS 变量的值。+ * @param variableNames - CSS 变量名称数组(例如,['--main-color', '--secondary-color'])。+ * @param element - 获取 CSS 变量的元素(默认为 document.documentElement)。+ * @returns 一个对象,其中 CSS 变量名称作为键,变量值作为值。+ */+const useCssVariables = (+ variableNames: string[] = [+ "--theColor1",+ "--theColor2",+ "--theColor3",+ "--theColor4",+ "--theColor5",+ "--theColor6",+ "--theColor7",+ "--theColor8",+ "--theColor9",+ ],+ element: HTMLElement = document.documentElement,+): Record<string, string> => {+ const [variables, setVariables] = useState<Record<string, string>>({});++ useEffect(() => {+ const handleVariableChange = () => {+ const computedStyle = getComputedStyle(element);+ const newVariables = variableNames.reduce(+ (acc, name) => {+ acc[name] = computedStyle.getPropertyValue(name).trim();+ return acc;+ },+ {} as Record<string, string>,+ );++ // 只有在变量值实际发生变化时才更新状态+ setVariables((prevVariables) => {+ const hasChanged = variableNames.some(+ (name) => prevVariables[name] !== newVariables[name],+ );+ return hasChanged ? newVariables : prevVariables;+ });+ };++ // 初始获取+ handleVariableChange();++ // 可选:观察元素样式的变化+ const observer = new MutationObserver(handleVariableChange);+ observer.observe(element, {+ attributes: true,+ attributeFilter: ["style", "class"],+ });++ return () => {+ observer.disconnect();+ };+ }, [variableNames, element]);++ return variables;+};++export default useCssVariables;diff --git a/src/app/i18n/settings.ts b/src/app/i18n/settings.tsindex 40d6d20c..251d6b21 100644--- a/src/app/i18n/settings.ts+++ b/src/app/i18n/settings.ts@@ -1,5 +1,5 @@export const fallbackLng = "en";-export const languages = [fallbackLng, "de", "it", "zh-CN"];+export const languages = [fallbackLng, "zh-CN"];export const defaultNS = "translation";export const cookieName = "i18next";diff --git a/src/app/layout/Providers.tsx b/src/app/layout/Providers.tsxindex e420c9be..372beb88 100644--- a/src/app/layout/Providers.tsx+++ b/src/app/layout/Providers.tsx@@ -2,18 +2,41 @@import { GoogleOAuthProvider } from "@react-oauth/google";import { ConfigProvider, App } from "antd";-import zh_CN from "antd/es/locale/zh_CN";import { AntdRegistry } from "@ant-design/nextjs-registry";import { Suspense } from "react";+import type { Locale } from "antd/es/locale";+import zh_CN from "antd/es/locale/zh_CN";+import en_US from "antd/es/locale/en_US";+import { fallbackLng } from "@/app/i18n/settings";+import useCssVariables from "@/app/hooks/useCssVariables";++// antd 语言获取+const getAntdLang = (lang: string): Locale => {+ switch (lang) {+ case "zh-CN":+ return zh_CN;+ case "en":+ return en_US;+ }+ return getAntdLang(fallbackLng);+};++export function Providers({+ children,+ lng,+}: {+ children: React.ReactNode;+ lng: string;+}) {+ const cssVariables = useCssVariables();-export function Providers({ children }: { children: React.ReactNode }) {return (<GoogleOAuthProvider clientId="471394506793-t2g4mg90vr8qgpq5stbua0b22pofqrne.apps.googleusercontent.com"><ConfigProvider- locale={zh_CN}+ locale={getAntdLang(lng)}theme={{token: {- colorPrimary: "#d49c61",+ colorPrimary: cssVariables["--theColor5"],},}}>diff --git a/src/var.css b/src/var.cssindex e53f6f47..88b8820b 100644--- a/src/var.css+++ b/src/var.css@@ -38,15 +38,15 @@}/* 主题色由浅到深 */- --theColor1: #fbf0ff;- --theColor2: #fbf0ff;- --theColor3: #f9f0ff;- --theColor4: #f4e6ff;- --theColor5: #d8b5f5;- --theColor6: #b08fcf;- --theColor7: #896ca8;- --theColor8: #654d82;- --theColor9: #45355c;+ --theColor1: #faf0ff;+ --theColor2: #f9f0ff;+ --theColor3: #e4c5fc;+ --theColor4: #c595f0;+ --theColor5: #a66ae4;+ --theColor6: #814dbd;+ --theColor7: #603696;+ --theColor8: #422370;+ --theColor9: #29164a;--primary-color: var(--theColor5);/* 原先为:#D7B4F5 */--primary-hover: var(--theColor6);