mirror of
https://github.com/jnsahaj/tweakcn.git
synced 2026-08-30 18:10:28 +08:00
cbd0ac431b
This reverts commit 88261b5c86.
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { defaultDarkThemeStyles, defaultLightThemeStyles } from "@/config/theme";
|
|
|
|
export function ThemeScript() {
|
|
const scriptContent = `
|
|
(function() {
|
|
const storageKey = "editor-storage";
|
|
const root = document.documentElement;
|
|
const defaultLightStyles = ${JSON.stringify(defaultLightThemeStyles)};
|
|
const defaultDarkStyles = ${JSON.stringify(defaultDarkThemeStyles)};
|
|
|
|
let themeState = null;
|
|
try {
|
|
const persistedStateJSON = localStorage.getItem(storageKey);
|
|
if (persistedStateJSON) {
|
|
themeState = JSON.parse(persistedStateJSON)?.state?.themeState;
|
|
}
|
|
} catch (e) {
|
|
console.warn("Theme initialization: Failed to read/parse localStorage:", e);
|
|
}
|
|
|
|
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
const mode = themeState?.currentMode ?? (prefersDark ? "dark" : "light");
|
|
|
|
const activeStyles =
|
|
mode === "dark"
|
|
? themeState?.styles?.dark || defaultDarkStyles
|
|
: themeState?.styles?.light || defaultLightStyles;
|
|
|
|
const stylesToApply = Object.keys(defaultLightStyles);
|
|
|
|
for (const styleName of stylesToApply) {
|
|
const value = activeStyles[styleName];
|
|
if (value !== undefined) {
|
|
root.style.setProperty(\`--\${styleName}\`, value);
|
|
}
|
|
}
|
|
})();
|
|
`;
|
|
|
|
return (
|
|
<script
|
|
dangerouslySetInnerHTML={{ __html: scriptContent }}
|
|
suppressHydrationWarning
|
|
/>
|
|
);
|
|
}
|