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: Cleanup menu control, add description (#3130)

Oleg Isonen5ed56ac4637d
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 1dce1fc1da..ea5d233310 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,37 +1,46 @@
import { toValue } from "@webstudio-is/css-engine";
-import type { IconComponent, IconRecord } from "@webstudio-is/icons";
+import type { IconComponent } from "@webstudio-is/icons";
import {
+ Box,
DropdownMenu,
DropdownMenuArrow,
DropdownMenuContent,
+ DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
+ DropdownMenuSeparator,
DropdownMenuTrigger,
Flex,
IconButton,
theme,
} from "@webstudio-is/design-system";
import type { ControlProps } from "../types";
-import { iconConfigs, styleConfigByName } from "../../shared/configs";
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";
export const MenuControl = ({
currentStyle,
property,
- icons,
- DefaultIcon,
- items: passedItems,
+ items,
setProperty,
deleteProperty,
isAdvanced,
-}: ControlProps & { icons?: IconRecord; DefaultIcon?: IconComponent }) => {
- const { label, items: defaultItems } = styleConfigByName(property);
+}: Omit<ControlProps, "items"> & {
+ items: Array<{
+ name: string;
+ label: string;
+ icon: IconComponent;
+ }>;
+}) => {
const styleValue = currentStyle[property];
const value = styleValue?.value;
const styleSource = getStyleSource(styleValue);
+ const [descriptionValue, setDescriptionValue] = useState<string>();
if (value === undefined) {
return;
@@ -39,18 +48,17 @@ export const MenuControl = ({
const setValue = setProperty(property);
const currentValue = toValue(value);
-
- const iconProps = iconConfigs[property];
-
- const items = (passedItems ?? defaultItems)
- .map((item) => {
- const ItemIcon = icons?.[item.name] ?? iconProps?.[item.name];
- return { ...item, icon: ItemIcon && <ItemIcon /> };
- })
- .filter((item) => item.icon);
- const icon = items.find(({ name }) => name === currentValue)?.icon;
+ const currentItem = items.find((item) => item.name === currentValue);
+ const Icon = currentItem?.icon ?? items[0].icon;
+ const description =
+ declarationDescriptions[
+ `${property}:${
+ 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 ?? icon === undefined;
+ isAdvanced = isAdvanced ?? currentItem === undefined;
+
return (
<DropdownMenu modal={false}>
<AdvancedValueTooltip
@@ -61,14 +69,15 @@ export const MenuControl = ({
deleteProperty={deleteProperty}
>
<PropertyTooltip
- title={label}
+ title={currentItem?.label}
properties={[property]}
+ scrollableContent={`${toKebabCase(property)}: ${currentValue};`}
style={currentStyle}
onReset={() => deleteProperty(property)}
>
<DropdownMenuTrigger asChild>
<IconButton
- disabled={isAdvanced ?? icon === undefined}
+ disabled={isAdvanced}
variant={styleSource}
onPointerDown={(event) => {
// tooltip reset property when click with altKey
@@ -77,7 +86,7 @@ export const MenuControl = ({
}
}}
>
- {icon ?? (DefaultIcon ? <DefaultIcon /> : <></>)}
+ <Icon />
</IconButton>
</DropdownMenuTrigger>
</PropertyTooltip>
@@ -88,24 +97,26 @@ export const MenuControl = ({
value={currentValue}
onValueChange={(value) => setValue({ type: "keyword", value })}
>
- {items.map(({ name, label, icon }) => {
+ {items.map(({ name, label, icon: Icon }) => {
return (
<DropdownMenuRadioItem
text="sentence"
key={name}
- value={label}
- onFocus={() =>
+ value={name}
+ onFocus={() => {
setValue(
{ type: "keyword", value: name },
{ isEphemeral: true }
- )
- }
- onBlur={() =>
+ );
+ setDescriptionValue(name);
+ }}
+ onBlur={() => {
setValue(
{ type: "keyword", value: currentValue },
{ isEphemeral: true }
- )
- }
+ );
+ setDescriptionValue(undefined);
+ }}
>
<Flex
css={{
@@ -115,13 +126,17 @@ export const MenuControl = ({
align="center"
justify="center"
>
- {icon}
+ <Icon />
</Flex>
{label}
</DropdownMenuRadioItem>
);
})}
</DropdownMenuRadioGroup>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem hint>
+ <Box css={{ width: theme.spacing[25] }}>{description}</Box>
+ </DropdownMenuItem>
<DropdownMenuArrow />
</DropdownMenuContent>
</DropdownMenuPortal>
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 865f55cfee..dfc0bcc956 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
@@ -3,8 +3,15 @@ import { styleConfigByName } from "../../shared/configs";
import { PropertyTooltip } from "../../shared/property-name";
import type { ControlProps } from "../types";
import { getStyleSource } from "../../shared/style-info";
-import type { IconRecord } from "@webstudio-is/icons";
+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;
+};
export const ToggleControl = ({
property,
@@ -12,11 +19,9 @@ export const ToggleControl = ({
setProperty,
deleteProperty,
items,
- icons,
isAdvanced,
}: Omit<ControlProps, "items"> & {
- items: [string, string];
- icons: IconRecord;
+ items: [Item, Item];
}) => {
const { label } = styleConfigByName(property);
const styleValue = currentStyle[property]?.value;
@@ -26,11 +31,10 @@ export const ToggleControl = ({
}
// First item is the pressed state
- const isPressed = items[0] === styleValue.value ? true : false;
- const Icon = icons[styleValue.value] ?? icons[items[0]];
- isAdvanced =
- isAdvanced ??
- (items[0] !== styleValue.value && items[1] !== styleValue.value);
+ const isPressed = items[0].name === styleValue.value ? true : false;
+ const currentItem = items.find((item) => item.name === styleValue.value);
+ const Icon = currentItem?.icon ?? items[0].icon;
+ isAdvanced = isAdvanced ?? currentItem === undefined;
return (
<AdvancedValueTooltip
@@ -42,6 +46,7 @@ export const ToggleControl = ({
>
<PropertyTooltip
title={label}
+ scrollableContent={`${toKebabCase(property)}: ${styleValue.value};`}
properties={[property]}
style={currentStyle}
onReset={() => deleteProperty(property)}
@@ -52,7 +57,7 @@ export const ToggleControl = ({
onPressedChange={(isPressed) => {
setProperty(property)({
type: "keyword",
- value: isPressed ? items[0] : items[1],
+ value: isPressed ? items[0].name : items[1].name,
});
}}
variant={getStyleSource(currentStyle[property])}
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 f95cc5c408..3dae9ec1d0 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
@@ -22,6 +22,19 @@ import {
AICenterIcon,
JCCenterIcon,
ACCenterIcon,
+ AIStartIcon,
+ AIEndIcon,
+ AIBaselineIcon,
+ AIStretchIcon,
+ JCStartIcon,
+ JCEndIcon,
+ JCSpaceBetweenIcon,
+ JCSpaceAroundIcon,
+ ACStartIcon,
+ ACEndIcon,
+ ACSpaceAroundIcon,
+ ACSpaceBetweenIcon,
+ ACStretchIcon,
} from "@webstudio-is/icons";
import type { SectionProps } from "../shared/section";
import { FlexGrid } from "./shared/flex-grid";
@@ -339,24 +352,38 @@ const LayoutSectionFlex = ({
<Flex css={{ gap: theme.spacing[7] }}>
<MenuControl
property="flexDirection"
- icons={{
- row: ArrowRightIcon,
- "row-reverse": ArrowLeftIcon,
- column: ArrowDownIcon,
- "column-reverse": ArrowUpIcon,
- }}
- DefaultIcon={ArrowRightIcon}
+ items={[
+ { name: "row", label: "Row", icon: ArrowRightIcon },
+ {
+ name: "row-reverse",
+ label: "Row Reverse",
+ icon: ArrowLeftIcon,
+ },
+ { name: "column", label: "Column", icon: ArrowDownIcon },
+ {
+ name: "column-reverse",
+ label: "Column Reverse",
+ icon: ArrowUpIcon,
+ },
+ ]}
currentStyle={currentStyle}
setProperty={setProperty}
deleteProperty={deleteProperty}
/>
<ToggleControl
property="flexWrap"
- items={["wrap", "nowrap"]}
- icons={{
- wrap: WrapIcon,
- nowrap: NoWrapIcon,
- }}
+ items={[
+ {
+ name: "nowrap",
+ label: "No Wrap",
+ icon: NoWrapIcon,
+ },
+ {
+ name: "wrap",
+ label: "Wrap",
+ icon: WrapIcon,
+ },
+ ]}
currentStyle={currentStyle}
setProperty={setProperty}
deleteProperty={deleteProperty}
@@ -368,7 +395,13 @@ const LayoutSectionFlex = ({
currentStyle={mapNormalTo(currentStyle, "alignItems", "stretch")}
setProperty={setProperty}
deleteProperty={deleteProperty}
- DefaultIcon={AICenterIcon}
+ items={[
+ { name: "stretch", label: "Stretch", icon: AIStretchIcon },
+ { name: "baseline", label: "Baseline", icon: AIBaselineIcon },
+ { name: "center", label: "Center", icon: AICenterIcon },
+ { name: "start", label: "Start", icon: AIStartIcon },
+ { name: "end", label: "End", icon: AIEndIcon },
+ ]}
/>
<MenuControl
property="justifyContent"
@@ -379,7 +412,21 @@ const LayoutSectionFlex = ({
)}
setProperty={setProperty}
deleteProperty={deleteProperty}
- DefaultIcon={JCCenterIcon}
+ items={[
+ {
+ name: "space-between",
+ label: "Space Between",
+ icon: JCSpaceBetweenIcon,
+ },
+ {
+ name: "space-around",
+ label: "Space Around",
+ icon: JCSpaceAroundIcon,
+ },
+ { name: "center", label: "Center", icon: JCCenterIcon },
+ { name: "start", label: "Start", icon: JCStartIcon },
+ { name: "end", label: "End", icon: JCEndIcon },
+ ]}
/>
{showAlignContent && (
<MenuControl
@@ -391,7 +438,22 @@ const LayoutSectionFlex = ({
)}
setProperty={setProperty}
deleteProperty={deleteProperty}
- DefaultIcon={ACCenterIcon}
+ items={[
+ {
+ name: "space-between",
+ label: "Space Between",
+ icon: ACSpaceBetweenIcon,
+ },
+ {
+ name: "space-around",
+ label: "Space Around",
+ icon: ACSpaceAroundIcon,
+ },
+ { name: "stretch", label: "Stretch", icon: ACStretchIcon },
+ { name: "center", label: "Center", icon: ACCenterIcon },
+ { name: "start", label: "Start", icon: ACStartIcon },
+ { name: "end", label: "End", icon: ACEndIcon },
+ ]}
/>
)}
</Flex>
diff --git a/apps/builder/app/builder/features/style-panel/shared/configs.ts b/apps/builder/app/builder/features/style-panel/shared/configs.ts
index 8bd1530446..4e0f2040bd 100644
--- a/apps/builder/app/builder/features/style-panel/shared/configs.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/configs.ts
@@ -1,32 +1,6 @@
import type { StyleProperty } from "@webstudio-is/css-engine";
import { keywordValues, properties } from "@webstudio-is/css-data";
import { humanizeString } from "~/shared/string-utils";
-import {
- type IconRecords,
- JustifyItemsStartIcon,
- JustifyItemsEndIcon,
- JustifyItemsCenterIcon,
- ACStartIcon,
- ACEndIcon,
- ACCenterIcon,
- ACStretchIcon,
- ACSpaceAroundIcon,
- ACSpaceBetweenIcon,
- AIStartIcon,
- AIEndIcon,
- AICenterIcon,
- AIBaselineIcon,
- AIStretchIcon,
- NoWrapIcon,
- WrapIcon,
- JCStartIcon,
- JCEndIcon,
- JCCenterIcon,
- JCSpaceBetweenIcon,
- JCSpaceAroundIcon,
- GapHorizontalIcon,
- GapVerticalIcon,
-} from "@webstudio-is/icons";
import type { AppliesTo } from "./dependencies";
import type * as Controls from "../controls";
@@ -94,158 +68,3 @@ export const styleConfigByName = (propertyName: StyleProperty): StyleConfig => {
return result;
};
-
-export const iconConfigs: IconRecords = {
- // layout
- alignContent: {
- start: ACStartIcon,
- end: ACEndIcon,
- center: ACCenterIcon,
- stretch: ACStretchIcon,
- "space-around": ACSpaceAroundIcon,
- "space-between": ACSpaceBetweenIcon,
- },
- alignItems: {
- start: AIStartIcon,
- end: AIEndIcon,
- center: AICenterIcon,
- baseline: AIBaselineIcon,
- stretch: AIStretchIcon,
- },
- flexWrap: {
- nowrap: NoWrapIcon,
- wrap: WrapIcon,
- },
- justifyContent: {
- start: JCStartIcon,
- center: JCCenterIcon,
- end: JCEndIcon,
- "space-between": JCSpaceBetweenIcon,
- "space-around": JCSpaceAroundIcon,
- },
- justifyItems: {
- normal: JustifyItemsStartIcon,
- start: JustifyItemsStartIcon,
- end: JustifyItemsEndIcon,
- center: JustifyItemsCenterIcon,
- },
- rowGap: {
- normal: GapVerticalIcon,
- },
- columnGap: {
- normal: GapHorizontalIcon,
- },
- // // flex child
- // flexShrink: {
- // normal: ColumnGapIcon,
- // },
- // flexGrow: {
- // normal: ColumnGapIcon,
- // },
- // flexBasis: {
- // normal: ColumnGapIcon,
- // },
- // // grid child
- // alignSelf: {
- // normal: ColumnGapIcon,
- // },
- // order: {
- // normal: ColumnGapIcon,
- // },
- // justifySelf: {
- // normal: ColumnGapIcon,
- // },
- // // size
- // width: {
- // normal: ColumnGapIcon,
- // },
- // height: {
- // normal: ColumnGapIcon,
- // },
- // minWidth: {
- // normal: ColumnGapIcon,
- // },
- // minHeight: {
- // normal: ColumnGapIcon,
- // },
- // maxWidth: {
- // normal: ColumnGapIcon,
- // },
- // maxHeight: {
- // normal: ColumnGapIcon,
- // },
- // overflow: {
- // normal: ColumnGapIcon,
- // },
- // objectFit: {
- // normal: ColumnGapIcon,
- // },
- // // position
- // position: {
- // normal: ColumnGapIcon,
- // },
- // float: {
- // normal: ColumnGapIcon,
- // },
- // // typography
- // fontFamily: {
- // normal: ColumnGapIcon,
- // },
- // fontWeight: {
- // normal: ColumnGapIcon,
- // },
- // fontSize: {
- // normal: ColumnGapIcon,
- // },
- // lineHeight: {
- // normal: ColumnGapIcon,
- // },
- // color: {
- // normal: ColumnGapIcon,
- // },
- // textAlign: {
- // normal: ColumnGapIcon,
- // },
- // fontStyle: {
- // normal: ColumnGapIcon,
- // },
- // textDecorationColor: {
- // normal: ColumnGapIcon,
- // },
- // textDecorationLine: {
- // normal: ColumnGapIcon,
- // },
- // textDecorationStyle: {
- // normal: ColumnGapIcon,
- // },
- // textIndent: {
- // normal: ColumnGapIcon,
- // },
- // letterSpacing: {
- // normal: ColumnGapIcon,
- // },
- // columnCount: {
- // normal: ColumnGapIcon,
- // },
- // columnRuleStyle: {
- // normal: ColumnGapIcon,
- // },
- // columnRuleWidth: {
- // normal: ColumnGapIcon,
- // },
- // columnRuleColor: {
- // normal: ColumnGapIcon,
- // },
- // textTransform: {
- // normal: ColumnGapIcon,
- // },
- // direction: {
- // normal: ColumnGapIcon,
- // },
- // whiteSpace: {
- // normal: ColumnGapIcon,
- // },
- // textShadow: {
- // normal: ColumnGapIcon,
- // },
-};