Skip to content

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.

feat: Improve search and new property autocompletion in advanced style panel (#4979)

Oleg Isonen9133c7296179
diff --git a/apps/builder/app/builder/shared/css-editor/add-style-input.tsx b/apps/builder/app/builder/shared/css-editor/add-style-input.tsx
index ece493abc9..8322f24677 100644
--- a/apps/builder/app/builder/shared/css-editor/add-style-input.tsx
+++ b/apps/builder/app/builder/shared/css-editor/add-style-input.tsx
@@ -37,7 +37,12 @@ import {
import { composeEventHandlers } from "~/shared/event-utils";
import { parseStyleInput } from "./parse-style-input";
-type SearchItem = { property: string; label: string; value?: string };
+type SearchItem = {
+ property: string;
+ label: string;
+ value?: string;
+ key: string;
+};
const autoCompleteItems: Array<SearchItem> = [];
@@ -54,15 +59,18 @@ const getAutocompleteItems = () => {
return autoCompleteItems;
}
for (const property in propertiesData) {
- const hyphenatedProperty = hyphenateProperty(property);
autoCompleteItems.push({
- property: hyphenatedProperty,
- label: hyphenatedProperty,
+ // Allow matching "gr te co" -> "grid-template-columns"
+ key: property.replaceAll("-", " "),
+ property,
+ label: property,
});
}
for (const property of shorthandProperties) {
autoCompleteItems.push({
+ // Allow matching "gr te co" -> "grid-template-columns"
+ key: property.replaceAll("-", " "),
property,
label: property,
});
@@ -78,6 +86,8 @@ const getAutocompleteItems = () => {
}
const hyphenatedProperty = hyphenateProperty(property);
autoCompleteItems.push({
+ // Allow matching "gr te co" -> "grid-template-columns"
+ key: `${hyphenatedProperty.replaceAll("-", " ")} ${value}`,
property: hyphenatedProperty,
value,
label: `${hyphenatedProperty}: ${value}`,
@@ -92,13 +102,9 @@ const getAutocompleteItems = () => {
return autoCompleteItems;
};
-const matchOrSuggestToCreate = (
- search: string,
- items: Array<SearchItem>,
- itemToString: (item: SearchItem) => string
-) => {
+const matchOrSuggestToCreate = (search: string, items: Array<SearchItem>) => {
const matched = matchSorter(items, search, {
- keys: [itemToString],
+ keys: ["key"],
});
// Limit the array to 100 elements
@@ -112,6 +118,7 @@ const matchOrSuggestToCreate = (
// We will suggest to insert their shorthand first.
if (styleMap.size > 1) {
matched.push({
+ key: "",
property: search,
label: `Create "${search}"`,
});
@@ -119,6 +126,7 @@ const matchOrSuggestToCreate = (
// Now we will suggest to insert each longhand separately.
for (const [property, value] of styleMap) {
matched.push({
+ key: "",
property,
value: toValue(value),
label: `Create "${generateStyleMap(new Map([[property, value]]))}"`,
@@ -150,6 +158,7 @@ export const AddStyleInput = forwardRef<
const [item, setItem] = useState<SearchItem>({
property: "",
label: "",
+ key: "",
});
const highlightedItemRef = useRef<SearchItem>();
@@ -160,7 +169,13 @@ export const AddStyleInput = forwardRef<
defaultHighlightedIndex: 0,
getItemProps: () => ({ text: "sentence" }),
match: matchOrSuggestToCreate,
- onChange: (value) => setItem({ property: value ?? "", label: value ?? "" }),
+ onChange: (input) => {
+ return setItem({
+ property: input ?? "",
+ label: input ?? "",
+ key: input ?? "",
+ });
+ },
onItemSelect: (item) => {
clear();
// When there is no value, property can be:
@@ -198,7 +213,7 @@ export const AddStyleInput = forwardRef<
const inputProps = combobox.getInputProps();
const clear = () => {
- setItem({ property: "", label: "" });
+ setItem({ property: "", label: "", key: "" });
};
const handleEnter = (event: KeyboardEvent) => {
diff --git a/apps/builder/app/builder/shared/css-editor/css-editor.tsx b/apps/builder/app/builder/shared/css-editor/css-editor.tsx
index ef36439c62..0f2a056409 100644
--- a/apps/builder/app/builder/shared/css-editor/css-editor.tsx
+++ b/apps/builder/app/builder/shared/css-editor/css-editor.tsx
@@ -392,11 +392,16 @@ export const CssEditor = ({
const styles = [];
for (const [property, value] of styleMap) {
- styles.push({ property, value: toValue(value) });
+ styles.push({
+ // Allows searching for property or value and using something like this:
+ // "gr te co" -> "grid-template-columns"
+ key: `${property.replaceAll("-", " ")} ${toValue(value)}`,
+ property,
+ });
}
const matched = matchSorter(styles, search, {
- keys: ["property", "value"],
+ keys: ["key"],
}).map(({ property }) => property);
setSearchProperties(matched);