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
wip showing only font weights that are available
Oleg Isonenfe0d3669c470
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsxnew file mode 100644index 0000000000..0cd132a64a--- /dev/null+++ b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx@@ -0,0 +1,90 @@+import { Select } from "@webstudio-is/design-system";+import { FontWeight, fontWeights } from "@webstudio-is/fonts";+import { useMemo } from "react";+import { useAssets } from "~/designer/shared/assets";+import { getFinalValue } from "../../shared/get-final-value";+import type { ControlProps } from "../../style-sections";++const allFontWeightOptions = (+ Object.keys(fontWeights) as Array<FontWeight>+).map((weight) => ({+ label: `${fontWeights[weight].label} (${weight})`,+ name: weight,+}));++const useFontWeightOptions = (currentFamily: string) => {+ const { assets } = useAssets("font");++ const options = allFontWeightOptions.filter((option) => {+ return assets.find((asset) => {+ return (+ "meta" in asset &&+ "family" in asset.meta &&+ asset.meta.family === currentFamily &&+ String(asset.meta.weight) === option.name+ );+ });+ });++ return options.length === 0 ? allFontWeightOptions : options;+};++export const FontWeightControl = ({+ currentStyle,+ inheritedStyle,+ setProperty,+ styleConfig,+}: ControlProps) => {+ // @todo show which instance we inherited the value from+ const value = getFinalValue({+ currentStyle,+ inheritedStyle,+ property: styleConfig.property,+ });++ const family = getFinalValue({+ currentStyle,+ inheritedStyle,+ property: "fontFamily",+ });++ const fontWeightOptions = useFontWeightOptions(String(family?.value));++ const [options, selectedOptionLabel] = useMemo(() => {+ const selectedOption = fontWeightOptions.find(+ (option) => option.name == value?.value+ );+ const options = fontWeightOptions.map((option) => option.label);+ return [options, selectedOption?.label];+ }, [value?.value, fontWeightOptions]);++ if (value === undefined) return null;++ const setValue = setProperty(styleConfig.property);++ return (+ <Select+ options={options}+ value={selectedOptionLabel}+ onChange={(label) => {+ const option = fontWeightOptions.find(+ (option) => option.label == label+ );+ if (option) {+ setValue(option.name);+ }+ }}+ ghost+ css={{+ // @todo this shouldn't be in design system by default+ gap: "calc($sizes$1 / 2)",+ paddingLeft: "calc($sizes$4 / 2)",+ height: "calc($sizes$5 + $sizes$1)",+ boxShadow: "inset 0 0 0 1px $colors$slate7",+ textTransform: "capitalize",+ fontWeight: "inherit",+ "&:hover": { background: "none" },+ }}+ />+ );+};diff --git a/apps/designer/app/designer/features/style-panel/controls/index.ts b/apps/designer/app/designer/features/style-panel/controls/index.tsindex f6e4b98bb3..8d0e75c464 100644--- a/apps/designer/app/designer/features/style-panel/controls/index.ts+++ b/apps/designer/app/designer/features/style-panel/controls/index.ts@@ -3,4 +3,5 @@ export * from "./color/color-control";export * from "./select/select-control";export * from "./menu/menu-control";export * from "./font-family/font-family-control";+export * from "./font-weight/font-weight-control";export * from "./image/image-control";diff --git a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsxindex 7f11a16f29..b09942843e 100644--- a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx@@ -1,5 +1,4 @@import { Select } from "@webstudio-is/design-system";-import { useMemo } from "react";import { getFinalValue } from "../../shared/get-final-value";import type { ControlProps } from "../../style-sections";@@ -16,29 +15,17 @@ export const SelectControl = ({property: styleConfig.property,});- const [options, selectedOption] = useMemo(() => {- const item = styleConfig.items.find((item) => item.name == value?.value);- const options = styleConfig.items.map((item) => item.label);- return [options, item?.label];- }, [value?.value, styleConfig.items]);-if (value === undefined) return null;const setValue = setProperty(styleConfig.property);return (<Select- options={options}- value={selectedOption}- onChange={(option) => {- const item = styleConfig.items.find((item) => item.label == option);- if (item) {- setValue(item.name);- }- }}+ options={styleConfig.items.map(({ label }) => label)}+ value={String(value.value)}+ onChange={setValue}ghostcss={{- // @todo this shouldn't be in design system by defaultgap: "calc($sizes$1 / 2)",paddingLeft: "calc($sizes$4 / 2)",height: "calc($sizes$5 + $sizes$1)",diff --git a/apps/designer/app/designer/features/style-panel/shared/configs.ts b/apps/designer/app/designer/features/style-panel/shared/configs.tsindex f441849ca5..d0f27eaa7a 100644--- a/apps/designer/app/designer/features/style-panel/shared/configs.ts+++ b/apps/designer/app/designer/features/style-panel/shared/configs.ts@@ -39,7 +39,6 @@ import {ColumnGapIcon,} from "@webstudio-is/icons";import { isFeatureEnabled } from "~/shared/feature-flags";-import { FontWeight, fontWeights } from "@webstudio-is/fonts";type BaseStyleConfig = {label: string;@@ -54,7 +53,8 @@ export type Control =| "MenuControl"| "SelectControl"| "FontFamilyControl"- | "ImageControl";+ | "ImageControl"+ | "FontWeightControl";export type StyleConfig = BaseStyleConfig & {control: Control;@@ -62,7 +62,6 @@ export type StyleConfig = BaseStyleConfig & {};type Property = keyof typeof keywordValues;-type Keywords = typeof keywordValues[Property];const getControl = (property: StyleProperty): Control => {if (property.toLocaleLowerCase().includes("color")) {@@ -77,31 +76,13 @@ const getControl = (property: StyleProperty): Control => {return isFeatureEnabled("assets") ? "ImageControl" : "TextControl";}case "fontWeight": {- return "SelectControl";+ return "FontWeightControl";}}return "TextControl";};-const fontWeightItems = (Object.keys(fontWeights) as Array<FontWeight>).map(- (weight) => ({- label: `${weight} - ${fontWeights[weight].label}`,- name: weight,- })-);--const getItems = (property: StyleProperty, keywords: Keywords) => {- if (property === "fontWeight") {- return fontWeightItems;- }-- return keywords.map((keyword) => ({- label: keyword,- name: keyword,- }));-};-const createStyleConfigs = () => {// We have same properties in different categories: alignSelf is in grid children and flex children// but this list has to contain only unique props@@ -122,7 +103,10 @@ const createStyleConfigs = () => {property,appliesTo: properties[property].appliesTo,control: getControl(property),- items: getItems(property, keywords),+ items: keywords.map((keyword) => ({+ label: keyword,+ name: keyword,+ })),};}}diff --git a/packages/react-sdk/src/css/get-browser-style.ts b/packages/react-sdk/src/css/get-browser-style.tsindex fabb46ef6b..679f9cd9f6 100644--- a/packages/react-sdk/src/css/get-browser-style.ts+++ b/packages/react-sdk/src/css/get-browser-style.ts@@ -43,7 +43,7 @@ export const getBrowserStyle = (element?: Element): Style => {// We need a single font-family that is actually rendered. Computed style will return a list of potential fonts.browserStyle.fontFamily = {type: "fontFamily",- value: detectFont(element),+ value: [detectFont(element)],};return browserStyle;};