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 version

Oleg Isonen2a0e920725f6
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index 72e203e2fb..7532283100 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -24,13 +24,13 @@ import {
useUnselectInstance,
useUpdateSelectedInstance,
} from "./shared/instance";
-import { usePreviewStyle, useUpdateStyle } from "./shared/style";
+import { useManageStyles } from "./shared/styles";
import { useTrackSelectedElement } from "./shared/use-track-selected-element";
import { WrapperComponentDev } from "./features/wrapper-component";
import { useSync } from "./shared/sync";
import { useManageProps } from "./shared/props";
import {
- useHandleBreakpoints,
+ useManageBreakpoints,
useInitializeBreakpoints,
} from "./shared/breakpoints";
import {
@@ -93,11 +93,10 @@ type DesignModeProps = {
};
const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
- useUpdateStyle();
- usePreviewStyle();
+ useManageBreakpoints();
+ useManageStyles();
useManageProps();
usePublishSelectedInstanceData(treeId);
- useHandleBreakpoints();
useInsertInstance();
useReparentInstance();
useDeleteInstance();
diff --git a/apps/designer/app/canvas/features/wrapper-component/index.ts b/apps/designer/app/canvas/features/wrapper-component/index.ts
index 5547e9088a..750ef14699 100644
--- a/apps/designer/app/canvas/features/wrapper-component/index.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/index.ts
@@ -1 +1,2 @@
export { WrapperComponentDev } from "./wrapper-component";
+export * from "./wrapper-component-css";
diff --git a/apps/designer/app/canvas/features/wrapper-component/designer-css.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component-css.tsx
similarity index 55%
rename from apps/designer/app/canvas/features/wrapper-component/designer-css.tsx
rename to apps/designer/app/canvas/features/wrapper-component/wrapper-component-css.tsx
index 9889079f1a..d71b8a3346 100644
--- a/apps/designer/app/canvas/features/wrapper-component/designer-css.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component-css.tsx
@@ -1,30 +1,31 @@
-import { css as createCss, type CSS } from "@webstudio-is/design-system";
+import { globalCss } from "@webstudio-is/design-system";
const voidElements =
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr";
// Helper styles on for canvas in design mode
-const designerCss: CSS = {
- "&": {
+const styles = {
+ "[data-ws-component]": {
// When double clicking into an element to edit text, it should not select the word.
userSelect: "none",
},
- [`&:not(${voidElements}):not(body):empty`]: {
+ [`[data-ws-component]:not(${voidElements}):not(body):empty`]: {
outline: "1px dashed #555",
outlineOffset: -1,
paddingTop: 50,
paddingRight: 50,
},
- "&[contenteditable], &:focus": {
+ "[data-ws-component][contenteditable], [data-ws-component]:focus": {
outline: 0,
},
- "&[contenteditable]": {
+ "[data-ws-component][contenteditable]": {
boxShadow: "0 0 0px 4px rgb(36 150 255 / 20%)",
},
// Text Editor wraps each line into a p, so we need to make sure there is no jump between regular rendering and editing
- "&[contenteditable] p": {
+ "[data-ws-component][contenteditable] p": {
margin: 0,
},
};
-export const designerClass = createCss(designerCss)();
+// @todo rewrite to use css engine and remove stitches from canvas entirely
+export const wrapperComponentGlobalStyles = globalCss(styles);
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 9c51a8725f..846675c5f7 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -1,17 +1,17 @@
-import { MouseEvent, FormEvent, useMemo } from "react";
+import { MouseEvent, FormEvent } from "react";
import { Suspense, lazy, useCallback } from "react";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import {
- type Instance,
- type CSS,
- type OnChangeChildren,
- css as createCss,
useUserProps,
renderWrapperComponentChildren,
components,
+ type Instance,
+ type OnChangeChildren,
+ type Breakpoint,
} from "@webstudio-is/react-sdk";
+import { useCssRules } from "@webstudio-is/css-engine";
import { useTextEditingInstanceId } from "~/shared/nano-states";
-import { designerClass } from "./designer-css";
+import "./wrapper-component-css";
import { useSelectedElement } from "~/canvas/shared/nano-states";
const TextEditor = lazy(() => import("../text-editor"));
@@ -40,19 +40,20 @@ const ContentEditable = ({
type WrapperComponentDevProps = {
instance: Instance;
- css: CSS;
+ breakpoints: Array<Breakpoint>;
children: Array<JSX.Element | string>;
onChangeChildren?: OnChangeChildren;
};
export const WrapperComponentDev = ({
instance,
- css,
+ breakpoints,
children,
onChangeChildren,
...rest
}: WrapperComponentDevProps) => {
- const className = useMemo(() => createCss(css)(), [css]);
+ useCssRules(instance);
+
const [editingInstanceId] = useTextEditingInstanceId();
const [, setSelectedElement] = useSelectedElement();
@@ -74,15 +75,13 @@ export const WrapperComponentDev = ({
...userProps,
...rest,
...readonlyProps,
- // @todo merge className with props
- className: `${className} ${designerClass} ${userProps.className || ""}`,
tabIndex: 0,
// @todo stop using id to free it up to the user
// we should replace id, data-component and data-id with "data-ws"=instance.id and grab the rest always over the id
// for this we need to also make search by id fast
id: instance.id,
- "data-component": instance.component,
- "data-id": instance.id,
+ "data-ws-component": instance.component,
+ "data-ws-id": instance.id,
onClick: (event: MouseEvent) => {
if (instance.component === "Link") {
event.preventDefault();
diff --git a/apps/designer/app/canvas/shared/breakpoints.ts b/apps/designer/app/canvas/shared/breakpoints.ts
index 4dce724395..476d0aa7c7 100644
--- a/apps/designer/app/canvas/shared/breakpoints.ts
+++ b/apps/designer/app/canvas/shared/breakpoints.ts
@@ -9,6 +9,7 @@ import {
useBreakpoints,
} from "~/shared/nano-states";
import { publish } from "~/shared/pubsub";
+import { addBreakpoints } from "@webstudio-is/css-engine";
export const useInitializeBreakpoints = (breakpoints: Array<Breakpoint>) => {
const [, setCurrentBreakpoints] = useBreakpoints();
@@ -18,6 +19,7 @@ export const useInitializeBreakpoints = (breakpoints: Array<Breakpoint>) => {
ref.current = true;
setBreakpoints(breakpoints);
setCurrentBreakpoints(breakpoints);
+ addBreakpoints(breakpoints);
}
};
@@ -66,7 +68,7 @@ const useBreakpointDelete = () => {
});
};
-export const useHandleBreakpoints = () => {
+export const useManageBreakpoints = () => {
usePublishBreakpoints();
useBreakpointChange();
useBreakpointDelete();
diff --git a/apps/designer/app/canvas/shared/style.ts b/apps/designer/app/canvas/shared/styles.ts
similarity index 54%
rename from apps/designer/app/canvas/shared/style.ts
rename to apps/designer/app/canvas/shared/styles.ts
index c6c016e9bf..4e12c31cce 100644
--- a/apps/designer/app/canvas/shared/style.ts
+++ b/apps/designer/app/canvas/shared/styles.ts
@@ -8,6 +8,7 @@ import {
toValue,
toVarNamespace,
} from "@webstudio-is/react-sdk";
+import { globalCss } from "@webstudio-is/design-system";
const setCssVar = (id: string, property: string, value?: StyleValue) => {
const customProperty = `--${toVarNamespace(id, property)}`;
@@ -18,7 +19,7 @@ const setCssVar = (id: string, property: string, value?: StyleValue) => {
document.body.style.setProperty(customProperty, toValue(value));
};
-export const useUpdateStyle = () => {
+const useUpdateStyle = () => {
const [selectedInstance] = useSelectedInstance();
useSubscribe("updateStyle", ({ id, updates, breakpoint }) => {
// Only update styles if they match the selected instance
@@ -38,10 +39,46 @@ export const useUpdateStyle = () => {
});
};
-export const usePreviewStyle = () => {
+const usePreviewStyle = () => {
useSubscribe("previewStyle", ({ id, updates }) => {
for (const update of updates) {
setCssVar(id, update.property, update.value);
}
});
};
+
+const voidElements =
+ "area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr";
+
+// Helper styles on for canvas in design mode
+const styles = {
+ "[data-ws-component]": {
+ // When double clicking into an element to edit text, it should not select the word.
+ userSelect: "none",
+ },
+ [`[data-ws-component]:not(${voidElements}):not(body):empty`]: {
+ outline: "1px dashed #555",
+ outlineOffset: -1,
+ paddingTop: 50,
+ paddingRight: 50,
+ },
+ "[data-ws-component][contenteditable], [data-ws-component]:focus": {
+ outline: 0,
+ },
+ "[data-ws-component][contenteditable]": {
+ boxShadow: "0 0 0px 4px rgb(36 150 255 / 20%)",
+ },
+ // Text Editor wraps each line into a p, so we need to make sure there is no jump between regular rendering and editing
+ "[data-ws-component][contenteditable] p": {
+ margin: 0,
+ },
+};
+
+// @todo rewrite to use css engine and remove stitches from canvas entirely
+const wrapperComponentGlobalStyles = globalCss(styles);
+
+export const useManageStyles = () => {
+ wrapperComponentGlobalStyles();
+ useUpdateStyle();
+ usePreviewStyle();
+};
diff --git a/apps/designer/app/canvas/shared/use-track-selected-element.ts b/apps/designer/app/canvas/shared/use-track-selected-element.ts
index d76f255ad4..fbc3bbcda0 100644
--- a/apps/designer/app/canvas/shared/use-track-selected-element.ts
+++ b/apps/designer/app/canvas/shared/use-track-selected-element.ts
@@ -74,9 +74,10 @@ export const useTrackSelectedElement = () => {
let element = event.target as HTMLElement;
// If we click on an element that is not a component, we search for a parent component.
- if (element.dataset.component === undefined) {
- const instanceElement =
- element.closest<HTMLElement>("[data-component]");
+ if (element.dataset.wsComponent === undefined) {
+ const instanceElement = element.closest<HTMLElement>(
+ "[data-ws-component]"
+ );
if (instanceElement === null) {
return;
}
@@ -92,7 +93,7 @@ export const useTrackSelectedElement = () => {
// It's the second click in a double click.
if (event.detail === 2) {
- const component = dataset.component as Instance["component"];
+ const component = dataset.wsComponent as Instance["component"];
if (component === undefined || component in components === false) {
return;
}
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 5b7838adeb..5308398930 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
@@ -3,7 +3,7 @@ import type {
SelectedInstanceData,
StyleUpdates,
} from "~/shared/canvas-components";
-import { Instance, type StyleProperty } from "@webstudio-is/react-sdk";
+import { type StyleProperty } from "@webstudio-is/react-sdk";
import { type Publish } from "~/shared/pubsub";
import { useSelectedBreakpoint } from "~/designer/shared/nano-states";
import { parseCssValue } from "./parse-css-value";
diff --git a/apps/designer/package.json b/apps/designer/package.json
index f55478f001..5528740b1c 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -26,6 +26,7 @@
"@vercel/node": "^2.4.1",
"@webstudio-is/asset-uploader": "*",
"@webstudio-is/css-vars": "*",
+ "@webstudio-is/css-engine": "*",
"@webstudio-is/design-system": "*",
"@webstudio-is/icons": "*",
"@webstudio-is/prisma-client": "*",
diff --git a/apps/designer/tsconfig.json b/apps/designer/tsconfig.json
index a94926beec..07716eb5bc 100644
--- a/apps/designer/tsconfig.json
+++ b/apps/designer/tsconfig.json
@@ -13,6 +13,10 @@
"path": "../../packages/asset-uploader"
},
+ {
+ "path": "../../packages/css-engine"
+ },
+
{
"path": "../../packages/css-vars"
},
diff --git a/packages/css-engine/package.json b/packages/css-engine/package.json
index 4808a26dfa..6144a149bd 100644
--- a/packages/css-engine/package.json
+++ b/packages/css-engine/package.json
@@ -16,7 +16,9 @@
},
"dependencies": {
"@webstudio-is/react-sdk": "*",
- "hyphenate-style-name": "^1.0.4"
+ "hyphenate-style-name": "^1.0.4",
+ "react": "^17.0.2",
+ "react-dom": "^17.0.2"
},
"devDependencies": {
"@storybook/addon-actions": "^6.5.6",
@@ -30,7 +32,9 @@
"@storybook/testing-library": "^0.0.13",
"@webstudio-is/jest-config": "*",
"tsup": "^6.1.3",
- "typescript": "4.7.4"
+ "typescript": "4.7.4",
+ "@types/react": "^17.0.24",
+ "@types/react-dom": "^17.0.9"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
diff --git a/packages/css-engine/src/css-engine.stories.tsx b/packages/css-engine/src/core/css-engine.stories.tsx
similarity index 100%
rename from packages/css-engine/src/css-engine.stories.tsx
rename to packages/css-engine/src/core/css-engine.stories.tsx
diff --git a/packages/css-engine/src/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
similarity index 100%
rename from packages/css-engine/src/css-engine.test.ts
rename to packages/css-engine/src/core/css-engine.test.ts
diff --git a/packages/css-engine/src/css-engine.ts b/packages/css-engine/src/core/css-engine.ts
similarity index 100%
rename from packages/css-engine/src/css-engine.ts
rename to packages/css-engine/src/core/css-engine.ts
diff --git a/packages/css-engine/src/core/index.ts b/packages/css-engine/src/core/index.ts
new file mode 100644
index 0000000000..1639686447
--- /dev/null
+++ b/packages/css-engine/src/core/index.ts
@@ -0,0 +1,2 @@
+export { CssEngine } from "./css-engine";
+export type { AnyRule, StyleRule, MediaRule } from "./rules";
diff --git a/packages/css-engine/src/rules.ts b/packages/css-engine/src/core/rules.ts
similarity index 100%
rename from packages/css-engine/src/rules.ts
rename to packages/css-engine/src/core/rules.ts
diff --git a/packages/css-engine/src/style-element.ts b/packages/css-engine/src/core/style-element.ts
similarity index 100%
rename from packages/css-engine/src/style-element.ts
rename to packages/css-engine/src/core/style-element.ts
diff --git a/packages/css-engine/src/style-sheet.ts b/packages/css-engine/src/core/style-sheet.ts
similarity index 100%
rename from packages/css-engine/src/style-sheet.ts
rename to packages/css-engine/src/core/style-sheet.ts
diff --git a/packages/css-engine/src/index.ts b/packages/css-engine/src/index.ts
index 1639686447..c8fe1353f8 100644
--- a/packages/css-engine/src/index.ts
+++ b/packages/css-engine/src/index.ts
@@ -1,2 +1,2 @@
-export { CssEngine } from "./css-engine";
-export type { AnyRule, StyleRule, MediaRule } from "./rules";
+export * from "./core";
+export * from "./react";
diff --git a/packages/css-engine/src/react.ts b/packages/css-engine/src/react.ts
new file mode 100644
index 0000000000..0444adfa60
--- /dev/null
+++ b/packages/css-engine/src/react.ts
@@ -0,0 +1,84 @@
+import {
+ CssRule,
+ toVarNamespace,
+ validStaticValueTypes,
+ type Breakpoint,
+ type Style,
+ type StyleProperty,
+ type ValidStaticStyleValue,
+} from "@webstudio-is/react-sdk";
+import { useEffect, useLayoutEffect, useRef } from "react";
+import { CssEngine } from "./core/css-engine";
+import type { StyleRule } from "./core/rules";
+
+const cssEngine = new CssEngine();
+
+const useIsomorphicLayoutEffect =
+ typeof window === "undefined" ? useEffect : useLayoutEffect;
+
+// Wrapps a normal StyleValue into a VarStyleValue that uses the previous style value as a fallback and allows
+// to quickly pass the values over CSS variable witout rerendering the components tree.
+const toVarStyleWithFallback = (instanceId: string, style: Style): Style => {
+ const dynamicStyle: Style = {};
+ let property: StyleProperty;
+ for (property in style) {
+ const value = style[property];
+ if (value === undefined) {
+ continue;
+ }
+ if (value.type === "var") {
+ dynamicStyle[property] = value;
+ continue;
+ }
+ if (
+ validStaticValueTypes.includes(
+ value.type as typeof validStaticValueTypes[number]
+ )
+ ) {
+ dynamicStyle[property] = {
+ type: "var",
+ value: toVarNamespace(instanceId, property),
+ fallbacks: [value as ValidStaticStyleValue],
+ };
+ }
+ }
+ return dynamicStyle;
+};
+
+export const addBreakpoints = (breakpoints: Array<Breakpoint>) => {
+ for (const breakpoint of breakpoints) {
+ cssEngine.addBreakpoint(breakpoint);
+ }
+};
+
+export const useCssRules = ({
+ id,
+ cssRules,
+}: {
+ id: string;
+ cssRules: Array<CssRule>;
+}) => {
+ const ruleMap = useRef<Map<string, StyleRule>>(new Map());
+
+ useIsomorphicLayoutEffect(() => {
+ for (const cssRule of cssRules) {
+ const key = id + cssRule.breakpoint;
+ let rule = ruleMap.current.get(key);
+ if (rule === undefined) {
+ const selectorText = `[data-ws-id="${id}"]`;
+ rule = cssEngine.addRule(selectorText, {
+ ...cssRule,
+ style: toVarStyleWithFallback(id, cssRule.style),
+ });
+ ruleMap.current.set(key, rule);
+ continue;
+ }
+ const dynamicStyle = toVarStyleWithFallback(id, cssRule.style);
+ let property: StyleProperty;
+ for (property in dynamicStyle) {
+ rule.styleMap.set(property, dynamicStyle[property]);
+ }
+ }
+ cssEngine.render();
+ }, [id, cssRules]);
+};
diff --git a/packages/react-sdk/src/css/schema.ts b/packages/react-sdk/src/css/schema.ts
index 604ce6bf04..6e10af2722 100644
--- a/packages/react-sdk/src/css/schema.ts
+++ b/packages/react-sdk/src/css/schema.ts
@@ -41,14 +41,28 @@ export const UnsetValue = z.object({
});
export type UnsetValue = z.infer<typeof UnsetValue>;
-export const StyleValue = z.union([
+export const validStaticValueTypes = ["unit", "keyword", "fontFamily"] as const;
+
+export const ValidStaticStyleValue = z.union([
UnitValue,
KeywordValue,
- InvalidValue,
- UnsetValue,
FontFamilyValue,
]);
+export type ValidStaticStyleValue = z.infer<typeof ValidStaticStyleValue>;
+export const VarValue = z.object({
+ type: z.literal("var"),
+ value: z.string(),
+ fallbacks: z.array(ValidStaticStyleValue),
+});
+export type VarValue = z.infer<typeof VarValue>;
+
+export const StyleValue = z.union([
+ ValidStaticStyleValue,
+ InvalidValue,
+ UnsetValue,
+ VarValue,
+]);
export type StyleValue = z.infer<typeof StyleValue>;
export const Style = z.record(z.string(), StyleValue);
diff --git a/packages/react-sdk/src/stitches/to-css.test.ts b/packages/react-sdk/src/stitches/to-css.test.ts
index ef6518fe31..b18084d17f 100644
--- a/packages/react-sdk/src/stitches/to-css.test.ts
+++ b/packages/react-sdk/src/stitches/to-css.test.ts
@@ -40,6 +40,119 @@ describe("Convert WS CSS rules to stitches", () => {
});
});
+ test("unit", () => {
+ const instance = createInstance([
+ {
+ style: {
+ width: {
+ type: "unit",
+ value: 10,
+ unit: "px",
+ },
+ },
+ breakpoint: "0",
+ },
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
+ expect(stitchesCss).toEqual({
+ "@0": {
+ width: "10px",
+ },
+ });
+ });
+
+ test("invalid", () => {
+ const instance = createInstance([
+ {
+ style: {
+ width: {
+ type: "invalid",
+ value: "bad",
+ },
+ },
+ breakpoint: "0",
+ },
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
+ expect(stitchesCss).toEqual({
+ "@0": {
+ width: "bad",
+ },
+ });
+ });
+
+ test("unset", () => {
+ const instance = createInstance([
+ {
+ style: {
+ width: {
+ type: "unset",
+ value: "",
+ },
+ },
+ breakpoint: "0",
+ },
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
+ expect(stitchesCss).toEqual({
+ "@0": {
+ width: "",
+ },
+ });
+ });
+
+ test("var", () => {
+ const instance = createInstance([
+ {
+ style: {
+ width: {
+ type: "var",
+ value: "namespace",
+ fallbacks: [],
+ },
+ },
+ breakpoint: "0",
+ },
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
+ expect(stitchesCss).toEqual({
+ "@0": {
+ width: "var(--namespace)",
+ },
+ });
+ });
+
+ test("var with fallbacks", () => {
+ const instance = createInstance([
+ {
+ style: {
+ width: {
+ type: "var",
+ value: "namespace",
+ fallbacks: [
+ {
+ type: "keyword",
+ value: "normal",
+ },
+ {
+ type: "unit",
+ value: 10,
+ unit: "px",
+ },
+ ],
+ },
+ },
+ breakpoint: "0",
+ },
+ ]);
+ const stitchesCss = toCss(instance, breakpoints);
+ expect(stitchesCss).toEqual({
+ "@0": {
+ width: "var(--namespace, normal, 10px)",
+ },
+ });
+ });
+
test("fontFamily", () => {
const instance = createInstance([
{
diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.ts
index 20acbbd630..b5b49efabe 100644
--- a/packages/react-sdk/src/stitches/to-css.ts
+++ b/packages/react-sdk/src/stitches/to-css.ts
@@ -3,7 +3,7 @@ import type { StyleProperty, StyleValue, Breakpoint } from "../css";
import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
import { Instance } from "..";
-type Options = {
+type ToCssOptions = {
withFallback: boolean;
};
@@ -13,14 +13,14 @@ const defaultOptions = {
export const toValue = (
value?: StyleValue,
- { withFallback }: Options = defaultOptions
+ options: ToCssOptions = defaultOptions
): string => {
if (value === undefined) return "";
if (value.type === "unit") {
return value.value + (value.unit === "number" ? "" : value.unit);
}
if (value.type === "fontFamily") {
- if (withFallback === false) {
+ if (options.withFallback === false) {
return value.value[0];
}
const family = value.value[0];
@@ -30,6 +30,15 @@ export const toValue = (
}
return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");
}
+ if (value.type === "var") {
+ const fallbacks = [];
+ for (const fallback of value.fallbacks) {
+ fallbacks.push(toValue(fallback, options));
+ }
+ const fallbacksString =
+ fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";
+ return `var(--${value.value}${fallbacksString})`;
+ }
return value.value;
};
@@ -37,25 +46,16 @@ export const toVarNamespace = (id: string, property: string) => {
return `${property}-${id}`;
};
-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 = (
instance: Instance,
breakpoints: Array<Breakpoint>,
- options: Options = defaultOptions
+ options: ToCssOptions = defaultOptions
): CSS => {
const css: CSS = {};
- const { cssRules, id } = instance;
+ const { cssRules } = instance;
const breakpointsMap: Record<Breakpoint["id"], number> = {};
for (const breakpoint of breakpoints) {
breakpointsMap[breakpoint.id] = breakpoint.minWidth;
@@ -74,8 +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.id, property);
- style[property] = toVarValue(namespace, value, options);
+ style[property] = toValue(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 6154a1db03..b096b9d15c 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -1,8 +1,7 @@
-import { Fragment } from "react";
-import { toCss } from "../stitches";
+import { type ComponentProps, Fragment } from "react";
import type { Instance } from "../db";
import type { Breakpoint } from "../css";
-import { type WrapperComponentProps } from "./wrapper-component";
+import { WrapperComponent } from "./wrapper-component";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import { SessionStoragePolyfill } from "./session-storage-polyfill";
@@ -30,7 +29,7 @@ export const createElementsTree = ({
sandbox?: boolean;
instance: Instance;
breakpoints: Array<Breakpoint>;
- Component: (props: WrapperComponentProps) => JSX.Element;
+ Component: (props: ComponentProps<typeof WrapperComponent>) => JSX.Element;
onChangeChildren?: OnChangeChildren;
}) => {
const children = createInstanceChildrenElements({
@@ -63,7 +62,7 @@ const createInstanceChildrenElements = ({
}: {
children: Instance["children"];
breakpoints: Array<Breakpoint>;
- Component: (props: WrapperComponentProps) => JSX.Element;
+ Component: (props: ComponentProps<typeof WrapperComponent>) => JSX.Element;
onChangeChildren?: OnChangeChildren;
}) => {
const elements = [];
@@ -99,14 +98,14 @@ const createInstanceElement = ({
}: {
instance: Instance;
breakpoints: Array<Breakpoint>;
- Component: (props: WrapperComponentProps) => JSX.Element;
+ Component: (props: ComponentProps<typeof WrapperComponent>) => JSX.Element;
onChangeChildren?: OnChangeChildren;
children?: Array<JSX.Element | string>;
}) => {
const props = {
instance,
+ breakpoints,
children,
- css: toCss(instance, breakpoints),
key: instance.id,
onChangeChildren,
};
diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.ts
index 6106b4b404..9cf24f9d77 100644
--- a/packages/react-sdk/src/tree/root.ts
+++ b/packages/react-sdk/src/tree/root.ts
@@ -3,17 +3,14 @@ import type { Tree, InstanceProps } from "../db";
import type { Breakpoint } from "../css";
import { globalCss, setBreakpoints, renderFontFaces } from "../stitches";
import { createElementsTree } from "./create-elements-tree";
-import {
- WrapperComponent,
- type WrapperComponentProps,
-} from "./wrapper-component";
+import { WrapperComponent } from "./wrapper-component";
import {
type FontFormat,
FONT_FORMATS,
getFontFaces,
} from "@webstudio-is/fonts";
import type { Asset, FontAsset } from "@webstudio-is/asset-uploader";
-import { useRef } from "react";
+import { type ComponentProps, useRef } from "react";
export type Data = {
tree: Tree | null;
@@ -45,7 +42,7 @@ export const useGlobalStyles = ({ assets }: { assets: Array<Asset> }) => {
type RootProps = {
data: Data;
- Component?: (props: WrapperComponentProps) => JSX.Element;
+ Component?: (props: ComponentProps<typeof WrapperComponent>) => JSX.Element;
};
export const InstanceRoot = ({
diff --git a/packages/react-sdk/src/tree/wrapper-component.tsx b/packages/react-sdk/src/tree/wrapper-component.tsx
index dc01081dc9..2e8455c92a 100644
--- a/packages/react-sdk/src/tree/wrapper-component.tsx
+++ b/packages/react-sdk/src/tree/wrapper-component.tsx
@@ -1,9 +1,9 @@
-import React, { useMemo, Fragment } from "react";
+import React, { Fragment } from "react";
import type { Instance } from "../db";
import * as components from "../components";
import { useUserProps } from "../user-props/use-user-props";
-import { type CSS, css as createCss } from "../stitches";
import type { OnChangeChildren } from "./create-elements-tree";
+import { Breakpoint } from "..";
const renderText = (text: string): Array<JSX.Element> => {
const lines = text.split("\n");
@@ -26,21 +26,22 @@ export const renderWrapperComponentChildren = (
});
};
-export type WrapperComponentProps = {
+type WrapperComponentProps = {
instance: Instance;
- css: CSS;
+ breakpoints: Array<Breakpoint>;
children: Array<JSX.Element | string>;
onChangeChildren?: OnChangeChildren;
};
export const WrapperComponent = ({
instance,
- css,
+ breakpoints,
onChangeChildren, // prevent it from passing to sdk component
children,
...rest
}: WrapperComponentProps) => {
- const className = useMemo(() => createCss(css)(), [css]);
+ // const className = useMemo(() => createCss(css)(), [css]);
+ const className = "";
const { Component } = components[instance.component];
const userProps = useUserProps(instance.id);
return (