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.

refactor: migrate select control to style object model (#4025)

Bogdan Chadkin6d5f18c87d08
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 e06d8d470a..cc3eecb4d1 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
@@ -1,25 +1,24 @@
import { declarationDescriptions, parseCssValue } from "@webstudio-is/css-data";
-import { toValue } from "@webstudio-is/css-engine";
+import { toValue, type StyleProperty } from "@webstudio-is/css-engine";
import { Box, Select, theme } from "@webstudio-is/design-system";
import { styleConfigByName } from "../../shared/configs";
import { toKebabCase } from "../../shared/keyword-utils";
-import type { ControlProps } from "../types";
+import { useComputedStyleDecl } from "../../shared/model";
+import { resetEphemeralStyles, setProperty } from "../../shared/use-style-data";
export const SelectControl = ({
property,
- currentStyle,
- setProperty,
- deleteProperty,
- items,
- isAdvanced,
-}: ControlProps) => {
- const { items: defaultItems } = styleConfigByName(property);
- const styleValue = currentStyle[property]?.value;
+ items = styleConfigByName(property).items,
+}: {
+ property: StyleProperty;
+ items?: Array<{ label: string; name: string }>;
+}) => {
+ const computedStyleDecl = useComputedStyleDecl(property);
const setValue = setProperty(property);
- const options = (items ?? defaultItems).map(({ name }) => name);
+ const options = items.map(({ name }) => name);
// We can't render an empty string as a value when display was added but without a value.
// One case is when advanced property is being added, but no value is set.
- const value = toValue(styleValue) || "empty";
+ const value = toValue(computedStyleDecl.cascadedValue) || "empty";
// Append selected value when not present in the list of options
// because radix requires values to always be in the list.
@@ -29,7 +28,6 @@ export const SelectControl = ({
return (
<Select
- disabled={isAdvanced}
// Show empty field instead of radix placeholder like css value input does.
placeholder=""
options={options}
@@ -42,9 +40,7 @@ export const SelectControl = ({
onItemHighlight={(name) => {
// Remove preview when mouse leaves the item.
if (name === undefined) {
- if (styleValue !== undefined) {
- setValue(styleValue, { isEphemeral: true });
- }
+ setValue(computedStyleDecl.usedValue, { isEphemeral: true });
return;
}
// Preview on mouse enter or focus.
@@ -53,8 +49,8 @@ export const SelectControl = ({
}}
onOpenChange={(isOpen) => {
// Remove ephemeral changes when closing the menu.
- if (isOpen === false && styleValue !== undefined) {
- deleteProperty(property, { isEphemeral: true });
+ if (isOpen === false) {
+ resetEphemeralStyles();
}
}}
getDescription={(option) => {
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
index b60ccfadc0..b9980655f1 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
@@ -183,12 +183,7 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
properties={["backgroundClip"]}
/>
- <SelectControl
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- currentStyle={currentStyle}
- property="backgroundClip"
- />
+ <SelectControl property="backgroundClip" />
<PropertyInlineLabel
label="Origin"
@@ -196,12 +191,7 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
properties={["backgroundOrigin"]}
/>
- <SelectControl
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- currentStyle={currentStyle}
- property="backgroundOrigin"
- />
+ <SelectControl property="backgroundOrigin" />
</Grid>
<Spacer />
@@ -313,12 +303,7 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
properties={["backgroundBlendMode"]}
/>
- <SelectControl
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- currentStyle={currentStyle}
- property="backgroundBlendMode"
- />
+ <SelectControl property="backgroundBlendMode" />
</Grid>
</BackgroundSection>
<Separator css={{ gridColumn: "span 2" }} />
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.tsx
index 1dfcd2390e..930d8abc28 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
@@ -432,12 +432,6 @@ if (isFeatureEnabled("displayContents")) {
orderedDisplayValues.push("contents");
}
-const compareDisplayValues = (a: { name: string }, b: { name: string }) => {
- const aIndex = orderedDisplayValues.indexOf(a.name);
- const bIndex = orderedDisplayValues.indexOf(b.name);
- return aIndex - bIndex;
-};
-
export const properties = [
"display",
"flexDirection",
@@ -451,13 +445,11 @@ export const properties = [
export const Section = ({
currentStyle,
- setProperty,
deleteProperty,
createBatchUpdate,
}: SectionProps) => {
const value = toValue(currentStyle.display?.value);
- const { items } = styleConfigByName("display");
return (
<CollapsibleSection
label="Layout"
@@ -479,12 +471,7 @@ export const Section = ({
/>
<SelectControl
property="display"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- items={items
- .filter((item) => orderedDisplayValues.includes(item.name))
- .sort(compareDisplayValues)}
+ items={orderedDisplayValues.map((name) => ({ name, label: name }))}
/>
</Grid>
diff --git a/apps/builder/app/builder/features/style-panel/sections/list-item.tsx b/apps/builder/app/builder/features/style-panel/sections/list-item.tsx
index d7e17acf66..60a7712e90 100644
--- a/apps/builder/app/builder/features/style-panel/sections/list-item.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/list-item.tsx
@@ -8,11 +8,7 @@ import { PropertyLabel } from "../property-label";
export const properties = ["listStyleType"] satisfies Array<StyleProperty>;
-export const Section = ({
- currentStyle: style,
- setProperty,
- deleteProperty,
-}: SectionProps) => {
+export const Section = ({ currentStyle: style }: SectionProps) => {
return (
<CollapsibleSection
label="List Item"
@@ -25,12 +21,7 @@ export const Section = ({
description={propertyDescriptions.listStyleType}
properties={["listStyleType"]}
/>
- <SelectControl
- property={"listStyleType"}
- currentStyle={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SelectControl property="listStyleType" />
</Grid>
</CollapsibleSection>
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx b/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
index 6aad599e10..7720cc796f 100644
--- a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
@@ -72,13 +72,7 @@ export const Section = ({
description={propertyDescriptions.position}
properties={["position"]}
/>
- <SelectControl
- property={"position"}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- items={positionItems}
- />
+ <SelectControl property="position" items={positionItems} />
{showZindexControl && showPositionControls === false && (
<>
<PropertyLabel
diff --git a/apps/builder/app/builder/features/style-panel/sections/size/size.tsx b/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
index ea07de32a7..5bef4d8a65 100644
--- a/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
@@ -243,12 +243,7 @@ export const Section = ({
description={propertyDescriptions.objectFit}
properties={["objectFit"]}
/>
- <SelectControl
- property="objectFit"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SelectControl property="objectFit" />
<PropertyLabel
label="Object Position"
description={propertyDescriptions.objectPosition}
diff --git a/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx b/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
index 87143814c1..c7d38b1e23 100644
--- a/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
@@ -355,8 +355,7 @@ const advancedProperties: StyleProperty[] = [
];
export const TypographySectionAdvancedPopover = (props: SectionProps) => {
- const { deleteProperty, setProperty, createBatchUpdate, currentStyle } =
- props;
+ const { createBatchUpdate, currentStyle } = props;
return (
<FloatingPanel
title="Advanced Typography"
@@ -374,12 +373,7 @@ export const TypographySectionAdvancedPopover = (props: SectionProps) => {
description={propertyDescriptions.whiteSpaceCollapse}
properties={["whiteSpaceCollapse"]}
/>
- <SelectControl
- property="whiteSpaceCollapse"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SelectControl property="whiteSpaceCollapse" />
</Grid>
<Grid css={{ gridTemplateColumns: "4fr 6fr" }} gap={2}>
<PropertyLabel
@@ -387,12 +381,7 @@ export const TypographySectionAdvancedPopover = (props: SectionProps) => {
description={propertyDescriptions.textWrapMode}
properties={["textWrapMode"]}
/>
- <SelectControl
- property="textWrapMode"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SelectControl property="textWrapMode" />
</Grid>
<Grid css={{ gridTemplateColumns: "4fr 6fr" }} gap={2}>
<PropertyLabel
@@ -400,12 +389,7 @@ export const TypographySectionAdvancedPopover = (props: SectionProps) => {
description={propertyDescriptions.textWrapStyle}
properties={["textWrapStyle"]}
/>
- <SelectControl
- property="textWrapStyle"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SelectControl property="textWrapStyle" />
</Grid>
<Grid css={{ gridTemplateColumns: "4fr auto" }}>
<PropertyLabel
diff --git a/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts b/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
index b853dd9476..daa041ae73 100644
--- a/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
@@ -197,3 +197,7 @@ export const createBatchUpdate = () => {
publish,
};
};
+
+export const resetEphemeralStyles = () => {
+ $ephemeralStyles.set([]);
+};