Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

working theme switcher over local storage

Oleg Isonen1f9a9de521ea
diff --git a/apps/designer/app/designer/shared/client-settings/settings.ts b/apps/designer/app/designer/shared/client-settings/settings.ts
index 3b0c57d6b0..2235afa106 100644
--- a/apps/designer/app/designer/shared/client-settings/settings.ts
+++ b/apps/designer/app/designer/shared/client-settings/settings.ts
@@ -12,7 +12,9 @@ const read = (): Settings => {
const settings = localStorage.getItem(namespace);
if (settings === null) return {};
return JSON.parse(settings);
- } catch (error) {}
+ } catch (error) {
+ // We don't need to handle this one.
+ }
return {};
};
diff --git a/apps/designer/app/shared/design-system/components/theme.tsx b/apps/designer/app/shared/design-system/components/theme.tsx
deleted file mode 100644
index f12f2a00be..0000000000
--- a/apps/designer/app/shared/design-system/components/theme.tsx
+++ /dev/null
@@ -1,30 +0,0 @@
-import { getSetting } from "~/designer/shared/client-settings";
-import { darkTheme } from "../stitches.config";
-
-const getTheme = () => {
- const theme = getSetting("theme");
- if (theme === "dark") return darkTheme.toString();
- return "light-theme";
- // @todo handle system theme
-};
-
-export const Theme = () => {
- globalThis.theme = getTheme();
- console.log(1111, globalThis.theme);
-
- const code = `
- !(function () {
- setTimeout(function() {
- console.log(222, globalThis.theme)
- document.documentElement.classList.add(globalThis.theme)
- });
- })();
- `;
- return (
- <script
- dangerouslySetInnerHTML={{
- __html: code,
- }}
- />
- );
-};
diff --git a/apps/designer/app/shared/design-system/index.ts b/apps/designer/app/shared/design-system/index.ts
index b28fc45209..8e58533e09 100644
--- a/apps/designer/app/shared/design-system/index.ts
+++ b/apps/designer/app/shared/design-system/index.ts
@@ -11,7 +11,6 @@ export {
theme,
type CSS,
} from "./stitches.config";
-export * from "./components/theme";
export * as Collapsible from "@radix-ui/react-collapsible";
export { AccessibleIcon } from "@radix-ui/react-accessible-icon";
diff --git a/apps/designer/app/shared/documents/designer.tsx b/apps/designer/app/shared/documents/designer.tsx
index 122249e9a9..9a6fcf5b97 100644
--- a/apps/designer/app/shared/documents/designer.tsx
+++ b/apps/designer/app/shared/documents/designer.tsx
@@ -1,7 +1,7 @@
import { Links, LiveReload, Meta, Outlet, Scripts } from "@remix-run/react";
import { CriticalCss } from "@webstudio-is/sdk";
import { Env } from "~/shared/env";
-import { Theme } from "../design-system";
+import { Theme } from "~/shared/theme";
export const Designer = () => {
return (
diff --git a/apps/designer/app/shared/theme/index.ts b/apps/designer/app/shared/theme/index.ts
new file mode 100644
index 0000000000..3797aeae07
--- /dev/null
+++ b/apps/designer/app/shared/theme/index.ts
@@ -0,0 +1 @@
+export * from "./theme";
diff --git a/apps/designer/app/shared/theme/theme.tsx b/apps/designer/app/shared/theme/theme.tsx
new file mode 100644
index 0000000000..c0abf05e20
--- /dev/null
+++ b/apps/designer/app/shared/theme/theme.tsx
@@ -0,0 +1,27 @@
+import { darkTheme } from "../design-system/stitches.config";
+
+export const Theme = () => {
+ darkTheme.toString();
+ // @todo switch the default to system theme once we support both.
+ const code = `
+ !(function () {
+ try {
+ var theme = 'dark';
+ var savedTheme = localStorage.getItem("__webstudio_theme__");
+ if (savedTheme === 'system') {
+ theme = matchMedia("(prefers-color-scheme: light)").matches ? 'light' : 'dark';
+ }
+ if (savedTheme && savedTheme !== theme) theme = savedTheme
+ document.documentElement.classList.add(theme + "-theme");
+ document.documentElement.style.colorScheme = theme;
+ } catch (e) {}
+ })();
+ `;
+ return (
+ <script
+ dangerouslySetInnerHTML={{
+ __html: code,
+ }}
+ />
+ );
+};