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.

Add property tooltip to icon controls (#1831)

Bogdan Chadkin9976b7e9756b
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 041f5aa73d..5dc266ccbd 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,22 +1,33 @@
-import { IconButtonWithMenu } from "@webstudio-is/design-system";
+import { toValue } from "@webstudio-is/css-engine";
+import {
+ DropdownMenu,
+ DropdownMenuArrow,
+ DropdownMenuContent,
+ DropdownMenuPortal,
+ DropdownMenuRadioGroup,
+ DropdownMenuRadioItem,
+ DropdownMenuTrigger,
+ Flex,
+ IconButton,
+ theme,
+} from "@webstudio-is/design-system";
import type { ControlProps } from "../../style-sections";
import { iconConfigs, styleConfigByName } from "../../shared/configs";
-import { toValue } from "@webstudio-is/css-engine";
-import { getStyleSource, type StyleValueInfo } from "../../shared/style-info";
+import { getStyleSource } from "../../shared/style-info";
+import { PropertyTooltip } from "../../shared/property-name";
export const MenuControl = ({
+ currentStyle,
property,
items: passedItems,
- styleValue,
setProperty,
deleteProperty,
}: Pick<
ControlProps,
- "property" | "items" | "setProperty" | "deleteProperty"
-> & {
- styleValue?: StyleValueInfo;
-}) => {
+ "currentStyle" | "property" | "items" | "setProperty" | "deleteProperty"
+>) => {
const { label, items: defaultItems } = styleConfigByName(property);
+ const styleValue = currentStyle[property];
const value = styleValue?.value;
const styleSource = getStyleSource(styleValue);
@@ -37,19 +48,71 @@ export const MenuControl = ({
.filter((item) => item.icon);
return (
- <IconButtonWithMenu
- variant={styleSource}
- icon={items.find(({ name }) => name === currentValue)?.icon}
- label={label}
- items={items}
- value={String(currentValue)}
- onChange={(value) => setValue({ type: "keyword", value })}
- onHover={(value) =>
- setValue({ type: "keyword", value }, { isEphemeral: true })
- }
- onReset={() => {
- deleteProperty(property);
- }}
- />
+ <DropdownMenu modal={false}>
+ <DropdownMenuTrigger asChild>
+ <div>
+ <PropertyTooltip
+ title={label}
+ properties={[property]}
+ style={currentStyle}
+ onReset={() => deleteProperty(property)}
+ >
+ <IconButton
+ variant={styleSource}
+ onPointerDown={(event) => {
+ // tooltip reset property when click with altKey
+ if (event.altKey) {
+ event.preventDefault();
+ }
+ }}
+ >
+ {items.find(({ name }) => name === currentValue)?.icon ?? <></>}
+ </IconButton>
+ </PropertyTooltip>
+ </div>
+ </DropdownMenuTrigger>
+ <DropdownMenuPortal>
+ <DropdownMenuContent sideOffset={4} collisionPadding={16} side="bottom">
+ <DropdownMenuRadioGroup
+ value={currentValue}
+ onValueChange={(value) => setValue({ type: "keyword", value })}
+ >
+ {items.map(({ name, label, icon }) => {
+ return (
+ <DropdownMenuRadioItem
+ key={name}
+ value={label}
+ onFocus={() =>
+ setValue(
+ { type: "keyword", value: name },
+ { isEphemeral: true }
+ )
+ }
+ onBlur={() =>
+ setValue(
+ { type: "keyword", value: currentValue },
+ { isEphemeral: true }
+ )
+ }
+ >
+ <Flex
+ css={{
+ width: theme.spacing[11],
+ height: theme.spacing[11],
+ }}
+ align="center"
+ justify="center"
+ >
+ {icon}
+ </Flex>
+ {label}
+ </DropdownMenuRadioItem>
+ );
+ })}
+ </DropdownMenuRadioGroup>
+ <DropdownMenuArrow />
+ </DropdownMenuContent>
+ </DropdownMenuPortal>
+ </DropdownMenu>
);
};
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 9a0a6fb38e..b0a13143a5 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,14 +22,10 @@ import {
import type { RenderCategoryProps } from "../../style-sections";
import { FlexGrid } from "./shared/flex-grid";
import { MenuControl, SelectControl } from "../../controls";
-import { PropertyName } from "../../shared/property-name";
+import { PropertyName, PropertyTooltip } from "../../shared/property-name";
import { styleConfigByName } from "../../shared/configs";
import type { CreateBatchUpdate } from "../../shared/use-style-data";
-import {
- getStyleSource,
- type StyleInfo,
- type StyleValueInfo,
-} from "../../shared/style-info";
+import { getStyleSource, type StyleInfo } from "../../shared/style-info";
import { CollapsibleSection } from "../../shared/collapsible-section";
import {
type IntermediateStyleValue,
@@ -261,12 +257,22 @@ const FlexGap = ({
};
const mapNormalTo = (
- toValue: string,
- current?: StyleValueInfo
-): StyleValueInfo | undefined =>
- current?.value.type === "keyword" && current?.value.value === "normal"
- ? { ...current, value: { type: "keyword", value: toValue } }
- : current;
+ 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 Toggle = ({
property,
@@ -293,14 +299,13 @@ const Toggle = ({
styleValue?.type === "keyword" && styleValue?.value === valueOn;
return (
- <EnhancedTooltip content={label}>
+ <PropertyTooltip
+ title={label}
+ properties={[property]}
+ style={currentStyle}
+ onReset={() => deleteProperty(property)}
+ >
<ToggleButton
- onClick={(event) => {
- if (event.altKey) {
- event.preventDefault();
- deleteProperty(property);
- }
- }}
pressed={isPressed}
onPressedChange={(isPressed) => {
setProperty(property)({
@@ -312,7 +317,7 @@ const Toggle = ({
>
{isPressed ? iconOn : iconOff}
</ToggleButton>
- </EnhancedTooltip>
+ </PropertyTooltip>
);
};
@@ -367,20 +372,28 @@ const LayoutSectionFlex = ({
<Flex css={{ gap: theme.spacing[7] }}>
<MenuControl
property="alignItems"
- styleValue={mapNormalTo("stretch", currentStyle.alignItems)}
+ currentStyle={mapNormalTo(currentStyle, "alignItems", "stretch")}
setProperty={setProperty}
deleteProperty={deleteProperty}
/>
<MenuControl
property="justifyContent"
- styleValue={mapNormalTo("start", currentStyle.justifyContent)}
+ currentStyle={mapNormalTo(
+ currentStyle,
+ "justifyContent",
+ "start"
+ )}
setProperty={setProperty}
deleteProperty={deleteProperty}
/>
{showAlignContent && (
<MenuControl
property="alignContent"
- styleValue={mapNormalTo("stretch", currentStyle.alignContent)}
+ currentStyle={mapNormalTo(
+ currentStyle,
+ "alignContent",
+ "stretch"
+ )}
setProperty={setProperty}
deleteProperty={deleteProperty}
/>
diff --git a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
index 39ebc3c50b..6143106fe5 100644
--- a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
@@ -112,14 +112,12 @@ const TooltipContent = ({
properties,
style,
onReset,
- onClose,
}: {
- title: string;
+ title?: string;
description?: React.ReactNode;
properties: StyleProperty[];
style: StyleInfo;
onReset?: undefined | (() => void);
- onClose: () => void;
}) => {
const breakpoints = useStore(breakpointsStore);
const selectedBreakpoint = useStore(selectedBreakpointStore);
@@ -155,7 +153,7 @@ const TooltipContent = ({
return (
<Flex direction="column" gap="2" css={{ maxWidth: theme.spacing[28] }}>
- <Text variant="titles">{title}</Text>
+ <Text variant="titles">{title ?? humanizeString(properties[0])}</Text>
<ScrollArea>
<Text
variant="monoBold"
@@ -199,14 +197,7 @@ const TooltipContent = ({
color="dark"
prefix={<ResetIcon />}
css={{ flexGrow: 1 }}
- onMouseDown={(event) => {
- // Prevent closing tooltip
- event.preventDefault();
- }}
- onClickCapture={() => {
- onReset();
- onClose();
- }}
+ onClick={onReset}
>
Reset value
</Button>
@@ -215,6 +206,60 @@ const TooltipContent = ({
);
};
+export const PropertyTooltip = ({
+ openWithClick = false,
+ title,
+ description,
+ properties,
+ style,
+ onReset,
+ children,
+}: {
+ openWithClick?: boolean;
+ title?: string;
+ description?: React.ReactNode;
+ properties: StyleProperty[];
+ style: StyleInfo;
+ onReset?: undefined | (() => void);
+ children: ReactElement;
+}) => {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <Tooltip
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ // prevent closing tooltip on content click
+ onPointerDown={(event) => event.preventDefault()}
+ triggerProps={{
+ onClick: (event) => {
+ if (event.altKey) {
+ event.preventDefault();
+ onReset?.();
+ return;
+ }
+ if (openWithClick) {
+ setIsOpen(true);
+ }
+ },
+ }}
+ content={
+ <TooltipContent
+ title={title}
+ description={description}
+ properties={properties}
+ style={style}
+ onReset={() => {
+ onReset?.();
+ setIsOpen(false);
+ }}
+ />
+ }
+ >
+ {children}
+ </Tooltip>
+ );
+};
+
type PropertyNameInternalProps = {
style: StyleInfo;
properties: StyleProperty[];
@@ -234,43 +279,20 @@ const PropertyNameInternal = ({
onReset,
disabled,
}: PropertyNameInternalProps) => {
- const [isOpen, setIsOpen] = useState(false);
// When we have multiple properties, they must be originating from the same source, so we can just use one.
const property = properties[0];
return (
<Flex align="center">
- <Tooltip
- open={isOpen}
- onOpenChange={setIsOpen}
- content={
- <TooltipContent
- title={
- title ??
- (typeof label === "string" ? label : humanizeString(property))
- }
- description={description}
- properties={properties}
- style={style}
- onReset={onReset}
- onClose={() => {
- setIsOpen(false);
- }}
- />
- }
+ <PropertyTooltip
+ openWithClick={true}
+ title={title ?? (typeof label === "string" ? label : undefined)}
+ description={description}
+ properties={properties}
+ style={style}
+ onReset={onReset}
>
- <Flex
- shrink
- gap={1}
- align="center"
- onClick={(event) => {
- if (event.altKey) {
- onReset?.();
- return;
- }
- setIsOpen(true);
- }}
- >
+ <Flex shrink gap={1} align="center">
{typeof label === "string" && property ? (
<Label
color={
@@ -287,7 +309,7 @@ const PropertyNameInternal = ({
label
)}
</Flex>
- </Tooltip>
+ </PropertyTooltip>
</Flex>
);
};
diff --git a/packages/design-system/src/components/icon-button-with-menu.tsx b/packages/design-system/src/components/icon-button-with-menu.tsx
deleted file mode 100644
index 7493b3409b..0000000000
--- a/packages/design-system/src/components/icon-button-with-menu.tsx
+++ /dev/null
@@ -1,87 +0,0 @@
-import type { IconComponent } from "@webstudio-is/icons";
-import {
- DropdownMenu,
- DropdownMenuTrigger,
- DropdownMenuPortal,
- DropdownMenuContent,
- DropdownMenuArrow,
- DropdownMenuRadioGroup,
- DropdownMenuRadioItem,
-} from "./dropdown-menu";
-import { IconButton } from "./icon-button";
-import { Flex } from "./flex";
-import { theme } from "../stitches.config";
-import { EnhancedTooltip } from "./enhanced-tooltip";
-
-export const IconButtonWithMenu = ({
- variant,
- icon,
- label,
- value,
- items,
- onChange,
- onHover,
- onReset,
-}: {
- variant: "default" | "preset" | "local" | "overwritten" | "remote";
- icon?: JSX.Element;
- label?: string;
- value: string;
- items: Array<{
- label: string;
- name: string;
- icon?: ReturnType<IconComponent>;
- }>;
- onChange?: (value: string) => void;
- onHover?: (value: string) => void;
- onReset?: () => void;
-}) => {
- return (
- <DropdownMenu modal={false}>
- <EnhancedTooltip content={label}>
- <DropdownMenuTrigger asChild>
- <IconButton
- variant={variant}
- onPointerDown={(event) => {
- if (event.altKey) {
- event.preventDefault();
- onReset?.();
- }
- }}
- >
- {icon}
- </IconButton>
- </DropdownMenuTrigger>
- </EnhancedTooltip>
- <DropdownMenuPortal>
- <DropdownMenuContent sideOffset={4} collisionPadding={16} side="bottom">
- <DropdownMenuRadioGroup value={value} onValueChange={onChange}>
- {items.map(({ name, label, icon }) => {
- return (
- <DropdownMenuRadioItem
- key={name}
- value={label}
- onFocus={() => onHover?.(name)}
- onBlur={() => onHover?.(value)}
- >
- <Flex
- css={{
- width: theme.spacing[11],
- height: theme.spacing[11],
- }}
- align="center"
- justify="center"
- >
- {icon}
- </Flex>
- {label}
- </DropdownMenuRadioItem>
- );
- })}
- </DropdownMenuRadioGroup>
- <DropdownMenuArrow />
- </DropdownMenuContent>
- </DropdownMenuPortal>
- </DropdownMenu>
- );
-};
diff --git a/packages/design-system/src/components/tooltip.tsx b/packages/design-system/src/components/tooltip.tsx
index 80a18720d4..2e50927271 100644
--- a/packages/design-system/src/components/tooltip.tsx
+++ b/packages/design-system/src/components/tooltip.tsx
@@ -79,7 +79,9 @@ export const Tooltip = forwardRef(
ref={ref}
side="top"
align="center"
- sideOffset={5}
+ sideOffset={2}
+ collisionPadding={8}
+ arrowPadding={8}
{...props}
>
{typeof content === "string" ? <Text>{content}</Text> : content}
diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.ts
index 552de49c86..a46d4536f4 100644
--- a/packages/design-system/src/index.ts
+++ b/packages/design-system/src/index.ts
@@ -62,7 +62,6 @@ export {
useEnhancedTooltipProps,
} from "./components/enhanced-tooltip";
export * from "./components/avatar";
-export * from "./components/icon-button-with-menu";
export * from "./components/tree";
// No need to align