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 css input (scrub, etc) Beautify options and input for css-value-input (#716)
Ivan Starkovf9996fe1952d
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 af9c68b84d..66e330ba42 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@@ -1,3 +1,4 @@+import { matchSorter } from "match-sorter";import {Box,TextField,@@ -32,6 +33,7 @@ import { parseIntermediateOrInvalidValue } from "./parse-intermediate-or-invalidimport { toValue } from "@webstudio-is/css-engine";import { useDebouncedCallback } from "use-debounce";import type { StyleSource } from "../style-info";+import { toPascalCase } from "./keyword-utils";// We increment by 10 when shift is pressed, by 0.1 when alt/option is pressed and by 1 by default.const calcNumberChange = (@@ -228,6 +230,24 @@ const initialValue: IntermediateStyleValue = {value: "",};+const itemToString = (item: CssValueInputValue | null) =>+ item === null+ ? ""+ : item.type === "keyword"+ ? toPascalCase(item.value)+ : item.type === "intermediate" || item.type === "unit"+ ? String(item.value)+ : toValue(item);++const match = <Item,>(+ search: string,+ items: Array<Item>,+ itemToString: (item: Item | null) => string+) =>+ matchSorter(items, search, {+ keys: [itemToString, (item) => itemToString(item).replace(/\s/g, "-")],+ });+/*** Common:* - Free text editing@@ -312,12 +332,8 @@ export const CssValueInput = ({items: keywords,value,selectedItem: props.value,- itemToString: (item) =>- item === null- ? ""- : item.type === "intermediate" || item.type === "unit"- ? String(item.value)- : toValue(item),+ itemToString,+ match,onInputChange: (inputValue) => {onChange(inputValue);},@@ -466,7 +482,7 @@ export const CssValueInput = ({}}onBlur={handleOnBlur}onKeyDown={handleKeyDown}- baseRef={scrubRef}+ containerRef={scrubRef}inputRef={inputRef}name={property}state={value.type === "invalid" ? "invalid" : undefined}@@ -487,7 +503,7 @@ export const CssValueInput = ({{...getItemProps({ item, index })}key={index}>- {item.type === "intermediate" ? item.value : toValue(item)}+ {itemToString(item)}</ComboboxListboxItem>))}</ComboboxListbox>diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.test.ts b/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.test.tsnew file mode 100644index 0000000000..bbe55b2c4d--- /dev/null+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.test.ts@@ -0,0 +1,34 @@+import { describe, test, expect } from "@jest/globals";+import { toKebabCase } from "./keyword-utils";++describe("toKebabCase", () => {+ test("PascalCase", () => {+ expect(toKebabCase("PascalCase")).toEqual("pascal-case");+ });++ test("Pascal Case", () => {+ expect(toKebabCase("Pascal Case")).toEqual("pascal-case");+ expect(toKebabCase("Pascal Case")).toEqual("pascal-case");+ });++ test("Pascal Case", () => {+ expect(toKebabCase("Pascal Case")).toEqual("pascal-case");+ expect(toKebabCase("Pascal Case")).toEqual("pascal-case");+ });++ test("camelCase", () => {+ expect(toKebabCase("camelCase")).toEqual("camel-case");+ });++ test("camel Case", () => {+ expect(toKebabCase("camel Case")).toEqual("camel-case");+ });++ test("kebab-case", () => {+ expect(toKebabCase("kebab-case")).toEqual("kebab-case");+ });++ test("not touches numerics", () => {+ expect(toKebabCase("02020px")).toEqual("02020px");+ });+});diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.ts b/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.tsnew file mode 100644index 0000000000..16ec6f773a--- /dev/null+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/keyword-utils.ts@@ -0,0 +1,28 @@+/**+ * Beautify a CSS keyword by capitalizing the first letter of each word,+ * and replacing dashes with spaces.+ */+export const toPascalCase = (keyword: string) => {+ const label = keyword+ .replace(/-/g, " ")+ .split(" ")+ .map((word) => {+ return word[0].toUpperCase() + word.slice(1);+ })+ .join(" ");++ return label;+};++/**+ * Try to transform something from Pascal Case / Pascal Case / camelCase / camel Case to kebab-case+ */+export const toKebabCase = (keyword: string) => {+ const label = keyword+ .trim()+ .replace(/([a-z])([A-Z])/g, "$1-$2")+ .replace(/\s+/g, "-")+ .toLowerCase();++ return label;+};diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts b/apps/designer/app/designer/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.tsindex 3d917c046a..4920c9bc76 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts@@ -4,11 +4,10 @@ import type {InvalidValue,} from "@webstudio-is/css-data";import type { IntermediateStyleValue } from "./css-value-input";-import { parseCssValue } from "../parse-css-value";-import { evaluateMath } from "./evaluate-math";import { units } from "@webstudio-is/css-data";+import { toKebabCase } from "./keyword-utils";export const parseIntermediateOrInvalidValue = (property: StyleProperty,@@ -29,6 +28,13 @@ export const parseIntermediateOrInvalidValue = (return styleInput;}+ // Probably in kebab-case value will be valid+ styleInput = parseCssValue(property, toKebabCase(value.value));++ if (styleInput.type !== "invalid") {+ return styleInput;+ }+// Try evaluate something like 10px + 4 or 13 + 4em// Try to extract/remove anything similar to unit valuediff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsxindex 6080a3de1e..fc4ef0a4c3 100644--- a/packages/design-system/src/components/combobox.tsx+++ b/packages/design-system/src/components/combobox.tsx@@ -6,6 +6,7 @@ import {type ForwardRefRenderFunction,useEffect,useRef,+ type ChangeEvent,} from "react";import { CheckIcon, ChevronDownIcon } from "@webstudio-is/icons";import { Popper, PopperContent, PopperAnchor } from "@radix-ui/react-popper";@@ -13,6 +14,7 @@ import {type DownshiftState,type UseComboboxStateChangeOptions,type UseComboboxProps as UseDownshiftComboboxProps,+ type UseComboboxGetInputPropsOptions,useCombobox as useDownshiftCombobox,} from "downshift";import { matchSorter } from "match-sorter";@@ -92,7 +94,7 @@ const defaultMatch = <Item,>(itemToString: (item: Item | null) => string) =>matchSorter(items, search, {- keys: [(item) => itemToString(item)],+ keys: [itemToString],});const useFilter = <Item,>({@@ -198,7 +200,6 @@ export const useCombobox = <Item,>({onInputValueChange({ inputValue, type }) {if (type === comboboxStateChangeTypes.InputChange) {filter(inputValue);- onInputChange?.(inputValue);}},onSelectedItemChange({ selectedItem, type }) {@@ -220,7 +221,8 @@ export const useCombobox = <Item,>({},});- const { getItemProps, highlightedIndex, getMenuProps } = downshiftProps;+ const { getItemProps, highlightedIndex, getMenuProps, getInputProps } =+ downshiftProps;useEffect(() => {if (isOpen === false) {@@ -228,6 +230,22 @@ export const useCombobox = <Item,>({}}, [isOpen, resetFilter]);+ const enhancedGetInputProps = useCallback(+ (options?: UseComboboxGetInputPropsOptions) => {+ const inputProps = getInputProps(options);+ return {+ ...inputProps,+ onChange: (event: ChangeEvent<HTMLInputElement>) => {+ inputProps.onChange(event);+ // If we want controllable input we need to call onInputChange here+ // see https://github.com/downshift-js/downshift/issues/1108+ onInputChange?.(event.target.value);+ },+ };+ },+ [getInputProps, onInputChange]+ );+const enhancedGetItemProps = useCallback((options) => {return getItemProps({@@ -259,6 +277,7 @@ export const useCombobox = <Item,>({items: filteredItems,getItemProps: enhancedGetItemProps,getMenuProps: enhancedGetMenuProps,+ getInputProps: enhancedGetInputProps,resetFilter,};};