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
refactor: autocomplete variables lazily (#4220)
Bogdan Chadkin4a8a4a9a739c
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsxindex 9b0044d4e6..e682a46fb3 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx@@ -114,7 +114,7 @@ const AddPropertyOrAttribute = ({autoFocuscolor={isValid ? undefined : "error"}placeholder="Select or create"- items={availableProps}+ getItems={() => availableProps}itemToString={itemToString}onItemSelect={(item) => {if (diff --git a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsxindex d9eed291c8..b27f29a751 100644--- a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx@@ -1,4 +1,3 @@-import { useStore } from "@nanostores/react";import type { StyleProperty } from "@webstudio-is/css-engine";import { ColorPicker } from "../../shared/color-picker";import { styleConfigByName } from "../../shared/configs";@@ -7,8 +6,6 @@ import { deleteProperty, setProperty } from "../../shared/use-style-data";export const ColorControl = ({ property }: { property: StyleProperty }) => {const computedStyleDecl = useComputedStyleDecl(property);- const { items } = styleConfigByName(property);- const availableVariables = useStore($availableVariables);const value = computedStyleDecl.cascadedValue;const currentColor = computedStyleDecl.usedValue;const setValue = setProperty(property);@@ -18,11 +15,11 @@ export const ColorControl = ({ property }: { property: StyleProperty }) => {value={value}currentColor={currentColor}options={[- ...items.map((item) => ({+ ...styleConfigByName(property).items.map((item) => ({type: "keyword" as const,value: item.name,})),- ...availableVariables,+ ...$availableVariables.get(),]}onChange={(styleValue) => setValue(styleValue, { isEphemeral: true })}onChangeComplete={setValue}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.tsxindex 6354663295..e739955d17 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@@ -82,7 +82,7 @@ export const FontFamilyControl = () => {</FloatingPanel>}defaultHighlightedIndex={0}- items={items}+ getItems={() => items}itemToString={(item) => item?.label ?? item?.value ?? ""}onItemHighlight={(item) => {if (item === null) {diff --git a/apps/builder/app/builder/features/style-panel/controls/position/position-control.tsx b/apps/builder/app/builder/features/style-panel/controls/position/position-control.tsxindex e3672c60cc..1fbfeca6f1 100644--- a/apps/builder/app/builder/features/style-panel/controls/position/position-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/position/position-control.tsx@@ -110,7 +110,7 @@ export const PositionControl = ({<CssValueInputContainerproperty={property}styleSource={styleDecl.source.name}- options={keywords}+ getOptions={() => keywords}value={value.value[0]}setValue={setValueX}deleteProperty={deleteProperty}@@ -123,7 +123,7 @@ export const PositionControl = ({<CssValueInputContainerproperty={property}styleSource={styleDecl.source.name}- options={keywords}+ getOptions={() => keywords}value={value.value[1]}setValue={setValueY}deleteProperty={deleteProperty}diff --git a/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx b/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsxindex d4f58c93aa..adbeaed7db 100644--- a/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx@@ -1,5 +1,4 @@import { useState } from "react";-import { useStore } from "@nanostores/react";import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";import {CssValueInput,@@ -16,20 +15,18 @@ export const TextControl = ({ property }: { property: StyleProperty }) => {const [intermediateValue, setIntermediateValue] = useState<StyleValue | IntermediateStyleValue>();- const items = styleConfigByName(property).items;- const availableVariables = useStore($availableVariables);return (<CssValueInputstyleSource={computedStyleDecl.source.name}property={property}value={value}intermediateValue={intermediateValue}- options={[- ...items.map((item) => ({+ getOptions={() => [+ ...styleConfigByName(property).items.map((item) => ({type: "keyword" as const,value: item.name,})),- ...availableVariables,+ ...$availableVariables.get(),]}onChange={(styleValue) => {setIntermediateValue(styleValue);diff --git a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsxindex d5914a72e9..a12ea5dc8e 100644--- a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx@@ -143,7 +143,7 @@ const AdvancedSearch = ({<ComboboxautoFocusplaceholder="Find or create a property"- items={availableProperties}+ getItems={() => availableProperties}defaultHighlightedIndex={0}value={item}itemToString={(item) => item?.label ?? ""}@@ -218,8 +218,6 @@ const AdvancedPropertyValue = ({property: StyleProperty;}) => {const styleDecl = useComputedStyleDecl(property);- const availableVariables = useStore($availableVariables);- const { items } = styleConfigByName(property);const inputRef = useRef<HTMLInputElement>(null);useEffect(() => {if (autoFocus) {@@ -255,12 +253,12 @@ const AdvancedPropertyValue = ({}property={property}styleSource={styleDecl.source.name}- options={[- ...items.map((item) => ({+ getOptions={() => [+ ...styleConfigByName(property).items.map((item) => ({type: "keyword" as const,value: item.name,})),- ...availableVariables,+ ...$availableVariables.get(),]}value={styleDecl.cascadedValue}setValue={(styleValue, options) => {diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsxindex bf2b3489da..0a955ae21e 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx@@ -73,7 +73,7 @@ export const BackgroundPosition = ({ index }: { index: number }) => {<CssValueInputContainerproperty="backgroundPositionX"styleSource="default"- options={[+ getOptions={() => [{ type: "keyword", value: "center" },{ type: "keyword", value: "left" },{ type: "keyword", value: "right" },@@ -96,7 +96,7 @@ export const BackgroundPosition = ({ index }: { index: number }) => {<CssValueInputContainerproperty="backgroundPositionY"styleSource="default"- options={[+ getOptions={() => [{ type: "keyword", value: "center" },{ type: "keyword", value: "top" },{ type: "keyword", value: "bottom" },diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsxindex 0855276ef4..a179548a38 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx@@ -146,7 +146,7 @@ export const BackgroundSize = ({ index }: { index: number }) => {disabled={customSizeDisabled}property={property}styleSource="default"- options={customSizeOptions}+ getOptions={() => customSizeOptions}value={customSizeValue.value[0]}setValue={setValueX}deleteProperty={() => {}}@@ -156,7 +156,7 @@ export const BackgroundSize = ({ index }: { index: number }) => {disabled={customSizeDisabled}property={property}styleSource="default"- options={customSizeOptions}+ getOptions={() => customSizeOptions}value={customSizeValue.value[1]}setValue={setValueY}deleteProperty={() => {}}diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/border-property.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/border-property.tsxindex 6d26cb35dc..4eb296a2f7 100644--- a/apps/builder/app/builder/features/style-panel/sections/borders/border-property.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/borders/border-property.tsx@@ -85,7 +85,7 @@ export const BorderProperty = ({<CssValueInputContainerproperty={firstPropertyName}styleSource={styleValueSourceColor}- options={keywords}+ getOptions={() => keywords}value={value}setValue={(newValue, options) => {const batch = createBatchUpdate();@@ -123,7 +123,7 @@ export const BorderProperty = ({}property={styleDecl.property as StyleProperty}styleSource={styleDecl.source.name}- options={keywords}+ getOptions={() => keywords}value={styleDecl.cascadedValue}setValue={setProperty(styleDecl.property as StyleProperty)}deleteProperty={deleteProperty}diff --git a/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx b/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsxindex c675a2905b..0ed7e5049a 100644--- a/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx@@ -152,10 +152,9 @@ const GapInput = ({property={property}value={styleDecl.cascadedValue}intermediateValue={intermediateValue}- options={items.map((item) => ({- type: "keyword",- value: item.name,- }))}+ getOptions={() =>+ items.map((item) => ({ type: "keyword", value: item.name }))+ }onChange={(styleValue) => {onIntermediateChange(styleValue);if (styleValue === undefined) {diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-and-perspective-origin.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-and-perspective-origin.tsxindex 46c98cb31c..dd70ea4fa3 100644--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-and-perspective-origin.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-and-perspective-origin.tsx@@ -159,7 +159,7 @@ export const TransformAndPerspectiveOrigin = (/><CssValueInputContainervalue={origin.x}- options={xOriginKeywords}+ getOptions={() => xOriginKeywords}styleSource="local"property={fakePropertyX}deleteProperty={() => {}}@@ -186,7 +186,7 @@ export const TransformAndPerspectiveOrigin = (/><CssValueInputContainervalue={origin.y}- options={yOriginKeywords}+ getOptions={() => yOriginKeywords}styleSource="local"property={fakePropertyY}deleteProperty={() => {}}diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsxindex da59e2c149..0ac8a67ae9 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsx@@ -82,10 +82,11 @@ export const TransitionProperty = ({getMenuProps,getItemProps,} = useCombobox<NameAndLabel>({- items: properties.map((prop) => ({- name: prop,- label: prop === "transform" ? `${prop} (rotate, skew)` : prop,- })),+ getItems: () =>+ properties.map((prop) => ({+ name: prop,+ label: prop === "transform" ? `${prop} (rotate, skew)` : prop,+ })),value: { name: inputValue as AnimatableProperties, label: inputValue },selectedItem: undefined,itemToString: (value) => value?.label || "",diff --git a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsxindex 8e67d37ac6..c8b692e9ae 100644--- a/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/color-picker.tsx@@ -276,7 +276,7 @@ export const ColorPicker = ({property={property}value={value}intermediateValue={intermediateValue}- options={options}+ getOptions={() => options ?? []}onChange={(styleValue) => {if (styleValue === undefined) {setIntermediateValue(styleValue);diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input-container.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input-container.tsxindex 6ec1202791..b23ce060a5 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input-container.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input-container.tsx@@ -17,7 +17,6 @@ type CssValueInputContainerProps = {export const CssValueInputContainer = ({property,- options,setValue,deleteProperty,...props@@ -31,7 +30,6 @@ export const CssValueInputContainer = ({{...props}property={property}intermediateValue={intermediateValue}- options={options}onChange={(styleValue) => {setIntermediateValue(styleValue);diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.stories.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.stories.tsxindex 183ff33137..161e96667e 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.stories.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.stories.tsx@@ -26,7 +26,7 @@ export const WithKeywords = () => {property="width"value={value}intermediateValue={intermediateValue}- options={[+ getOptions={() => [{ type: "keyword", value: "auto" },{ type: "keyword", value: "min-content" },{ type: "keyword", value: "max-content" },@@ -67,7 +67,7 @@ export const WithIcons = () => {property="alignItems"value={value}intermediateValue={intermediateValue}- options={[+ getOptions={() => [{ type: "keyword", value: "normal" },{ type: "keyword", value: "start" },{ type: "keyword", value: "end" },@@ -113,7 +113,7 @@ export const WithUnits = () => {property="rowGap"value={value}intermediateValue={intermediateValue}- options={[+ getOptions={() => [{ type: "keyword", value: "auto" },{ type: "keyword", value: "min-content" },{ type: "keyword", value: "max-content" },diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsxindex 05f9ec20d5..0967b2ec83 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -311,7 +311,7 @@ type CssValueInputProps = Pick</*** Selected item in the dropdown*/- options?: Array<KeywordValue | VarValue>;+ getOptions?: () => Array<KeywordValue | VarValue>;onChange: (value: CssValueInputValue | undefined) => void;onChangeComplete: (event: ChangeCompleteEvent) => void;onHighlight: (value: StyleValue | undefined) => void;@@ -376,7 +376,7 @@ export const CssValueInput = ({showSuffix = true,styleSource,property,- options = [],+ getOptions = () => [],onHighlight,onAbort,disabled,@@ -446,7 +446,7 @@ export const CssValueInput = ({highlightedIndex,} = useCombobox<CssValueInputValue>({// Used for description to match the item when nothing is highlighted yet and value is still in non keyword mode- items: options,+ getItems: getOptions,value,selectedItem: props.value,itemToString,diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsxindex 64fa90b4ca..556ad3b379 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx@@ -476,7 +476,7 @@ export const StyleSourceInput = (getItemProps,isOpen,} = useCombobox<IntermediateItem>({- items: markAddedValues(props.items ?? [], value),+ getItems: () => markAddedValues(props.items ?? [], value),value: {label,disabled: false,diff --git a/packages/design-system/src/components/combobox.stories.tsx b/packages/design-system/src/components/combobox.stories.tsxindex 2144c4700e..48628e066a 100644--- a/packages/design-system/src/components/combobox.stories.tsx+++ b/packages/design-system/src/components/combobox.stories.tsx@@ -24,7 +24,7 @@ export const Basic = () => {<Combobox<string>value={value}itemToString={(item) => item ?? ""}- items={["Apple", "Banana", "Orange"]}+ getItems={() => ["Apple", "Banana", "Orange"]}onItemSelect={setValue}onChange={(value) => {setValue(value ?? "");@@ -79,7 +79,7 @@ export const Complex = () => {getInputProps,isOpen,} = useCombobox<string>({- items: ["Apple", "Banana", "Orange"],+ getItems: () => ["Apple", "Banana", "Orange"],value,selectedItem: value,itemToString: (item) => item ?? "",diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsxindex 9061fb439c..92aeebf6d7 100644--- a/packages/design-system/src/components/combobox.tsx+++ b/packages/design-system/src/components/combobox.tsx@@ -227,48 +227,10 @@ const defaultMatch = <Item,>(keys: [itemToString],});-const defaultItemToString = <Item,>(item: Item) =>- typeof item === "string" ? item : "";--const useFilter = <Item,>({- items,- itemToString = defaultItemToString,- match = defaultMatch,-}: {- items: Array<Item>;- itemToString?: (item: Item | null) => string;- match?: Match<Item>;-}) => {- const [filteredItems, setFilteredItems] = useState<Array<Item>>(items);- const cachedItems = useRef(items);-- const filter = useCallback(- (search?: string) => {- const foundItems = match(search ?? "", items, itemToString);- setFilteredItems(foundItems);- },- [itemToString, items, match]- );-- const resetFilter = useCallback(() => {- setFilteredItems(cachedItems.current);- }, []);-- useEffect(() => {- cachedItems.current = items;- }, [items]);-- return {- filteredItems,- filter,- resetFilter,- };-};-type ItemToString<Item> = (item: Item | null) => string;-type UseComboboxProps<Item> = UseDownshiftComboboxProps<Item> & {- items: Array<Item>;+type UseComboboxProps<Item> = Omit<UseDownshiftComboboxProps<Item>, "items"> & {+ getItems: () => Array<Item>;itemToString: ItemToString<Item>;getDescription?: (item: Item | null) => ReactNode;getItemProps?: (@@ -290,7 +252,7 @@ type UseComboboxProps<Item> = UseDownshiftComboboxProps<Item> & {export const comboboxStateChangeTypes = useDownshiftCombobox.stateChangeTypes;export const useCombobox = <Item,>({- items,+ getItems,value,selectedItem,getItemProps,@@ -299,50 +261,49 @@ export const useCombobox = <Item,>({onItemSelect,onItemHighlight,stateReducer = (_state, { changes }) => changes,- match,+ match = defaultMatch,defaultHighlightedIndex = -1,...rest}: UseComboboxProps<Item>) => {const [isOpen, setIsOpen] = useState(false);const selectedItemRef = useRef<Item>();-- const { filteredItems, filter, resetFilter } = useFilter<Item>({- items,- itemToString,- match,- });-- if (isOpen && filteredItems.length === 0) {- setIsOpen(false);- }+ const itemsCache = useRef<Item[]>([]);+ const [matchedItems, setMatchedItems] = useState<Item[]>([]);const downshiftProps = useDownshiftCombobox({...rest,- items: filteredItems,+ items: matchedItems,defaultHighlightedIndex,selectedItem: selectedItem ?? null, // Prevent downshift warning about switching controlled modeisOpen,onIsOpenChange({ isOpen, inputValue }) {- const foundItems =- match !== undefined- ? match(inputValue ?? "", items, itemToString)- : defaultMatch(inputValue ?? "", items, itemToString);-- // Don't set isOpen to true if there are no items to show- // because otherwise first ESC press will try to close it and only next ESC- // will reset the value. When list is empty, first ESC should reset the value.- const nextIsOpen = isOpen === true && foundItems.length !== 0;-- setIsOpen(nextIsOpen);+ if (isOpen) {+ itemsCache.current = getItems();+ const matchedItems = match(+ inputValue ?? "",+ itemsCache.current,+ itemToString+ );+ // Don't set isOpen to true if there are no items to show+ // because otherwise first ESC press will try to close it and only next ESC+ // will reset the value. When list is empty, first ESC should reset the value.+ setMatchedItems(matchedItems);+ setIsOpen(matchedItems.length > 0);+ } else {+ setMatchedItems([]);+ setIsOpen(false);+ }},stateReducer,itemToString,- inputValue: value ? itemToString(value) : undefined,+ inputValue: value ? itemToString(value) : "",onInputValueChange({ inputValue, type }) {if (type === comboboxStateChangeTypes.InputChange) {- filter(inputValue);+ setMatchedItems(+ match(inputValue ?? "", itemsCache.current, itemToString)+ );}},onSelectedItemChange({ selectedItem, type }) {@@ -361,17 +322,11 @@ export const useCombobox = <Item,>({},onHighlightedIndexChange({ highlightedIndex }) {if (highlightedIndex !== undefined) {- onItemHighlight?.(filteredItems[highlightedIndex] ?? null);+ onItemHighlight?.(matchedItems[highlightedIndex] ?? null);}},});- useEffect(() => {- if (isOpen === false) {- resetFilter();- }- }, [isOpen, resetFilter]);-useEffect(() => {// Selecting the item when the popover is closed.if (isOpen === false && selectedItemRef.current) {@@ -431,19 +386,18 @@ export const useCombobox = <Item,>({return {...downshiftGetMenuProps(options, { suppressRefError: true }),state: isOpen ? "open" : "closed",- empty: filteredItems.length === 0,+ empty: matchedItems.length === 0,};},- [downshiftGetMenuProps, isOpen, filteredItems.length]+ [downshiftGetMenuProps, isOpen, matchedItems.length]);return {...downshiftProps,- items: filteredItems,+ items: matchedItems,getItemProps: enhancedGetItemProps,getMenuProps: enhancedGetMenuProps,getInputProps: enhancedGetInputProps,- resetFilter,};};diff --git a/packages/design-system/src/components/menu.stories.tsx b/packages/design-system/src/components/menu.stories.tsxindex 7c028c3469..69d16bafb2 100644--- a/packages/design-system/src/components/menu.stories.tsx+++ b/packages/design-system/src/components/menu.stories.tsx@@ -154,7 +154,7 @@ const ComboboxDemo = () => {getMenuProps,getItemProps,} = useCombobox<Fruit>({- items: fruits,+ getItems: () => fruits,itemToString: (item) => item ?? "",value: null,selectedItem,