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.

remodeling

Oleg Isonen27ade6adc729
diff --git a/packages/css-engine/src/css-engine.test.ts b/packages/css-engine/src/css-engine.test.ts
new file mode 100644
index 0000000000..6c5448ed99
--- /dev/null
+++ b/packages/css-engine/src/css-engine.test.ts
@@ -0,0 +1,91 @@
+import { CssEngine } from "./css-engine";
+import { VirtualStyleSheet } from "./style-sheet";
+
+const style0 = {
+ display: { type: "keyword", value: "block" },
+} as const;
+
+const breakpoint0 = { minWidth: 0, id: "0", label: "0" } as const;
+
+describe("CssEngine", () => {
+ let sheet: VirtualStyleSheet;
+
+ beforeEach(() => {
+ sheet = new VirtualStyleSheet();
+ });
+
+ test("throw when breakpoint is not registered", () => {
+ const engine = new CssEngine(sheet);
+ expect(() => {
+ engine.addRule({
+ style: style0,
+ breakpoint: "0",
+ });
+ }).toThrowErrorMatchingInlineSnapshot(`"Unknown breakpoint: 0"`);
+ });
+
+ test("rule with multiple properties", () => {
+ const engine = new CssEngine(sheet);
+ engine.addBreakpoint(breakpoint0);
+ engine.addRule({
+ style: {
+ ...style0,
+ color: { type: "keyword", value: "red" },
+ },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media (min-width: 0px) {
+ .s0 { display: block; color: red }
+ }"
+ `);
+ });
+
+ test("add one rule", () => {
+ const engine = new CssEngine(sheet);
+ engine.addBreakpoint(breakpoint0);
+ const rule = engine.addRule({
+ style: {
+ ...style0,
+ color: { type: "keyword", value: "red" },
+ },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media (min-width: 0px) {
+ .s0 { display: block; color: red }
+ }"
+ `);
+ expect(rule.cssText).toMatchInlineSnapshot(
+ `".s0 { display: block; color: red }"`
+ );
+ });
+
+ test("skip breakpoint without rules", () => {
+ const engine = new CssEngine(sheet);
+ engine.addBreakpoint(breakpoint0);
+ expect(sheet.cssText).toBe("");
+ });
+
+ test("multiple breakpoints", () => {
+ const engine = new CssEngine(sheet);
+ engine.addBreakpoint(breakpoint0);
+ engine.addBreakpoint({ minWidth: 100, id: "1", label: "1" });
+ engine.addRule({
+ style: style0,
+ breakpoint: "0",
+ });
+ engine.addRule({
+ style: style0,
+ breakpoint: "1",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media (min-width: 0px) {
+ .s0 { display: block }
+ }
+ @media (min-width: 100px) {
+ .s1 { display: block }
+ }"
+ `);
+ });
+});
diff --git a/packages/css-engine/src/css-engine.ts b/packages/css-engine/src/css-engine.ts
new file mode 100644
index 0000000000..22541e8cfc
--- /dev/null
+++ b/packages/css-engine/src/css-engine.ts
@@ -0,0 +1,45 @@
+import { Breakpoint, CssRule } from "@webstudio-is/react-sdk";
+import { MediaRule, StyleRule, VirtualStyleSheet } from "./style-sheet";
+
+export class CssEngine {
+ sheet: VirtualStyleSheet;
+ mediaRules: Map<Breakpoint["id"], MediaRule> = new Map();
+ constructor(sheet: VirtualStyleSheet) {
+ this.sheet = sheet;
+ }
+ addBreakpoint(breakpoint: Breakpoint) {
+ const mediaRule = new MediaRule(breakpoint);
+ this.mediaRules.set(breakpoint.id, mediaRule);
+ this.sheet.insertRule(mediaRule);
+ }
+ 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.sheet.rulesCounter;
+ return mediaRule.insertRule(styleRule);
+ }
+ mount() {
+ if (this.sheet instanceof CSSStyleSheet) {
+ // @ts-expect-error TS2339: Property 'adoptedStyleSheets' does not exist on type 'Document'.
+ document.adoptedStyleSheets = [this.sheet];
+ return;
+ }
+ const style = document.createElement("style");
+ let cssText = "";
+ for (const rule of this.sheet.rules) {
+ cssText += rule.cssText;
+ }
+ style.textContent = cssText;
+ }
+ get cssText() {
+ const css = [];
+ for (const mediaRule of this.mediaRules.values()) {
+ const { cssText } = mediaRule;
+ if (cssText !== "") css.push(cssText);
+ }
+ return css.join("\n");
+ }
+}
diff --git a/packages/css-engine/src/index.ts b/packages/css-engine/src/index.ts
index 6511200d6e..351f754559 100644
--- a/packages/css-engine/src/index.ts
+++ b/packages/css-engine/src/index.ts
@@ -1 +1,2 @@
-export { StyleSheet } from "./style-sheet";
+export { CssEngine } from "./css-engine";
+export { VirtualStyleSheet } from "./style-sheet";
diff --git a/packages/css-engine/src/style-sheet.test.ts b/packages/css-engine/src/style-sheet.test.ts
deleted file mode 100644
index 2f6ec0c8e5..0000000000
--- a/packages/css-engine/src/style-sheet.test.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-import { resetRuleCounter, StyleSheet } from "./style-sheet";
-
-const style0 = {
- display: { type: "keyword", value: "block" },
-} as const;
-
-const breakpoint0 = { minWidth: 0, id: "0", label: "0" } as const;
-
-describe("StyleSheet", () => {
- beforeEach(() => {
- resetRuleCounter();
- });
-
- test("throw when breakpoint is not registered", () => {
- const sheet = new StyleSheet();
- expect(() => {
- sheet.addRules([
- {
- style: style0,
- breakpoint: "0",
- },
- ]);
- }).toThrowErrorMatchingInlineSnapshot(`"Unknown breakpoint: 0"`);
- });
-
- test("rule with multiple properties", () => {
- const sheet = new StyleSheet();
- sheet.addBreakpoints([breakpoint0]);
- sheet.addRules([
- {
- style: {
- ...style0,
- color: { type: "keyword", value: "red" },
- },
- breakpoint: "0",
- },
- ]);
- expect(sheet.toString()).toMatchInlineSnapshot(`
- "@media (min-width: 0px) {
- .s0 { display: block; color: red }
- }"
- `);
- });
-
- test("add one rule", () => {
- const sheet = new StyleSheet();
- sheet.addBreakpoints([breakpoint0]);
- const rule = sheet.addRule({
- style: {
- ...style0,
- color: { type: "keyword", value: "red" },
- },
- breakpoint: "0",
- });
- expect(sheet.toString()).toMatchInlineSnapshot(`
- "@media (min-width: 0px) {
- .s0 { display: block; color: red }
- }"
- `);
- expect(rule.toString()).toMatchInlineSnapshot(
- `".s0 { display: block; color: red }"`
- );
- });
-
- test("skip breakpoint without rules", () => {
- const sheet = new StyleSheet();
- sheet.addBreakpoints([breakpoint0]);
- expect(sheet.toString()).toBe("");
- });
-
- test("multiple breakpoints", () => {
- const sheet = new StyleSheet();
- sheet.addBreakpoints([breakpoint0, { minWidth: 100, id: "1", label: "1" }]);
- sheet.addRules([
- {
- style: style0,
- breakpoint: "0",
- },
- {
- style: style0,
- breakpoint: "1",
- },
- ]);
- expect(sheet.toString()).toMatchInlineSnapshot(`
- "@media (min-width: 0px) {
- .s0 { display: block }
- }
- @media (min-width: 100px) {
- .s1 { display: block }
- }"
- `);
- });
-});
diff --git a/packages/css-engine/src/style-sheet.ts b/packages/css-engine/src/style-sheet.ts
index 322d715ea3..20a2169a88 100644
--- a/packages/css-engine/src/style-sheet.ts
+++ b/packages/css-engine/src/style-sheet.ts
@@ -1,21 +1,14 @@
-import { Breakpoint, CssRule, Style, toValue } from "@webstudio-is/react-sdk";
+import { Breakpoint, Style, toValue } from "@webstudio-is/react-sdk";
-let ruleCounter = -1;
-
-export const resetRuleCounter = () => {
- ruleCounter = -1;
-};
-
-class StyleRule {
- id: number;
+export class StyleRule {
+ id = -1;
style: Style;
// @todo name can be composition token name or component name
name = "s";
constructor(style: Style) {
this.style = style;
- this.id = ++ruleCounter;
}
- toString() {
+ get cssText() {
const block = [];
let property: keyof Style;
for (property in this.style) {
@@ -28,63 +21,37 @@ class StyleRule {
}
}
-class MediaRule {
- id = "";
- breakpoint: Breakpoint;
+export class MediaRule {
+ #id = "";
+ #breakpoint: Breakpoint;
rules: Array<StyleRule> = [];
constructor(breakpoint: Breakpoint) {
- this.breakpoint = breakpoint;
- this.id = breakpoint.id;
+ this.#breakpoint = breakpoint;
+ this.#id = breakpoint.id;
}
- addRule(rule: StyleRule) {
+ insertRule(rule: StyleRule) {
this.rules.push(rule);
return rule;
}
- toString() {
+ get cssText() {
if (this.rules.length === 0) return "";
- const query = `min-width: ${this.breakpoint.minWidth}px`;
- const rules = this.rules.map((rule) => ` ${rule}`).join("\n");
- return `@media (${query}) {\n${rules}\n}`;
+ const rules = this.rules.map((rule) => ` ${rule.cssText}`).join("\n");
+ return `@media (min-width: ${this.#breakpoint.minWidth}px) {\n${rules}\n}`;
}
}
-export class StyleSheet {
- mediaRules: Map<Breakpoint["id"], MediaRule> = new Map();
- style?: HTMLStyleElement;
- addBreakpoints(breakpoints: Array<Breakpoint>) {
- for (const breakpoint of breakpoints) {
- this.mediaRules.set(breakpoint.id, new MediaRule(breakpoint));
- }
- }
- addRule(rule: CssRule) {
- const mediaRule = this.mediaRules.get(rule.breakpoint);
- if (mediaRule === undefined) {
- throw new Error(`Unknown breakpoint: ${rule.breakpoint}`);
- }
- return mediaRule.addRule(new StyleRule(rule.style));
- }
- addRules(rules: Array<CssRule>) {
- for (const rule of rules) {
- this.addRule(rule);
- }
- }
- mount() {
- if (this.style !== undefined) {
- throw new Error("StyleSheet already mounted");
- }
- this.style = document.createElement("style");
- this.style.textContent = this.toString();
- document.head.appendChild(this.style);
- }
- unmount() {
- this.style?.parentElement?.removeChild(this.style);
+type Rule = StyleRule | MediaRule;
+
+export class VirtualStyleSheet {
+ rulesCounter = -1;
+ rules: Array<Rule> = [];
+ insertRule(rule: Rule) {
+ this.rules.push(rule);
}
- toString() {
- const css = [];
- for (const mediaRule of this.mediaRules.values()) {
- const str = mediaRule.toString();
- if (str !== "") css.push(mediaRule.toString());
- }
- return css.join("\n");
+ get cssText() {
+ if (this.rules.length === 0) return "";
+ return this.rules.map((rule) => rule.cssText).join("\n");
}
}
+
+export type StyleSheet = CSSStyleSheet | VirtualStyleSheet;