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.

experimental: migrate borders section to style object model (#4076)

Bogdan Chadkinb75626cc5ca0
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.tsx
index 806871b251..3a2111e71f 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
@@ -1,5 +1,5 @@
import { type ReactNode } from "react";
-import type { StyleProperty, UnitValue } from "@webstudio-is/css-engine";
+import type { StyleProperty } from "@webstudio-is/css-engine";
import { toValue } from "@webstudio-is/css-engine";
import {
Box,
@@ -9,31 +9,25 @@ import {
} from "@webstudio-is/design-system";
import { CssValueInputContainer } from "../../shared/css-value-input";
import { styleConfigByName } from "../../shared/configs";
-import { getStyleSource } from "../../shared/style-info";
-import type { SectionProps } from "../shared/section";
-import { deleteAllProperties, rowCss, setAllProperties } from "./utils";
+import { rowCss } from "./utils";
import { useSelectedInstanceKv } from "../../shared/instances-kv";
-import { PropertyLabel } from "../../property-label";
-
-const borderPropertyStyleValueDefault: UnitValue = {
- type: "unit",
- value: 0,
- unit: "number",
-};
+import {
+ getPriorityStyleValueSource,
+ PropertyLabel,
+} from "../../property-label";
+import {
+ createBatchUpdate,
+ deleteProperty,
+ setProperty,
+} from "../../shared/use-style-data";
+import { useComputedStyles } from "../../shared/model";
export const BorderProperty = ({
- currentStyle,
- setProperty,
- deleteProperty,
- createBatchUpdate,
individualModeIcon,
borderPropertyOptions,
label,
description,
-}: Pick<
- SectionProps,
- "currentStyle" | "setProperty" | "deleteProperty" | "createBatchUpdate"
-> & {
+}: {
individualModeIcon?: ReactNode;
borderPropertyOptions: Partial<{
[property in StyleProperty]: { icon?: ReactNode };
@@ -45,15 +39,11 @@ export const BorderProperty = ({
StyleProperty,
...StyleProperty[],
];
-
+ const styles = useComputedStyles(borderProperties);
+ const styleValueSourceColor = getPriorityStyleValueSource(styles);
const allPropertyValuesAreEqual =
- new Set(
- borderProperties.map(
- (property) =>
- toValue(currentStyle[property]?.value) ??
- toValue(borderPropertyStyleValueDefault)
- )
- ).size === 1;
+ new Set(styles.map((styleDecl) => toValue(styleDecl.cascadedValue)))
+ .size === 1;
/**
* We do not use shorthand properties such as borderWidth or borderRadius in our code.
@@ -69,7 +59,7 @@ export const BorderProperty = ({
const { items: borderPropertyItems } = styleConfigByName(firstPropertyName);
- const borderWidthKeywords = borderPropertyItems.map((item) => ({
+ const keywords = borderPropertyItems.map((item) => ({
type: "keyword" as const,
value: item.name,
}));
@@ -80,32 +70,7 @@ export const BorderProperty = ({
* We have decided to show the first defined value, as it is difficult to determine a maximum value
* when there are keywords (such as "thin" or "thick") and different units involved.
**/
- const borderWidthStyleInfo = borderProperties
- .map((property) => currentStyle[property]?.value)
- .find((styleValue) => {
- if (styleValue === undefined) {
- return false;
- }
-
- return (
- (styleValue.type === "unit" && styleValue.value > 0) ||
- styleValue.type === "keyword"
- );
- });
-
- const borderWidthStyleSource = getStyleSource(
- ...borderProperties.map((property) => currentStyle[property])
- );
-
- const deleteBorderProperties = deleteAllProperties(
- borderProperties,
- createBatchUpdate
- );
-
- const setBorderProperties = setAllProperties(
- borderProperties,
- createBatchUpdate
- )(firstPropertyName);
+ const value = styles[0].cascadedValue;
return (
<Grid gap={1}>
@@ -124,11 +89,23 @@ export const BorderProperty = ({
>
<CssValueInputContainer
property={firstPropertyName}
- styleSource={borderWidthStyleSource}
- keywords={borderWidthKeywords}
- value={borderWidthStyleInfo}
- setValue={setBorderProperties}
- deleteProperty={deleteBorderProperties}
+ styleSource={styleValueSourceColor}
+ keywords={keywords}
+ value={value}
+ setValue={(newValue, options) => {
+ const batch = createBatchUpdate();
+ for (const property of borderProperties) {
+ batch.setProperty(property)(newValue);
+ }
+ batch.publish(options);
+ }}
+ deleteProperty={(_property, options) => {
+ const batch = createBatchUpdate();
+ for (const property of borderProperties) {
+ batch.deleteProperty(property);
+ }
+ batch.publish(options);
+ }}
/>
</Box>
@@ -143,21 +120,22 @@ export const BorderProperty = ({
</Grid>
{showIndividualMode && (
<Grid columns={2} gap={1}>
- {borderProperties.map((property) => (
+ {styles.map((styleDecl) => (
<CssValueInputContainer
+ key={styleDecl.property}
icon={
<NestedIconLabel>
- {borderPropertyOptions[property]?.icon}
+ {
+ borderPropertyOptions[styleDecl.property as StyleProperty]
+ ?.icon
+ }
</NestedIconLabel>
}
- key={property}
- property={property}
- styleSource={getStyleSource(currentStyle[property])}
- keywords={borderWidthKeywords}
- value={
- currentStyle[property]?.value ?? borderPropertyStyleValueDefault
- }
- setValue={setProperty(property)}
+ property={styleDecl.property as StyleProperty}
+ styleSource={styleDecl.source.name}
+ keywords={keywords}
+ value={styleDecl.cascadedValue}
+ setValue={setProperty(styleDecl.property as StyleProperty)}
deleteProperty={deleteProperty}
/>
))}
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/border-radius.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/border-radius.tsx
index 85a736c595..db8bf79b61 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/border-radius.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/border-radius.tsx
@@ -6,7 +6,6 @@ import {
BorderRadiusTopRightIcon,
BorderRadiusBottomLeftIcon,
} from "@webstudio-is/icons";
-import type { SectionProps } from "../shared/section";
import { BorderProperty } from "./border-property";
export const properties = [
@@ -31,18 +30,9 @@ const borderPropertyOptions = {
},
} as const satisfies Partial<{ [property in StyleProperty]: unknown }>;
-export const BorderRadius = (
- props: Pick<
- SectionProps,
- "currentStyle" | "setProperty" | "deleteProperty" | "createBatchUpdate"
- >
-) => {
+export const BorderRadius = () => {
return (
<BorderProperty
- currentStyle={props.currentStyle}
- setProperty={props.setProperty}
- deleteProperty={props.deleteProperty}
- createBatchUpdate={props.createBatchUpdate}
label="Radius"
description="Sets the radius of border"
borderPropertyOptions={borderPropertyOptions}
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/border-width.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/border-width.tsx
index 8ff574c936..8360a823dd 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/border-width.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/border-width.tsx
@@ -6,8 +6,6 @@ import {
BorderWidthBottomIcon,
BorderWidthLeftIcon,
} from "@webstudio-is/icons";
-
-import type { SectionProps } from "../shared/section";
import { BorderProperty } from "./border-property";
export const properties = [
@@ -32,18 +30,9 @@ const borderPropertyOptions = {
},
} as const satisfies Partial<{ [property in StyleProperty]: unknown }>;
-export const BorderWidth = (
- props: Pick<
- SectionProps,
- "currentStyle" | "setProperty" | "deleteProperty" | "createBatchUpdate"
- >
-) => {
+export const BorderWidth = () => {
return (
<BorderProperty
- currentStyle={props.currentStyle}
- setProperty={props.setProperty}
- deleteProperty={props.deleteProperty}
- createBatchUpdate={props.createBatchUpdate}
label="Width"
description="Sets the width of the border"
borderPropertyOptions={borderPropertyOptions}
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
index a068318daa..4708e3fbd3 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
@@ -1,89 +1,15 @@
import { styled, theme } from "@webstudio-is/design-system";
-import { useRef, useState } from "react";
-import type { StyleInfo } from "../../shared/style-info";
-import type {
- CreateBatchUpdate,
- DeleteProperty,
- SetProperty,
-} from "../../shared/use-style-data";
import { Section } from "./borders";
-const styleInfoInitial: StyleInfo = {
- borderTopColor: {
- local: { type: "rgb", r: 0, g: 0, b: 0, alpha: 1 },
- value: { type: "rgb", r: 0, g: 0, b: 0, alpha: 1 },
- },
-};
-
const Panel = styled("div", {
width: theme.spacing[30],
boxSizing: "border-box",
});
export const Borders = () => {
- const [styleInfo, setStyleInfo] = useState(() => styleInfoInitial);
-
- const setProperty: SetProperty = (name) => (value, options) => {
- if (options?.isEphemeral) {
- return;
- }
-
- setStyleInfo((styleInfo) => ({
- ...styleInfo,
- [name]: {
- ...styleInfo[name],
- value: value,
- local: value,
- },
- }));
- };
-
- const deleteProperty: DeleteProperty = (name, options) => {
- if (options?.isEphemeral) {
- return;
- }
-
- setStyleInfo((styleInfo) => {
- const { [name]: _, ...rest } = styleInfo;
- return rest;
- });
- };
-
- const execCommands = useRef<(() => void)[]>([]);
-
- const createBatchUpdate: CreateBatchUpdate = () => ({
- deleteProperty: (property) => {
- execCommands.current.push(() => {
- deleteProperty(property);
- });
- },
- setProperty: (property) => (style) => {
- execCommands.current.push(() => {
- setProperty(property)(style);
- });
- },
- publish: (options) => {
- if (options?.isEphemeral) {
- execCommands.current = [];
- return;
- }
-
- for (const command of execCommands.current) {
- command();
- }
-
- execCommands.current = [];
- },
- });
-
return (
<Panel>
- <Section
- currentStyle={styleInfo}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- createBatchUpdate={createBatchUpdate}
- />
+ <Section />
</Panel>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
index d433beabdf..c4a10a89e3 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
@@ -1,6 +1,5 @@
import { Flex } from "@webstudio-is/design-system";
import type { StyleProperty } from "@webstudio-is/css-engine";
-import type { SectionProps } from "../shared/section";
import { StyleSection } from "../../shared/style-section";
import {
BorderRadius,
@@ -26,14 +25,14 @@ export const properties = [
...borderWidthProperties,
] satisfies Array<StyleProperty>;
-export const Section = (props: SectionProps) => {
+export const Section = () => {
return (
<StyleSection label="Borders" properties={properties}>
<Flex direction="column" gap={2}>
<BorderStyle />
<BorderColor />
- <BorderWidth {...props} />
- <BorderRadius {...props} />
+ <BorderWidth />
+ <BorderRadius />
</Flex>
</StyleSection>
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/utils.ts b/apps/builder/app/builder/features/style-panel/sections/borders/utils.ts
index 3d07721a13..c4e2b4c8d0 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/utils.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/utils.ts
@@ -1,36 +1,4 @@
import { theme, type CSS } from "@webstudio-is/design-system";
-import type { StyleProperty } from "@webstudio-is/css-engine";
-import type {
- CreateBatchUpdate,
- DeleteProperty,
- SetProperty,
-} from "../../shared/use-style-data";
-
-export const deleteAllProperties: (
- styleProperties: readonly StyleProperty[],
- createBatchUpdate: CreateBatchUpdate
-) => DeleteProperty =
- (styleProperties, createBatchUpdate) => (_propertyName, options) => {
- const batch = createBatchUpdate();
- for (const property of styleProperties) {
- batch.deleteProperty(property);
- }
- batch.publish(options);
- };
-
-export const setAllProperties: (
- styleProperties: readonly StyleProperty[],
- createBatchUpdate: CreateBatchUpdate
-) => SetProperty =
- (styleProperties, createBatchUpdate) =>
- (_propertyName) =>
- (value, options) => {
- const batch = createBatchUpdate();
- for (const property of styleProperties) {
- batch.setProperty(property)(value);
- }
- batch.publish(options);
- };
export const rowCss: CSS = {
// Our aim is to maintain consistent styling throughout the property and align