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 fontFamily css value type, render fallback fonts for system and uploaded fonts
Oleg Isonene1e2f6e53a05
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.tsindex a6f16d414b..0b2d3b3906 100644--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts@@ -104,6 +104,9 @@ export const useStyleData = ({const currentValue = currentStyle[property];const defaultUnit =currentValue?.type === "unit" ? currentValue?.unit : undefined;+ if (property === "fontFamily") {+ return { type: "fontFamily" as const, value: [value] };+ }return parseCssValue(property, value, defaultUnit);};diff --git a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsxindex d0cd377cf2..065f2d700c 100644--- a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx+++ b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx@@ -20,7 +20,7 @@ import { DotsHorizontalIcon } from "@webstudio-is/icons";const getItems = (assets: Array<Asset | PreviewAsset>): Array<{ label: string }> => {- const system = SYSTEM_FONTS.map((item) => ({ label: item.family }));+ const system = Object.keys(SYSTEM_FONTS).map((label) => ({ label }));// We can have 2+ assets with the same family name, so we use a map to dedupe.const uploaded = new Map();for (const asset of assets) {@@ -105,7 +105,7 @@ export const FontsManager = ({ value, onChange }: FontsManagerProps) => {)}renderPopperContent={(props) => <>{props.children}</>}renderItem={(props) => (- // @ts-ignore temp+ // @ts-expect-error temp<ComboboxListboxItem{...props}suffix={diff --git a/packages/fonts/src/constants.ts b/packages/fonts/src/constants.tsindex 4405e36007..42b34cc09a 100644--- a/packages/fonts/src/constants.ts+++ b/packages/fonts/src/constants.ts@@ -1,23 +1,11 @@import type { FontFormat } from "./types";-export const SYSTEM_FONTS = [- {- family: "Arial",- fallbacks: ["sans-serif"],- },- {- family: "Times New Roman",- fallbacks: ["serif"],- },- {- family: "Courier New",- fallbacks: ["monospace"],- },- {- family: "system-ui",- fallbacks: ["system-ui"],- },-] as const;+export const SYSTEM_FONTS: Record<string, Array<string>> = {+ Arial: ["sans-serif"],+ "Times New Roman": ["serif"],+ "Courier New": ["monospace"],+ "system-ui": [],+};export const DEFAULT_FONT_FALLBACK = "sans-serif";diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.jsonindex 985e303844..4ca285d0ea 100644--- a/packages/react-sdk/package.json+++ b/packages/react-sdk/package.json@@ -53,7 +53,6 @@"@remix-run/react": "^1.2.3","@remix-run/server-runtime": "^1.2.3","@storybook/react": "^6.5.6",- "@webstudio-is/fonts": "*","react": "^17.0.2","react-dom": "^17.0.2"},@@ -62,6 +61,7 @@"@webstudio-is/asset-uploader": "^*","@webstudio-is/icons": "*","@webstudio-is/jest-config": "*",+ "@webstudio-is/fonts": "*","immer": "^9.0.12","mitt": "^3.0.0","react-nano-state": "^0.4.0",diff --git a/packages/react-sdk/src/css/schema.ts b/packages/react-sdk/src/css/schema.tsindex 4cd2443d10..e8d1e8ae27 100644--- a/packages/react-sdk/src/css/schema.ts+++ b/packages/react-sdk/src/css/schema.ts@@ -15,6 +15,11 @@ const KeywordValueSchema = z.object({value: z.string(),});+const FontFamilyValueSchema = z.object({+ type: z.literal("fontFamily"),+ value: z.array(z.string()),+});+// We want to be able to render the invalid value// and show it is invalid visually, without saving it to the dbconst InvalidValueSchema = z.object({@@ -26,6 +31,7 @@ export const StyleValueSchema = z.union([UnitValueSchema,KeywordValueSchema,InvalidValueSchema,+ FontFamilyValueSchema,]);export const StyleSchema = z.record(z.string(), StyleValueSchema);diff --git a/packages/react-sdk/src/stitches/to-css.test.ts b/packages/react-sdk/src/stitches/to-css.test.tsindex 7e70a477f1..dde8f51b09 100644--- a/packages/react-sdk/src/stitches/to-css.test.ts+++ b/packages/react-sdk/src/stitches/to-css.test.ts@@ -10,7 +10,7 @@ const breakpoints: Array<Breakpoint> = initialBreakpoints.map();describe("Convert WS CSS rules to stitches", () => {- test("basic", () => {+ test("keyword", () => {const cssRules: Array<CssRule> = [{style: {@@ -30,6 +30,26 @@ describe("Convert WS CSS rules to stitches", () => {});});+ test("fontFamily", () => {+ const cssRules: Array<CssRule> = [+ {+ style: {+ fontFamily: {+ type: "fontFamily",+ value: ["Courier New"],+ },+ },+ breakpoint: "0",+ },+ ];+ const stitchesCss = toCss(cssRules, breakpoints);+ expect(stitchesCss).toEqual({+ "@0": {+ fontFamily: "Courier New, monospace",+ },+ });+ });+test("sort order based on maxWidth in breakpoints", () => {const cssRules: Array<CssRule> = [{diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsindex 5c4420caa5..7abfd55129 100644--- a/packages/react-sdk/src/stitches/to-css.ts+++ b/packages/react-sdk/src/stitches/to-css.ts@@ -1,10 +1,18 @@import type { CSS } from "./css";import type { StyleProperty, StyleValue, CssRule, Breakpoint } from "../css";+import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";export const toValue = (value: StyleValue): string => {if (value.type === "unit") {return value.value + (value.unit === "number" ? "" : value.unit);}+ if (value.type === "fontFamily") {+ const family = value.value[0];+ if (family in SYSTEM_FONTS) {+ return [...value.value, ...SYSTEM_FONTS[family]].join(", ");+ }+ return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");+ }return value.value;};