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 text control to style object model (#4036)

Bogdan Chadkinac26e8f7f7c3
diff --git a/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx b/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx
index 1ca2c4704b..6ad8e53334 100644
--- a/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/text/text-control.tsx
@@ -1,73 +1,52 @@
+import { useState } from "react";
+import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";
import {
CssValueInput,
type IntermediateStyleValue,
} from "../../shared/css-value-input";
-import type { ControlProps } from "../types";
-import type { StyleValue } from "@webstudio-is/css-engine";
-import { useState } from "react";
-import { Box } from "@webstudio-is/design-system";
import { styleConfigByName } from "../../shared/configs";
-import { getStyleSource } from "../../shared/style-info";
-
-export const TextControl = ({
- property,
- items,
- currentStyle,
- setProperty,
- deleteProperty,
- icon,
- isAdvanced,
-}: ControlProps & { icon?: JSX.Element }) => {
- const { items: defaultItems } = styleConfigByName(property);
- const styleInfo = currentStyle[property];
- const value = styleInfo?.value;
+import { deleteProperty, setProperty } from "../../shared/use-style-data";
+import { useComputedStyleDecl } from "../../shared/model";
+export const TextControl = ({ property }: { property: StyleProperty }) => {
+ const computedStyleDecl = useComputedStyleDecl(property);
+ const value = computedStyleDecl.cascadedValue;
const setValue = setProperty(property);
-
const [intermediateValue, setIntermediateValue] = useState<
StyleValue | IntermediateStyleValue
>();
-
+ const items = styleConfigByName(property).items;
return (
- <Box>
- <CssValueInput
- disabled={isAdvanced}
- styleSource={getStyleSource(styleInfo)}
- icon={icon}
- property={property}
- value={value}
- intermediateValue={intermediateValue}
- keywords={(items ?? defaultItems).map((item) => ({
- type: "keyword",
- value: item.name,
- }))}
- onChange={(styleValue) => {
- setIntermediateValue(styleValue);
-
- if (styleValue === undefined) {
- deleteProperty(property, { isEphemeral: true });
- return;
- }
-
- if (styleValue.type !== "intermediate") {
- setValue(styleValue, { isEphemeral: true });
- }
- }}
- onHighlight={(styleValue) => {
- if (styleValue !== undefined) {
- setValue(styleValue, { isEphemeral: true });
- } else {
- deleteProperty(property, { isEphemeral: true });
- }
- }}
- onChangeComplete={({ value }) => {
- setValue(value);
- setIntermediateValue(undefined);
- }}
- onAbort={() => {
+ <CssValueInput
+ styleSource={computedStyleDecl.source.name}
+ property={property}
+ value={value}
+ intermediateValue={intermediateValue}
+ keywords={items.map((item) => ({ type: "keyword", value: item.name }))}
+ onChange={(styleValue) => {
+ setIntermediateValue(styleValue);
+ if (styleValue === undefined) {
+ deleteProperty(property, { isEphemeral: true });
+ return;
+ }
+ if (styleValue.type !== "intermediate") {
+ setValue(styleValue, { isEphemeral: true });
+ }
+ }}
+ onHighlight={(styleValue) => {
+ if (styleValue !== undefined) {
+ setValue(styleValue, { isEphemeral: true });
+ } else {
deleteProperty(property, { isEphemeral: true });
- }}
- />
- </Box>
+ }
+ }}
+ onChangeComplete={({ value }) => {
+ setIntermediateValue(undefined);
+ setValue(value);
+ }}
+ onAbort={() => {
+ deleteProperty(property, { isEphemeral: true });
+ }}
+ />
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx b/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
index d1617b5bd5..121b6c91d1 100644
--- a/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
@@ -214,7 +214,7 @@ const FlexChildSectionSizing = (props: SectionProps) => {
propertyValues: ["flex-grow: 0;", "flex-shrink: 1;"],
},
{
- child: <FlexChildSectionSizingPopover {...props} />,
+ child: <FlexChildSectionSizingPopover />,
title: "Flex",
description:
"More sizing options, set flex-basis, flex-grow, flex-shrink individually",
@@ -231,11 +231,7 @@ const FlexChildSectionSizing = (props: SectionProps) => {
);
};
-const FlexChildSectionSizingPopover = ({
- currentStyle,
- setProperty,
- deleteProperty,
-}: SectionProps) => {
+const FlexChildSectionSizingPopover = () => {
return (
<FloatingPanel
title="Sizing"
@@ -253,12 +249,7 @@ const FlexChildSectionSizingPopover = ({
description={propertyDescriptions.flexGrow}
label="Grow"
/>
- <TextControl
- property="flexGrow"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="flexGrow" />
</Grid>
<Grid css={{ gridTemplateColumns: "auto", gap: theme.spacing[3] }}>
<PropertyLabel
@@ -266,12 +257,7 @@ const FlexChildSectionSizingPopover = ({
description={propertyDescriptions.flexShrink}
properties={["flexShrink"]}
/>
- <TextControl
- property="flexShrink"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="flexShrink" />
</Grid>
<Grid css={{ gridTemplateColumns: "auto", gap: theme.spacing[3] }}>
<PropertyLabel
@@ -279,12 +265,7 @@ const FlexChildSectionSizingPopover = ({
description={propertyDescriptions.flexBasis}
properties={["flexBasis"]}
/>
- <TextControl
- property="flexBasis"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="flexBasis" />
</Grid>
</Grid>
}
@@ -353,7 +334,7 @@ const FlexChildSectionOrder = (props: SectionProps) => {
propertyValues: "order: 1;",
},
{
- child: <FlexChildSectionOrderPopover {...props} />,
+ child: <FlexChildSectionOrderPopover />,
title: "Order",
description: "Customize order",
value: "",
@@ -365,8 +346,7 @@ const FlexChildSectionOrder = (props: SectionProps) => {
);
};
-const FlexChildSectionOrderPopover = (props: SectionProps) => {
- const { currentStyle, setProperty, deleteProperty } = props;
+const FlexChildSectionOrderPopover = () => {
return (
<FloatingPanel
title="Order"
@@ -378,12 +358,7 @@ const FlexChildSectionOrderPopover = (props: SectionProps) => {
description={propertyDescriptions.order}
properties={["order"]}
/>
- <TextControl
- property="order"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="order" />
</Grid>
</Grid>
}
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 8d473816c8..6fe32a7d76 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
@@ -28,7 +28,6 @@ const positionControlVisibleProperties = [
const zIndexParents = ["flex", "grid", "inline-flex", "inline-grid"] as const;
export const Section = ({
- setProperty,
deleteProperty,
currentStyle,
createBatchUpdate,
@@ -37,12 +36,12 @@ export const Section = ({
const positionValue = currentStyle.position?.value;
- const showPositionControls =
+ const showInsetControl =
positionValue?.type === "keyword" &&
positionControlVisibleProperties.includes(positionValue.value as never);
const showZindexControl =
- showPositionControls ||
+ showInsetControl ||
(parentStyle?.display?.value.type === "keyword" &&
zIndexParents.includes(parentStyle?.display?.value.value as never));
@@ -73,24 +72,19 @@ export const Section = ({
properties={["position"]}
/>
<SelectControl property="position" items={positionItems} />
- {showZindexControl && showPositionControls === false && (
+ {showZindexControl && showInsetControl === false && (
<>
<PropertyLabel
label="Z Index"
description={propertyDescriptions.zIndex}
properties={["zIndex"]}
/>
- <TextControl
- property={"zIndex"}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="zIndex" />
</>
)}
</Grid>
- {showPositionControls && (
+ {showInsetControl && (
<Grid gap={3} columns={2}>
<InsetControl
currentStyle={currentStyle}
@@ -103,12 +97,7 @@ export const Section = ({
description={propertyDescriptions.zIndex}
properties={["zIndex"]}
/>
- <TextControl
- property={"zIndex"}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="zIndex" />
</Grid>
</Grid>
)}
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 5bef4d8a65..f0ad82a58a 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
@@ -29,12 +29,7 @@ import { FloatingPanel } from "~/builder/shared/floating-panel";
import { humanizeString } from "~/shared/string-utils";
import { PropertyLabel } from "../../property-label";
-const SizeProperty = ({
- property,
- currentStyle,
- setProperty,
- deleteProperty,
-}: ControlProps) => {
+const SizeProperty = ({ property }: { property: StyleProperty }) => {
return (
<Grid gap={1}>
<PropertyLabel
@@ -44,12 +39,7 @@ const SizeProperty = ({
}
properties={[property]}
/>
- <TextControl
- property={property}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property={property} />
</Grid>
);
};
@@ -132,53 +122,18 @@ export const Section = ({
fullWidth
>
<SectionLayout columns={2}>
- <SizeProperty
- property="width"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeProperty
- property="height"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeProperty
- property="minWidth"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeProperty
- property="minHeight"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeProperty
- property="maxWidth"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeProperty
- property="maxHeight"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <SizeProperty property="width" />
+ <SizeProperty property="height" />
+ <SizeProperty property="minWidth" />
+ <SizeProperty property="minHeight" />
+ <SizeProperty property="maxWidth" />
+ <SizeProperty property="maxHeight" />
<PropertyLabel
label="Aspect Ratio"
description={propertyDescriptions.aspectRatio}
properties={["aspectRatio"]}
/>
- <TextControl
- property={"aspectRatio"}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="aspectRatio" />
</SectionLayout>
<Separator />
<SectionLayout columns={2}>
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-backface-visibility.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-backface-visibility.tsx
index 1d3c8cc2d7..510547bc27 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-backface-visibility.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-backface-visibility.tsx
@@ -1,17 +1,9 @@
import { propertyDescriptions } from "@webstudio-is/css-data";
import { Grid, theme } from "@webstudio-is/design-system";
import { TextControl } from "../../controls";
-import type { SectionProps } from "../shared/section";
import { PropertyLabel } from "../../property-label";
-export const BackfaceVisibility = (props: SectionProps) => {
- const { currentStyle, setProperty, deleteProperty } = props;
- const value = currentStyle.backfaceVisibility?.local;
-
- if (value?.type !== "keyword") {
- return;
- }
-
+export const BackfaceVisibility = () => {
return (
<Grid
css={{
@@ -24,12 +16,7 @@ export const BackfaceVisibility = (props: SectionProps) => {
description={propertyDescriptions.backfaceVisibility}
properties={["backfaceVisibility"]}
/>
- <TextControl
- property="backfaceVisibility"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="backfaceVisibility" />
</Grid>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-perspective.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-perspective.tsx
index b462006c74..4f145aa533 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-perspective.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-perspective.tsx
@@ -1,17 +1,9 @@
import { propertyDescriptions } from "@webstudio-is/css-data";
import { Grid, theme } from "@webstudio-is/design-system";
import { TextControl } from "../../controls";
-import type { SectionProps } from "../shared/section";
import { PropertyLabel } from "../../property-label";
-export const TransformPerspective = (props: SectionProps) => {
- const { currentStyle, setProperty, deleteProperty } = props;
- const value = currentStyle.perspective?.local;
-
- if (value?.type !== "keyword" && value?.type !== "unit") {
- return;
- }
-
+export const TransformPerspective = () => {
return (
<Grid
css={{
@@ -24,12 +16,7 @@ export const TransformPerspective = (props: SectionProps) => {
description={propertyDescriptions.perspective}
properties={["perspective"]}
/>
- <TextControl
- property="perspective"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="perspective" />
</Grid>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
index 703647cfc4..3c4d15ad9d 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
@@ -160,9 +160,9 @@ export const Section = (props: SectionProps) => {
</CssValueListArrowFocus>
) : undefined}
- <BackfaceVisibility {...props} />
+ <BackfaceVisibility />
<TransformAndPerspectiveOrigin property="transformOrigin" {...props} />
- <TransformPerspective {...props} />
+ <TransformPerspective />
<TransformAndPerspectiveOrigin property="perspectiveOrigin" {...props} />
</CollapsibleSectionRoot>
);
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 34c59e50a6..945dc4ddb1 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
@@ -68,7 +68,7 @@ export const Section = (props: SectionProps) => {
>
<Flex gap="2" direction="column">
<TypographySectionFont {...props} />
- <TypographySectionSizing {...props} />
+ <TypographySectionSizing />
<TypographySectionAdvanced {...props} />
</Flex>
</CollapsibleSection>
@@ -112,9 +112,7 @@ export const TypographySectionFont = (props: SectionProps) => {
);
};
-export const TypographySectionSizing = (props: SectionProps) => {
- const { currentStyle, setProperty, deleteProperty } = props;
-
+const TypographySectionSizing = () => {
return (
<Grid gap="2" css={{ gridTemplateColumns: "1fr 1fr 1fr" }}>
<Grid gap="1">
@@ -123,12 +121,7 @@ export const TypographySectionSizing = (props: SectionProps) => {
description={propertyDescriptions.fontSize}
properties={["fontSize"]}
/>
- <TextControl
- property="fontSize"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="fontSize" />
</Grid>
<Grid gap="1">
<PropertyLabel
@@ -136,12 +129,7 @@ export const TypographySectionSizing = (props: SectionProps) => {
description={propertyDescriptions.lineHeight}
properties={["lineHeight"]}
/>
- <TextControl
- property="lineHeight"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="lineHeight" />
</Grid>
<Grid gap="1">
<PropertyLabel
@@ -149,12 +137,7 @@ export const TypographySectionSizing = (props: SectionProps) => {
description={propertyDescriptions.letterSpacing}
properties={["letterSpacing"]}
/>
- <TextControl
- property="letterSpacing"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <TextControl property="letterSpacing" />
</Grid>
</Grid>
);