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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
working version of the engine
Oleg Isonene1986382e68b
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsxindex 5102358d15..9c51a8725f 100644--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx@@ -12,7 +12,6 @@ import {} from "@webstudio-is/react-sdk";import { useTextEditingInstanceId } from "~/shared/nano-states";import { designerClass } from "./designer-css";-import noop from "lodash.noop";import { useSelectedElement } from "~/canvas/shared/nano-states";const TextEditor = lazy(() => import("../text-editor"));@@ -50,7 +49,7 @@ export const WrapperComponentDev = ({instance,css,children,- onChangeChildren = noop,+ onChangeChildren,...rest}: WrapperComponentDevProps) => {const className = useMemo(() => createCss(css)(), [css]);@@ -115,7 +114,7 @@ export const WrapperComponentDev = ({/>}onChange={(updates) => {- onChangeChildren({ instanceId: instance.id, updates });+ onChangeChildren?.({ instanceId: instance.id, updates });}}/></Suspense>diff --git a/packages/css-engine/src/css-engine.stories.tsx b/packages/css-engine/src/css-engine.stories.tsxindex bbc78fa40d..97de4faf5f 100644--- a/packages/css-engine/src/css-engine.stories.tsx+++ b/packages/css-engine/src/css-engine.stories.tsx@@ -1,5 +1,4 @@import * as React from "react";-import type { ComponentStory } from "@storybook/react";import { CssEngine } from "./css-engine";export default {@@ -12,15 +11,13 @@ const style0 = {const breakpoint0 = { minWidth: 0, id: "0", label: "0" } as const;-export const Test = () => {- const sheet = new CSSStyleSheet();- const engine = new CssEngine<CSSStyleSheet>(sheet);- engine.mount();+export const Basic = () => {+ const engine = new CssEngine();engine.addBreakpoint(breakpoint0);const rule = engine.addRule({style: style0,breakpoint: "0",});- console.log(rule);- return <div className={rule.className}>"Test"</div>;+ engine.render();+ return <div className={rule.className}>Should be red</div>;};diff --git a/packages/css-engine/src/css-engine.test.ts b/packages/css-engine/src/css-engine.test.tsindex db1a7c7a8a..c49b29d663 100644--- a/packages/css-engine/src/css-engine.test.ts+++ b/packages/css-engine/src/css-engine.test.ts@@ -1,5 +1,4 @@import { CssEngine } from "./css-engine";-import { VirtualStyleSheet } from "./style-sheet";const style0 = {display: { type: "keyword", value: "block" },@@ -11,8 +10,7 @@ describe("CssEngine", () => {let engine: CssEngine;beforeEach(() => {- const sheet = new VirtualStyleSheet();- engine = new CssEngine(sheet);+ engine = new CssEngine();});test("throw when breakpoint is not registered", () => {@@ -35,7 +33,7 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media (min-width: 0px) {- .s0 { display: block; color: red }+ .c0 { display: block; color: red }}"`);});@@ -51,19 +49,14 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media (min-width: 0px) {- .s0 { display: block; color: red }+ .c0 { display: block; color: red }}"`);expect(rule.cssText).toMatchInlineSnapshot(- `".s0 { display: block; color: red }"`+ `".c0 { display: block; color: red }"`);});- test("skip breakpoint without rules", () => {- engine.addBreakpoint(breakpoint0);- expect(engine.cssText).toBe("");- });-test("multiple breakpoints", () => {engine.addBreakpoint(breakpoint0);engine.addBreakpoint({ minWidth: 100, id: "1", label: "1" });@@ -77,10 +70,10 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media (min-width: 0px) {- .s0 { display: block }+ .c0 { display: block }}@media (min-width: 100px) {- .s1 { display: block }+ .c1 { display: block }}"`);});diff --git a/packages/css-engine/src/css-engine.ts b/packages/css-engine/src/css-engine.tsindex d4b90f65e3..0e3dd3fafd 100644--- a/packages/css-engine/src/css-engine.ts+++ b/packages/css-engine/src/css-engine.ts@@ -1,49 +1,32 @@import { Breakpoint, CssRule } from "@webstudio-is/react-sdk";-import { MediaRule, StyleRule, VirtualStyleSheet } from "./style-sheet";+import { MediaRule, StyleRule } from "./rules";+import { StyleSheet } from "./style-sheet";-export class CssEngine<Sheet> {+export class CssEngine {rulesCounter = -1;- sheet: Sheet;mediaRules: Map<Breakpoint["id"], MediaRule> = new Map();- constructor(sheet: Sheet) {- this.sheet = sheet;+ sheet: StyleSheet;+ constructor() {+ this.sheet = new StyleSheet();}-addBreakpoint(breakpoint: Breakpoint) {const mediaRule = new MediaRule(breakpoint);this.mediaRules.set(breakpoint.id, mediaRule);- console.log(111, mediaRule.cssText);- this.sheet.insertRule(mediaRule.cssText);}-addRule(rule: CssRule) {const mediaRule = this.mediaRules.get(rule.breakpoint);if (mediaRule === undefined) {throw new Error(`Unknown breakpoint: ${rule.breakpoint}`);}- const styleRule = new StyleRule(rule.style);- styleRule.id = ++this.rulesCounter;+ const styleRule = new StyleRule(rule.style, ++this.rulesCounter);return mediaRule.insertRule(styleRule);}- mount() {- if (- this.sheet instanceof CSSStyleSheet &&- typeof document === "object" &&- "adoptedStyleSheets" in document- ) {- console.log(111, this.sheet);- // @ts-expect-error TS2339: Property 'adoptedStyleSheets' does not exist on type 'Document'.- document.adoptedStyleSheets = [this.sheet];- return;- }- const style = document.createElement("style");- for (const rule of this.sheet.rules) {- cssText += rule;- }- style.textContent = cssText;+ render() {+ this.sheet.mount();+ this.sheet.replaceSync(this.cssText);}get cssText() {- const css = [];+ const css: Array<string> = [];for (const mediaRule of this.mediaRules.values()) {const { cssText } = mediaRule;if (cssText !== "") css.push(cssText);diff --git a/packages/css-engine/src/index.ts b/packages/css-engine/src/index.tsindex 351f754559..867abe6a5d 100644--- a/packages/css-engine/src/index.ts+++ b/packages/css-engine/src/index.ts@@ -1,2 +1 @@export { CssEngine } from "./css-engine";-export { VirtualStyleSheet } from "./style-sheet";diff --git a/packages/css-engine/src/rules.ts b/packages/css-engine/src/rules.tsnew file mode 100644index 0000000000..88d9527b36--- /dev/null+++ b/packages/css-engine/src/rules.ts@@ -0,0 +1,45 @@+import { Breakpoint, Style, toValue } from "@webstudio-is/react-sdk";++export class StyleRule {+ id = -1;+ style: Style;+ // @todo name can be composition token name or component name+ name = "c";+ get className() {+ return `${this.name}${this.id}`;+ }+ constructor(style: Style, id: number) {+ this.style = style;+ this.id = id;+ }+ get cssText() {+ const block: Array<string> = [];+ let property: keyof Style;+ for (property in this.style) {+ const value = this.style[property];+ if (value === undefined) continue;+ block.push(`${property}: ${toValue(value)}`);+ }+ return `.${this.className} { ${block.join("; ")} }`;+ }+}++export class MediaRule {+ #id = "";+ #breakpoint: Breakpoint;+ rules: Array<StyleRule> = [];+ constructor(breakpoint: Breakpoint) {+ this.#breakpoint = breakpoint;+ this.#id = breakpoint.id;+ }+ insertRule(rule: StyleRule) {+ this.rules.push(rule);+ return rule;+ }+ get cssText() {+ const rules = this.rules.map((rule) => ` ${rule.cssText}`).join("\n");+ return `@media (min-width: ${this.#breakpoint.minWidth}px) {\n${rules}\n}`;+ }+}++export type Rule = StyleRule | MediaRule;diff --git a/packages/css-engine/src/style-sheet.ts b/packages/css-engine/src/style-sheet.tsindex 52e8f78db4..3dd834ad3a 100644--- a/packages/css-engine/src/style-sheet.ts+++ b/packages/css-engine/src/style-sheet.ts@@ -1,52 +1,21 @@-import { Breakpoint, Style, toValue } from "@webstudio-is/react-sdk";--export class StyleRule {- id = -1;- style: Style;- // @todo name can be composition token name or component name- name = "s";- get className() {- return `${this.name}${this.id}`;- }- constructor(style: Style) {- this.style = style;- }- get cssText() {- const block = [];- let property: keyof Style;- for (property in this.style) {- const value = this.style[property];- if (value === undefined) continue;- block.push(`${property}: ${toValue(value)}`);+export class StyleSheet {+ #style?: HTMLStyleElement;+ mount() {+ //if (typeof document === "object" && "adoptedStyleSheets" in document) {+ // // @ts-expect-error TS2339: Property 'adoptedStyleSheets' does not exist on type 'Document'.+ // document.adoptedStyleSheets = [this.sheet];+ // return;+ //}+ if (this.#style === undefined) {+ this.#style = document.createElement("style");+ this.#style.setAttribute("data-webstudio", "");+ document.head.appendChild(this.#style);}- return `.${this.className} { ${block.join("; ")} }`;- }-}--export class MediaRule {- #id = "";- #breakpoint: Breakpoint;- rules: Array<StyleRule> = [];- constructor(breakpoint: Breakpoint) {- this.#breakpoint = breakpoint;- this.#id = breakpoint.id;}- insertRule(rule: StyleRule) {- this.rules.push(rule);- return rule;- }- get cssText() {- const rules = this.rules.map((rule) => ` ${rule.cssText}`).join("\n");- return `@media (min-width: ${this.#breakpoint.minWidth}px) {\n${rules}\n}`;- }-}--export class VirtualStyleSheet {- rules: Array<string> = [];- insertRule(rule: string) {- this.rules.push(rule);+ replaceSync(cssText: string) {+ if (this.#style === undefined) {+ throw new Error("StyleSheet not mounted");+ }+ this.#style.textContent = cssText;}}--export type Rule = StyleRule | MediaRule;-export type StyleSheet = CSSStyleSheet | VirtualStyleSheet;diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsindex 0d576f45a7..20acbbd630 100644--- a/packages/react-sdk/src/stitches/to-css.ts+++ b/packages/react-sdk/src/stitches/to-css.ts@@ -1,5 +1,5 @@import type { CSS } from "./stitches";-import type { StyleProperty, StyleValue, CssRule, Breakpoint } from "../css";+import type { StyleProperty, StyleValue, Breakpoint } from "../css";import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";import { Instance } from "..";