Skip to content

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.

fix: Font familes with a space were not rendering correctly (#2860)

Oleg Isonende7f76493247
diff --git a/apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx b/apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx
index c8dc26968d..1853bf3224 100644
--- a/apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx
@@ -29,7 +29,8 @@ export const FontFamilyControl = ({
onOpenChange={setIsOpen}
>
<DeprecatedTextField
- defaultValue={toValue(value)}
+ // Replacing the quotes just to make it look cleaner in the UI
+ defaultValue={toValue(value).replace(/"/, "")}
state={isOpen ? "active" : undefined}
/>
</FloatingPanel>
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index cbf2508fe3..a5f3ff1b1b 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -180,9 +180,12 @@ export class FontFaceRule {
const decls = [];
const { fontFamily, fontStyle, fontWeight, fontDisplay, src } =
this.options;
- decls.push(
- `font-family: ${/\s/.test(fontFamily) ? `"${fontFamily}"` : fontFamily}`
+ const value = toValue(
+ { type: "fontFamily", value: [fontFamily] },
+ // Avoids adding a fallback automatically which needs to happen for font family in general but not for font face.
+ (value) => value
);
+ decls.push(`font-family: ${value}`);
decls.push(`font-style: ${fontStyle}`);
decls.push(`font-weight: ${fontWeight}`);
decls.push(`font-display: ${fontDisplay}`);
diff --git a/packages/css-engine/src/core/style-sheet-regular.test.ts b/packages/css-engine/src/core/style-sheet-regular.test.ts
index cf53467732..5b6a522160 100644
--- a/packages/css-engine/src/core/style-sheet-regular.test.ts
+++ b/packages/css-engine/src/core/style-sheet-regular.test.ts
@@ -451,19 +451,19 @@ describe("Style Sheet Regular", () => {
`);
});
- test("font family rule", () => {
+ test("font family rule with space in the name", () => {
sheet.addFontFaceRule({
- fontFamily: "Roboto",
+ fontFamily: "Some Font",
fontStyle: "normal",
fontWeight: 400,
fontDisplay: "swap",
src: "url(/src)",
});
expect(sheet.cssText).toMatchInlineSnapshot(`
- "@font-face {
- font-family: Roboto; font-style: normal; font-weight: 400; font-display: swap; src: url(/src);
- }"
- `);
+"@font-face {
+ font-family: "Some Font"; font-style: normal; font-weight: 400; font-display: swap; src: url(/src);
+}"
+`);
});
test("clear", () => {
diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.ts
index 5dab921abb..7ace3985d7 100644
--- a/packages/css-engine/src/core/to-value.test.ts
+++ b/packages/css-engine/src/core/to-value.test.ts
@@ -51,7 +51,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {
type: "fontFamily",
value: ["Courier New"],
});
- expect(value).toBe("Courier New, monospace");
+ expect(value).toBe('"Courier New", monospace');
});
test("Transform font family value to override default fallback", () => {
@@ -69,7 +69,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {
}
}
);
- expect(value).toBe("Courier New");
+ expect(value).toBe('"Courier New"');
});
test("array", () => {
diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.ts
index 1f4ae40651..c0d9856c90 100644
--- a/packages/css-engine/src/core/to-value.ts
+++ b/packages/css-engine/src/core/to-value.ts
@@ -39,7 +39,11 @@ export const toValue = (
return value.value + (value.unit === "number" ? "" : value.unit);
}
if (value.type === "fontFamily") {
- return value.value.join(", ");
+ const families = [];
+ for (const family of value.value) {
+ families.push(family.includes(" ") ? `"${family}"` : family);
+ }
+ return families.join(", ");
}
if (value.type === "var") {
const fallbacks = [];