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
initial css engine
Oleg Isonen9a8a3021bf78
diff --git a/packages/css-engine/LICENSE b/packages/css-engine/LICENSEnew file mode 100644index 0000000000..1f6edad9a7--- /dev/null+++ b/packages/css-engine/LICENSE@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2022 Webstudio++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/packages/css-engine/README.md b/packages/css-engine/README.mdnew file mode 100644index 0000000000..5837fc07c9--- /dev/null+++ b/packages/css-engine/README.md@@ -0,0 +1,4 @@+## CSS Engine++This package is designed to render effectively CSS in the format produced by Webstudio Designer.+It is using CSS variables for all dynamic parts and can render at runtime as for usage on Live Canvas in the Designer as well as for extracting a fully static style sheet for production.diff --git a/packages/css-engine/jest.config.js b/packages/css-engine/jest.config.jsnew file mode 100644index 0000000000..4eeecbaf4e--- /dev/null+++ b/packages/css-engine/jest.config.js@@ -0,0 +1 @@+module.exports = require("@webstudio-is/jest-config");diff --git a/packages/css-engine/package.json b/packages/css-engine/package.jsonnew file mode 100644index 0000000000..11a2ee1a2c--- /dev/null+++ b/packages/css-engine/package.json@@ -0,0 +1,40 @@+{+ "name": "@webstudio-is/css-engine",+ "version": "0.1.0",+ "description": "CSS Renderer for Webstudio",+ "author": "Webstudio <[email protected]>",+ "homepage": "https://webstudio.is",+ "scripts": {+ "typecheck": "tsc --noEmit",+ "checks": "yarn typecheck && yarn lint && yarn test",+ "dev": "tsup --watch",+ "build": "rm -fr lib tsconfig.tsbuildinfo && tsc",+ "test": "jest",+ "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"+ },+ "dependencies": {+ "@webstudio-is/react-sdk": "*"+ },+ "devDependencies": {+ "@webstudio-is/jest-config": "*",+ "tsup": "^6.1.3",+ "typescript": "4.7.4"+ },+ "main": "lib/index.js",+ "types": "lib/index.d.ts",+ "files": [+ "lib/*",+ "README.md",+ "!*.test.*"+ ],+ "license": "MIT",+ "private": false,+ "sideEffects": false,+ "tsup": {+ "entry": [+ "src/index.ts"+ ],+ "format": "cjs",+ "outDir": "lib"+ }+}diff --git a/packages/css-engine/src/index.ts b/packages/css-engine/src/index.tsnew file mode 100644index 0000000000..6511200d6e--- /dev/null+++ b/packages/css-engine/src/index.ts@@ -0,0 +1 @@+export { StyleSheet } from "./style-sheet";diff --git a/packages/css-engine/src/style-sheet.test.ts b/packages/css-engine/src/style-sheet.test.tsnew file mode 100644index 0000000000..952b30c695--- /dev/null+++ b/packages/css-engine/src/style-sheet.test.ts@@ -0,0 +1,21 @@+import { StyleSheet } from "./style-sheet";++describe("StyleSheet", () => {+ describe("basics", () => {+ test("rule with multiple properties", () => {+ const sheet = new StyleSheet();+ sheet.addRules([+ {+ style: {+ display: { type: "keyword", value: "block" },+ color: { type: "keyword", value: "red" },+ },+ breakpoint: "0",+ },+ ]);+ expect(sheet.toString()).toMatchInlineSnapshot(+ `".s0 { display: block; color: red }"`+ );+ });+ });+});diff --git a/packages/css-engine/src/style-sheet.ts b/packages/css-engine/src/style-sheet.tsnew file mode 100644index 0000000000..5149e6569e--- /dev/null+++ b/packages/css-engine/src/style-sheet.ts@@ -0,0 +1,62 @@+import {+ Breakpoint,+ CssRule,+ Style,+ StyleValue,+ toValue,+} from "@webstudio-is/react-sdk";++let ruleCounter = -1;++class Rule {+ style: Style;+ // @todo name can be composition token name or component name+ name: string = "s";+ constructor(style: Style) {+ this.style = style;+ }+ toString() {+ const block = [];+ let property: keyof Style;+ for (property in this.style) {+ const value = this.style[property];+ if (value === undefined) continue;+ block.push(`${property}: ${toValue(value)}`);+ }+ const selector = `${this.name}${++ruleCounter}`;+ return `.${selector} { ${block.join("; ")} }`;+ }+}++const createRule = (rule: CssRule) => new Rule(rule.style);++export class StyleSheet {+ rules: Array<Rule> = [];+ breakpoints: Array<Breakpoint> = [];+ style?: HTMLStyleElement;+ addBreakpoints(breakpoints: Array<Breakpoint>) {+ this.breakpoints.push(...breakpoints);+ }+ addRules(rules: Array<CssRule>) {+ // @todo throw if referenced breakpoint doesn't exist?+ this.rules.push(...rules.map(createRule));+ }+ mount() {+ if (this.style !== undefined) {+ throw new Error("StyleSheet already mounted");+ }+ this.style = document.createElement("style");+ this.style.textContent = this.toString();+ document.head.appendChild(this.style);+ }+ unmount() {+ this.style?.parentElement?.removeChild(this.style);+ }+ toString() {+ const css = [];+ for (const rule of this.rules) {+ css.push(rule.toString());+ }+ return css.join("\n");+ }+}diff --git a/packages/css-engine/tsconfig.json b/packages/css-engine/tsconfig.jsonnew file mode 100644index 0000000000..26ae4e3c56--- /dev/null+++ b/packages/css-engine/tsconfig.json@@ -0,0 +1,19 @@+{+ // Extends can't use node's resolution rn https://github.com/dividab/tsconfig-paths/issues/209#issuecomment-1177261091+ "extends": "../../node_modules/@webstudio-is/tsconfig/base.json",+ "include": ["./src/**/*.ts"],+ "compilerOptions": {+ "module": "commonjs",+ "target": "ES2020",+ "strict": true,+ "baseUrl": ".",+ "outDir": "./lib",+ "rootDir": "./src"+ },+ // this is auto-generated by update-ts-project-references.ts+ "references": [+ {+ "path": "../react-sdk"+ }+ ]+}diff --git a/tsconfig.json b/tsconfig.jsonindex 17d272b593..29691a4bb6 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -4,6 +4,7 @@"references": [{ "path": "apps/designer" },{ "path": "packages/asset-uploader" },+ { "path": "packages/css-engine" },{ "path": "packages/css-vars" },{ "path": "packages/design-system" },{ "path": "packages/fonts" },@@ -11,6 +12,9 @@{ "path": "packages/icons" },{ "path": "packages/prisma-client" },{ "path": "packages/project" },- { "path": "packages/react-sdk" }++ {+ "path": "packages/react-sdk"+ }]}