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.

breakpoints

Oleg Isonen89d9c9aaf61e
diff --git a/packages/css-engine/src/style-sheet.test.ts b/packages/css-engine/src/style-sheet.test.ts
index 952b30c695..9fa9201023 100644
--- a/packages/css-engine/src/style-sheet.test.ts
+++ b/packages/css-engine/src/style-sheet.test.ts
@@ -1,21 +1,73 @@
-import { StyleSheet } from "./style-sheet";
+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", () => {
- describe("basics", () => {
- test("rule with multiple properties", () => {
- const sheet = new StyleSheet();
+ beforeEach(() => {
+ resetRuleCounter();
+ });
+
+ test("throw when breakpoint is not registered", () => {
+ const sheet = new StyleSheet();
+ expect(() => {
sheet.addRules([
{
- style: {
- display: { type: "keyword", value: "block" },
- color: { type: "keyword", value: "red" },
- },
+ style: style0,
breakpoint: "0",
},
]);
- expect(sheet.toString()).toMatchInlineSnapshot(
- `".s0 { display: block; color: red }"`
- );
- });
+ }).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("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 5149e6569e..f7804b1602 100644
--- a/packages/css-engine/src/style-sheet.ts
+++ b/packages/css-engine/src/style-sheet.ts
@@ -1,19 +1,19 @@
-import {
- Breakpoint,
- CssRule,
- Style,
- StyleValue,
- toValue,
-} from "@webstudio-is/react-sdk";
+import { Breakpoint, CssRule, Style, toValue } from "@webstudio-is/react-sdk";
let ruleCounter = -1;
-class Rule {
+export const resetRuleCounter = () => {
+ ruleCounter = -1;
+};
+
+class StyleRule {
+ id: number;
style: Style;
// @todo name can be composition token name or component name
- name: string = "s";
+ name = "s";
constructor(style: Style) {
this.style = style;
+ this.id = ++ruleCounter;
}
toString() {
const block = [];
@@ -23,23 +23,47 @@ class Rule {
if (value === undefined) continue;
block.push(`${property}: ${toValue(value)}`);
}
- const selector = `${this.name}${++ruleCounter}`;
+ const selector = `${this.name}${this.id}`;
return `.${selector} { ${block.join("; ")} }`;
}
}
-const createRule = (rule: CssRule) => new Rule(rule.style);
+class MediaRule {
+ id = "";
+ breakpoint: Breakpoint;
+ rules: Array<StyleRule> = [];
+ constructor(breakpoint: Breakpoint) {
+ this.breakpoint = breakpoint;
+ this.id = breakpoint.id;
+ }
+ addRule(rule: StyleRule) {
+ this.rules.push(rule);
+ }
+ toString() {
+ 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}`;
+ }
+}
export class StyleSheet {
- rules: Array<Rule> = [];
- breakpoints: Array<Breakpoint> = [];
+ mediaRules: Map<Breakpoint["id"], MediaRule> = new Map();
style?: HTMLStyleElement;
addBreakpoints(breakpoints: Array<Breakpoint>) {
- this.breakpoints.push(...breakpoints);
+ for (const breakpoint of breakpoints) {
+ this.mediaRules.set(breakpoint.id, new MediaRule(breakpoint));
+ }
}
addRules(rules: Array<CssRule>) {
- // @todo throw if referenced breakpoint doesn't exist?
- this.rules.push(...rules.map(createRule));
+ for (const rule of rules) {
+ const mediaRule = this.mediaRules.get(rule.breakpoint);
+ if (mediaRule === undefined) {
+ throw new Error(`Unknown breakpoint: ${rule.breakpoint}`);
+ }
+
+ mediaRule.addRule(new StyleRule(rule.style));
+ }
}
mount() {
if (this.style !== undefined) {
@@ -54,8 +78,9 @@ export class StyleSheet {
}
toString() {
const css = [];
- for (const rule of this.rules) {
- css.push(rule.toString());
+ for (const mediaRule of this.mediaRules.values()) {
+ const str = mediaRule.toString();
+ if (str !== "") css.push(mediaRule.toString());
}
return css.join("\n");
}