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
emove stitches from sdk
Oleg Isonen7d74fb51ef9d
diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.tsindex 23da6a9d4c..faf44832cd 100644--- a/apps/designer/app/canvas/shared/styles.ts+++ b/apps/designer/app/canvas/shared/styles.ts@@ -4,8 +4,6 @@ import { setInstanceStyleMutable } from "~/shared/tree-utils";import { useSelectedInstance } from "./nano-states";import { rootInstanceContainer } from "~/shared/nano-states";import {- toValue,- toVarNamespace,validStaticValueTypes,idAttribute,type Breakpoint,@@ -18,6 +16,7 @@ import {import { useEffect } from "react";import {createCssEngine,+ toValue,type CssEngine,type StyleRule,type PlaintextRule,@@ -175,6 +174,10 @@ export const useCssRules = ({}, [id, cssRules]);};+const toVarNamespace = (id: string, property: string) => {+ return `${property}-${id}`;+};+const setCssVar = (id: string, property: string, value?: StyleValue) => {const customProperty = `--${toVarNamespace(id, property)}`;if (value === undefined) {diff --git a/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsxindex a00f040f15..bfe647e781 100644--- a/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx@@ -1,6 +1,6 @@import { Select } from "@webstudio-is/design-system";import { FontWeight, fontWeights } from "@webstudio-is/fonts";-import { toValue } from "@webstudio-is/react-sdk";+import { toValue } from "@webstudio-is/css-engine";import { useMemo } from "react";import { useAssets } from "~/designer/shared/assets";import { getFinalValue } from "../../shared/get-final-value";diff --git a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsxindex ed1c01dbda..41aa81f7ed 100644--- a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx@@ -1,7 +1,8 @@import { CssValueInput } from "../../shared/css-value-input";import { getFinalValue } from "../../shared/get-final-value";import { ControlProps } from "../../style-sections";-import { type StyleValue, toValue } from "@webstudio-is/react-sdk";+import { type StyleValue } from "@webstudio-is/react-sdk";+import { toValue } from "@webstudio-is/css-engine";import { useState } from "react";import { Box, Tooltip } from "@webstudio-is/design-system";diff --git a/apps/designer/app/designer/features/style-panel/sections/spacing/spacing.tsx b/apps/designer/app/designer/features/style-panel/sections/spacing/spacing.tsxindex cb0328b750..c5fe5662ee 100644--- a/apps/designer/app/designer/features/style-panel/sections/spacing/spacing.tsx+++ b/apps/designer/app/designer/features/style-panel/sections/spacing/spacing.tsx@@ -1,11 +1,11 @@import { useEffect, useState } from "react";import {categories,- toValue,type StyleProperty,type StyleValue,} from "@webstudio-is/react-sdk";import { Box } from "@webstudio-is/design-system";+import { toValue } from "@webstudio-is/css-engine";import { SetProperty } from "../../shared/use-style-data";import { useIsFromCurrentBreakpoint } from "../../shared/use-is-from-current-breakpoint";import { propertyNameColorForSelectedBreakpoint } from "../../shared/constants";diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsxindex f92544cf75..6012a7dfc0 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx@@ -1,10 +1,6 @@import { useState, useMemo } from "react";-import {- type Unit,- type UnitValue,- StyleValue,- toValue,-} from "@webstudio-is/react-sdk";+import { type Unit, type UnitValue, StyleValue } from "@webstudio-is/react-sdk";+import { toValue } from "@webstudio-is/css-engine";import * as SelectPrimitive from "@radix-ui/react-select";import {styled,diff --git a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts b/apps/designer/app/designer/features/style-panel/shared/get-final-value.tsindex ccdf9d8ce1..07fb72ba94 100644--- a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts+++ b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts@@ -1,9 +1,5 @@-import {- Style,- StyleValue,- StyleProperty,- toValue,-} from "@webstudio-is/react-sdk";+import type { Style, StyleValue, StyleProperty } from "@webstudio-is/react-sdk";+import { toValue } from "@webstudio-is/css-engine";import type { InheritedStyle } from "./get-inherited-style";import { isValid } from "./parse-css-value";diff --git a/apps/designer/app/designer/features/style-panel/style-sections.tsx b/apps/designer/app/designer/features/style-panel/style-sections.tsxindex 04112db691..ed77fa82d3 100644--- a/apps/designer/app/designer/features/style-panel/style-sections.tsx+++ b/apps/designer/app/designer/features/style-panel/style-sections.tsx@@ -1,10 +1,9 @@-import { Grid } from "@webstudio-is/design-system";+import { Grid, type CSS } from "@webstudio-is/design-system";import type { StyleConfig } from "./shared/configs";import {type Style,type StyleProperty,type Category,- type CSS,} from "@webstudio-is/react-sdk";import type { SetProperty, CreateBatchUpdate } from "./shared/use-style-data";import type { InheritedStyle } from "./shared/get-inherited-style";diff --git a/packages/css-engine/src/core/index.ts b/packages/css-engine/src/core/index.tsindex 7edd74db29..a985ac4dc5 100644--- a/packages/css-engine/src/core/index.ts+++ b/packages/css-engine/src/core/index.ts@@ -7,3 +7,4 @@ export type {FontFaceRule,} from "./rules";export * from "./create-css-engine";+export * from "./to-value";diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.tsindex fa8639c720..46db116cf1 100644--- a/packages/css-engine/src/core/rules.ts+++ b/packages/css-engine/src/core/rules.ts@@ -1,10 +1,10 @@import {- toValue,type Style,type StyleProperty,type StyleValue,} from "@webstudio-is/react-sdk";import hyphenate from "hyphenate-style-name";+import { toValue } from "./to-value";class StylePropertyMap {#styleMap: Map<StyleProperty, StyleValue | undefined> = new Map();diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.tsnew file mode 100644index 0000000000..7051daf341--- /dev/null+++ b/packages/css-engine/src/core/to-value.test.ts@@ -0,0 +1,66 @@+import { toValue } from "./to-value";++describe("Convert WS CSS Values to native CSS strings", () => {+ test("keyword", () => {+ const value = toValue({ type: "keyword", value: "red" });+ expect(value).toBe("red");+ });++ test("unit", () => {+ const value = toValue({ type: "unit", value: 10, unit: "px" });+ expect(value).toBe("10px");+ });++ test("invalid", () => {+ const value = toValue({ type: "invalid", value: "bad" });+ expect(value).toBe("bad");+ });++ test("unset", () => {+ const value = toValue({ type: "unset", value: "" });+ expect(value).toBe("");+ });++ test("var", () => {+ const value = toValue({ type: "var", value: "namespace", fallbacks: [] });+ expect(value).toBe("var(--namespace)");+ });++ test("var with fallbacks", () => {+ const value = toValue({+ type: "var",+ value: "namespace",+ fallbacks: [+ {+ type: "keyword",+ value: "normal",+ },+ {+ type: "unit",+ value: 10,+ unit: "px",+ },+ ],+ });+ expect(value).toBe("var(--namespace, normal, 10px)");+ });++ test("fontFamily", () => {+ const value = toValue({+ type: "fontFamily",+ value: ["Courier New"],+ });+ expect(value).toBe("Courier New, monospace");+ });++ test("withFallback=false", () => {+ const value = toValue(+ {+ type: "fontFamily",+ value: ["Courier New"],+ },+ { withFallback: false }+ );+ expect(value).toBe("Courier New");+ });+});diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.tsnew file mode 100644index 0000000000..a87bd37773--- /dev/null+++ b/packages/css-engine/src/core/to-value.ts@@ -0,0 +1,41 @@+import type { StyleValue } from "@webstudio-is/react-sdk";+import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";++type ToCssOptions = {+ withFallback: boolean;+};++const defaultOptions = {+ withFallback: true,+};++export const toValue = (+ value?: StyleValue,+ options: ToCssOptions = defaultOptions+): string => {+ if (value === undefined) return "";+ if (value.type === "unit") {+ return value.value + (value.unit === "number" ? "" : value.unit);+ }+ if (value.type === "fontFamily") {+ if (options.withFallback === false) {+ return value.value[0];+ }+ const family = value.value[0];+ const fallbacks = SYSTEM_FONTS.get(family);+ if (Array.isArray(fallbacks)) {+ return [...value.value, ...fallbacks].join(", ");+ }+ return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");+ }+ if (value.type === "var") {+ const fallbacks = [];+ for (const fallback of value.fallbacks) {+ fallbacks.push(toValue(fallback, options));+ }+ const fallbacksString =+ fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";+ return `var(--${value.value}${fallbacksString})`;+ }+ return value.value;+};diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.jsonindex 5c51b2848d..b176120cc8 100644--- a/packages/react-sdk/package.json+++ b/packages/react-sdk/package.json@@ -59,7 +59,6 @@"react-dom": "^17.0.2"},"dependencies": {- "@stitches/core": "^1.2.7","@webstudio-is/asset-uploader": "^*","@webstudio-is/fonts": "*","@webstudio-is/icons": "*",diff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.tsindex 7458f83c4d..ec43abd184 100644--- a/packages/react-sdk/src/index.ts+++ b/packages/react-sdk/src/index.ts@@ -1,6 +1,5 @@export * from "./css";export * from "./tree";-export * from "./stitches";export * as components from "./components";export * as componentsMeta from "./components/meta";export * from "./user-props";diff --git a/packages/react-sdk/src/remix/constants.ts b/packages/react-sdk/src/remix/constants.tsdeleted file mode 100644index b5ddbd71f5..0000000000--- a/packages/react-sdk/src/remix/constants.ts+++ /dev/null@@ -1 +0,0 @@-export const CRITICAL_CSS_MARKER = "__CRITICAL_CSS__";diff --git a/packages/react-sdk/src/remix/handle-request.server.tsx b/packages/react-sdk/src/remix/handle-request.server.tsxindex 8efe2c9943..66f255cb8a 100644--- a/packages/react-sdk/src/remix/handle-request.server.tsx+++ b/packages/react-sdk/src/remix/handle-request.server.tsx@@ -1,7 +1,6 @@import { renderToString } from "react-dom/server";import { RemixServer } from "@remix-run/react";import type { EntryContext } from "@remix-run/node";-import { insertCriticalCss } from "./insert-critical-css";export const handleRequest = (request: Request,@@ -15,7 +14,7 @@ export const handleRequest = (responseHeaders.set("Content-Type", "text/html");- return new Response("<!DOCTYPE html>" + insertCriticalCss(markup), {+ return new Response(`<!DOCTYPE html>${markup}`, {status: responseStatusCode,headers: responseHeaders,});diff --git a/packages/react-sdk/src/remix/insert-critical-css.ts b/packages/react-sdk/src/remix/insert-critical-css.tsdeleted file mode 100644index 9f0bdd20ca..0000000000--- a/packages/react-sdk/src/remix/insert-critical-css.ts+++ /dev/null@@ -1,6 +0,0 @@-import { flushCss } from "../stitches";-import { CRITICAL_CSS_MARKER } from "./constants";--export const insertCriticalCss = (markup: string): string => {- return markup.replace(CRITICAL_CSS_MARKER, `<style>${flushCss()}</style>`);-};diff --git a/packages/react-sdk/src/remix/root.tsx b/packages/react-sdk/src/remix/root.tsxindex c584e65ebf..3dc43f8d81 100644--- a/packages/react-sdk/src/remix/root.tsx+++ b/packages/react-sdk/src/remix/root.tsx@@ -1,6 +1,5 @@import { Links, Meta, Outlet as DefaultOutlet } from "@remix-run/react";import { DynamicLinks } from "remix-utils";-import { CRITICAL_CSS_MARKER } from "./constants";/*** We are using Outlet prop from index layout when user renders site from a subdomain.@@ -20,7 +19,6 @@ export const Root = ({<Meta /><DynamicLinks /><Links />- {typeof document === "undefined" ? CRITICAL_CSS_MARKER : null}</head><Outlet />diff --git a/packages/react-sdk/src/stitches/index.ts b/packages/react-sdk/src/stitches/index.tsdeleted file mode 100644index cba98204fc..0000000000--- a/packages/react-sdk/src/stitches/index.ts+++ /dev/null@@ -1,2 +0,0 @@-export * from "./to-css";-export * from "./stitches";diff --git a/packages/react-sdk/src/stitches/stitches.ts b/packages/react-sdk/src/stitches/stitches.tsdeleted file mode 100644index ba3f755fbc..0000000000--- a/packages/react-sdk/src/stitches/stitches.ts+++ /dev/null@@ -1,48 +0,0 @@-import { createStitches, type CSS } from "@stitches/core";-import type { Breakpoint } from "../css";--let media = {};--const createStitchesInstance = () => createStitches({ media });--type StitchesInstance = ReturnType<typeof createStitchesInstance>;--let stitches: StitchesInstance | undefined;--let fontsCss = "";--export { type CSS };--const getCachedConfig = () => {- if (stitches === undefined) {- stitches = createStitchesInstance();- }- return stitches;-};--// eslint-disable-next-line @typescript-eslint/no-explicit-any-export const css: StitchesInstance["css"] = (...args: any[]) => {- return getCachedConfig().css(...args);-};--// eslint-disable-next-line @typescript-eslint/no-explicit-any-export const globalCss: StitchesInstance["globalCss"] = (...args: any[]) => {- return getCachedConfig().globalCss(...args);-};--export const flushCss = () => {- const config = getCachedConfig();- const css = config.getCssText() + fontsCss;- config.reset();- fontsCss = "";- return css;-};--export const setBreakpoints = (breakpoints: Array<Breakpoint>) => {- const nextMedia: Record<string, string> = {};- for (const breakpoint of breakpoints) {- nextMedia[breakpoint.id] = `(min-width: ${breakpoint.minWidth}px)`;- }- media = nextMedia;- stitches = undefined;-};diff --git a/packages/react-sdk/src/stitches/to-css.test.ts b/packages/react-sdk/src/stitches/to-css.test.tsdeleted file mode 100644index b18084d17f..0000000000--- a/packages/react-sdk/src/stitches/to-css.test.ts+++ /dev/null@@ -1,231 +0,0 @@-import type { Instance } from "../db";-import { type CssRule, initialBreakpoints, type Breakpoint } from "../css";-import { toCss } from "./to-css";--const breakpoints: Array<Breakpoint> = initialBreakpoints.map(- (breakpoint, index) => ({- ...breakpoint,- id: String(index),- projectId: "projectId",- })-);--const createInstance = (cssRules: Array<CssRule>): Instance => {- return {- id: "0",- cssRules,- component: "Box",- children: [],- };-};--describe("Convert WS CSS rules to stitches", () => {- test("keyword", () => {- const instance = createInstance([- {- style: {- color: {- type: "keyword",- value: "red",- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- color: "red",- },- });- });-- test("unit", () => {- const instance = createInstance([- {- style: {- width: {- type: "unit",- value: 10,- unit: "px",- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- width: "10px",- },- });- });-- test("invalid", () => {- const instance = createInstance([- {- style: {- width: {- type: "invalid",- value: "bad",- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- width: "bad",- },- });- });-- test("unset", () => {- const instance = createInstance([- {- style: {- width: {- type: "unset",- value: "",- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- width: "",- },- });- });-- test("var", () => {- const instance = createInstance([- {- style: {- width: {- type: "var",- value: "namespace",- fallbacks: [],- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- width: "var(--namespace)",- },- });- });-- test("var with fallbacks", () => {- const instance = createInstance([- {- style: {- width: {- type: "var",- value: "namespace",- fallbacks: [- {- type: "keyword",- value: "normal",- },- {- type: "unit",- value: 10,- unit: "px",- },- ],- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- width: "var(--namespace, normal, 10px)",- },- });- });-- test("fontFamily", () => {- const instance = createInstance([- {- style: {- fontFamily: {- type: "fontFamily",- value: ["Courier New"],- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints);- expect(stitchesCss).toEqual({- "@0": {- fontFamily: "Courier New, monospace",- },- });- });-- test("withFallback option", () => {- const instance = createInstance([- {- style: {- fontFamily: {- type: "fontFamily",- value: ["Courier New"],- },- },- breakpoint: "0",- },- ]);- const stitchesCss = toCss(instance, breakpoints, { withFallback: false });- expect(stitchesCss).toEqual({- "@0": {- fontFamily: "Courier New",- },- });- });-- test("sort order based on maxWidth in breakpoints", () => {- const instance = createInstance([- {- style: {- color: {- type: "keyword",- value: "green",- },- },- breakpoint: "2",- },-- {- style: {- color: {- type: "keyword",- value: "blue",- },- },- breakpoint: "0",- },- {- style: {- color: {- type: "keyword",- value: "red",- },- },- breakpoint: "1",- },- ]);-- const stitchesCss = toCss(instance, breakpoints);- expect(Object.keys(stitchesCss)).toStrictEqual(["@0", "@1", "@2"]);- });-});diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsdeleted file mode 100644index b5b49efabe..0000000000--- a/packages/react-sdk/src/stitches/to-css.ts+++ /dev/null@@ -1,88 +0,0 @@-import type { CSS } from "./stitches";-import type { StyleProperty, StyleValue, Breakpoint } from "../css";-import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";-import { Instance } from "..";--type ToCssOptions = {- withFallback: boolean;-};--const defaultOptions = {- withFallback: true,-};--export const toValue = (- value?: StyleValue,- options: ToCssOptions = defaultOptions-): string => {- if (value === undefined) return "";- if (value.type === "unit") {- return value.value + (value.unit === "number" ? "" : value.unit);- }- if (value.type === "fontFamily") {- if (options.withFallback === false) {- return value.value[0];- }- const family = value.value[0];- const fallbacks = SYSTEM_FONTS.get(family);- if (Array.isArray(fallbacks)) {- return [...value.value, ...fallbacks].join(", ");- }- return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");- }- if (value.type === "var") {- const fallbacks = [];- for (const fallback of value.fallbacks) {- fallbacks.push(toValue(fallback, options));- }- const fallbacksString =- fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";- return `var(--${value.value}${fallbacksString})`;- }- return value.value;-};--export const toVarNamespace = (id: string, property: string) => {- return `${property}-${id}`;-};--/**- * Convert instance cssRules to a stitches CSS object.- */-export const toCss = (- instance: Instance,- breakpoints: Array<Breakpoint>,- options: ToCssOptions = defaultOptions-): CSS => {- const css: CSS = {};- const { cssRules } = instance;- const breakpointsMap: Record<Breakpoint["id"], number> = {};- for (const breakpoint of breakpoints) {- breakpointsMap[breakpoint.id] = breakpoint.minWidth;- }-- const sortedCssRules = [...cssRules].sort((ruleA, ruleB) => {- // If a rule references a breakpoint that was not found in breakpoints,- // we must have removed the breakpoint and now we fall back to 0.- const maxWidthA = breakpointsMap[ruleA.breakpoint] ?? 0;- const maxWidthB = breakpointsMap[ruleB.breakpoint] ?? 0;- return maxWidthA - maxWidthB;- });-- for (const cssRule of sortedCssRules) {- const style: CSS = {};- for (const property in cssRule.style) {- const value = cssRule.style[property as StyleProperty];- if (value === undefined) continue;- style[property] = toValue(value, options);- }-- if (cssRule.breakpoint in breakpointsMap) {- css["@" + cssRule.breakpoint] = style;- continue;- }-- Object.assign(css, style);- }- return css;-};diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.tsindex 797382133c..aae3b24513 100644--- a/packages/react-sdk/src/tree/root.ts+++ b/packages/react-sdk/src/tree/root.ts@@ -1,7 +1,6 @@import { useAllUserProps } from "../user-props/";import type { Tree, InstanceProps } from "../db";import type { Breakpoint } from "../css";-import { setBreakpoints } from "../stitches";import { createElementsTree } from "./create-elements-tree";import { WrapperComponent } from "./wrapper-component";import type { Asset } from "@webstudio-is/asset-uploader";@@ -26,7 +25,6 @@ export const InstanceRoot = ({if (data.tree === null) {throw new Error("Tree is null");}- setBreakpoints(data.breakpoints);useAllUserProps(data.props);return createElementsTree({instance: data.tree.root,diff --git a/yarn.lock b/yarn.lockindex 97eb7b56f2..df74f1ea59 100644--- a/yarn.lock+++ b/yarn.lock@@ -3854,11 +3854,6 @@dependencies:"@sinonjs/commons" "^1.7.0"-"@stitches/core@^1.2.7":- version "1.2.8"- resolved "https://registry.yarnpkg.com/@stitches/core/-/core-1.2.8.tgz#dce3b8fdc764fbc6dbea30c83b73bfb52cf96173"- integrity sha512-Gfkvwk9o9kE9r9XNBmJRfV8zONvXThnm1tcuojL04Uy5uRyqg93DC83lDebl0rocZCfKSjUv+fWYtMQmEDJldg==-"@stitches/react@^1.2.8":version "1.2.8"resolved "https://registry.yarnpkg.com/@stitches/react/-/react-1.2.8.tgz#954f8008be8d9c65c4e58efa0937f32388ce3a38"