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
don't display empty unitless dropdown (#478)
natlusyhtb4f981f68313
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsxindex 58054efb6c..924aea89bb 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -29,12 +29,10 @@ import {} from "react";import { useIsFromCurrentBreakpoint } from "../use-is-from-current-breakpoint";import { useUnitSelect } from "./unit-select";+import { isValid, isNumericString } from "../parse-css-value";const unsetValue: UnsetValue = { type: "unset", value: "" };-const isNumericString = (input: string) =>- String(input).trim().length !== 0 && isNaN(Number(input)) === false;-// We increment by 10 when shift is pressed, by 0.1 when alt/option is pressed and by 1 by default.const calcNumberChange = (value: number,@@ -46,6 +44,7 @@ const calcNumberChange = (};const useHandleOnChange = (+ property: StyleProperty,value: StyleValue,input: string,onChange: (value: StyleValue) => void@@ -63,8 +62,13 @@ const useHandleOnChange = (if (value.type === "unit" && String(Number(input)) !== input) return;onChange?.({type: "unit",- // Use previously known unit or fallback to px.- unit: valueRef.current.type === "unit" ? valueRef.current.unit : "px",+ // Use previously known unit or fallback to the most common unit: px, if supported+ unit:+ valueRef.current.type === "unit"+ ? valueRef.current.unit+ : isValid(property, input + "px")+ ? "px"+ : "number",value: Number(input),});return;@@ -74,7 +78,7 @@ const useHandleOnChange = (type: "keyword",value: input,});- }, [input, value.type, onChange]);+ }, [property, input, value.type, onChange]);useEffect(() => {valueRef.current = value;@@ -113,24 +117,24 @@ const useScrub = ({// Since scrub is going to call onChange and onChangeComplete callbacks, it will result in a new value and potentially new callback refs.// We need this effect to ONLY run when type or unit changes, but not when callbacks or value.value changes.useEffect(() => {+ const inputRefCurrent = inputRef.current;+ const scrubRefCurrent = scrubRef.current;if (type !== "unit" ||unit === undefined ||- inputRef.current === null ||- scrubRef.current === null+ inputRefCurrent === null ||+ scrubRefCurrent === null) {return;}- const scrub = numericScrubControl(scrubRef.current, {- initialValue: valueRef.current.value,++ const value = valueRef.current.value;+ const scrub = numericScrubControl(scrubRefCurrent, {+ initialValue: value,onValueInput(event) {- onChangeRef.current({- type,- unit,- value: event.value,- });+ inputRefCurrent.value = String(event.value);setIsInputActive(true);- inputRef.current?.blur();+ inputRefCurrent.blur();},onValueChange(event) {onChangeCompleteRef.current({@@ -139,8 +143,8 @@ const useScrub = ({value: event.value,});setIsInputActive(false);- inputRef.current?.focus();- inputRef.current?.select();+ inputRefCurrent.focus();+ inputRefCurrent.select();},shouldHandleEvent: shouldHandleEvent,});@@ -254,7 +258,7 @@ export const CssValueInput = ({const inputProps = getInputProps();- useHandleOnChange(value, inputProps.value, onChange);+ useHandleOnChange(property, value, inputProps.value, onChange);const [isUnitsOpen, unitSelectElement] = useUnitSelect({property,@@ -303,19 +307,25 @@ export const CssValueInput = ({</CssValueInputIconButton>);+ const keywordButtonElement = (+ <TextFieldIconButton+ {...getToggleButtonProps()}+ state={isOpen ? "active" : undefined}+ >+ <ChevronDownIcon />+ </TextFieldIconButton>+ );+ const hasItems = items.length !== 0;+ const isUnitValue = value.type === "unit";+ const isKeywordValue = value.type === "keyword" && hasItems;const suffixRef = useRef<HTMLDivElement | null>(null);const suffix = (<Box ref={suffixRef}>- {value.type === "keyword" ? (- <TextFieldIconButton- {...getToggleButtonProps()}- state={isOpen ? "active" : undefined}- >- <ChevronDownIcon />- </TextFieldIconButton>- ) : value.type === "unit" ? (- unitSelectElement- ) : null}+ {isUnitValue+ ? unitSelectElement+ : isKeywordValue+ ? keywordButtonElement+ : null}</Box>);diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsxindex b5276b02fe..227f7c51d3 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx@@ -65,7 +65,8 @@ export const useUnitSelect = ({if (value === undefined ||renderUnits == undefined ||- renderValue === undefined+ renderValue === undefined ||+ renderUnits.length < 2) {return [isOpen, null];}diff --git a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts b/apps/designer/app/designer/features/style-panel/shared/get-final-value.tsindex bd0f47f32d..e429616d50 100644--- a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts+++ b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts@@ -1,7 +1,7 @@import type { Style, StyleValue, StyleProperty } from "@webstudio-is/css-data";import { toValue } from "@webstudio-is/css-engine";import type { InheritedStyle } from "./get-inherited-style";-import { isValid } from "./parse-css-value";+import { isValid, isNumericString } from "./parse-css-value";// @todo expose which instance we inherited the value fromexport const getFinalValue = ({@@ -19,6 +19,12 @@ export const getFinalValue = ({if (currentValue?.value === "inherit" && inheritedValue !== undefined) {return inheritedValue;}+ if (+ currentValue?.type !== "unit" &&+ isNumericString(String(currentValue?.value))+ ) {+ return { value: Number(currentValue?.value), type: "unit", unit: "number" };+ }if (currentValue &¤tValue.type === "unit" &&diff --git a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.tsindex 16d253c65d..44a1e5dfc4 100644--- a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts+++ b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts@@ -4,6 +4,9 @@ import { units } from "@webstudio-is/css-data";const unitRegex = new RegExp(`${[...units, "number"].join("|")}`);+export const isNumericString = (input: string) =>+ String(input).trim().length !== 0 && isNaN(Number(input)) === false;+export const isValid = (property: string, value: string): boolean => {// Only browsers with houdini api will provide validation for now// @todo add a polyfill maybediff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsxindex 9b9dfb1f22..121c6f6ad6 100644--- a/packages/design-system/src/components/text-field.tsx+++ b/packages/design-system/src/components/text-field.tsx@@ -36,6 +36,7 @@ const textFieldIconBaseStyle = css({export const TextFieldIconButton = styled("button",{+ background: "none",border: "none",boxSizing: "border-box",fontFamily: "inherit",