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.

render tokens (make breakpoint optional for CssRule)

Oleg Isonenedad49148aa9
diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.ts
index e3b6f26a7f..d0a2776666 100644
--- a/apps/designer/app/canvas/shared/styles.ts
+++ b/apps/designer/app/canvas/shared/styles.ts
@@ -69,8 +69,6 @@ const fontsAndDefaultsCssEngine = createCssEngine();
const tokensCssEngine = createCssEngine();
export const GlobalStyles = ({ assets }: { assets: Array<Asset> }) => {
- const varsRuleRef = useRef<StyleRule>();
-
useIsomorphicLayoutEffect(() => {
for (const style of helperStyles) {
helpersCssEngine.addPlaintextRule(style);
@@ -91,20 +89,10 @@ export const GlobalStyles = ({ assets }: { assets: Array<Asset> }) => {
const [tokens] = useDesignTokens();
useIsomorphicLayoutEffect(() => {
+ tokensCssEngine.clear();
if (tokens.length !== 0) {
const style = tokenToStyle(tokens);
- if (varsRuleRef.current === undefined) {
- varsRuleRef.current = tokensCssEngine.addStyleRule(`:root`, {
- breakpoint: "",
- style,
- });
- } else {
- varsRuleRef.current.styleMap.clear();
- let property: StyleProperty;
- for (property in style) {
- varsRuleRef.current.styleMap.set(property, style[property]);
- }
- }
+ tokensCssEngine.addStyleRule(`:root`, { style });
}
if (typeof document !== "undefined") {
diff --git a/packages/css-data/src/schema.ts b/packages/css-data/src/schema.ts
index 606d981884..ab96274004 100644
--- a/packages/css-data/src/schema.ts
+++ b/packages/css-data/src/schema.ts
@@ -87,7 +87,7 @@ export type Style = {
export const CssRule = z.object({
style: Style,
- breakpoint: z.string(),
+ breakpoint: z.optional(z.string()),
});
export type CssRule = z.infer<typeof CssRule>;
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 6ba8452d5f..32a2bb24a5 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -17,7 +17,7 @@ describe("CssEngine", () => {
test("use default media rule when there is no matching one registrered", () => {
engine.addStyleRule(".c", {
style: style0,
- breakpoint: "0",
+ breakpoint: "x",
});
expect(engine.cssText).toMatchInlineSnapshot(`
"@media all {
@@ -26,7 +26,7 @@ describe("CssEngine", () => {
`);
engine.addStyleRule(".c1", {
style: { color: { type: "keyword", value: "red" } },
- breakpoint: "0",
+ breakpoint: "x",
});
expect(engine.cssText).toMatchInlineSnapshot(`
"@media all {
@@ -38,7 +38,7 @@ describe("CssEngine", () => {
engine.addMediaRule(mediaId, mediaRuleOptions0);
engine.addStyleRule(".c1", {
style: { color: { type: "keyword", value: "blue" } },
- breakpoint: "0",
+ breakpoint: mediaId,
});
// Default media query should allways be the first to have the lowest source order specificity
expect(engine.cssText).toMatchInlineSnapshot(`
diff --git a/packages/css-engine/src/core/css-engine.ts b/packages/css-engine/src/core/css-engine.ts
index 204bfcbb10..37fa98ba19 100644
--- a/packages/css-engine/src/core/css-engine.ts
+++ b/packages/css-engine/src/core/css-engine.ts
@@ -23,7 +23,6 @@ export class CssEngine {
constructor() {
this.#element = new StyleElement();
this.#sheet = new StyleSheet(this.#element);
- this.addMediaRule(defaultMediaRuleId);
}
addMediaRule(id: string, options?: MediaRuleOptions) {
let mediaRule = this.#mediaRules.get(id);
@@ -35,9 +34,7 @@ export class CssEngine {
return mediaRule;
}
addStyleRule(selectorText: string, rule: CssRule) {
- const mediaRule =
- this.#mediaRules.get(rule.breakpoint) ??
- this.#mediaRules.get(defaultMediaRuleId);
+ const mediaRule = this.addMediaRule(rule.breakpoint || defaultMediaRuleId);
this.#isDirty = true;
const styleRule = new StyleRule(selectorText, rule.style);
styleRule.onChange = this.#onChangeRule;
@@ -51,9 +48,11 @@ export class CssEngine {
addPlaintextRule(cssText: string) {
const rule = this.#plainRules.get(cssText);
if (rule !== undefined) return rule;
+ this.#isDirty = true;
return this.#plainRules.set(cssText, new PlaintextRule(cssText));
}
addFontFaceRule(options: FontFaceOptions) {
+ this.#isDirty = true;
return this.#fontFaceRules.push(new FontFaceRule(options));
}
clear() {