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: switch menu and toggle controls to style object model (#4023)

Bogdan Chadkinc3cd25a868a0
diff --git a/apps/builder/app/builder/features/style-panel/controls/menu/menu-control.tsx b/apps/builder/app/builder/features/style-panel/controls/menu/menu-control.tsx
index ea5d233310..e9d19e1d5d 100644
--- a/apps/builder/app/builder/features/style-panel/controls/menu/menu-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/menu/menu-control.tsx
@@ -1,4 +1,5 @@
-import { toValue } from "@webstudio-is/css-engine";
+import { useState } from "react";
+import { toValue, type StyleProperty } from "@webstudio-is/css-engine";
import type { IconComponent } from "@webstudio-is/icons";
import {
Box,
@@ -15,39 +16,28 @@ import {
IconButton,
theme,
} from "@webstudio-is/design-system";
-import type { ControlProps } from "../types";
-import { getStyleSource } from "../../shared/style-info";
-import { PropertyTooltip } from "../../shared/property-name";
-import { AdvancedValueTooltip } from "../advanced-value-tooltip";
-import { toKebabCase } from "../../shared/keyword-utils";
-import { useState } from "react";
import { declarationDescriptions } from "@webstudio-is/css-data";
+import { humanizeString } from "~/shared/string-utils";
+import { setProperty } from "../../shared/use-style-data";
+import { useComputedStyleDecl } from "../../shared/model";
+import { PropertyValueTooltip } from "../../property-label";
export const MenuControl = ({
- currentStyle,
property,
items,
- setProperty,
- deleteProperty,
- isAdvanced,
-}: Omit<ControlProps, "items"> & {
+}: {
+ property: StyleProperty;
items: Array<{
name: string;
label: string;
icon: IconComponent;
}>;
}) => {
- const styleValue = currentStyle[property];
- const value = styleValue?.value;
- const styleSource = getStyleSource(styleValue);
+ const computedStyleDecl = useComputedStyleDecl(property);
const [descriptionValue, setDescriptionValue] = useState<string>();
- if (value === undefined) {
- return;
- }
-
const setValue = setProperty(property);
- const currentValue = toValue(value);
+ const currentValue = toValue(computedStyleDecl.cascadedValue);
const currentItem = items.find((item) => item.name === currentValue);
const Icon = currentItem?.icon ?? items[0].icon;
const description =
@@ -56,41 +46,33 @@ export const MenuControl = ({
descriptionValue ?? currentValue
}` as keyof typeof declarationDescriptions
];
- // If there is no icon, we can't represent the value visually and assume the value comes from advanced section.
- isAdvanced = isAdvanced ?? currentItem === undefined;
+ // consider defined (not default) value as advanced
+ // when there is no matching item
+ const isAdvanced =
+ computedStyleDecl.source.name !== "default" && currentItem === undefined;
return (
<DropdownMenu modal={false}>
- <AdvancedValueTooltip
- isAdvanced={isAdvanced}
+ <PropertyValueTooltip
+ label={currentItem?.label ?? humanizeString(property)}
property={property}
- value={currentValue}
- currentStyle={currentStyle}
- deleteProperty={deleteProperty}
+ isAdvanced={isAdvanced}
>
- <PropertyTooltip
- title={currentItem?.label}
- properties={[property]}
- scrollableContent={`${toKebabCase(property)}: ${currentValue};`}
- style={currentStyle}
- onReset={() => deleteProperty(property)}
- >
- <DropdownMenuTrigger asChild>
- <IconButton
- disabled={isAdvanced}
- variant={styleSource}
- onPointerDown={(event) => {
- // tooltip reset property when click with altKey
- if (event.altKey) {
- event.preventDefault();
- }
- }}
- >
- <Icon />
- </IconButton>
- </DropdownMenuTrigger>
- </PropertyTooltip>
- </AdvancedValueTooltip>
+ <DropdownMenuTrigger asChild>
+ <IconButton
+ aria-disabled={isAdvanced}
+ variant={computedStyleDecl.source.name}
+ onPointerDown={(event) => {
+ // tooltip reset property when click with altKey
+ if (event.altKey) {
+ event.preventDefault();
+ }
+ }}
+ >
+ <Icon />
+ </IconButton>
+ </DropdownMenuTrigger>
+ </PropertyValueTooltip>
<DropdownMenuPortal>
<DropdownMenuContent sideOffset={4} collisionPadding={16} side="bottom">
<DropdownMenuRadioGroup
diff --git a/apps/builder/app/builder/features/style-panel/controls/toggle/toggle-control.tsx b/apps/builder/app/builder/features/style-panel/controls/toggle/toggle-control.tsx
index dfc0bcc956..251cfb7720 100644
--- a/apps/builder/app/builder/features/style-panel/controls/toggle/toggle-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/toggle/toggle-control.tsx
@@ -1,70 +1,60 @@
import { ToggleButton } from "@webstudio-is/design-system";
-import { styleConfigByName } from "../../shared/configs";
-import { PropertyTooltip } from "../../shared/property-name";
-import type { ControlProps } from "../types";
-import { getStyleSource } from "../../shared/style-info";
+import { toValue, type StyleProperty } from "@webstudio-is/css-engine";
import type { IconComponent } from "@webstudio-is/icons";
-import { AdvancedValueTooltip } from "../advanced-value-tooltip";
-import { toKebabCase } from "../../shared/keyword-utils";
-
-type Item = {
- name: string;
- label: string;
- icon: IconComponent;
-};
+import { humanizeString } from "~/shared/string-utils";
+import { setProperty } from "../../shared/use-style-data";
+import { useComputedStyleDecl } from "../../shared/model";
+import { PropertyValueTooltip } from "../../property-label";
export const ToggleControl = ({
property,
- currentStyle,
- setProperty,
- deleteProperty,
items,
- isAdvanced,
-}: Omit<ControlProps, "items"> & {
- items: [Item, Item];
+}: {
+ property: StyleProperty;
+ items: Array<{
+ name: string;
+ label: string;
+ icon: IconComponent;
+ }>;
}) => {
- const { label } = styleConfigByName(property);
- const styleValue = currentStyle[property]?.value;
-
- if (styleValue?.type !== "keyword") {
- return;
- }
+ const computedStyleDecl = useComputedStyleDecl(property);
+ const currentValue = toValue(computedStyleDecl.cascadedValue);
+ const currentItem = items.find((item) => item.name === currentValue);
+ const setValue = setProperty(property);
// First item is the pressed state
- const isPressed = items[0].name === styleValue.value ? true : false;
- const currentItem = items.find((item) => item.name === styleValue.value);
+ const isPressed = items[0].name === currentValue ? true : false;
const Icon = currentItem?.icon ?? items[0].icon;
- isAdvanced = isAdvanced ?? currentItem === undefined;
+ // consider defined (not default) value as advanced
+ // when there is no matching item
+ const isAdvanced =
+ computedStyleDecl.source.name !== "default" && currentItem === undefined;
return (
- <AdvancedValueTooltip
- isAdvanced={isAdvanced}
+ <PropertyValueTooltip
+ label={currentItem?.label ?? humanizeString(property)}
property={property}
- value={styleValue.value}
- currentStyle={currentStyle}
- deleteProperty={deleteProperty}
+ isAdvanced={isAdvanced}
>
- <PropertyTooltip
- title={label}
- scrollableContent={`${toKebabCase(property)}: ${styleValue.value};`}
- properties={[property]}
- style={currentStyle}
- onReset={() => deleteProperty(property)}
+ <ToggleButton
+ aria-disabled={isAdvanced}
+ variant={computedStyleDecl.source.name}
+ pressed={isPressed}
+ onPressedChange={(isPressed) => {
+ setValue({
+ type: "keyword",
+ value: isPressed ? items[0].name : items[1].name,
+ });
+ }}
+ onPointerDown={(event) => {
+ // tooltip reset property when click with altKey
+ if (event.altKey) {
+ event.preventDefault();
+ }
+ }}
>
- <ToggleButton
- disabled={isAdvanced}
- pressed={isPressed}
- onPressedChange={(isPressed) => {
- setProperty(property)({
- type: "keyword",
- value: isPressed ? items[0].name : items[1].name,
- });
- }}
- variant={getStyleSource(currentStyle[property])}
- >
- <Icon />
- </ToggleButton>
- </PropertyTooltip>
- </AdvancedValueTooltip>
+ <Icon />
+ </ToggleButton>
+ </PropertyValueTooltip>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/property-label.tsx b/apps/builder/app/builder/features/style-panel/property-label.tsx
index d1c567335b..c6c9574ab8 100644
--- a/apps/builder/app/builder/features/style-panel/property-label.tsx
+++ b/apps/builder/app/builder/features/style-panel/property-label.tsx
@@ -1,16 +1,19 @@
import { computed } from "nanostores";
import { useStore } from "@nanostores/react";
-import { useMemo, useState } from "react";
-import { ResetIcon } from "@webstudio-is/icons";
+import { useMemo, useState, type ReactNode } from "react";
+import { AlertIcon, ResetIcon } from "@webstudio-is/icons";
import {
hyphenateProperty,
+ toValue,
type StyleProperty,
} from "@webstudio-is/css-engine";
+import { declarationDescriptions } from "@webstudio-is/css-data";
import {
Button,
Flex,
Kbd,
Label,
+ rawTheme,
SectionTitleLabel,
Text,
theme,
@@ -34,12 +37,14 @@ import { createBatchUpdate } from "./shared/use-style-data";
export const PropertyInfo = ({
title,
+ code,
description,
styles,
onReset,
}: {
title: string;
- description: string;
+ code?: string;
+ description: ReactNode;
styles: ComputedStyleDecl[];
onReset: () => void;
}) => {
@@ -105,7 +110,7 @@ export const PropertyInfo = ({
userSelect="text"
css={{ whiteSpace: "break-spaces", cursor: "text" }}
>
- {properties.join("\n")}
+ {code ?? properties.join("\n")}
</Text>
<Text>{description}</Text>
{(styleSourceNameSet.size > 0 || instanceSet.size > 0) && (
@@ -365,3 +370,76 @@ export const PropertyInlineLabel = ({
</Flex>
);
};
+
+export const PropertyValueTooltip = ({
+ label,
+ property,
+ isAdvanced,
+ children,
+}: {
+ label: string;
+ property: StyleProperty;
+ isAdvanced?: boolean;
+ children: ReactNode;
+}) => {
+ const $computedStyleDecl = useMemo(
+ () => createComputedStyleDeclStore(property),
+ [property]
+ );
+ const computedStyleDecl = useStore($computedStyleDecl);
+ const [isOpen, setIsOpen] = useState(false);
+ const resetProperty = () => {
+ const batch = createBatchUpdate();
+ batch.deleteProperty(property);
+ batch.publish();
+ };
+ const value = toValue(computedStyleDecl.usedValue);
+ const css = `${hyphenateProperty(property)}: ${value};`;
+ const description =
+ declarationDescriptions[
+ `${property}:${value}` as keyof typeof declarationDescriptions
+ ];
+ return (
+ <Flex align="center">
+ <Tooltip
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ // prevent closing tooltip on content click
+ onPointerDown={(event) => event.preventDefault()}
+ triggerProps={{
+ onClick: (event) => {
+ if (event.altKey) {
+ event.preventDefault();
+ resetProperty();
+ return;
+ }
+ },
+ }}
+ content={
+ <PropertyInfo
+ title={label}
+ code={css}
+ description={
+ <Flex gap="2" direction="column">
+ {description}
+ {isAdvanced && (
+ <Flex gap="1">
+ <AlertIcon color={rawTheme.colors.backgroundAlertMain} />{" "}
+ This value was defined in the Advanced section.
+ </Flex>
+ )}
+ </Flex>
+ }
+ styles={[computedStyleDecl]}
+ onReset={() => {
+ resetProperty();
+ setIsOpen(false);
+ }}
+ />
+ }
+ >
+ {children}
+ </Tooltip>
+ </Flex>
+ );
+};
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 b25d5dfecd..1dfcd2390e 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
@@ -306,32 +306,12 @@ const FlexGap = ({
);
};
-const mapNormalTo = (
- style: StyleInfo,
- property: StyleProperty,
- newValue: string
-): StyleInfo => {
- const styleInfoValue = style[property]?.value;
- if (styleInfoValue?.type === "keyword" && styleInfoValue.value === "normal") {
- return {
- ...style,
- [property]: {
- ...style[property],
- value: { type: "keyword", value: newValue },
- },
- };
- }
- return style;
-};
-
const LayoutSectionFlex = ({
currentStyle,
- setProperty,
deleteProperty,
createBatchUpdate,
}: {
currentStyle: SectionProps["currentStyle"];
- setProperty: SectionProps["setProperty"];
deleteProperty: SectionProps["deleteProperty"];
createBatchUpdate: SectionProps["createBatchUpdate"];
}) => {
@@ -367,9 +347,6 @@ const LayoutSectionFlex = ({
icon: ArrowUpIcon,
},
]}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
/>
<ToggleControl
property="flexWrap"
@@ -377,17 +354,11 @@ const LayoutSectionFlex = ({
{ name: "nowrap", label: "No Wrap", icon: NoWrapIcon },
{ name: "wrap", label: "Wrap", icon: WrapIcon },
]}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
/>
</Flex>
<Flex css={{ gap: theme.spacing[7] }}>
<MenuControl
property="alignItems"
- currentStyle={mapNormalTo(currentStyle, "alignItems", "stretch")}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
items={[
{ name: "stretch", label: "Stretch", icon: AIStretchIcon },
{ name: "baseline", label: "Baseline", icon: AIBaselineIcon },
@@ -398,13 +369,6 @@ const LayoutSectionFlex = ({
/>
<MenuControl
property="justifyContent"
- currentStyle={mapNormalTo(
- currentStyle,
- "justifyContent",
- "start"
- )}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
items={[
{
name: "space-between",
@@ -424,13 +388,6 @@ const LayoutSectionFlex = ({
{showAlignContent && (
<MenuControl
property="alignContent"
- currentStyle={mapNormalTo(
- currentStyle,
- "alignContent",
- "stretch"
- )}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
items={[
{
name: "space-between",
@@ -534,7 +491,6 @@ export const Section = ({
{(value === "flex" || value === "inline-flex") && (
<LayoutSectionFlex
currentStyle={currentStyle}
- setProperty={setProperty}
deleteProperty={deleteProperty}
createBatchUpdate={createBatchUpdate}
/>
diff --git a/apps/builder/app/builder/features/style-panel/shared/model.tsx b/apps/builder/app/builder/features/style-panel/shared/model.tsx
index d7c18d9d80..3c5f44e4c4 100644
--- a/apps/builder/app/builder/features/style-panel/shared/model.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/model.tsx
@@ -19,6 +19,7 @@ import {
getPresetStyleDeclKey,
type StyleObjectModel,
} from "~/shared/style-object-model";
+import { useMemo } from "react";
const $presetStyles = computed($registeredComponentMetas, (metas) => {
const presetStyles = new Map<string, StyleValue>();
@@ -123,3 +124,11 @@ export const createComputedStyleDeclStore = (property: string) => {
export const useStyleObjectModel = () => {
return useStore($model);
};
+
+export const useComputedStyleDecl = (property: string) => {
+ const $store = useMemo(
+ () => createComputedStyleDeclStore(property),
+ [property]
+ );
+ return useStore($store);
+};
diff --git a/packages/design-system/src/components/icon-button.tsx b/packages/design-system/src/components/icon-button.tsx
index d453a43f33..e26af728c2 100644
--- a/packages/design-system/src/components/icon-button.tsx
+++ b/packages/design-system/src/components/icon-button.tsx
@@ -11,6 +11,15 @@ const openOrHoverStateStyle = {
backgroundColor: theme.colors.backgroundHover,
};
+const disabledVariantStyles = {
+ "&:disabled, &[aria-disabled=true]": {
+ color: theme.colors.foregroundDisabled,
+ "&:hover": {
+ backgroundColor: theme.colors.backgroundHover,
+ },
+ },
+};
+
export const IconButton = styled("button", {
// reset styles
boxSizing: "border-box",
@@ -34,9 +43,8 @@ export const IconButton = styled("button", {
outlineOffset: -2,
},
- "&:disabled": {
+ "&:disabled, &[aria-disabled=true]": {
borderColor: "transparent",
- pointerEvents: "none",
backgroundColor: "transparent",
},
@@ -57,10 +65,7 @@ export const IconButton = styled("button", {
"&:hover, &[data-hovered=true]": openOrHoverStateStyle,
},
-
- "&:disabled": {
- color: theme.colors.foregroundDisabled,
- },
+ ...disabledVariantStyles,
},
preset: {
@@ -70,9 +75,7 @@ export const IconButton = styled("button", {
"&:hover, &[data-hovered=true]": {
backgroundColor: theme.colors.backgroundPresetHover,
},
- "&:disabled": {
- color: theme.colors.foregroundDisabled,
- },
+ ...disabledVariantStyles,
},
local: {
@@ -82,9 +85,7 @@ export const IconButton = styled("button", {
"&:hover, &[data-hovered=true]": {
backgroundColor: theme.colors.backgroundLocalHover,
},
- "&:disabled": {
- color: theme.colors.foregroundDisabled,
- },
+ ...disabledVariantStyles,
},
overwritten: {
@@ -94,9 +95,7 @@ export const IconButton = styled("button", {
"&:hover, &[data-hovered=true]": {
backgroundColor: theme.colors.backgroundOverwrittenHover,
},
- "&:disabled": {
- color: theme.colors.foregroundDisabled,
- },
+ ...disabledVariantStyles,
},
remote: {
@@ -106,9 +105,7 @@ export const IconButton = styled("button", {
"&:hover, &[data-hovered=true]": {
backgroundColor: theme.colors.backgroundRemoteHover,
},
- "&:disabled": {
- color: theme.colors.foregroundDisabled,
- },
+ ...disabledVariantStyles,
},
},
state: {