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
move react hooks from css engine to shared styles, it's too tight with app
Oleg Isonen005baac2eda3
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsxindex b8d463bb99..e346c8a7a6 100644--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx@@ -8,9 +8,9 @@ import {type Instance,type OnChangeChildren,} from "@webstudio-is/react-sdk";-import { useCssRules } from "@webstudio-is/css-engine";import { useTextEditingInstanceId } from "~/shared/nano-states";import { useSelectedElement } from "~/canvas/shared/nano-states";+import { useCssRules } from "~/canvas/shared/styles";const TextEditor = lazy(() => import("../text-editor"));diff --git a/apps/designer/app/canvas/shared/breakpoints.ts b/apps/designer/app/canvas/shared/breakpoints.tsindex 59351dc833..15a2bacef0 100644--- a/apps/designer/app/canvas/shared/breakpoints.ts+++ b/apps/designer/app/canvas/shared/breakpoints.ts@@ -9,7 +9,7 @@ import {useBreakpoints,} from "~/shared/nano-states";import { publish } from "~/shared/pubsub";-import { addMediaRules } from "@webstudio-is/css-engine";+import { addMediaRules } from "./styles";export const useInitializeBreakpoints = (breakpoints: Array<Breakpoint>) => {const [, setCurrentBreakpoints] = useBreakpoints();diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.tsindex 32706d46e4..2625105c69 100644--- a/apps/designer/app/canvas/shared/styles.ts+++ b/apps/designer/app/canvas/shared/styles.ts@@ -4,12 +4,20 @@ import { setInstanceStyleMutable } from "~/shared/tree-utils";import { useSelectedInstance } from "./nano-states";import { rootInstanceContainer } from "~/shared/nano-states";import {- type StyleValue,toValue,toVarNamespace,+ type StyleValue,+ type CssRule,+ type StyleProperty,+ type Style,+ Breakpoint,+ ValidStaticStyleValue,+ validStaticValueTypes,} from "@webstudio-is/react-sdk";import { globalCss } from "@webstudio-is/design-system";-import { useEffect } from "react";+import { useEffect, useRef } from "react";+import { createCssEngine, type StyleRule } from "@webstudio-is/css-engine";+import { useIsomorphicLayoutEffect } from "react-use";const setCssVar = (id: string, property: string, value?: StyleValue) => {const customProperty = `--${toVarNamespace(id, property)}`;@@ -93,3 +101,74 @@ export const useManageStyles = () => {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)`+const toVarStyleWithFallback = (instanceId: string, style: Style): Style => {+ const dynamicStyle: Style = {};+ let property: StyleProperty;+ for (property in style) {+ const value = style[property];+ if (value === undefined) {+ continue;+ }+ if (value.type === "var") {+ dynamicStyle[property] = value;+ continue;+ }+ if (+ validStaticValueTypes.includes(+ value.type as typeof validStaticValueTypes[number]+ )+ ) {+ dynamicStyle[property] = {+ type: "var",+ value: toVarNamespace(instanceId, property),+ fallbacks: [value as ValidStaticStyleValue],+ };+ }+ }+ return dynamicStyle;+};++export const addMediaRules = (breakpoints: Array<Breakpoint>) => {+ for (const breakpoint of breakpoints) {+ cssEngine.addMediaRule(breakpoint.id, breakpoint);+ }+};++export const useCssRules = ({+ id,+ cssRules,+}: {+ id: string;+ cssRules: Array<CssRule>;+}) => {+ const ruleMap = useRef<Map<string, StyleRule>>(new Map());++ useIsomorphicLayoutEffect(() => {+ for (const cssRule of cssRules) {+ const key = id + cssRule.breakpoint;++ let rule = ruleMap.current.get(key);+ if (rule === undefined) {+ const selectorText = `[data-ws-id="${id}"]`;+ rule = cssEngine.addStyleRule(selectorText, {+ ...cssRule,+ style: toVarStyleWithFallback(id, cssRule.style),+ });+ ruleMap.current.set(key, rule);+ continue;+ }+ const dynamicStyle = toVarStyleWithFallback(id, cssRule.style);+ let property: StyleProperty;+ for (property in dynamicStyle) {+ rule.styleMap.set(property, dynamicStyle[property]);+ }+ }+ cssEngine.render();+ }, [id, cssRules]);+};diff --git a/packages/css-engine/src/index.ts b/packages/css-engine/src/index.tsindex c8fe1353f8..8d119dee81 100644--- a/packages/css-engine/src/index.ts+++ b/packages/css-engine/src/index.ts@@ -1,2 +1 @@export * from "./core";-export * from "./react";diff --git a/packages/css-engine/src/react.ts b/packages/css-engine/src/react.tsdeleted file mode 100644index 1bf7759ba6..0000000000--- a/packages/css-engine/src/react.ts+++ /dev/null@@ -1,86 +0,0 @@-import {- CssRule,- toVarNamespace,- validStaticValueTypes,- type Breakpoint,- type Style,- type StyleProperty,- type ValidStaticStyleValue,-} from "@webstudio-is/react-sdk";-import { useEffect, useLayoutEffect, useRef } from "react";-import { createCssEngine } from "./core";-import type { StyleRule } from "./core/rules";--const cssEngine = createCssEngine();--const useIsomorphicLayoutEffect =- typeof window === "undefined" ? useEffect : useLayoutEffect;--// 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)`-const toVarStyleWithFallback = (instanceId: string, style: Style): Style => {- const dynamicStyle: Style = {};- let property: StyleProperty;- for (property in style) {- const value = style[property];- if (value === undefined) {- continue;- }- if (value.type === "var") {- dynamicStyle[property] = value;- continue;- }- if (- validStaticValueTypes.includes(- value.type as typeof validStaticValueTypes[number]- )- ) {- dynamicStyle[property] = {- type: "var",- value: toVarNamespace(instanceId, property),- fallbacks: [value as ValidStaticStyleValue],- };- }- }- return dynamicStyle;-};--export const addMediaRules = (breakpoints: Array<Breakpoint>) => {- for (const breakpoint of breakpoints) {- cssEngine.addMediaRule(breakpoint.id, breakpoint);- }-};--export const useCssRules = ({- id,- cssRules,-}: {- id: string;- cssRules: Array<CssRule>;-}) => {- const ruleMap = useRef<Map<string, StyleRule>>(new Map());-- useIsomorphicLayoutEffect(() => {- for (const cssRule of cssRules) {- const key = id + cssRule.breakpoint;-- let rule = ruleMap.current.get(key);- if (rule === undefined) {- const selectorText = `[data-ws-id="${id}"]`;- rule = cssEngine.addStyleRule(selectorText, {- ...cssRule,- style: toVarStyleWithFallback(id, cssRule.style),- });- ruleMap.current.set(key, rule);- continue;- }- const dynamicStyle = toVarStyleWithFallback(id, cssRule.style);- let property: StyleProperty;- for (property in dynamicStyle) {- rule.styleMap.set(property, dynamicStyle[property]);- }- }- cssEngine.render();- }, [id, cssRules]);-};