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.

remove usage of stitches for helper css on canvas

Oleg Isonen628081d0fa70
diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.ts
index 81a2cffe30..03f6e3eb22 100644
--- a/apps/designer/app/canvas/shared/styles.ts
+++ b/apps/designer/app/canvas/shared/styles.ts
@@ -6,59 +6,56 @@ import { rootInstanceContainer } from "~/shared/nano-states";
import {
toValue,
toVarNamespace,
+ validStaticValueTypes,
+ idAttribute,
+ type Breakpoint,
+ type ValidStaticStyleValue,
type StyleValue,
type CssRule,
type StyleProperty,
type Style,
- Breakpoint,
- ValidStaticStyleValue,
- validStaticValueTypes,
- idAttribute,
} from "@webstudio-is/react-sdk";
-import { globalCss } from "@webstudio-is/design-system";
import { useEffect } from "react";
import { createCssEngine, type StyleRule } from "@webstudio-is/css-engine";
import { useIsomorphicLayoutEffect } from "react-use";
+import { PlaintextRule } from "@webstudio-is/css-engine/src/core/rules";
+
+const cssEngine = createCssEngine();
const voidElements =
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr";
// Helper styles on for canvas in design mode
-const styles = {
- "[data-ws-component]": {
- // When double clicking into an element to edit text, it should not select the word.
- userSelect: "none",
- },
- [`[data-ws-component]:not(${voidElements}):not(body):empty`]: {
- outline: "1px dashed #555",
- outlineOffset: -1,
- paddingTop: 50,
- paddingRight: 50,
- },
- "[data-ws-component][contenteditable], [data-ws-component]:focus": {
- outline: 0,
- },
- "[data-ws-component][contenteditable]": {
- boxShadow: "0 0 0px 4px rgb(36 150 255 / 20%)",
- },
+const styles = [
+ // When double clicking into an element to edit text, it should not select the word.
+ `[${idAttribute}] {
+ user-select: none;
+ }`,
+ `[${idAttribute}]:not(${voidElements}):not(body):empty {
+ outline: 1px dashed #555;
+ outline-offset: -1;
+ padding-top: 50;
+ padding-right: 50;
+ }`,
+ `[${idAttribute}][contenteditable], [${idAttribute}]:focus {
+ outline: 0;
+ }`,
+ `[${idAttribute}][contenteditable] {
+ box-shadow: 0 0 0px 4px rgb(36 150 255 / 20%)
+ }`,
// Text Editor wraps each line into a p, so we need to make sure there is no jump between regular rendering and editing
- "[data-ws-component][contenteditable] p": {
- margin: 0,
- },
-};
-
-// @todo rewrite to use css engine and remove stitches from canvas entirely
-const wrapperComponentGlobalStyles = globalCss(styles);
+ `[${idAttribute}][contenteditable] p {
+ margin: 0
+ }`,
+];
+for (const style of styles) cssEngine.addPlaintextRule(style);
export const useManageStyles = () => {
- wrapperComponentGlobalStyles();
useUpdateStyle();
usePreviewStyle();
useRemoveSsrStyles();
};
-const cssEngine = createCssEngine();
-
// Wrapps a normal StyleValue into a VarStyleValue that uses the previous style value as a fallback and allows
// to quickly pass the values over CSS variable witout rerendering the components tree.
// Results in values like this: `var(--namespace, staticValue)`
@@ -95,7 +92,7 @@ export const addMediaRules = (breakpoints: Array<Breakpoint>) => {
}
};
-const wrappedRulesMap = new Map<string, StyleRule>();
+const wrappedRulesMap = new Map<string, StyleRule | PlaintextRule>();
const addRule = (id: string, cssRule: CssRule) => {
const key = id + cssRule.breakpoint;
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 490fb26bd3..ddf3ba3f33 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -164,4 +164,13 @@ describe("CssEngine", () => {
}"
`);
});
+
+ test("plaintext rule", () => {
+ engine.addPlaintextRule(".c { color: red }");
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all {
+ .c { color: red }
+ }"
+ `);
+ });
});
diff --git a/packages/css-engine/src/core/css-engine.ts b/packages/css-engine/src/core/css-engine.ts
index b25a9703fb..fd4cf420eb 100644
--- a/packages/css-engine/src/core/css-engine.ts
+++ b/packages/css-engine/src/core/css-engine.ts
@@ -1,5 +1,10 @@
import { CssRule } from "@webstudio-is/react-sdk";
-import { MediaRule, StyleRule, type MediaRuleOptions } from "./rules";
+import {
+ MediaRule,
+ PlaintextRule,
+ StyleRule,
+ type MediaRuleOptions,
+} from "./rules";
import { StyleElement } from "./style-element";
import { StyleSheet } from "./style-sheet";
@@ -38,6 +43,14 @@ export class CssEngine {
}
return mediaRule.insertRule(styleRule);
}
+ addPlaintextRule(cssText: string) {
+ const mediaRule = this.#mediaRules.get(defaultMediaRuleId);
+ if (mediaRule === undefined) {
+ // Should be impossible to reach.
+ throw new Error("No media rule found");
+ }
+ return mediaRule.insertRule(new PlaintextRule(cssText));
+ }
render() {
this.#element.mount();
// This isn't going to do anything if the `cssText` hasn't changed.
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index 8438f96230..a2a9d78f68 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -60,13 +60,13 @@ export type MediaRuleOptions = {
export class MediaRule {
#options: MediaRuleOptions;
- rules: Array<StyleRule> = [];
+ rules: Array<StyleRule | PlaintextRule> = [];
#mediaType;
constructor(options: MediaRuleOptions = {}) {
this.#options = options;
this.#mediaType = options.mediaType ?? "all";
}
- insertRule(rule: StyleRule) {
+ insertRule(rule: StyleRule | PlaintextRule) {
this.rules.push(rule);
return rule;
}
@@ -87,4 +87,12 @@ export class MediaRule {
}
}
-export type AnyRule = StyleRule | MediaRule;
+export class PlaintextRule {
+ cssText;
+ styleMap = new Map();
+ constructor(cssText: string) {
+ this.cssText = cssText;
+ }
+}
+
+export type AnyRule = StyleRule | MediaRule | PlaintextRule;