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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Render static css from normalized styles (#750)
Bogdan Chadkin6387accf0d53
diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.tsindex bd9ebaba12..6b9b198216 100644--- a/apps/designer/app/canvas/shared/styles.ts+++ b/apps/designer/app/canvas/shared/styles.ts@@ -1,6 +1,10 @@import store from "immerhin";import { useSubscribe } from "~/shared/pubsub";-import { addGlobalRules, utils } from "@webstudio-is/project";+import {+ addGlobalRules,+ getPresetStyleRules,+ utils,+} from "@webstudio-is/project";import { useSelectedInstance } from "./nano-states";import {designTokensContainer,@@ -108,12 +112,14 @@ export const GlobalStyles = ({ assets }: { assets: Array<Asset> }) => {useIsomorphicLayoutEffect(() => {presetStylesEngine.clear();- const presetStylesMap = utils.tree.getPresetStylesMap(presetStyles);+ const presetStyleRules = getPresetStyleRules(presetStyles);for (const component of getComponentNames()) {const meta = getComponentMeta(component);// render preset style and fallback to hardcoded one// because could not be added yet to db- const presetStyle = presetStylesMap.get(component) ?? meta.presetStyle;+ const presetStyle =+ presetStyleRules.find((item) => item.component === component)?.style ??+ meta.presetStyle;if (presetStyle !== undefined) {presetStylesEngine.addStyleRule(`[data-ws-component=${component}]`, {style: presetStyle,diff --git a/apps/designer/app/shared/css-utils/generate-css-text.ts b/apps/designer/app/shared/css-utils/generate-css-text.tsindex 1f306e178d..90b1f50418 100644--- a/apps/designer/app/shared/css-utils/generate-css-text.ts+++ b/apps/designer/app/shared/css-utils/generate-css-text.ts@@ -1,6 +1,10 @@import { json } from "@remix-run/node";import { db } from "@webstudio-is/project/server";-import { addGlobalRules, utils } from "@webstudio-is/project";+import {+ addGlobalRules,+ getPresetStyleRules,+ getStyleRules,+} from "@webstudio-is/project";import { loadCanvasData } from "~/shared/db";import { createCssEngine } from "@webstudio-is/css-engine";import { idAttribute } from "@webstudio-is/react-sdk";@@ -31,17 +35,19 @@ export const generateCssText = async (buildParams: BuildParams) => {engine.addMediaRule(breakpoint.id, breakpoint);}- const presetStylesMap = utils.tree.getPresetStylesMap(- canvasData.tree?.presetStyles- );- for (const [component, style] of presetStylesMap) {+ const presetStyleRules = getPresetStyleRules(canvasData.tree?.presetStyles);+ for (const { component, style } of presetStyleRules) {engine.addStyleRule(`[data-ws-component="${component}"]`, { style });}- const cssRules = utils.tree.getCssRules(canvasData.tree?.root);- for (const [instanceId, cssRule] of cssRules) {- engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, cssRule);+ const styleRules = getStyleRules(canvasData.tree?.styles);+ for (const { breakpointId, instanceId, style } of styleRules) {+ engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, {+ breakpoint: breakpointId,+ style,+ });}+const { cssText } = engine;return cssText;};diff --git a/packages/project/src/shared/styles/index.ts b/packages/project/src/shared/styles/index.tsindex 8b053f67d0..1964a1fa6f 100644--- a/packages/project/src/shared/styles/index.ts+++ b/packages/project/src/shared/styles/index.ts@@ -1 +1,2 @@export * from "./global-rules";+export * from "./style-rules";diff --git a/packages/project/src/shared/styles/style-rules.test.ts b/packages/project/src/shared/styles/style-rules.test.tsnew file mode 100644index 0000000000..e11e69c205--- /dev/null+++ b/packages/project/src/shared/styles/style-rules.test.ts@@ -0,0 +1,109 @@+import { test, expect } from "@jest/globals";+import type { Styles } from "@webstudio-is/react-sdk";+import { getStyleRules, getPresetStyleRules } from "./style-rules";++test("get a list of style rules grouped by instance and breakpoint", () => {+ const styles: Styles = [+ {+ breakpointId: "a",+ instanceId: "1",+ property: "width",+ value: { type: "unit", value: 10, unit: "px" },+ },+ {+ breakpointId: "a",+ instanceId: "2",+ property: "display",+ value: { type: "keyword", value: "block" },+ },+ {+ breakpointId: "a",+ instanceId: "3",+ property: "color",+ value: { type: "keyword", value: "red" },+ },+ ];++ expect(getStyleRules(styles)).toMatchInlineSnapshot(`+ [+ {+ "breakpointId": "a",+ "instanceId": "1",+ "style": {+ "width": {+ "type": "unit",+ "unit": "px",+ "value": 10,+ },+ },+ },+ {+ "breakpointId": "a",+ "instanceId": "2",+ "style": {+ "display": {+ "type": "keyword",+ "value": "block",+ },+ },+ },+ {+ "breakpointId": "a",+ "instanceId": "3",+ "style": {+ "color": {+ "type": "keyword",+ "value": "red",+ },+ },+ },+ ]+ `);+});++test("get a list of preset styles grouped by component", () => {+ expect(+ getPresetStyleRules([+ {+ component: "Box",+ property: "width",+ value: { type: "keyword", value: "auto" },+ },+ {+ component: "Box",+ property: "height",+ value: { type: "keyword", value: "auto" },+ },+ {+ component: "Paragraph",+ property: "width",+ value: { type: "keyword", value: "auto" },+ },+ ])+ ).toMatchInlineSnapshot(`+ [+ {+ "component": "Box",+ "style": {+ "height": {+ "type": "keyword",+ "value": "auto",+ },+ "width": {+ "type": "keyword",+ "value": "auto",+ },+ },+ },+ {+ "component": "Paragraph",+ "style": {+ "width": {+ "type": "keyword",+ "value": "auto",+ },+ },+ },+ ]+ `);+});diff --git a/packages/project/src/shared/styles/style-rules.ts b/packages/project/src/shared/styles/style-rules.tsnew file mode 100644index 0000000000..3572ffea65--- /dev/null+++ b/packages/project/src/shared/styles/style-rules.ts@@ -0,0 +1,46 @@+import type { Style } from "@webstudio-is/css-data";+import type { PresetStyles, Styles } from "@webstudio-is/react-sdk";++type StyleRule = {+ instanceId: string;+ breakpointId: string;+ style: Style;+};++export const getStyleRules = (styles?: Styles) => {+ const stylesMap = new Map<string, StyleRule>();+ if (styles === undefined) {+ return [];+ }+ for (const { breakpointId, instanceId, property, value } of styles) {+ const id = `${breakpointId}:${instanceId}`;+ let styleRule = stylesMap.get(id);+ if (styleRule === undefined) {+ styleRule = { breakpointId, instanceId, style: {} };+ stylesMap.set(id, styleRule);+ }+ styleRule.style[property] = value;+ }+ return Array.from(stylesMap.values());+};++type PresetStyleRule = {+ component: string;+ style: Style;+};++export const getPresetStyleRules = (presetStyles?: PresetStyles) => {+ const presetStylesMap = new Map<string, PresetStyleRule>();+ if (presetStyles === undefined) {+ return [];+ }+ for (const { component, property, value } of presetStyles) {+ let presetStyleRule = presetStylesMap.get(component);+ if (presetStyleRule === undefined) {+ presetStyleRule = { component, style: {} };+ presetStylesMap.set(component, presetStyleRule);+ }+ presetStyleRule.style[property] = value;+ }+ return Array.from(presetStylesMap.values());+};diff --git a/packages/project/src/shared/tree-utils/get-css-rules.test.ts b/packages/project/src/shared/tree-utils/get-css-rules.test.tsdeleted file mode 100644index 06847cbc9f..0000000000--- a/packages/project/src/shared/tree-utils/get-css-rules.test.ts+++ /dev/null@@ -1,129 +0,0 @@-import { describe, test, expect } from "@jest/globals";-import { type Instance } from "@webstudio-is/react-sdk";-import { getCssRules, getPresetStylesMap } from "./get-css-rules";--describe("Get all cssRules from an instance including children", () => {- test("getCssRules", () => {- const rootInstance: Instance = {- type: "instance",- component: "Box",- id: "1",- cssRules: [- {- style: { width: { type: "unit", value: 10, unit: "px" } },- breakpoint: "a",- },- ],- children: [- {- type: "instance",- component: "Box",- id: "2",- cssRules: [- {- style: { display: { type: "keyword", value: "block" } },- breakpoint: "a",- },- ],- children: [- {- type: "instance",- component: "Box",- id: "3",- cssRules: [- {- style: { color: { type: "keyword", value: "red" } },- breakpoint: "a",- },- ],- children: [],- },- ],- },- ],- };-- expect(getCssRules(rootInstance)).toMatchInlineSnapshot(`- [- [- "1",- {- "breakpoint": "a",- "style": {- "width": {- "type": "unit",- "unit": "px",- "value": 10,- },- },- },- ],- [- "2",- {- "breakpoint": "a",- "style": {- "display": {- "type": "keyword",- "value": "block",- },- },- },- ],- [- "3",- {- "breakpoint": "a",- "style": {- "color": {- "type": "keyword",- "value": "red",- },- },- },- ],- ]- `);- });-});--test("get a map of preset style objects per component", () => {- expect(- getPresetStylesMap([- {- component: "Box",- property: "width",- value: { type: "keyword", value: "auto" },- },- {- component: "Box",- property: "height",- value: { type: "keyword", value: "auto" },- },- {- component: "Paragraph",- property: "width",- value: { type: "keyword", value: "auto" },- },- ])- ).toMatchInlineSnapshot(`- Map {- "Box" => {- "height": {- "type": "keyword",- "value": "auto",- },- "width": {- "type": "keyword",- "value": "auto",- },- },- "Paragraph" => {- "width": {- "type": "keyword",- "value": "auto",- },- },- }- `);-});diff --git a/packages/project/src/shared/tree-utils/get-css-rules.ts b/packages/project/src/shared/tree-utils/get-css-rules.tsdeleted file mode 100644index 4638af04a6..0000000000--- a/packages/project/src/shared/tree-utils/get-css-rules.ts+++ /dev/null@@ -1,41 +0,0 @@-import type { CssRule, Style } from "@webstudio-is/css-data";-import type { Instance, PresetStyles } from "@webstudio-is/react-sdk";--const aggregateCssRules = (- instance: Instance,- result: Array<[string, CssRule]>-) => {- for (const cssRule of instance.cssRules) {- result.push([instance.id, cssRule]);- }- for (const child of instance.children) {- if (child.type === "text") {- continue;- }- aggregateCssRules(child, result);- }-};--export const getCssRules = (instance?: Instance) => {- const result: Array<[string, CssRule]> = [];- if (instance) {- aggregateCssRules(instance, result);- }- return result;-};--export const getPresetStylesMap = (presetStyles?: PresetStyles) => {- const presetStylesMap = new Map<string, Style>();- if (presetStyles === undefined) {- return presetStylesMap;- }- for (const { component, property, value } of presetStyles) {- let style = presetStylesMap.get(component);- if (style === undefined) {- style = {};- presetStylesMap.set(component, style);- }- style[property] = value;- }- return presetStylesMap;-};diff --git a/packages/project/src/shared/tree-utils/index.ts b/packages/project/src/shared/tree-utils/index.tsindex 59acb2ba39..b83053f11a 100644--- a/packages/project/src/shared/tree-utils/index.ts+++ b/packages/project/src/shared/tree-utils/index.ts@@ -9,6 +9,5 @@ export * from "./find-parent-instance";export * from "./clone-instance";export * from "./reparent-instance";export * from "./find-closest-non-inline-parent";-export * from "./get-css-rules";export * from "./set-instance-children";export * from "./find-instance";diff --git a/packages/react-sdk/src/tree/wrapper-component.tsx b/packages/react-sdk/src/tree/wrapper-component.tsxindex 052c886433..57876e6703 100644--- a/packages/react-sdk/src/tree/wrapper-component.tsx+++ b/packages/react-sdk/src/tree/wrapper-component.tsx@@ -41,10 +41,16 @@ export const WrapperComponent = ({}: WrapperComponentProps) => {const Component = getComponent(instance.component);const userProps = useUserProps(instance.id);- const props = { ...userProps, ...rest, [idAttribute]: instance.id };+ const props = {+ ...userProps,+ ...rest,+ [idAttribute]: instance.id,+ [componentAttribute]: instance.component,+ };return (<Component {...props}>{renderWrapperComponentChildren(children)}</Component>);};export const idAttribute = "data-ws-id";+export const componentAttribute = "data-ws-component";