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.

simplify css preview logic

Oleg Isonen1a623b7e9d0f
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index e87056b67f..72e203e2fb 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -24,7 +24,7 @@ import {
useUnselectInstance,
useUpdateSelectedInstance,
} from "./shared/instance";
-import { useUpdateStyle } from "./shared/style";
+import { usePreviewStyle, useUpdateStyle } from "./shared/style";
import { useTrackSelectedElement } from "./shared/use-track-selected-element";
import { WrapperComponentDev } from "./features/wrapper-component";
import { useSync } from "./shared/sync";
@@ -94,6 +94,7 @@ type DesignModeProps = {
const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
useUpdateStyle();
+ usePreviewStyle();
useManageProps();
usePublishSelectedInstanceData(treeId);
useHandleBreakpoints();
diff --git a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
index ef84f5f1ed..3e62f710e9 100644
--- a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
@@ -1,32 +1,14 @@
-import { useEffect, useMemo, useState } from "react";
+import { useMemo } from "react";
import {
type Instance,
css as createCss,
type CSS,
- toValue,
- toVarNamespace,
} from "@webstudio-is/react-sdk";
-import { useSubscribe } from "~/shared/pubsub";
-import { type StyleUpdates } from "~/shared/canvas-components";
type UseCssProps = {
- instance: Instance;
css: CSS;
};
-const usePreviewCss = (instance: Instance) => {
- useSubscribe(`previewStyle:${instance.id}`, ({ updates }) => {
- for (const update of updates) {
- const property = `--${toVarNamespace(instance, update.property)}`;
- if (update.value === undefined) {
- document.body.style.removeProperty(property);
- continue;
- }
- document.body.style.setProperty(property, toValue(update.value));
- }
- });
-};
-
const voidElements =
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr";
@@ -53,7 +35,6 @@ const defaultStyle = {
},
};
-export const useCss = ({ instance, css }: UseCssProps): string => {
- usePreviewCss(instance);
+export const useCss = ({ css }: UseCssProps): string => {
return useMemo(() => createCss(css)(defaultStyle), [css]);
};
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
index 962509087e..f4fdfcd845 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -52,7 +52,7 @@ export const WrapperComponentDev = ({
onChangeChildren = noop,
...rest
}: WrapperComponentDevProps) => {
- const className = useCss({ instance, css });
+ const className = useCss({ css });
const [editingInstanceId] = useTextEditingInstanceId();
const [, setSelectedElement] = useSelectedElement();
diff --git a/apps/designer/app/canvas/shared/style.ts b/apps/designer/app/canvas/shared/style.ts
index 013e0d99b8..9e805848f8 100644
--- a/apps/designer/app/canvas/shared/style.ts
+++ b/apps/designer/app/canvas/shared/style.ts
@@ -3,6 +3,7 @@ import { useSubscribe } from "~/shared/pubsub";
import { setInstanceStyleMutable } from "~/shared/tree-utils";
import { useSelectedInstance } from "./nano-states";
import { rootInstanceContainer } from "~/shared/nano-states";
+import { toValue, toVarNamespace } from "@webstudio-is/react-sdk";
export const useUpdateStyle = () => {
const [selectedInstance] = useSelectedInstance();
@@ -19,3 +20,16 @@ export const useUpdateStyle = () => {
});
});
};
+
+export const usePreviewStyle = () => {
+ useSubscribe("previewStyle", ({ id, updates }) => {
+ for (const update of updates) {
+ const property = `--${toVarNamespace(id, update.property)}`;
+ if (update.value === undefined) {
+ document.body.style.removeProperty(property);
+ continue;
+ }
+ document.body.style.setProperty(property, toValue(update.value));
+ }
+ });
+};
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
index d199b7d220..5b7838adeb 100644
--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
@@ -14,7 +14,7 @@ import { useRootInstance } from "~/shared/nano-states";
declare module "~/shared/pubsub" {
export interface PubsubMap {
updateStyle: StyleUpdates;
- [key: `previewStyle:${Instance["id"]}`]: StyleUpdates;
+ previewStyle: StyleUpdates;
}
}
@@ -87,10 +87,7 @@ export const useStyleData = ({
return;
}
publish({
- type:
- type === "update"
- ? "updateStyle"
- : (`previewStyle:${selectedInstanceData.id}` as const),
+ type: type === "update" ? "updateStyle" : "previewStyle",
payload: {
id: selectedInstanceData.id,
updates,
diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.ts
index ce29759db2..0d576f45a7 100644
--- a/packages/react-sdk/src/stitches/to-css.ts
+++ b/packages/react-sdk/src/stitches/to-css.ts
@@ -33,8 +33,8 @@ export const toValue = (
return value.value;
};
-export const toVarNamespace = (instance: Instance, property: string) => {
- return `${property}-${instance.id}`;
+export const toVarNamespace = (id: string, property: string) => {
+ return `${property}-${id}`;
};
const toVarValue = (
@@ -74,7 +74,7 @@ export const toCss = (
for (const property in cssRule.style) {
const value = cssRule.style[property as StyleProperty];
if (value === undefined) continue;
- const namespace = toVarNamespace(instance, property);
+ const namespace = toVarNamespace(instance.id, property);
style[property] = toVarValue(namespace, value, options);
}