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.

cleanup, docs

Oleg Isonen37da56d46996
diff --git a/apps/designer/app/routes/dashboard.tsx b/apps/designer/app/routes/dashboard.tsx
index ca41594442..20bd4e9ce0 100644
--- a/apps/designer/app/routes/dashboard.tsx
+++ b/apps/designer/app/routes/dashboard.tsx
@@ -1,12 +1,12 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Designer } from "~/shared/documents/designer";
import env from "~/env.server";
-import { getServerTheme } from "~/shared/theme/theme.server";
+import { getThemeData } from "~/shared/theme";
export const loader: LoaderFunction = async ({ request }) => {
return {
env,
- theme: await getServerTheme(request),
+ theme: await getThemeData(request),
};
};
diff --git a/apps/designer/app/routes/designer.tsx b/apps/designer/app/routes/designer.tsx
index e7ebd09bc3..7097e1b5cf 100644
--- a/apps/designer/app/routes/designer.tsx
+++ b/apps/designer/app/routes/designer.tsx
@@ -1,12 +1,12 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Designer } from "~/shared/documents/designer";
import env from "~/env.server";
-import { getServerTheme } from "~/shared/theme/theme.server";
+import { getThemeData } from "~/shared/theme";
export const loader: LoaderFunction = async ({ request }) => {
return {
env,
- theme: await getServerTheme(request),
+ theme: await getThemeData(request),
};
};
diff --git a/apps/designer/app/routes/login.tsx b/apps/designer/app/routes/login.tsx
index 66b2c3c640..99d9febb01 100644
--- a/apps/designer/app/routes/login.tsx
+++ b/apps/designer/app/routes/login.tsx
@@ -1,12 +1,12 @@
import { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Designer } from "~/shared/documents/designer";
import env from "~/env.server";
-import { getServerTheme } from "~/shared/theme/theme.server";
+import { getThemeData } from "~/shared/theme";
export const loader: LoaderFunction = async ({ request }) => {
return {
env,
- theme: await getServerTheme(request),
+ theme: await getThemeData(request),
};
};
diff --git a/apps/designer/app/routes/rest/theme.$setting.ts b/apps/designer/app/routes/rest/theme.$setting.ts
index dd705c8198..eae98b218b 100644
--- a/apps/designer/app/routes/rest/theme.$setting.ts
+++ b/apps/designer/app/routes/rest/theme.$setting.ts
@@ -1,5 +1,5 @@
import { json, type LoaderFunction } from "@remix-run/node";
-import { themeCookieParser } from "~/shared/theme/index.server";
+import { themeCookieParser } from "~/shared/theme";
export const loader: LoaderFunction = async ({ params }) => {
const { setting } = params;
diff --git a/apps/designer/app/shared/theme/index.server.ts b/apps/designer/app/shared/theme/index.server.ts
deleted file mode 100644
index 6a20ed7455..0000000000
--- a/apps/designer/app/shared/theme/index.server.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./theme";
-export * from "./theme.server";
diff --git a/apps/designer/app/shared/theme/index.ts b/apps/designer/app/shared/theme/index.ts
index 66b920f045..c13a0815fd 100644
--- a/apps/designer/app/shared/theme/index.ts
+++ b/apps/designer/app/shared/theme/index.ts
@@ -1,2 +1,3 @@
export * from "./theme";
-export type { ThemeSetting } from "./shared";
+export * from "./theme.server";
+export type { ThemeSetting } from "./types";
diff --git a/apps/designer/app/shared/theme/theme.server.ts b/apps/designer/app/shared/theme/theme.server.ts
index d072f0bb0f..8252b8b984 100644
--- a/apps/designer/app/shared/theme/theme.server.ts
+++ b/apps/designer/app/shared/theme/theme.server.ts
@@ -1,14 +1,16 @@
import { createCookie } from "@remix-run/node";
-import { cookieNamespace, type ColorScheme, type ThemeSetting } from "./shared";
+import type { ColorScheme, ThemeSetting } from "./types";
+
+const cookieNamespace = "theme";
export const themeCookieParser = createCookie(cookieNamespace);
-export const getServerTheme = async (
+export const getThemeData = async (
request: Request
-): Promise<{ system?: ColorScheme; option?: ThemeSetting }> => {
+): Promise<{ system?: ColorScheme; setting?: ThemeSetting }> => {
const system = (request.headers.get("Sec-CH-Prefers-Color-Scheme") ??
undefined) as ColorScheme | undefined;
- const option: ThemeSetting =
+ const setting: ThemeSetting =
(await themeCookieParser.parse(request.headers.get("Cookie"))) ?? undefined;
- return { system, option };
+ return { system, setting };
};
diff --git a/apps/designer/app/shared/theme/theme.ts b/apps/designer/app/shared/theme/theme.ts
index fae431a5e9..05f672cabc 100644
--- a/apps/designer/app/shared/theme/theme.ts
+++ b/apps/designer/app/shared/theme/theme.ts
@@ -1,12 +1,15 @@
import { useLoaderData } from "@remix-run/react";
import { darkTheme } from "~/shared/design-system";
-import { type ColorScheme, type ThemeSetting } from "./shared";
+import type { ColorScheme, ThemeSetting } from "./types";
// User selected theme setting.
let setting: ThemeSetting = "dark";
// Current systeme theme.
let system: ColorScheme;
+/**
+ * Subscribes color scheme change event and rerenders
+ */
const subscribeSystemTheme = () => {
if (typeof matchMedia === "undefined") return;
const query = matchMedia("(prefers-color-scheme: light)");
@@ -17,59 +20,79 @@ const subscribeSystemTheme = () => {
query.addEventListener("change", (queryEvent) => {
system = getColorScheme(queryEvent);
- setDomProps();
+ renderThemeProps();
});
};
subscribeSystemTheme();
-export const getThemeSetting = (): ThemeSetting => {
- return setting;
-};
-
// @todo todo switch to light by default once ready
-export const defaultTheme = "dark";
+const defaultTheme = "dark";
-const selectTheme = ({
- system = defaultTheme,
- setting,
-}: {
- system: ColorScheme;
- setting?: ThemeSetting;
-}): ColorScheme => {
+/**
+ * Logic we use to decide depending on user setting which color scheme to use.
+ * @returns
+ */
+const getColorScheme = (): ColorScheme => {
if (setting === "system") {
- return system;
+ return system || defaultTheme;
}
return setting || defaultTheme;
};
const getThemeProps = () => {
- const theme = selectTheme({ setting, system });
- if (theme === "dark") {
- // We need to call it so that vars get injected!!!
- darkTheme.toString();
- }
+ const theme = getColorScheme();
return {
className: theme,
style: { colorScheme: theme },
};
};
-const setDomProps = () => {
- const props = getThemeProps();
- document.documentElement.className = props.className;
- document.documentElement.style.colorScheme = props.style.colorScheme;
+/**
+ * We need to call it so that vars get injected.
+ * Not loving this implicit behavior.
+ * Light scheme is also rendered by default, so we only need to do it for the dark.
+ */
+const renderDarkThemeVars = () => {
+ const theme = getColorScheme();
+ if (theme === "dark") {
+ darkTheme.toString();
+ }
+};
+
+/**
+ * When switching theme we need to apply the class/style manually.
+ */
+const renderThemeProps = () => {
+ const { className, style } = getThemeProps();
+ renderDarkThemeVars();
+ document.documentElement.className = className;
+ document.documentElement.style.colorScheme = style.colorScheme;
};
-export const setThemeSetting = (nextsetting: ThemeSetting) => {
- setting = nextsetting;
- setDomProps();
+/**
+ * Apply immediately and set the cookie on the server.
+ */
+export const setThemeSetting = (nextSetting: ThemeSetting) => {
+ setting = nextSetting;
+ renderThemeProps();
fetch(`/rest/theme/${setting}`);
};
+/**
+ * Returns the class and style to applied to some root node serverside and during hydration.
+ */
export const useThemeProps = () => {
const data = useLoaderData();
if (data.theme.setting) setting = data.theme.setting;
if (data.theme.system) system = data.theme.system;
+ renderDarkThemeVars();
return getThemeProps();
};
+
+/**
+ * Currently selected by user theme setting.
+ */
+export const getThemeSetting = (): ThemeSetting => {
+ return setting;
+};
diff --git a/apps/designer/app/shared/theme/shared.ts b/apps/designer/app/shared/theme/types.ts
similarity index 69%
rename from apps/designer/app/shared/theme/shared.ts
rename to apps/designer/app/shared/theme/types.ts
index 4860713b97..30553c6b07 100644
--- a/apps/designer/app/shared/theme/shared.ts
+++ b/apps/designer/app/shared/theme/types.ts
@@ -1,5 +1,2 @@
export type ColorScheme = "dark" | "light";
-
export type ThemeSetting = ColorScheme | "system";
-
-export const cookieNamespace = "theme";