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.

Preview on canvas when style panel select value is highlighted (#2328)

Oleg Isonen20572ead8641
diff --git a/apps/builder/app/builder/features/style-panel/controls/font-weight/font-weight-control.tsx b/apps/builder/app/builder/features/style-panel/controls/font-weight/font-weight-control.tsx
index 9744c4b645..0a33836c70 100644
--- a/apps/builder/app/builder/features/style-panel/controls/font-weight/font-weight-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/font-weight/font-weight-control.tsx
@@ -101,6 +101,15 @@ export const FontWeightControl = ({
const setValue = setProperty(property);
+ const setFontWeight = (label: string, options?: { isEphemeral: boolean }) => {
+ const selected = availableFontWeights.find(
+ (option) => option.label === label
+ );
+ if (selected) {
+ setValue({ type: "keyword", value: selected.weight }, options);
+ }
+ };
+
return (
<Select
// show empty field instead of radix placeholder
@@ -109,12 +118,22 @@ export const FontWeightControl = ({
options={labels}
// We use a weight as a value, because there are only 9 weights and they are unique.
value={selectedLabel}
- onChange={(label) => {
- const selected = availableFontWeights.find(
- (option) => option.label === label
- );
- if (selected) {
- setValue({ type: "keyword", value: selected.weight });
+ onChange={setFontWeight}
+ onItemHighlight={(label) => {
+ // Remove preview when mouse leaves the item.
+ if (label === undefined) {
+ if (fontWeight !== undefined) {
+ setValue(fontWeight, { isEphemeral: true });
+ }
+ return;
+ }
+ // Preview on mouse enter or focus.
+ setFontWeight(label, { isEphemeral: true });
+ }}
+ onOpenChange={(isOpen) => {
+ // Remove ephemeral changes when closing the menu.
+ if (isOpen === false && fontWeight !== undefined) {
+ setValue(fontWeight, { isEphemeral: true });
}
}}
/>
diff --git a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
index 5ac65e6f6f..1663e8a8cf 100644
--- a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
@@ -16,25 +16,40 @@ export const SelectControl = ({
const setValue = setProperty(property);
const options = (items ?? defaultItems).map(({ name }) => name);
const value = toValue(styleValue);
- // append selected value when not present in the list of options
- // because radix requires values to always be in the list
+ // Append selected value when not present in the list of options
+ // because radix requires values to always be in the list.
if (options.includes(value) === false) {
options.push(value);
}
return (
<Select
- // show empty field instead of radix placeholder
- // like css value input does
+ // Show empty field instead of radix placeholder like css value input does.
placeholder=""
options={options}
- getLabel={(name) => {
- return toPascalCase(name);
- }}
+ getLabel={toPascalCase}
value={value}
onChange={(name) => {
- const value = parseCssValue(property, name);
- setValue(value);
+ const nextValue = parseCssValue(property, name);
+ setValue(nextValue);
+ }}
+ onItemHighlight={(name) => {
+ // Remove preview when mouse leaves the item.
+ if (name === undefined) {
+ if (styleValue !== undefined) {
+ setValue(styleValue, { isEphemeral: true });
+ }
+ return;
+ }
+ // Preview on mouse enter or focus.
+ const nextValue = parseCssValue(property, name);
+ setValue(nextValue, { isEphemeral: true });
+ }}
+ onOpenChange={(isOpen) => {
+ // Remove ephemeral changes when closing the menu.
+ if (isOpen === false && styleValue !== undefined) {
+ setValue(styleValue, { isEphemeral: true });
+ }
}}
/>
);
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.tsx
index eb1afc95d4..30a4a81e03 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
@@ -86,15 +86,36 @@ export const BackgroundSize = (
options={selectOptions}
getLabel={toPascalCase}
value={selectValue}
- onChange={(name) => {
+ onChange={(name: string) => {
if (name === "custom") {
- setValue({
+ return setValue({
type: "tuple",
value: [StyleKeywordAuto, StyleKeywordAuto],
});
- } else {
- const cssValue = parseCssValue(property, name);
- setValue(cssValue);
+ }
+ const cssValue = parseCssValue(property, name);
+ setValue(cssValue);
+ }}
+ onItemHighlight={(name) => {
+ // No need to preview custom size as it needs additional user input.
+ if (name === "custom") {
+ return;
+ }
+ // Remove preview when mouse leaves the item.
+ if (name === undefined) {
+ if (styleValue !== undefined) {
+ setValue(styleValue, { isEphemeral: true });
+ }
+ return;
+ }
+ // Preview on mouse enter or focus.
+ const cssValue = parseCssValue(property, name);
+ setValue(cssValue, { isEphemeral: true });
+ }}
+ onOpenChange={(isOpen) => {
+ // Remove ephemeral changes when closing the menu.
+ if (isOpen === false && styleValue !== undefined) {
+ setValue(styleValue, { isEphemeral: true });
}
}}
/>
diff --git a/packages/design-system/src/components/select.tsx b/packages/design-system/src/components/select.tsx
index 7d446df0aa..4412bc20ba 100644
--- a/packages/design-system/src/components/select.tsx
+++ b/packages/design-system/src/components/select.tsx
@@ -86,6 +86,7 @@ export type SelectProps<Option = SelectOption> = {
defaultValue?: Option;
value?: Option;
onChange?: (option: Option) => void;
+ onItemHighlight?: (value?: Option) => void;
open?: boolean;
onOpenChange?: (open: boolean) => void;
placeholder?: string;
@@ -118,6 +119,7 @@ const SelectBase = <Option,>(
placeholder = "Select an option",
onChange,
onOpenChange,
+ onItemHighlight,
open,
getLabel = defaultGetValue,
getValue = defaultGetValue,
@@ -169,6 +171,15 @@ const SelectBase = <Option,>(
key={getValue(option)}
value={getValue(option) ?? ""}
textValue={getLabel(option)}
+ onMouseEnter={() => {
+ onItemHighlight?.(option);
+ }}
+ onMouseLeave={() => {
+ onItemHighlight?.();
+ }}
+ onFocus={() => {
+ onItemHighlight?.(option);
+ }}
>
{getLabel(option)}
</SelectItem>