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.

read cookie from js

Oleg Isonen08418a22041e
diff --git a/apps/designer/app/designer/features/topbar/menu/menu.tsx b/apps/designer/app/designer/features/topbar/menu/menu.tsx
index 05b14970f7..a4f312342b 100644
--- a/apps/designer/app/designer/features/topbar/menu/menu.tsx
+++ b/apps/designer/app/designer/features/topbar/menu/menu.tsx
@@ -20,7 +20,7 @@ import {
useIsPublishDialogOpen,
} from "~/designer/shared/nano-states";
import { isFeatureEnabled } from "~/shared/feature-flags";
-import { getSetting } from "~/designer/shared/client-settings";
+import { getBrowserTheme } from "~/shared/theme";
const menuItemCss = {
display: "flex",
@@ -38,7 +38,7 @@ const textCss = {
const ThemeMenuItem = () => {
if (isFeatureEnabled("theme") === false) return null;
- const currentTheme = getSetting("theme");
+ const currentTheme = getBrowserTheme();
const publishThemeChange = (value: string) => {
publish({
diff --git a/apps/designer/app/designer/shared/client-settings/values.ts b/apps/designer/app/designer/shared/client-settings/values.ts
index 6baca6b084..4862f44df8 100644
--- a/apps/designer/app/designer/shared/client-settings/values.ts
+++ b/apps/designer/app/designer/shared/client-settings/values.ts
@@ -1,4 +1,5 @@
-export const theme = {
- defaultValue: "system",
- values: ["dark", "light", "system"],
+// @todo delete example once we have other values
+export const example = {
+ defaultValue: "example",
+ values: ["one", "two", "three"],
} as const;
diff --git a/apps/designer/app/entry.server.tsx b/apps/designer/app/entry.server.tsx
index 7ee8c468dd..245bda73b5 100644
--- a/apps/designer/app/entry.server.tsx
+++ b/apps/designer/app/entry.server.tsx
@@ -5,12 +5,13 @@ import type { EntryContext } from "@remix-run/node";
import * as Sentry from "@sentry/remix";
import { initSentry } from "./shared/sentry";
import { prisma } from "./shared/db/prisma.server";
+import { insertTheme } from "./shared/theme";
initSentry({
integrations: [new Sentry.Integrations.Prisma({ client: prisma })],
});
-export default function handleRequest(
+export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
@@ -21,6 +22,7 @@ export default function handleRequest(
);
markup = insertCriticalCss(markup, request.url);
+ markup = await insertTheme(markup, request.headers);
responseHeaders.set("Content-Type", "text/html");
diff --git a/apps/designer/app/routes/designer.tsx b/apps/designer/app/routes/designer.tsx
index 932f3a8cac..748f54ac9b 100644
--- a/apps/designer/app/routes/designer.tsx
+++ b/apps/designer/app/routes/designer.tsx
@@ -1,4 +1,8 @@
-import type { LoaderFunction, MetaFunction } from "@remix-run/node";
+import type {
+ HeadersFunction,
+ LoaderFunction,
+ MetaFunction,
+} from "@remix-run/node";
import { Designer } from "~/shared/documents/designer";
import env from "~/env.server";
@@ -10,4 +14,8 @@ export const meta: MetaFunction = () => {
return { title: "Webstudio" };
};
+export const headers: HeadersFunction = () => ({
+ "Accept-CH": "Sec-CH-Prefers-Color-Scheme",
+});
+
export default Designer;
diff --git a/apps/designer/app/routes/index.tsx b/apps/designer/app/routes/index.tsx
index 26211f87dd..138289439f 100644
--- a/apps/designer/app/routes/index.tsx
+++ b/apps/designer/app/routes/index.tsx
@@ -1,4 +1,8 @@
-import { redirect, type LoaderFunction } from "@remix-run/node";
+import {
+ type HeadersFunction,
+ redirect,
+ type LoaderFunction,
+} from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { Root, type Data } from "@webstudio-is/sdk";
import config from "~/config";
@@ -13,6 +17,10 @@ type LoaderReturnProps = Promise<
(Data & { env: Env }) | (Error & { env: Env }) | Response
>;
+export const headers: HeadersFunction = () => ({
+ "Accept-CH": "Sec-CH-Prefers-Color-Scheme",
+});
+
export const loader: LoaderFunction = async ({
request,
}): LoaderReturnProps => {
diff --git a/apps/designer/app/shared/design-system/stitches.config.ts b/apps/designer/app/shared/design-system/stitches.config.ts
index 9b9daafa4d..09be767c99 100644
--- a/apps/designer/app/shared/design-system/stitches.config.ts
+++ b/apps/designer/app/shared/design-system/stitches.config.ts
@@ -433,7 +433,7 @@ export const {
export type CSS = Stitches.CSS<typeof config>;
-export const darkTheme = createTheme("dark-theme", {
+export const darkTheme = createTheme("dark", {
colors: {
...grayDark,
...mauveDark,
diff --git a/apps/designer/app/shared/documents/designer.tsx b/apps/designer/app/shared/documents/designer.tsx
index 9a6fcf5b97..c4771f8dbb 100644
--- a/apps/designer/app/shared/documents/designer.tsx
+++ b/apps/designer/app/shared/documents/designer.tsx
@@ -1,16 +1,15 @@
import { Links, LiveReload, Meta, Outlet, Scripts } from "@remix-run/react";
import { CriticalCss } from "@webstudio-is/sdk";
import { Env } from "~/shared/env";
-import { Theme } from "~/shared/theme";
+import { getThemePlaceholder } from "~/shared/theme";
export const Designer = () => {
return (
- <html lang="en">
+ <html lang="en" {...getThemePlaceholder()}>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
- <Theme />
<Links />
<CriticalCss />
</head>
diff --git a/apps/designer/app/shared/theme/theme.ts b/apps/designer/app/shared/theme/theme.ts
new file mode 100644
index 0000000000..6103d3d242
--- /dev/null
+++ b/apps/designer/app/shared/theme/theme.ts
@@ -0,0 +1,48 @@
+import { createCookie } from "@remix-run/node";
+import browserCookies from "js-cookie";
+import { darkTheme } from "~/shared/design-system/stitches.config";
+
+type ColorScheme = "dark" | "light";
+
+const placeholder = "__theme_placeholder__";
+
+const cookieNamespace = "theme";
+
+// @todo todo switch to light by default once ready
+const defaultTheme = "dark";
+
+export const getBrowserTheme = () => {
+ return browserCookies.get(cookieNamespace) || defaultTheme;
+};
+
+const getCookieTheme = async (headers: Headers): Promise<ColorScheme> => {
+ const cookieTheme = await createCookie(cookieNamespace).parse(
+ headers.get("Cookie")
+ );
+ if (cookieTheme === "system") {
+ const system = headers.get("Sec-CH-Prefers-Color-Scheme");
+ if (system === "light" || system === "dark") {
+ return system;
+ }
+ }
+ return cookieTheme || defaultTheme;
+};
+
+export const getThemePlaceholder = () => {
+ if (typeof document === "undefined") {
+ return {
+ className: placeholder,
+ style: { colorScheme: placeholder },
+ };
+ }
+ const theme = getBrowserTheme();
+ return {
+ className: theme === "dark" ? darkTheme.toString() : "light",
+ style: { colorScheme: theme },
+ };
+};
+
+export const insertTheme = async (markup: string, headers: Headers) => {
+ const theme = await getCookieTheme(headers);
+ return markup.replace(new RegExp(placeholder, "gi"), theme);
+};
diff --git a/apps/designer/app/shared/theme/theme.tsx b/apps/designer/app/shared/theme/theme.tsx
deleted file mode 100644
index c0abf05e20..0000000000
--- a/apps/designer/app/shared/theme/theme.tsx
+++ /dev/null
@@ -1,27 +0,0 @@
-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,
- }}
- />
- );
-};
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 87fe48f801..cc32e758b8 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -60,6 +60,7 @@
"hyphenate-style-name": "^1.0.4",
"immer": "^9.0.12",
"immerhin": "^0.1.2",
+ "js-cookie": "^3.0.1",
"lexical": "^0.3.5",
"lodash.capitalize": "^4.2.1",
"lodash.debounce": "^4.0.8",
diff --git a/yarn.lock b/yarn.lock
index b27560e4e0..9ddce00c82 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -10229,6 +10229,11 @@ js-cookie@^2.2.1:
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.2.1.tgz#69e106dc5d5806894562902aa5baec3744e9b2b8"
integrity sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==
+js-cookie@^3.0.1:
+ version "3.0.1"
+ resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-3.0.1.tgz#9e39b4c6c2f56563708d7d31f6f5f21873a92414"
+ integrity sha512-+0rgsUXZu4ncpPxRL+lNEptWMOWl9etvPHc/koSRp6MPwpRYAhmk0dUG00J4bxVV3r9uUzfo24wW0knS07SKSw==
+
js-string-escape@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/js-string-escape/-/js-string-escape-1.0.1.tgz#e2625badbc0d67c7533e9edc1068c587ae4137ef"