Skip to content

Commit 7b01153

Browse files
authoredFeb 26, 2025
experimental: Shorthands autocomplete (#4898)
ref #4816 ## Description Shows shorthands in autocomplete when adding properties. ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 06e0909 commit 7b01153

File tree

16 files changed

+620
-425
lines changed

16 files changed

+620
-425
lines changed
 

‎apps/builder/app/builder/features/style-panel/sections/advanced/add-style-input.tsx

+26-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "@webstudio-is/design-system";
1818
import {
1919
properties as propertiesData,
20+
shorthandProperties,
2021
keywordValues,
2122
propertyDescriptions,
2223
parseCssValue,
@@ -25,6 +26,7 @@ import {
2526
cssWideKeywords,
2627
generateStyleMap,
2728
hyphenateProperty,
29+
mergeStyles,
2830
toValue,
2931
type StyleProperty,
3032
} from "@webstudio-is/css-engine";
@@ -49,9 +51,17 @@ const getAutocompleteItems = () => {
4951
return autoCompleteItems;
5052
}
5153
for (const property in propertiesData) {
54+
const hyphenatedProperty = hyphenateProperty(property);
55+
autoCompleteItems.push({
56+
property: hyphenatedProperty,
57+
label: hyphenatedProperty,
58+
});
59+
}
60+
61+
for (const property of shorthandProperties) {
5262
autoCompleteItems.push({
5363
property,
54-
label: hyphenateProperty(property),
64+
label: property,
5565
});
5666
}
5767

@@ -63,10 +73,11 @@ const getAutocompleteItems = () => {
6373
if (ignoreValues.has(value)) {
6474
continue;
6575
}
76+
const hyphenatedProperty = hyphenateProperty(property);
6677
autoCompleteItems.push({
67-
property,
78+
property: hyphenatedProperty,
6879
value,
69-
label: `${hyphenateProperty(property)}: ${value}`,
80+
label: `${hyphenatedProperty}: ${value}`,
7081
});
7182
}
7283
}
@@ -91,21 +102,23 @@ const matchOrSuggestToCreate = (
91102
matched.length = Math.min(matched.length, 100);
92103

93104
if (matched.length === 0) {
94-
const parsedStyles = parseStyleInput(search);
105+
const parsedStyleMap = parseStyleInput(search);
106+
const styleMap = mergeStyles(parsedStyleMap);
107+
95108
// When parsedStyles is more than one, user entered a shorthand.
96109
// We will suggest to insert their shorthand first.
97-
if (parsedStyles.length > 1) {
110+
if (styleMap.size > 1) {
98111
matched.push({
99112
property: search,
100113
label: `Create "${search}"`,
101114
});
102115
}
103116
// Now we will suggest to insert each longhand separately.
104-
for (const style of parsedStyles) {
117+
for (const [property, value] of styleMap) {
105118
matched.push({
106-
property: style.property,
107-
value: toValue(style.value),
108-
label: `Create "${generateStyleMap(new Map([[style.property, style.value]]))}"`,
119+
property,
120+
value: toValue(value),
121+
label: `Create "${generateStyleMap(new Map([[property, value]]))}"`,
109122
});
110123
}
111124
}
@@ -114,13 +127,12 @@ const matchOrSuggestToCreate = (
114127
};
115128

116129
/**
117-
*
118130
* Advanced search control supports following interactions
119131
*
120-
* find property
121-
* create custom property
122-
* submit css declarations
123-
* paste css declarations
132+
* - find property
133+
* - create custom property
134+
* - submit css declarations
135+
* - paste css declarations
124136
*
125137
*/
126138
export const AddStyleInput = forwardRef<
@@ -232,7 +244,6 @@ export const AddStyleInput = forwardRef<
232244
<ComboboxAnchor>
233245
<InputField
234246
{...inputProps}
235-
autoFocus
236247
onFocus={onFocus}
237248
onBlur={handleBlur}
238249
inputRef={forwardedRef}
@@ -248,7 +259,6 @@ export const AddStyleInput = forwardRef<
248259
<ComboboxListboxItem
249260
{...combobox.getItemProps({ item, index })}
250261
key={index}
251-
asChild
252262
>
253263
<Text
254264
variant="labelsSentenceCase"

0 commit comments

Comments
 (0)