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.

set values over css vars

Oleg Isonena30b7ad5bec4
diff --git a/apps/designer/app/canvas/features/text-editor/text-editor.tsx b/apps/designer/app/canvas/features/text-editor/text-editor.tsx
index 71a6113e38..854ff654d0 100644
--- a/apps/designer/app/canvas/features/text-editor/text-editor.tsx
+++ b/apps/designer/app/canvas/features/text-editor/text-editor.tsx
@@ -29,7 +29,7 @@ const NodeConnector = ({
const [key] = nodeKey.split(":");
const element = editor.getElementByKey(key);
if (element) {
- const className = css(toCss(instance.cssRules, breakpoints))().toString();
+ const className = css(toCss(instance, breakpoints))().toString();
// assume only styles are important while editing text
element.className = className;
}
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 30fd71a772..db547b0882 100644
--- a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
@@ -4,6 +4,7 @@ import {
css as createCss,
type CSS,
toValue,
+ toVarNamespace,
} from "@webstudio-is/react-sdk";
import { useSubscribe } from "~/shared/pubsub";
import { type StyleUpdates } from "~/shared/canvas-components";
@@ -73,12 +74,14 @@ export const useCss = ({ instance, css }: UseCssProps): string => {
const previewCss = usePreviewCss({ instance, css });
return useMemo(() => {
- const overrides: CSS = { ...defaultStyle };
for (const update of previewCss) {
- if (update.value === undefined) continue;
- overrides[update.property as string] = toValue(update.value);
+ if (update.value === undefined) {
+ continue;
+ }
+ const property = `--${toVarNamespace(instance, update.property)}`;
+ document.body.style.setProperty(property, toValue(update.value));
}
- return createCss(css)({ css: overrides });
+ return createCss(css)(defaultStyle);
}, [css, previewCss]);
};
diff --git a/packages/react-sdk/src/stitches/to-css.test.ts b/packages/react-sdk/src/stitches/to-css.test.ts
index 2ed740c856..ef6518fe31 100644
--- a/packages/react-sdk/src/stitches/to-css.test.ts
+++ b/packages/react-sdk/src/stitches/to-css.test.ts
@@ -1,3 +1,4 @@
+import type { Instance } from "../db";
import { type CssRule, initialBreakpoints, type Breakpoint } from "../css";
import { toCss } from "./to-css";
@@ -9,9 +10,18 @@ const breakpoints: Array<Breakpoint> = initialBreakpoints.map(
})
);
+const createInstance = (cssRules: Array<CssRule>): Instance => {
+ return {
+ id: "0",
+ cssRules,
+ component: "Box",
+ children: [],
+ };
+};
+
describe("Convert WS CSS rules to stitches", () => {
test("keyword", () => {
- const cssRules: Array<CssRule> = [
+ const instance = createInstance([
{
style: {
color: {
@@ -21,8 +31,8 @@ describe("Convert WS CSS rules to stitches", () => {
},
breakpoint: "0",
},
- ];
- const stitchesCss = toCss(cssRules, breakpoints);
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
expect(stitchesCss).toEqual({
"@0": {
color: "red",
@@ -31,7 +41,7 @@ describe("Convert WS CSS rules to stitches", () => {
});
test("fontFamily", () => {
- const cssRules: Array<CssRule> = [
+ const instance = createInstance([
{
style: {
fontFamily: {
@@ -41,8 +51,8 @@ describe("Convert WS CSS rules to stitches", () => {
},
breakpoint: "0",
},
- ];
- const stitchesCss = toCss(cssRules, breakpoints);
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
expect(stitchesCss).toEqual({
"@0": {
fontFamily: "Courier New, monospace",
@@ -51,7 +61,7 @@ describe("Convert WS CSS rules to stitches", () => {
});
test("withFallback option", () => {
- const cssRules: Array<CssRule> = [
+ const instance = createInstance([
{
style: {
fontFamily: {
@@ -61,8 +71,8 @@ describe("Convert WS CSS rules to stitches", () => {
},
breakpoint: "0",
},
- ];
- const stitchesCss = toCss(cssRules, breakpoints, { withFallback: false });
+ ]);
+ const stitchesCss = toCss(instance, breakpoints, { withFallback: false });
expect(stitchesCss).toEqual({
"@0": {
fontFamily: "Courier New",
@@ -71,7 +81,7 @@ describe("Convert WS CSS rules to stitches", () => {
});
test("sort order based on maxWidth in breakpoints", () => {
- const cssRules: Array<CssRule> = [
+ const instance = createInstance([
{
style: {
color: {
@@ -100,9 +110,9 @@ describe("Convert WS CSS rules to stitches", () => {
},
breakpoint: "1",
},
- ];
+ ]);
- const stitchesCss = toCss(cssRules, breakpoints);
+ const stitchesCss = toCss(instance, breakpoints);
expect(Object.keys(stitchesCss)).toStrictEqual(["@0", "@1", "@2"]);
});
});
diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.ts
index 9b22caaec4..e2b0433c30 100644
--- a/packages/react-sdk/src/stitches/to-css.ts
+++ b/packages/react-sdk/src/stitches/to-css.ts
@@ -1,6 +1,7 @@
import type { CSS } from "./stitches";
import type { StyleProperty, StyleValue, CssRule, Breakpoint } from "../css";
import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
+import { Instance } from "..";
type Options = {
withFallback: boolean;
@@ -32,16 +33,29 @@ export const toValue = (
return value.value;
};
+export const toVarNamespace = (instance: Instance, property: string) => {
+ return `${property}-${instance.id}`;
+};
+
+export const toVarValue = (
+ namespace: string,
+ value?: StyleValue,
+ options: Options = defaultOptions
+): string => {
+ const cssValue = toValue(value, options);
+ return `var(--${namespace}, ${cssValue})`;
+};
+
/**
* Convert instance cssRules to a stitches CSS object.
*/
export const toCss = (
- cssRules: Array<CssRule>,
+ instance: Instance,
breakpoints: Array<Breakpoint>,
options: Options = defaultOptions
): CSS => {
const css: CSS = {};
-
+ const { cssRules, id } = instance;
const breakpointsMap: Record<Breakpoint["id"], number> = {};
for (const breakpoint of breakpoints) {
breakpointsMap[breakpoint.id] = breakpoint.minWidth;
@@ -60,7 +74,8 @@ export const toCss = (
for (const property in cssRule.style) {
const value = cssRule.style[property as StyleProperty];
if (value === undefined) continue;
- style[property] = toValue(value, options);
+ const namespace = toVarNamespace(instance, property);
+ style[property] = toVarValue(namespace, value, options);
}
if (cssRule.breakpoint in breakpointsMap) {
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index 22cfe2b05f..6154a1db03 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -106,7 +106,7 @@ const createInstanceElement = ({
const props = {
instance,
children,
- css: toCss(instance.cssRules, breakpoints),
+ css: toCss(instance, breakpoints),
key: instance.id,
onChangeChildren,
};