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.

feat: add value descriptions to all style panel select boxes (#3031)

Oleg Isonenab4e7a284ce7
diff --git a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
index 6b4018d60c..d36e2ab594 100644
--- a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
@@ -1,8 +1,8 @@
import { toValue } from "@webstudio-is/css-engine";
-import { Select } from "@webstudio-is/design-system";
+import { Box, Select, theme } from "@webstudio-is/design-system";
import { styleConfigByName } from "../../shared/configs";
import { toPascalCase } from "../../shared/keyword-utils";
-import { parseCssValue } from "@webstudio-is/css-data";
+import { declarationDescriptions, parseCssValue } from "@webstudio-is/css-data";
import type { ControlProps } from "../../style-sections";
export const SelectControl = ({
@@ -17,6 +17,7 @@ export const SelectControl = ({
const setValue = setProperty(property);
const options = (items ?? defaultItems).map(({ name }) => name);
const value = toValue(styleValue);
+
// Append selected value when not present in the list of options
// because radix requires values to always be in the list.
if (options.includes(value) === false) {
@@ -52,6 +53,16 @@ export const SelectControl = ({
deleteProperty(property, { isEphemeral: true });
}
}}
+ getDescription={(option) => {
+ const key =
+ `${property}:${option}` as keyof typeof declarationDescriptions;
+
+ return (
+ <Box css={{ width: theme.spacing[25] }}>
+ {declarationDescriptions[key]}
+ </Box>
+ );
+ }}
/>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
index 6d76d22ccc..c46299d481 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -12,6 +12,8 @@ import {
NestedIconLabel,
InputField,
handleNumericInputArrowKeys,
+ theme,
+ ComboboxItemDescription,
} from "@webstudio-is/design-system";
import type {
KeywordValue,
@@ -26,6 +28,7 @@ import {
useCallback,
useEffect,
useRef,
+ useState,
} from "react";
import { useUnitSelect } from "./unit-select";
import { parseIntermediateOrInvalidValue } from "./parse-intermediate-or-invalid-value";
@@ -33,7 +36,11 @@ import { toValue } from "@webstudio-is/css-engine";
import { useDebouncedCallback } from "use-debounce";
import type { StyleSource } from "../style-info";
import { toPascalCase } from "../keyword-utils";
-import { isValidDeclaration, properties } from "@webstudio-is/css-data";
+import {
+ declarationDescriptions,
+ isValidDeclaration,
+ properties,
+} from "@webstudio-is/css-data";
import {
$selectedInstanceBrowserStyle,
$selectedInstanceUnitSizes,
@@ -359,6 +366,10 @@ export const CssValueInput = ({
...props
}: CssValueInputProps) => {
const value = props.intermediateValue ?? props.value ?? initialValue;
+ // Used to show description
+ const [highlightedValue, setHighlighedValue] = useState<
+ StyleValue | undefined
+ >();
const onChange = (input: string | undefined) => {
if (input === undefined) {
@@ -406,6 +417,8 @@ export const CssValueInput = ({
isOpen,
highlightedIndex,
} = useCombobox<CssValueInputValue>({
+ // Used for description to match the item when nothing is highlighted yet and value is still in non keyword mode
+ defaultHighlightedIndex: 0,
items: keywords,
value,
selectedItem: props.value,
@@ -420,11 +433,13 @@ export const CssValueInput = ({
onItemHighlight: (value) => {
if (value == null) {
onHighlight(undefined);
+ setHighlighedValue(undefined);
return;
}
if (value.type !== "intermediate") {
onHighlight(value);
+ setHighlighedValue(value);
}
},
});
@@ -618,6 +633,23 @@ export const CssValueInput = ({
</Box>
);
+ let description;
+ // When user hovers or focuses an item in the combobox list we want to show the description of the item and otherwise show the description of the current value
+ const valueForDescription =
+ highlightedValue?.type === "keyword"
+ ? highlightedValue
+ : props.value?.type === "keyword"
+ ? props.value
+ : items[0]?.type === "keyword"
+ ? items[0]
+ : undefined;
+ if (valueForDescription) {
+ const key = `${property}:${toValue(
+ valueForDescription
+ )}` as keyof typeof declarationDescriptions;
+ description = declarationDescriptions[key];
+ }
+
return (
<Combobox open={isOpen}>
<Box {...getComboboxProps()}>
@@ -653,6 +685,11 @@ export const CssValueInput = ({
{itemToString(item)}
</ComboboxListboxItem>
))}
+ {description && (
+ <ComboboxItemDescription>
+ <Box css={{ width: theme.spacing[25] }}>{description}</Box>
+ </ComboboxItemDescription>
+ )}
</ComboboxListbox>
</ComboboxContent>
)}
diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
index d75502bdd2..211fbfb86c 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
@@ -399,7 +399,7 @@ const renderMenuItems = (props: {
Style instances without creating a token or override a token locally.
</DropdownMenuItem>
)}
- {props.item.source !== "local" && (
+ {props.item.source === "token" && (
<DropdownMenuItem hint>
Reuse styles across multiple instances by creating a token.
</DropdownMenuItem>
diff --git a/packages/design-system/src/components/combobox.stories.tsx b/packages/design-system/src/components/combobox.stories.tsx
index d88af13a09..5eaf0473c9 100644
--- a/packages/design-system/src/components/combobox.stories.tsx
+++ b/packages/design-system/src/components/combobox.stories.tsx
@@ -1,25 +1,29 @@
import { MagnifyingGlassIcon } from "@webstudio-is/icons";
import { useCallback, useState } from "react";
-import { DeprecatedTextField } from "./__DEPRECATED__/text-field";
+import type {
+ UseComboboxState,
+ UseComboboxStateChangeOptions,
+} from "downshift";
import {
ComboboxListboxItem,
useCombobox,
comboboxStateChangeTypes,
+ ComboboxContent,
+ Combobox,
+ ComboboxListbox,
+ ComboboxAnchor,
+ ComboboxItemDescription,
} from "./combobox";
import { Flex } from "./flex";
-import { theme } from "../stitches.config";
-import type {
- UseComboboxState,
- UseComboboxStateChangeOptions,
-} from "downshift";
+import { InputField } from "./input-field";
export const Complex = () => {
- const [value, setValue] = useState<string | null>(null);
+ const [value, setValue] = useState("");
const stateReducer = useCallback(
(
- state: UseComboboxState<string | null>,
- actionAndChanges: UseComboboxStateChangeOptions<string | null>
+ state: UseComboboxState<string>,
+ actionAndChanges: UseComboboxStateChangeOptions<string>
) => {
const { type, changes } = actionAndChanges;
switch (type) {
@@ -51,44 +55,57 @@ export const Complex = () => {
[]
);
- const { items, getComboboxProps, getMenuProps, getItemProps, getInputProps } =
- useCombobox<string | null>({
- items: ["Apple", "Banana", "Orange"],
- value,
- selectedItem: value,
- itemToString: (item) => item ?? "",
- stateReducer,
- onItemSelect: (value) => {
+ const {
+ items,
+ getComboboxProps,
+ getMenuProps,
+ getItemProps,
+ getInputProps,
+ isOpen,
+ } = useCombobox<string>({
+ items: ["Apple", "Banana", "Orange"],
+ value,
+ selectedItem: value,
+ itemToString: (item) => item ?? "",
+ stateReducer,
+ onItemSelect: setValue,
+ onInputChange: (value) => {
+ if (value) {
setValue(value);
- },
- });
+ }
+ },
+ });
return (
- <Flex
- {...getComboboxProps()}
- css={{ flexDirection: "column", gap: theme.spacing[9] }}
- >
- <DeprecatedTextField
- type="search"
- prefix={<MagnifyingGlassIcon />}
- {...getInputProps({})}
- />
- <fieldset>
- <legend>Choose an item</legend>
- <Flex {...getMenuProps()} css={{ flexDirection: "column" }}>
- {items.map((item, index) => {
- return (
- <ComboboxListboxItem
- key={index}
- {...getItemProps({ item, index })}
- >
- {item}
- </ComboboxListboxItem>
- );
- })}
- </Flex>
- </fieldset>
- </Flex>
+ <Combobox open={isOpen}>
+ <Flex {...getComboboxProps()} direction="column" gap="3">
+ <ComboboxAnchor>
+ <InputField
+ prefix={
+ <Flex align="center">
+ <MagnifyingGlassIcon />
+ </Flex>
+ }
+ {...getInputProps({ value })}
+ />
+ </ComboboxAnchor>
+ <ComboboxContent>
+ <ComboboxListbox {...getMenuProps()}>
+ {items.map((item, index) => {
+ return (
+ <ComboboxListboxItem
+ key={index}
+ {...getItemProps({ item, index })}
+ >
+ {item}
+ </ComboboxListboxItem>
+ );
+ })}
+ <ComboboxItemDescription>Description</ComboboxItemDescription>
+ </ComboboxListbox>
+ </ComboboxContent>
+ </Flex>
+ </Combobox>
);
};
diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsx
index b05cf6f236..2594323321 100644
--- a/packages/design-system/src/components/combobox.tsx
+++ b/packages/design-system/src/components/combobox.tsx
@@ -34,13 +34,16 @@ import {
separatorCss,
MenuCheckedIcon,
} from "./menu";
+import { ScrollArea } from "./scroll-area";
+import { Flex } from "./flex";
const Listbox = styled(
"ul",
{
+ display: "flex",
+ flexDirection: "column",
margin: "unset", // reset <ul>
- overflow: "auto",
- maxHeight: theme.spacing[34],
+ listStyle: "none",
variants: {
state: { closed: { display: "none" } },
empty: { true: { display: "none" } },
@@ -90,26 +93,87 @@ const ListboxItemBase: ForwardRefRenderFunction<
);
};
-export const ComboboxListbox = Listbox;
+export const ComboboxListbox = forwardRef<
+ HTMLUListElement,
+ ComponentProps<typeof Listbox>
+>(({ children, ...props }, ref) => {
+ return (
+ <Listbox {...props} ref={ref}>
+ <ScrollArea>
+ <Flex direction="column" css={{ maxHeight: theme.spacing[34] }}>
+ {children}
+ </Flex>
+ </ScrollArea>
+ </Listbox>
+ );
+});
+
+ComboboxListbox.displayName = "ComboboxListbox";
export const ComboboxListboxItem = forwardRef(ListboxItemBase);
+export const ComboboxItemDescription = ({
+ children,
+ style,
+ ...props
+}: ComponentProps<typeof ListboxItem>) => {
+ return (
+ <>
+ <ComboboxSeparator
+ style={{
+ display: `var(--ws-combobox-description-display-bottom, none)`,
+ order: "var(--ws-combobox-description-order)",
+ }}
+ />
+ <ListboxItem
+ {...props}
+ hint
+ style={{ ...style, order: "var(--ws-combobox-description-order)" }}
+ >
+ {children}
+ </ListboxItem>
+ <ComboboxSeparator
+ style={{
+ display: `var(--ws-combobox-description-display-top, none)`,
+ order: "var(--ws-combobox-description-order)",
+ }}
+ />
+ </>
+ );
+};
+
export const Combobox = (props: ComponentProps<typeof Popover>) => {
return <Popover {...props} modal />;
};
+const StyledPopoverContent = styled(PopoverContent, {
+ "&[data-side=top]": {
+ "--ws-combobox-description-display-top": "block",
+ "--ws-combobox-description-order": 0,
+ },
+ "&[data-side=bottom]": {
+ "--ws-combobox-description-display-bottom": "block",
+ "--ws-combobox-description-order": 2,
+ },
+});
+
export const ComboboxContent = forwardRef(
- (props: ComponentProps<typeof PopoverContent>, ref: Ref<HTMLDivElement>) => (
- <Portal>
- <PopoverContent
- ref={ref}
- onOpenAutoFocus={(event) => {
- event.preventDefault();
- }}
- {...props}
- />
- </Portal>
- )
+ (
+ { style, ...props }: ComponentProps<typeof PopoverContent>,
+ forwardRef: Ref<HTMLDivElement>
+ ) => {
+ return (
+ <Portal>
+ <StyledPopoverContent
+ ref={forwardRef}
+ onOpenAutoFocus={(event) => {
+ event.preventDefault();
+ }}
+ {...props}
+ />
+ </Portal>
+ );
+ }
);
ComboboxContent.displayName = "ComboboxContent";
diff --git a/packages/design-system/src/components/menu.tsx b/packages/design-system/src/components/menu.tsx
index fd28d6b4e9..eeb430bbca 100644
--- a/packages/design-system/src/components/menu.tsx
+++ b/packages/design-system/src/components/menu.tsx
@@ -50,6 +50,7 @@ export const menuItemCss = css(textVariants.labelsTitleCase, {
cursor: "default",
position: "relative",
display: "flex",
+ order: 1,
alignItems: "center",
color: theme.colors.foregroundMain,
mx: itemMargin,
diff --git a/packages/design-system/src/components/select.tsx b/packages/design-system/src/components/select.tsx
index 706e28ed5f..f52e622136 100644
--- a/packages/design-system/src/components/select.tsx
+++ b/packages/design-system/src/components/select.tsx
@@ -8,7 +8,7 @@ import {
useState,
} from "react";
import { ChevronDownIcon, ChevronUpIcon } from "@webstudio-is/icons";
-import { styled, theme } from "../stitches.config";
+import { rawTheme, styled, theme } from "../stitches.config";
import {
menuCss,
menuItemCss,
@@ -19,7 +19,16 @@ import {
} from "./menu";
import { SelectButton } from "./select-button";
-export const SelectContent = styled(Primitive.Content, menuCss);
+export const SelectContent = styled(Primitive.Content, menuCss, {
+ "&[data-side=top]": {
+ "--ws-select-description-display-top": "block",
+ "--ws-select-description-order": 0,
+ },
+ "&[data-side=bottom]": {
+ "--ws-select-description-display-bottom": "block",
+ "--ws-select-description-order": 2,
+ },
+});
export const SelectViewport = Primitive.Viewport;
@@ -76,7 +85,41 @@ export const SelectItem = forwardRef(SelectItemBase);
export type SelectOption = string;
-const Description = styled(menuItemCss);
+const Description = styled("div", menuItemCss);
+
+// Note this only works in combination with position: popper on Content component, because only popper exposes data-side attribute
+export const SelectItemDescription = ({
+ children,
+ style,
+ ...props
+}: ComponentProps<typeof Description>) => {
+ return (
+ <>
+ <SelectSeparator
+ style={{
+ display: `var(--ws-select-description-display-bottom, none)`,
+ order: "var(--ws-select-description-order)",
+ }}
+ />
+ <Description
+ {...props}
+ hint
+ style={{
+ ...style,
+ order: "var(--ws-select-description-order)",
+ }}
+ >
+ {children}
+ </Description>
+ <SelectSeparator
+ style={{
+ display: `var(--ws-select-description-display-top, none)`,
+ order: "var(--ws-select-description-order)",
+ }}
+ />
+ </>
+ );
+};
type TriggerPassThroughProps = Omit<
ComponentProps<typeof Primitive.Trigger>,
@@ -142,7 +185,8 @@ const SelectBase = <Option,>(
}
return map;
}, [options, getValue]);
- const [highlightedItem, setHighlightedItem] = useState<Option | undefined>();
+
+ const [highlightedItem, setHighlightedItem] = useState<Option>();
const itemForDescription = highlightedItem ?? value ?? defaultValue;
const description = itemForDescription
@@ -171,11 +215,15 @@ const SelectBase = <Option,>(
</SelectButton>
</Primitive.Trigger>
<Primitive.Portal>
- <SelectContent css={{ zIndex: props.css?.zIndex ?? 0 }}>
- <SelectScrollUpButton>
+ <SelectContent
+ position="popper"
+ css={{ zIndex: props.css?.zIndex ?? 0 }}
+ >
+ <SelectScrollUpButton css={{ order: 1 }}>
<ChevronUpIcon />
</SelectScrollUpButton>
- <SelectViewport>
+
+ <SelectViewport style={{ order: 1, maxHeight: rawTheme.spacing[34] }}>
{children ||
options.map((option) => (
<SelectItem
@@ -199,10 +247,14 @@ const SelectBase = <Option,>(
</SelectItem>
))}
</SelectViewport>
- <SelectScrollDownButton>
+
+ <SelectScrollDownButton css={{ order: 2 }}>
<ChevronDownIcon />
</SelectScrollDownButton>
- {description && <Description hint>{description}</Description>}
+
+ {description && (
+ <SelectItemDescription>{description}</SelectItemDescription>
+ )}
</SelectContent>
</Primitive.Portal>
</Primitive.Root>