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.

fix the media rule formatter

Oleg Isonen42fb00dfbe18
diff --git a/apps/designer/app/routes/s/css.ts b/apps/designer/app/routes/s/css.ts
index 7c5b172878..098924ea01 100644
--- a/apps/designer/app/routes/s/css.ts
+++ b/apps/designer/app/routes/s/css.ts
@@ -34,7 +34,7 @@ export const loader: ActionFunction = async ({ request }) => {
const engine = createCssEngine();
for (const breakpoint of canvasData.breakpoints) {
- engine.addMediaRule(breakpoint);
+ engine.addMediaRule(breakpoint.id, breakpoint);
}
const cssRules = getCssRules(canvasData.tree?.root);
diff --git a/packages/css-engine/src/core/css-engine.stories.tsx b/packages/css-engine/src/core/css-engine.stories.tsx
index 2af29eba80..23bf96bb72 100644
--- a/packages/css-engine/src/core/css-engine.stories.tsx
+++ b/packages/css-engine/src/core/css-engine.stories.tsx
@@ -9,11 +9,12 @@ const style0 = {
color: { type: "keyword", value: "red" },
} as const;
-const breakpoint0 = { minWidth: 0, id: "0", label: "0" } as const;
+const mediaRuleOptions0 = { minWidth: 0 } as const;
+const mediaId = "0";
export const Basic = () => {
const engine = new CssEngine();
- engine.addMediaRule(breakpoint0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
const rule = engine.addStyleRule(".test", {
style: style0,
breakpoint: "0",
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 7aee9c348c..490fb26bd3 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -4,7 +4,8 @@ const style0 = {
display: { type: "keyword", value: "block" },
} as const;
-const mediaRuleOptions0 = { minWidth: 0, id: "0", label: "0" } as const;
+const mediaRuleOptions0 = { minWidth: 0 } as const;
+const mediaId = "0";
describe("CssEngine", () => {
let engine: CssEngine;
@@ -23,10 +24,36 @@ describe("CssEngine", () => {
.c { display: block }
}"
`);
+ engine.addStyleRule(".c1", {
+ style: { color: { type: "keyword", value: "red" } },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all {
+ .c { display: block }
+ .c1 { color: red }
+ }"
+ `);
+
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
+ engine.addStyleRule(".c1", {
+ style: { color: { type: "keyword", value: "blue" } },
+ breakpoint: "0",
+ });
+ // Default media query should allways be the first to have the lowest source order specificity
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all {
+ .c { display: block }
+ .c1 { color: red }
+ }
+ @media all and (min-width: 0px) {
+ .c1 { color: blue }
+ }"
+ `);
});
test("rule with multiple properties", () => {
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
engine.addStyleRule(".c", {
style: {
...style0,
@@ -35,14 +62,14 @@ describe("CssEngine", () => {
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block; color: red }
}"
`);
});
test("hyphenate property", () => {
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
engine.addStyleRule(".c", {
style: {
backgroundColor: { type: "keyword", value: "red" },
@@ -50,14 +77,14 @@ describe("CssEngine", () => {
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { background-color: red }
}"
`);
});
test("add rule", () => {
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
const rule1 = engine.addStyleRule(".c", {
style: {
...style0,
@@ -66,7 +93,7 @@ describe("CssEngine", () => {
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block; color: red }
}"
`);
@@ -81,7 +108,7 @@ describe("CssEngine", () => {
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block; color: red }
.c2 { display: block; color: green }
}"
@@ -89,7 +116,7 @@ describe("CssEngine", () => {
});
test("update rule", () => {
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
const rule = engine.addStyleRule(".c", {
style: {
...style0,
@@ -98,7 +125,7 @@ describe("CssEngine", () => {
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block; color: red }
}"
`);
@@ -113,26 +140,26 @@ describe("CssEngine", () => {
);
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block; color: green }
}"
`);
});
test("don't override media queries", () => {
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
engine.addStyleRule(".c", {
style: style0,
breakpoint: "0",
});
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block }
}"
`);
- engine.addMediaRule(mediaRuleOptions0);
+ engine.addMediaRule(mediaId, mediaRuleOptions0);
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all (min-width: 0px) {
+ "@media all and (min-width: 0px) {
.c { display: block }
}"
`);
diff --git a/packages/css-engine/src/core/css-engine.ts b/packages/css-engine/src/core/css-engine.ts
index 69830fe469..b25a9703fb 100644
--- a/packages/css-engine/src/core/css-engine.ts
+++ b/packages/css-engine/src/core/css-engine.ts
@@ -3,33 +3,39 @@ import { MediaRule, StyleRule, type MediaRuleOptions } from "./rules";
import { StyleElement } from "./style-element";
import { StyleSheet } from "./style-sheet";
+const defaultMediaRuleId = "__default-media-rule__";
+
export class CssEngine {
#element;
- #mediaRules: Map<MediaRuleOptions["id"], MediaRule> = new Map();
+ #mediaRules: Map<string, MediaRule> = new Map();
#sheet: StyleSheet;
#isDirty = false;
#cssText = "";
constructor() {
this.#element = new StyleElement();
this.#sheet = new StyleSheet(this.#element);
+ this.addMediaRule(defaultMediaRuleId);
}
- addMediaRule(options: MediaRuleOptions) {
- let mediaRule = this.#mediaRules.get(options.id);
+ addMediaRule(id: string, options?: MediaRuleOptions) {
+ let mediaRule = this.#mediaRules.get(id);
if (mediaRule === undefined) {
mediaRule = new MediaRule(options);
- this.#mediaRules.set(options.id, mediaRule);
+ this.#mediaRules.set(id, mediaRule);
this.#isDirty = true;
}
return mediaRule;
}
addStyleRule(selectorText: string, rule: CssRule) {
- let mediaRule = this.#mediaRules.get(rule.breakpoint);
- if (mediaRule === undefined) {
- mediaRule = this.addMediaRule({ id: "__default-media-rule__" });
- }
+ const mediaRule =
+ this.#mediaRules.get(rule.breakpoint) ??
+ this.#mediaRules.get(defaultMediaRuleId);
this.#isDirty = true;
const styleRule = new StyleRule(selectorText, rule.style);
styleRule.onChange = this.#onChangeRule;
+ if (mediaRule === undefined) {
+ // Should be impossible to reach.
+ throw new Error("No media rule found");
+ }
return mediaRule.insertRule(styleRule);
}
render() {
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index 1d0e6c0deb..8438f96230 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -53,7 +53,6 @@ export class StyleRule {
}
export type MediaRuleOptions = {
- id: string;
minWidth?: number;
maxWidth?: number;
mediaType?: "all" | "screen" | "print";
@@ -63,7 +62,7 @@ export class MediaRule {
#options: MediaRuleOptions;
rules: Array<StyleRule> = [];
#mediaType;
- constructor(options: MediaRuleOptions) {
+ constructor(options: MediaRuleOptions = {}) {
this.#options = options;
this.#mediaType = options.mediaType ?? "all";
}
@@ -72,6 +71,7 @@ export class MediaRule {
return rule;
}
get cssText() {
+ if (this.rules.length === 0) return "";
const rules = [];
for (const rule of this.rules) {
rules.push(` ${rule.cssText}`);
@@ -80,7 +80,7 @@ export class MediaRule {
const { minWidth, maxWidth } = this.#options;
if (minWidth !== undefined) conditionText = `min-width: ${minWidth}px`;
if (maxWidth !== undefined) conditionText = `max-width: ${maxWidth}px`;
- if (conditionText) conditionText = `(${conditionText}) `;
+ if (conditionText) conditionText = `and (${conditionText}) `;
return `@media ${this.#mediaType} ${conditionText}{\n${rules.join(
"\n"
)}\n}`;
diff --git a/packages/css-engine/src/react.ts b/packages/css-engine/src/react.ts
index fdf834fbf1..1bf7759ba6 100644
--- a/packages/css-engine/src/react.ts
+++ b/packages/css-engine/src/react.ts
@@ -48,7 +48,7 @@ const toVarStyleWithFallback = (instanceId: string, style: Style): Style => {
export const addMediaRules = (breakpoints: Array<Breakpoint>) => {
for (const breakpoint of breakpoints) {
- cssEngine.addMediaRule(breakpoint);
+ cssEngine.addMediaRule(breakpoint.id, breakpoint);
}
};