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
fix: allow user to define a custom font stack (#4153)
Oleg Isonen0ff51ebfad91
diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.tsindex 5b5c8e9a47..e909933b8a 100644--- a/packages/css-engine/src/core/to-value.test.ts+++ b/packages/css-engine/src/core/to-value.test.ts@@ -46,7 +46,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {expect(value).toBe("var(--namespace, normal, 10px)");});- test("fontFamily stack", () => {+ test("fontFamily is known stack name", () => {expect(toValue({type: "fontFamily",@@ -57,7 +57,16 @@ describe("Convert WS CSS Values to native CSS strings", () => {);});- test("fontFamily unknown", () => {+ test("fontFamily is a custom stack", () => {+ expect(+ toValue({+ type: "fontFamily",+ value: ["DejaVu Sans Mono", "monospace"],+ })+ ).toBe('"DejaVu Sans Mono", monospace');+ });++ test("fontFamily is unknown family name", () => {expect(toValue({type: "fontFamily",@@ -66,6 +75,24 @@ describe("Convert WS CSS Values to native CSS strings", () => {).toBe("something-random, sans-serif");});+ test("fontFamily is empty", () => {+ expect(+ toValue({+ type: "fontFamily",+ value: [],+ })+ ).toBe("sans-serif");+ });++ test("fontFamily has duplicates", () => {+ expect(+ toValue({+ type: "fontFamily",+ value: ["a", "a", "b"],+ })+ ).toBe("a, b");+ });+test("Transform font family value to override default fallback", () => {const value = toValue({diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.tsindex 1d89ba899b..3f1a314bc3 100644--- a/packages/css-engine/src/core/to-value.ts+++ b/packages/css-engine/src/core/to-value.ts@@ -5,18 +5,28 @@ import type { StyleValue } from "../schema";export type TransformValue = (styleValue: StyleValue) => undefined | StyleValue;const fallbackTransform: TransformValue = (styleValue) => {- if (styleValue.type === "fontFamily") {- const fonts = SYSTEM_FONTS.get(styleValue.value[0])?.stack ?? [- styleValue.value[0],- DEFAULT_FONT_FALLBACK,- ];- const value = Array.from(new Set(fonts));-- return {- type: "fontFamily",- value,- };+ if (styleValue.type !== "fontFamily") {+ return;}++ // By default we assume its a custom font stack.+ let { value } = styleValue;++ // Shouldn't be possible, but just in case.+ if (value.length === 0) {+ value = [DEFAULT_FONT_FALLBACK];+ }++ // User provided a single name. It could be a specific font name or a stack name.+ if (value.length === 1) {+ const stack = SYSTEM_FONTS.get(value[0])?.stack;+ value = stack ?? [value[0], DEFAULT_FONT_FALLBACK];+ }++ return {+ type: "fontFamily",+ value: Array.from(new Set(value)),+ };};// Use JSON.stringify to escape double quotes and backslashes in strings as it automatically replaces " with \" and \ with \\.