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
Add font smoothing by default to the body (#1972)
Oleg Isonen57417c7a58c3
diff --git a/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsx b/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsxindex 37144e6daf..edc2f47305 100644--- a/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsx+++ b/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsx@@ -7,12 +7,14 @@ import {import {type StyleInfo,useStyleInfo,+ type StyleValueInfo,} from "../../style-panel/shared/style-info";import { createCssEngine } from "@webstudio-is/css-engine";import { CollapsibleSection } from "~/builder/shared/collapsible-section";import { useMemo } from "react";import * as Prism from "prismjs";import type { Style, StyleProperty } from "@webstudio-is/css-data";+import { captureError } from "@webstudio-is/error-utils";const preStyle = css(textVariants.mono, {margin: 0,@@ -21,33 +23,68 @@ const preStyle = css(textVariants.mono, {cursor: "text",});+// - Compiles a CSS string from the style info+// - Groups by category and separates categories with commentsconst getCssText = (instanceStyle: StyleInfo) => {const cssEngine = createCssEngine();- const style: Style = {};+ type StyleValueInfoMap = Map<+ StyleProperty,+ StyleValueInfo[keyof StyleValueInfo]+ >;+ const sourceStyles: StyleValueInfoMap = new Map();+ const inheritedStyles: StyleValueInfoMap = new Map();+ const cascadedStyles: StyleValueInfoMap = new Map();+ const presetStyles: StyleValueInfoMap = new Map();++ // Aggregate styles by category so we can group them when rendering.let property: StyleProperty;for (property in instanceStyle) {const value = instanceStyle[property];+ if (value === undefined) {+ continue;+ }+ if (value.local ?? value.nextSource?.value ?? value.previousSource?.value) {+ sourceStyles.set(property, value.value);+ continue;+ }+ if (value.preset) {+ presetStyles.set(property, value.value);+ continue;+ }+ if (value.cascaded?.value) {+ cascadedStyles.set(property, value.value);+ continue;+ }+ if (value.inherited?.value) {+ inheritedStyles.set(property, value.value);+ continue;+ }+ if (value.value) {+ // Doesn't need handling+ continue;+ }+ captureError(new Error("Unknown style source"), value);+ }- if (value) {- const userDefinedStyle: Style | undefined =- value.local ??- value.nextSource?.value ??- value.previousSource?.value ??- value.cascaded?.value ??- value.preset ??- value.inherited?.value;-- if (userDefinedStyle === undefined) {- continue;- }+ const result: Array<string> = [];- style[property] = value.value;+ const add = (comment: string, styles: StyleValueInfoMap) => {+ if (styles.size === 0) {+ return;}- }- const rule = cssEngine.addStyleRule("instance", {- style,- });- return rule.styleMap.toString();+ const rule = cssEngine.addStyleRule(comment, {+ style: Object.fromEntries(styles) as Style,+ });+ result.push(`/* ${comment} */`);+ result.push(rule.styleMap.toString());+ };++ add("Style Sources", sourceStyles);+ add("Cascaded", cascadedStyles);+ add("Inherited", inheritedStyles);+ add("Preset", presetStyles);++ return result.join("\n");};const useHighlightedCss = () => {diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.test.tsindex c37597b88e..ffa8db9043 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts@@ -193,6 +193,20 @@ test("compute inherited styles", () => {)).toMatchInlineSnapshot(`{+ "MozOsxFontSmoothing": {+ "instanceId": "1",+ "value": {+ "type": "keyword",+ "value": "grayscale",+ },+ },+ "WebkitFontSmoothing": {+ "instanceId": "1",+ "value": {+ "type": "keyword",+ "value": "antialiased",+ },+ },"fontFamily": {"instanceId": "1","value": {diff --git a/apps/builder/package.json b/apps/builder/package.jsonindex 6bab6cd5a3..d867bb3b89 100644--- a/apps/builder/package.json+++ b/apps/builder/package.json@@ -47,6 +47,7 @@"@webstudio-is/dashboard": "workspace:^","@webstudio-is/design-system": "workspace:^","@webstudio-is/domain": "workspace:^",+ "@webstudio-is/error-utils": "workspace:^","@webstudio-is/feature-flags": "workspace:^","@webstudio-is/fonts": "workspace:^","@webstudio-is/icons": "workspace:^",diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.tsindex 12083aa873..2eaba4e76f 100755--- a/packages/css-data/bin/mdn-data.ts+++ b/packages/css-data/bin/mdn-data.ts@@ -8,6 +8,7 @@ import properties from "mdn-data/css/properties.json";import syntaxes from "mdn-data/css/syntaxes.json";import data from "css-tree/dist/data";import { popularityIndex } from "../src/popularity-index";+import * as customData from "../src/custom-data";import camelCase from "camelcase";import type {KeywordValue,@@ -245,16 +246,7 @@ const filteredProperties: FilteredProperties = (() => {return result;})();-const propertiesData: {- // It's string because we camel-cased it- [property: string]: {- unitGroups: string[];- inherited: boolean;- initial: StyleValue;- popularity: number;- appliesTo: string;- };-} = {};+const propertiesData = { ...customData.propertiesData };const patchAppliesTo = (property: Property, config: Value) => {// see https://github.com/mdn/data/issues/585 alignItems and justifyItems have appliesTo = "allElements"@@ -344,7 +336,7 @@ const nonStandardValues = {const commonKeywords = ["initial", "inherit", "unset"];const keywordValues = (() => {- const result: Record<string, string[]> = {};+ const result = { ...customData.keywordValues };for (const property in filteredProperties) {const keywords = new Set<string>();diff --git a/packages/css-data/src/__generated__/keyword-values.ts b/packages/css-data/src/__generated__/keyword-values.tsindex 06613e19fd..16ba12491b 100644--- a/packages/css-data/src/__generated__/keyword-values.ts+++ b/packages/css-data/src/__generated__/keyword-values.ts@@ -1,5 +1,7 @@// This file was generated by pnpm mdn-dataexport const keywordValues = {+ WebkitFontSmoothing: ["auto", "none", "antialiased", "subpixel-antialiased"],+ MozOsxFontSmoothing: ["auto", "grayscale"],accentColor: ["auto","transparent",diff --git a/packages/css-data/src/__generated__/properties.ts b/packages/css-data/src/__generated__/properties.tsindex fb924cfd87..4ad72c2e30 100644--- a/packages/css-data/src/__generated__/properties.ts+++ b/packages/css-data/src/__generated__/properties.ts@@ -1,5 +1,25 @@// This file was generated by pnpm mdn-dataexport const properties = {+ WebkitFontSmoothing: {+ unitGroups: [],+ inherited: true,+ initial: {+ type: "keyword",+ value: "auto",+ },+ popularity: 0.61281794,+ appliesTo: "allElements",+ },+ MozOsxFontSmoothing: {+ unitGroups: [],+ inherited: true,+ initial: {+ type: "keyword",+ value: "auto",+ },+ popularity: 0,+ appliesTo: "allElements",+ },accentColor: {unitGroups: [],inherited: true,diff --git a/packages/css-data/src/custom-data.ts b/packages/css-data/src/custom-data.tsnew file mode 100644index 0000000000..82e805a1be--- /dev/null+++ b/packages/css-data/src/custom-data.ts@@ -0,0 +1,47 @@+import type { StyleValue } from "./schema";+import { popularityIndex } from "./popularity-index";++// Data type used before we generate a the constants.+type RawPropertyData = {+ unitGroups: string[];+ inherited: boolean;+ initial: StyleValue;+ popularity: number;+ appliesTo: string;+};++export const propertiesData: { [property: string]: RawPropertyData } = {};+export const keywordValues: { [property: string]: Array<string> } = {};++const getPopularityIndex = (property: string) =>+ popularityIndex.find((data) => data.property === property)?.dayPercentage ??+ 0;++propertiesData.WebkitFontSmoothing = {+ unitGroups: [],+ inherited: true,+ initial: {+ type: "keyword",+ value: "auto",+ },+ popularity: getPopularityIndex("webkit-font-smoothing"),+ appliesTo: "allElements",+};+keywordValues.WebkitFontSmoothing = [+ "auto",+ "none",+ "antialiased",+ "subpixel-antialiased",+];++propertiesData.MozOsxFontSmoothing = {+ unitGroups: [],+ inherited: true,+ initial: {+ type: "keyword",+ value: "auto",+ },+ popularity: getPopularityIndex("moz-osx-font-smoothing"),+ appliesTo: "allElements",+};+keywordValues.MozOsxFontSmoothing = ["auto", "grayscale"];diff --git a/packages/css-engine/package.json b/packages/css-engine/package.jsonindex 442b1156a6..bf75d8a3a4 100644--- a/packages/css-engine/package.json+++ b/packages/css-engine/package.json@@ -17,6 +17,7 @@},"dependencies": {"@webstudio-is/css-data": "workspace:^",+ "@webstudio-is/error-utils": "workspace:^","@webstudio-is/fonts": "workspace:^","hyphenate-style-name": "^1.0.4","react": "^18.2.0",diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.tsindex d632ab8b37..85f7eea845 100644--- a/packages/css-engine/src/core/rules.ts+++ b/packages/css-engine/src/core/rules.ts@@ -23,6 +23,9 @@ class StylePropertyMap {has(property: StyleProperty) {return this.#styleMap.has(property);}+ get size() {+ return this.#styleMap.size;+ }keys() {return this.#styleMap.keys();}diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.tsindex e13933175a..076f937687 100644--- a/packages/css-engine/src/core/to-value.ts+++ b/packages/css-engine/src/core/to-value.ts@@ -1,13 +1,9 @@import type { StyleValue } from "@webstudio-is/css-data";+import { captureError } from "@webstudio-is/error-utils";import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";export type TransformValue = (styleValue: StyleValue) => undefined | StyleValue;-// exhaustive check, should never happen in runtime as ts would give error-const assertUnreachable = (_arg: never, errorMessage: string) => {- throw new Error(errorMessage);-};-const fallbackTransform: TransformValue = (styleValue) => {if (styleValue.type === "fontFamily") {const firstFontFamily = styleValue.value[0];@@ -108,9 +104,5 @@ export const toValue = (return value.value.map((value) => toValue(value, transformValue)).join(" ");}- // Will give ts error in case of missing type- assertUnreachable(value, `Unknown value type`);-- // Will never happen- throw new Error("Unknown value type");+ return captureError(new Error("Unknown value type"), value);};diff --git a/packages/error-utils/LICENSE b/packages/error-utils/LICENSEnew file mode 100644index 0000000000..1f6edad9a7--- /dev/null+++ b/packages/error-utils/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/error-utils/README.md b/packages/error-utils/README.mdnew file mode 100644index 0000000000..2f33eda7b3--- /dev/null+++ b/packages/error-utils/README.md@@ -0,0 +1,21 @@+# Error utils++## API++`captureError(error, value)`++- Provides a compile error when value is not `never`+- Throws a runtime error in development synchronously to break intentionally+- Throws an error in production asynchronously to not break unintentionally++```ts+import { captureError } from "error-utils";++const fn = (value: { type: string }) => {+ if (value.type === "foo") {+ return value;+ }++ return captureError(new Error("Should never happen"), value);+};+```diff --git a/packages/error-utils/package.json b/packages/error-utils/package.jsonnew file mode 100644index 0000000000..3156b976fb--- /dev/null+++ b/packages/error-utils/package.json@@ -0,0 +1,34 @@+{+ "name": "@webstudio-is/error-utils",+ "version": "0.82.0",+ "description": "Error utils for all Webstudio packages",+ "author": "Webstudio <[email protected]>",+ "homepage": "https://webstudio.is",+ "type": "module",+ "scripts": {+ "typecheck": "tsc",+ "checks": "pnpm typecheck",+ "dev": "build-package --watch",+ "build": "build-package",+ "dts": "tsc --project tsconfig.dts.json"+ },+ "devDependencies": {+ "@webstudio-is/scripts": "workspace:^",+ "@webstudio-is/tsconfig": "workspace:^",+ "typescript": "5.1.6"+ },+ "exports": {+ "source": "./src/index.ts",+ "types": "./lib/types/index.d.ts",+ "import": "./lib/index.js",+ "require": "./lib/cjs/index.js"+ },+ "files": [+ "lib/*",+ "src/*",+ "!*.test.*"+ ],+ "license": "MIT",+ "private": false,+ "sideEffects": false+}diff --git a/packages/error-utils/src/index.ts b/packages/error-utils/src/index.tsnew file mode 100644index 0000000000..e783ce557c--- /dev/null+++ b/packages/error-utils/src/index.ts@@ -0,0 +1,18 @@+/**+ * Throws a provided error in development and makes sure error is captured in production by Sentry or any other tool we might decide to use.+ * If you pass `value` and value is not `never` it will give you a nice compile time error to statically check for branches that should never be entered.+ * Example: `if (value === "foo") { return "foo"; } else { return captureError(new Error("Should never happen"), value); }`+ */+export const captureError = <Value = undefined>(+ error: unknown,+ value: Value+) => {+ if (process.env.NODE_ENV === "development") {+ throw error;+ }+ // Let error logger catch it in production, so we don't break the UI+ setTimeout(() => {+ throw error;+ });+ return value;+};diff --git a/packages/error-utils/tsconfig.dts.json b/packages/error-utils/tsconfig.dts.jsonnew file mode 100644index 0000000000..e84928fb3a--- /dev/null+++ b/packages/error-utils/tsconfig.dts.json@@ -0,0 +1,7 @@+{+ "extends": "./tsconfig.json",+ "include": ["src"],+ "compilerOptions": {+ "declarationDir": "lib/types"+ }+}diff --git a/packages/error-utils/tsconfig.json b/packages/error-utils/tsconfig.jsonnew file mode 100644index 0000000000..d2e5040f8f--- /dev/null+++ b/packages/error-utils/tsconfig.json@@ -0,0 +1,3 @@+{+ "extends": "@webstudio-is/tsconfig/base.json"+}diff --git a/packages/sdk-components-react/src/body.ws.tsx b/packages/sdk-components-react/src/body.ws.tsxindex 707a6d1411..bb5db4d057 100644--- a/packages/sdk-components-react/src/body.ws.tsx+++ b/packages/sdk-components-react/src/body.ws.tsx@@ -10,7 +10,17 @@ import { props } from "./__generated__/body.props";import type { defaultTag } from "./body";const presetStyle = {- body,+ body: [+ ...body,+ {+ property: "WebkitFontSmoothing",+ value: { type: "keyword", value: "antialiased" },+ },+ {+ property: "MozOsxFontSmoothing",+ value: { type: "keyword", value: "grayscale" },+ },+ ],} satisfies PresetStyle<typeof defaultTag>;export const meta: WsComponentMeta = {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d1a2837907..0fb230b5bf 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -138,6 +138,9 @@ importers:'@webstudio-is/domain':specifier: workspace:^version: link:../../packages/domain+ '@webstudio-is/error-utils':+ specifier: workspace:^+ version: link:../../packages/error-utils'@webstudio-is/feature-flags':specifier: workspace:^version: link:../../packages/feature-flags@@ -496,6 +499,9 @@ importers:'@webstudio-is/css-data':specifier: workspace:^version: link:../css-data+ '@webstudio-is/error-utils':+ specifier: workspace:^+ version: link:../error-utils'@webstudio-is/fonts':specifier: workspace:^version: link:../fonts@@ -762,6 +768,18 @@ importers:specifier: 5.1.6version: 5.1.6+ packages/error-utils:+ devDependencies:+ '@webstudio-is/scripts':+ specifier: workspace:^+ version: link:../scripts+ '@webstudio-is/tsconfig':+ specifier: workspace:^+ version: link:../tsconfig+ typescript:+ specifier: 5.1.6+ version: 5.1.6+packages/eslint-config-custom:dependencies:'@typescript-eslint/eslint-plugin':