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.

Props panel UI (#1134)

Roman Pominov60a0c045852d
diff --git a/apps/builder/app/builder/features/inspector/inspector.tsx b/apps/builder/app/builder/features/inspector/inspector.tsx
index bbf5e233c9..2e1927b7c3 100644
--- a/apps/builder/app/builder/features/inspector/inspector.tsx
+++ b/apps/builder/app/builder/features/inspector/inspector.tsx
@@ -14,7 +14,7 @@ import {
EnhancedTooltipProvider,
} from "@webstudio-is/design-system";
import { StylePanel } from "~/builder/features/style-panel";
-import { PropsPanel } from "~/builder/features/props-panel";
+import { PropsPanelContainer } from "~/builder/features/props-panel";
import { FloatingPanelProvider } from "~/builder/shared/floating-panel";
import { theme } from "@webstudio-is/design-system";
import { selectedInstanceStore } from "~/shared/nano-states";
@@ -73,7 +73,7 @@ export const Inspector = ({ publish }: InspectorProps) => {
<StylePanel publish={publish} selectedInstance={selectedInstance} />
</TabsContent>
<TabsContent value="props" css={contentStyle}>
- <PropsPanel
+ <PropsPanelContainer
publish={publish}
key={selectedInstance.id /* Re-render when instance changes */}
selectedInstance={selectedInstance}
diff --git a/apps/builder/app/builder/features/props-panel/controls/boolean.tsx b/apps/builder/app/builder/features/props-panel/controls/boolean.tsx
index 9047d38857..8f0fd93149 100644
--- a/apps/builder/app/builder/features/props-panel/controls/boolean.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/boolean.tsx
@@ -1,6 +1,5 @@
-import { Button, Switch, Label, Flex } from "@webstudio-is/design-system";
-import { TrashIcon } from "@webstudio-is/icons";
-import { type ControlProps, getLabel } from "../shared";
+import { Switch, useId } from "@webstudio-is/design-system";
+import { type ControlProps, getLabel, HorizontalLayout } from "../shared";
export const BooleanControl = ({
meta,
@@ -8,18 +7,20 @@ export const BooleanControl = ({
propName,
onChange,
onDelete,
-}: ControlProps<"boolean", "boolean">) => (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex justify="between">
+}: ControlProps<"boolean", "boolean">) => {
+ const id = useId();
+
+ return (
+ <HorizontalLayout
+ id={id}
+ label={getLabel(meta, propName)}
+ onDelete={onDelete}
+ >
<Switch
- name={propName}
+ id={id}
checked={prop?.value ?? false}
onCheckedChange={(value) => onChange({ type: "boolean", value })}
/>
- {onDelete && (
- <Button color="ghost" prefix={<TrashIcon />} onClick={onDelete} />
- )}
- </Flex>
- </div>
-);
+ </HorizontalLayout>
+ );
+};
diff --git a/apps/builder/app/builder/features/props-panel/controls/check.tsx b/apps/builder/app/builder/features/props-panel/controls/check.tsx
index 1104398718..cd8c775dc4 100644
--- a/apps/builder/app/builder/features/props-panel/controls/check.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/check.tsx
@@ -1,12 +1,12 @@
import {
- Text,
- Flex,
+ Box,
Checkbox,
- Button,
- Label,
+ CheckboxAndLabel,
+ useId,
+ theme,
} from "@webstudio-is/design-system";
-import { TrashIcon } from "@webstudio-is/icons";
-import { type ControlProps, getLabel } from "../shared";
+import { humanizeString } from "~/shared/string-utils";
+import { type ControlProps, getLabel, VerticalLayout, Label } from "../shared";
const add = (array: string[], item: string) => {
if (array.includes(item)) {
@@ -29,16 +29,21 @@ export const CheckControl = ({
onChange,
onDelete,
}: ControlProps<"check" | "inline-check" | "multi-select", "string[]">) => {
- // @todo: handle the case when `value` contains something that isn't in `options`?
-
const value = prop?.value ?? [];
+ // making sure that the current value is in the list of options
+ const options = value.reduce(
+ (result, item) => (result.includes(item) ? result : [item, ...result]),
+ meta.options
+ );
+
+ const id = useId();
+
return (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1" justify="between">
- {meta.options.map((option) => (
- <Flex align="center" gap="1" key={option}>
+ <VerticalLayout label={getLabel(meta, propName)} onDelete={onDelete}>
+ <Box css={{ paddingTop: theme.spacing[2] }}>
+ {options.map((option) => (
+ <CheckboxAndLabel key={option}>
<Checkbox
checked={value.includes(option)}
onCheckedChange={(checked) => {
@@ -47,19 +52,12 @@ export const CheckControl = ({
value: checked ? add(value, option) : remove(value, option),
});
}}
+ id={`${id}:${option}`}
/>
- <Text>{value}</Text>
- </Flex>
+ <Label htmlFor={`${id}:${option}`}>{humanizeString(option)}</Label>
+ </CheckboxAndLabel>
))}
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
- />
- )}
- </Flex>
- </div>
+ </Box>
+ </VerticalLayout>
);
};
diff --git a/apps/builder/app/builder/features/props-panel/controls/color.tsx b/apps/builder/app/builder/features/props-panel/controls/color.tsx
index 4cff34ab50..2dfefb4a23 100644
--- a/apps/builder/app/builder/features/props-panel/controls/color.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/color.tsx
@@ -1,6 +1,14 @@
-import { TrashIcon } from "@webstudio-is/icons";
-import { TextField, Button, Label, Flex } from "@webstudio-is/design-system";
-import { type ControlProps, getLabel } from "../shared";
+import { TextField, useId } from "@webstudio-is/design-system";
+import {
+ type ControlProps,
+ getLabel,
+ useLocalValue,
+ HorizontalLayout,
+} from "../shared";
+
+// @todo:
+// use ColorPicker (need to refactor it first,
+// as it's currently tailored to work with styles only)
export const ColorControl = ({
meta,
@@ -8,26 +16,31 @@ export const ColorControl = ({
propName,
onChange,
onDelete,
-}: ControlProps<"color", "string">) => (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1">
+}: ControlProps<"color", "string">) => {
+ const id = useId();
+
+ const localValue = useLocalValue(prop?.value ?? "", (value) =>
+ onChange({ type: "string", value })
+ );
+
+ return (
+ <HorizontalLayout
+ label={getLabel(meta, propName)}
+ id={id}
+ onDelete={onDelete}
+ >
<TextField
- name={propName}
- value={prop?.value ?? ""}
- onChange={(event) => {
- onChange({ type: "string", value: event.target.value });
+ id={id}
+ value={localValue.value}
+ onChange={(event) => localValue.set(event.target.value)}
+ onBlur={localValue.save}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ localValue.save();
+ }
}}
- css={{ flexGrow: 1 }}
+ css={{ width: 120 }}
/>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
- />
- )}
- </Flex>
- </div>
-);
+ </HorizontalLayout>
+ );
+};
diff --git a/apps/builder/app/builder/features/props-panel/controls/file-image.tsx b/apps/builder/app/builder/features/props-panel/controls/file-image.tsx
index e688a3be5e..2230cf2403 100644
--- a/apps/builder/app/builder/features/props-panel/controls/file-image.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/file-image.tsx
@@ -1,12 +1,11 @@
import { useMemo } from "react";
import { computed } from "nanostores";
import { useStore } from "@nanostores/react";
-import { Button, Label, Flex } from "@webstudio-is/design-system";
-import { TrashIcon } from "@webstudio-is/icons";
+import { Button } from "@webstudio-is/design-system";
import { assetsStore } from "~/shared/nano-states";
import { FloatingPanel } from "~/builder/shared/floating-panel";
import { ImageManager } from "~/builder/shared/image-manager";
-import { type ControlProps, getLabel } from "../shared";
+import { type ControlProps, getLabel, VerticalLayout } from "../shared";
export const FileImageControl = ({
meta,
@@ -25,32 +24,21 @@ export const FileImageControl = ({
const asset = useStore(assetStore);
return (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1">
- <FloatingPanel
- title="Images"
- content={
- <ImageManager
- onChange={(asset) =>
- onChange({ type: "asset", value: asset.id }, asset)
- }
- />
- }
- >
- <Button color="neutral" css={{ flexGrow: 1, overflow: "hidden" }}>
- {asset?.name ?? "Choose image"}
- </Button>
- </FloatingPanel>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
+ <VerticalLayout label={getLabel(meta, propName)} onDelete={onDelete}>
+ <FloatingPanel
+ title="Images"
+ content={
+ <ImageManager
+ onChange={(asset) =>
+ onChange({ type: "asset", value: asset.id }, asset)
+ }
/>
- )}
- </Flex>
- </div>
+ }
+ >
+ <Button color="neutral" css={{ width: "100%" }}>
+ {asset?.name ?? "Choose image"}
+ </Button>
+ </FloatingPanel>
+ </VerticalLayout>
);
};
diff --git a/apps/builder/app/builder/features/props-panel/controls/number.tsx b/apps/builder/app/builder/features/props-panel/controls/number.tsx
index 42809ccb8d..56a51c69ee 100644
--- a/apps/builder/app/builder/features/props-panel/controls/number.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/number.tsx
@@ -1,6 +1,10 @@
-import { TrashIcon } from "@webstudio-is/icons";
-import { TextField, Button, Label, Flex } from "@webstudio-is/design-system";
-import { type ControlProps, getLabel } from "../shared";
+import { TextField, useId } from "@webstudio-is/design-system";
+import {
+ type ControlProps,
+ getLabel,
+ useLocalValue,
+ HorizontalLayout,
+} from "../shared";
export const NumberControl = ({
meta,
@@ -8,27 +12,40 @@ export const NumberControl = ({
propName,
onChange,
onDelete,
-}: ControlProps<"number", "number">) => (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1">
+}: ControlProps<"number", "number">) => {
+ const id = useId();
+
+ const localValue = useLocalValue(prop ? prop.value : "", (value) => {
+ if (typeof value === "number") {
+ onChange({ type: "number", value });
+ }
+ });
+
+ return (
+ <HorizontalLayout
+ id={id}
+ label={getLabel(meta, propName)}
+ onDelete={onDelete}
+ >
<TextField
+ id={id}
type="number"
- name={propName}
- value={prop ? prop.value : ""}
+ value={localValue.value}
onChange={(event) => {
- onChange({ type: "number", value: parseFloat(event.target.value) });
+ const asNumber = parseFloat(event.target.value);
+ localValue.set(
+ // just in case type="number" doesn't guarantee a number
+ Number.isNaN(asNumber) ? event.target.value : asNumber
+ );
+ }}
+ onBlur={localValue.save}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ localValue.save();
+ }
}}
- css={{ flexGrow: 1 }}
+ css={{ width: 98 }}
/>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
- />
- )}
- </Flex>
- </div>
-);
+ </HorizontalLayout>
+ );
+};
diff --git a/apps/builder/app/builder/features/props-panel/controls/radio.tsx b/apps/builder/app/builder/features/props-panel/controls/radio.tsx
index 5744144b6f..3a12b6eb5b 100644
--- a/apps/builder/app/builder/features/props-panel/controls/radio.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/radio.tsx
@@ -1,14 +1,13 @@
import {
- Text,
- Flex,
- Button,
+ Box,
RadioGroup,
Radio,
RadioAndLabel,
- Label,
+ useId,
+ theme,
} from "@webstudio-is/design-system";
-import { TrashIcon } from "@webstudio-is/icons";
-import { type ControlProps, getLabel } from "../shared";
+import { humanizeString } from "~/shared/string-utils";
+import { type ControlProps, getLabel, VerticalLayout, Label } from "../shared";
export const RadioControl = ({
meta,
@@ -17,33 +16,30 @@ export const RadioControl = ({
onChange,
onDelete,
}: ControlProps<"radio" | "inline-radio", "string">) => {
- // @todo: handle the case when `value` contains something that isn't in `options`?
+ // making sure that the current value is in the list of options
+ const options =
+ prop === undefined || meta.options.includes(prop.value)
+ ? meta.options
+ : [prop.value, ...meta.options];
+
+ const id = useId();
return (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1" justify="between">
+ <VerticalLayout label={getLabel(meta, propName)} onDelete={onDelete}>
+ <Box css={{ paddingTop: theme.spacing[2] }}>
<RadioGroup
name="value"
value={prop?.value}
onValueChange={(value) => onChange({ type: "string", value })}
>
- {meta.options.map((value) => (
+ {options.map((value) => (
<RadioAndLabel key={value}>
- <Radio value={value} />
- <Text>{value}</Text>
+ <Radio value={value} id={`${id}:${value}`} />
+ <Label htmlFor={`${id}:${value}`}>{humanizeString(value)}</Label>
</RadioAndLabel>
))}
</RadioGroup>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
- />
- )}
- </Flex>
- </div>
+ </Box>
+ </VerticalLayout>
);
};
diff --git a/apps/builder/app/builder/features/props-panel/controls/select.tsx b/apps/builder/app/builder/features/props-panel/controls/select.tsx
index 3bcdf734af..15f9f351ff 100644
--- a/apps/builder/app/builder/features/props-panel/controls/select.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/select.tsx
@@ -1,6 +1,5 @@
-import { Button, Select, Label, Flex } from "@webstudio-is/design-system";
-import { TrashIcon } from "@webstudio-is/icons";
-import { type ControlProps, getLabel } from "../shared";
+import { Flex, theme, useId, Select } from "@webstudio-is/design-system";
+import { type ControlProps, getLabel, VerticalLayout } from "../shared";
export const SelectControl = ({
meta,
@@ -9,28 +8,26 @@ export const SelectControl = ({
onChange,
onDelete,
}: ControlProps<"select", "string">) => {
- // @todo: handle the case when `value` contains something that isn't in `options`?
+ const id = useId();
+ const label = getLabel(meta, propName);
+
+ // making sure that the current value is in the list of options
+ const options =
+ prop === undefined || meta.options.includes(prop.value)
+ ? meta.options
+ : [prop.value, ...meta.options];
return (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1">
+ <VerticalLayout label={label} id={id} onDelete={onDelete}>
+ <Flex css={{ py: theme.spacing[2] }}>
<Select
- name={propName}
+ id={id}
value={prop?.value}
- options={meta.options}
+ options={options}
onChange={(value) => onChange({ type: "string", value })}
css={{ flexGrow: 1 }}
/>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
- />
- )}
</Flex>
- </div>
+ </VerticalLayout>
);
};
diff --git a/apps/builder/app/builder/features/props-panel/controls/text.tsx b/apps/builder/app/builder/features/props-panel/controls/text.tsx
index e4450a0a60..cc6027ea67 100644
--- a/apps/builder/app/builder/features/props-panel/controls/text.tsx
+++ b/apps/builder/app/builder/features/props-panel/controls/text.tsx
@@ -1,33 +1,102 @@
-import { TrashIcon } from "@webstudio-is/icons";
-import { TextField, Label, Button, Flex } from "@webstudio-is/design-system";
-import { type ControlProps, getLabel } from "../shared";
+import {
+ TextField,
+ Flex,
+ theme,
+ useId,
+ TextArea,
+} from "@webstudio-is/design-system";
+import {
+ type ControlProps,
+ getLabel,
+ useLocalValue,
+ VerticalLayout,
+ HorizontalLayout,
+} from "../shared";
-export const TextControl = ({
- meta,
- prop,
- propName,
+type ImplementationProps = {
+ label: string;
+ id: string;
+ value: string;
+ onChange: (value: string) => void;
+ onDelete?: () => void;
+};
+
+const AsInput = ({
+ label,
+ id,
+ value,
onChange,
onDelete,
-}: ControlProps<"text", "string">) => (
- <div>
- <Label>{getLabel(meta, propName)}</Label>
- <Flex gap="1">
+}: ImplementationProps) => {
+ const localValue = useLocalValue(value, onChange);
+
+ return (
+ <HorizontalLayout label={label} id={id} onDelete={onDelete}>
<TextField
- name={propName}
- value={prop?.value ?? ""}
- onChange={(event) => {
- onChange({ type: "string", value: event.target.value });
+ id={id}
+ value={localValue.value}
+ onChange={(event) => localValue.set(event.target.value)}
+ onBlur={localValue.save}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ localValue.save();
+ }
}}
- css={{ flexGrow: 1 }}
+ css={{ width: 120 }}
/>
- {onDelete && (
- <Button
- color="ghost"
- prefix={<TrashIcon />}
- onClick={onDelete}
- css={{ flexShrink: 1 }}
+ </HorizontalLayout>
+ );
+};
+
+// @todo: TextArea should support `rows` prop
+const TEXTAREA_LINE_HEIGHT = 16;
+
+const AsTextarea = ({
+ label,
+ id,
+ value,
+ onChange,
+ onDelete,
+ rows,
+}: ImplementationProps & { rows: number }) => {
+ const localValue = useLocalValue(value, onChange);
+ const height = rows * TEXTAREA_LINE_HEIGHT;
+
+ return (
+ <VerticalLayout label={label} id={id} onDelete={onDelete}>
+ <Flex css={{ py: theme.spacing[2] }}>
+ <TextArea
+ id={id}
+ value={localValue.value}
+ onChange={(event) => localValue.set(event.target.value)}
+ onBlur={localValue.save}
+ css={{ flexGrow: 1, height, minHeight: height }}
/>
- )}
- </Flex>
- </div>
-);
+ </Flex>
+ </VerticalLayout>
+ );
+};
+
+export const TextControl = ({
+ meta,
+ prop,
+ propName,
+ onChange,
+ onDelete,
+}: ControlProps<"text", "string">) => {
+ const props: ImplementationProps = {
+ id: useId(),
+ label: getLabel(meta, propName),
+ value: prop?.value ?? "",
+ onChange(value) {
+ onChange({ type: "string", value });
+ },
+ onDelete,
+ };
+
+ return meta.rows === 0 ? (
+ <AsInput {...props} />
+ ) : (
+ <AsTextarea {...props} rows={meta.rows ?? 1} />
+ );
+};
diff --git a/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx b/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx
new file mode 100644
index 0000000000..dffcc8c3a9
--- /dev/null
+++ b/apps/builder/app/builder/features/props-panel/props-panel.stories.tsx
@@ -0,0 +1,285 @@
+import { useState } from "react";
+import { ButtonIcon } from "@webstudio-is/icons";
+import { PropsPanel } from "./props-panel";
+import { usePropsLogic } from "./use-props-logic";
+import type { Prop } from "@webstudio-is/project-build";
+import type {
+ WsComponentMeta,
+ WsComponentPropsMeta,
+} from "@webstudio-is/react-sdk";
+
+type PropMeta = WsComponentPropsMeta["props"][string];
+
+const textProp = (label?: string): PropMeta => ({
+ type: "string",
+ control: "text",
+ required: false,
+ label,
+});
+
+const shortTextProp = (label?: string): PropMeta => ({
+ type: "string",
+ control: "text",
+ rows: 0,
+ required: false,
+ label,
+});
+
+const numberProp = (label?: string): PropMeta => ({
+ type: "number",
+ control: "number",
+ required: false,
+ label,
+});
+
+const booleanProp = (label?: string): PropMeta => ({
+ type: "boolean",
+ control: "boolean",
+ required: false,
+ label,
+});
+
+const colorProp = (label?: string): PropMeta => ({
+ type: "string",
+ control: "color",
+ required: false,
+ label,
+});
+
+const defaultOptions = ["one", "two", "three-the-very-long-one-so-much-long"];
+
+const radioProp = (options = defaultOptions, label?: string): PropMeta => ({
+ type: "string",
+ control: "radio",
+ options,
+ required: false,
+ label,
+});
+
+const inlineRadioProp = (
+ options = defaultOptions,
+ label?: string
+): PropMeta => ({
+ type: "string",
+ control: "inline-radio",
+ options,
+ required: false,
+ label,
+});
+
+const selectProp = (options = defaultOptions, label?: string): PropMeta => ({
+ type: "string",
+ control: "select",
+ options,
+ required: false,
+ label,
+});
+
+const checkProp = (options = defaultOptions, label?: string): PropMeta => ({
+ type: "string[]",
+ control: "check",
+ options,
+ required: false,
+ label,
+});
+
+const inlineCheckProp = (
+ options = defaultOptions,
+ label?: string
+): PropMeta => ({
+ type: "string[]",
+ control: "inline-check",
+ options,
+ required: false,
+ label,
+});
+
+const multiSelectProp = (
+ options = defaultOptions,
+ label?: string
+): PropMeta => ({
+ type: "string[]",
+ control: "multi-select",
+ options,
+ required: false,
+ label,
+});
+
+const instanceId = "0";
+
+const componentMeta: WsComponentMeta = {
+ type: "rich-text",
+ label: "Button",
+ Icon: ButtonIcon,
+};
+
+const componentPropsMeta: WsComponentPropsMeta = {
+ props: {
+ initialText: textProp(),
+ initialShortText: shortTextProp(),
+ initialNumber: numberProp(),
+ initialBoolean: booleanProp(),
+ initialColor: colorProp(),
+ initialRadio: radioProp(),
+ initialInlineRadio: inlineRadioProp(),
+ initialSelect: selectProp(),
+ initialCheck: checkProp(),
+ initialInlineCheck: inlineCheckProp(),
+ initialMultiSelect: multiSelectProp(),
+ addedText: textProp(),
+ addedShortText: shortTextProp(),
+ addedNumber: numberProp(),
+ addedBoolean: booleanProp(),
+ addedColor: colorProp(),
+ addedRadio: radioProp(),
+ addedInlineRadio: inlineRadioProp(),
+ addedSelect: selectProp(),
+ addedCheck: checkProp(),
+ addedInlineCheck: inlineCheckProp(),
+ addedMultiSelect: multiSelectProp(),
+ availableText: textProp(),
+ availableShortText: shortTextProp(),
+ availableNumber: numberProp(),
+ availableBoolean: booleanProp(),
+ availableColor: colorProp(),
+ availableRadio: radioProp(),
+ availableInlineRadio: inlineRadioProp(),
+ availableSelect: selectProp(),
+ availableCheck: checkProp(),
+ availableInlineCheck: inlineCheckProp(),
+ availableMultiSelect: multiSelectProp(),
+ },
+ initialProps: [
+ "initialText",
+ "initialShortText",
+ "initialNumber",
+ "initialBoolean",
+ "initialColor",
+ "initialRadio",
+ "initialInlineRadio",
+ "initialSelect",
+ "initialCheck",
+ "initialInlineCheck",
+ "initialMultiSelect",
+ ],
+};
+
+const startingProps: Prop[] = [
+ {
+ id: "0",
+ instanceId,
+ name: "addedText",
+ type: "string",
+ value: "some text",
+ },
+ {
+ id: "1",
+ instanceId,
+ name: "addedShortText",
+ type: "string",
+ value: "some short text",
+ },
+ {
+ id: "2",
+ instanceId,
+ name: "addedNumber",
+ type: "number",
+ value: 10,
+ },
+ {
+ id: "3",
+ instanceId,
+ name: "addedBoolean",
+ type: "boolean",
+ value: true,
+ },
+ {
+ id: "4",
+ instanceId,
+ name: "addedColor",
+ type: "string",
+ value: "#ff0000",
+ },
+ {
+ id: "5",
+ instanceId,
+ name: "addedRadio",
+ type: "string",
+ value: "two",
+ },
+ {
+ id: "6",
+ instanceId,
+ name: "addedInlineRadio",
+ type: "string",
+ value: "two",
+ },
+ {
+ id: "7",
+ instanceId,
+ name: "addedSelect",
+ type: "string",
+ value: "two",
+ },
+ {
+ id: "8",
+ instanceId,
+ name: "addedCheck",
+ type: "string[]",
+ value: ["one", "two"],
+ },
+ {
+ id: "9",
+ instanceId,
+ name: "addedInlineCheck",
+ type: "string[]",
+ value: ["one", "two"],
+ },
+ {
+ id: "10",
+ instanceId,
+ name: "addedMultiSelect",
+ type: "string[]",
+ value: ["one", "two"],
+ },
+];
+
+export const Story = () => {
+ const [props, setProps] = useState(startingProps);
+
+ const handleDelete = (id: Prop["id"]) => {
+ setProps((currernt) => currernt.filter((prop) => prop.id !== id));
+ };
+
+ const handleUpdate = (prop: Prop) => {
+ setProps((currernt) => {
+ const exists = currernt.find((item) => item.id === prop.id) !== undefined;
+ return exists
+ ? currernt.map((item) => (item.id === prop.id ? prop : item))
+ : [...currernt, prop];
+ });
+ };
+
+ const logic = usePropsLogic({
+ props,
+ meta: componentPropsMeta,
+ instanceId,
+ updateProp: handleUpdate,
+ deleteProp: handleDelete,
+ });
+
+ return (
+ <div style={{ width: 240, border: "dashed 3px #e3e3e3" }}>
+ <PropsPanel
+ propsLogic={logic}
+ component="Button"
+ instanceLabel="My Button"
+ componentMeta={componentMeta}
+ setCssProperty={() => () => undefined}
+ />
+ </div>
+ );
+};
+
+Story.storyName = "props-panel";
+export default { component: Story };
diff --git a/apps/builder/app/builder/features/props-panel/props-panel.tsx b/apps/builder/app/builder/features/props-panel/props-panel.tsx
index c11d8cb15d..5e7a573e50 100644
--- a/apps/builder/app/builder/features/props-panel/props-panel.tsx
+++ b/apps/builder/app/builder/features/props-panel/props-panel.tsx
@@ -3,7 +3,6 @@ import type { Instance, Prop } from "@webstudio-is/project-build";
import {
theme,
Box,
- Button,
useCombobox,
ComboboxPopper,
ComboboxPopperContent,
@@ -12,11 +11,15 @@ import {
ComboboxListboxItem,
TextField,
SmallIconButton,
+ Separator,
+ Flex,
+ Text,
+ CSS,
} from "@webstudio-is/design-system";
import { ChevronDownIcon, PlusIcon } from "@webstudio-is/icons";
import type { Publish } from "~/shared/pubsub";
import { propsStore, useInstanceProps } from "~/shared/nano-states";
-import { CollapsibleSection, ComponentInfo } from "~/builder/shared/inspector";
+import { CollapsibleSection } from "~/builder/shared/inspector";
import {
useStyleData,
type SetProperty as SetCssProperty,
@@ -24,12 +27,42 @@ import {
import { renderControl } from "./controls/combined";
import { usePropsLogic, type NameAndLabel } from "./use-props-logic";
import { type PropMeta, type PropValue, getLabel } from "./shared";
-
-import { useState } from "react";
+import { useState, type ReactNode } from "react";
+import {
+ getComponentPropsMeta,
+ getComponentMeta,
+ type WsComponentMeta,
+} from "@webstudio-is/react-sdk";
+import { getInstanceLabel } from "~/builder/shared/tree";
const itemToString = (item: NameAndLabel | null) =>
item ? getLabel(item, item.name) : "";
+const Row = ({ children, css }: { children: ReactNode; css?: CSS }) => (
+ <Flex css={{ px: theme.spacing[9], ...css }} gap="2" direction="column">
+ {children}
+ </Flex>
+);
+
+const InstanceInfo = ({
+ meta,
+ label,
+}: {
+ label: string;
+ meta: WsComponentMeta;
+}) => (
+ <Flex
+ gap="1"
+ css={{ height: theme.spacing[13], color: theme.colors.foregroundSubtle }}
+ align="center"
+ >
+ <meta.Icon />{" "}
+ <Text truncate variant="labelsSentenceCase">
+ {label}
+ </Text>
+ </Flex>
+);
+
const Combobox = ({
items,
onItemSelect,
@@ -85,6 +118,10 @@ const Combobox = ({
);
};
+// @todo:
+// at this point we need the <Property> wrapper only because
+// of complicated `onChange` inside.
+// need to refactor this somehow
const Property = ({
meta,
prop,
@@ -101,34 +138,31 @@ const Property = ({
onChange: (value: PropValue) => void;
onDelete?: () => void;
setCssProperty: SetCssProperty;
-}) => (
- <Box css={{ mb: theme.spacing[9] }}>
- {renderControl({
- meta,
- prop,
- propName,
- onDelete,
- onChange: (propValue, asset) => {
- onChange(propValue);
-
- // @todo: better way to do this?
- if (
- component === "Image" &&
- propName === "src" &&
- asset &&
- "width" in asset.meta &&
- "height" in asset.meta
- ) {
- setCssProperty("aspectRatio")({
- type: "unit",
- unit: "number",
- value: asset.meta.width / asset.meta.height,
- });
- }
- },
- })}
- </Box>
-);
+}) =>
+ renderControl({
+ meta,
+ prop,
+ propName,
+ onDelete,
+ onChange: (propValue, asset) => {
+ onChange(propValue);
+
+ // @todo: better way to do this?
+ if (
+ component === "Image" &&
+ propName === "src" &&
+ asset &&
+ "width" in asset.meta &&
+ "height" in asset.meta
+ ) {
+ setCssProperty("aspectRatio")({
+ type: "unit",
+ unit: "number",
+ value: asset.meta.width / asset.meta.height,
+ });
+ }
+ },
+ });
const AddPropertyForm = ({
availableProps,
@@ -137,73 +171,83 @@ const AddPropertyForm = ({
availableProps: NameAndLabel[];
onPropSelected: (propName: string) => void;
}) => (
- <Box css={{ mb: theme.spacing[9] }}>
+ <Flex css={{ height: theme.spacing[13] }} direction="column" justify="center">
<Combobox
items={availableProps}
onItemSelect={(item) => onPropSelected(item.name)}
/>
- </Box>
+ </Flex>
);
+// A UI componet with minimum logic that can be demoed in Storybook etc.
export const PropsPanel = ({
- selectedInstance,
- publish,
+ propsLogic: logic,
+ component,
+ instanceLabel,
+ componentMeta,
+ setCssProperty,
}: {
- publish: Publish;
- selectedInstance: Instance;
+ propsLogic: ReturnType<typeof usePropsLogic>;
+ component: Instance["component"];
+ instanceLabel: string;
+ componentMeta: WsComponentMeta;
+ setCssProperty: SetCssProperty;
}) => {
const [addingProp, setAddingProp] = useState(false);
- const logic = usePropsLogic({
- props: useInstanceProps(selectedInstance.id),
- instance: selectedInstance,
- updateProp: (update) => {
- store.createTransaction([propsStore], (props) => {
- props.set(update.id, update);
- });
- },
- deleteProp: (id) => {
- store.createTransaction([propsStore], (props) => {
- props.delete(id);
- });
- },
- });
-
- const styleData = useStyleData({ selectedInstance, publish });
-
return (
- <Box>
- <Box css={{ p: theme.spacing[9] }}>
- <ComponentInfo selectedInstance={selectedInstance} />
- </Box>
- <CollapsibleSection label="Properties" isOpenDefault>
- <div>
- {logic.initialProps.map(({ prop, propName, meta }) => (
- <Property
- key={propName}
- propName={propName}
- prop={prop}
- meta={meta}
- component={selectedInstance.component}
- onChange={(value) =>
- logic.handleChange({ prop, propName }, value)
- }
- setCssProperty={styleData.setProperty}
- />
- ))}
+ <Box css={{ paddingTop: theme.spacing[3] }}>
+ <Row>
+ <InstanceInfo meta={componentMeta} label={instanceLabel} />
+ </Row>
+
+ <Row
+ css={{
+ paddingTop: theme.spacing[3],
+ paddingBottom: theme.spacing[5],
+ }}
+ >
+ {logic.initialProps.map(({ prop, propName, meta }) => (
+ <Property
+ key={propName}
+ propName={propName}
+ prop={prop}
+ meta={meta}
+ component={component}
+ onChange={(value) => logic.handleChange({ prop, propName }, value)}
+ setCssProperty={setCssProperty}
+ />
+ ))}
+ </Row>
+ <Separator />
+ <CollapsibleSection
+ label="Properties"
+ isOpenDefault
+ rightSlot={
+ <SmallIconButton
+ icon={<PlusIcon />}
+ onClick={(event) => {
+ // to prevent the section from collapsing/expanding
+ event.stopPropagation();
+ setAddingProp(true);
+ }}
+ />
+ }
+ >
+ <Flex gap="2" direction="column">
{logic.addedProps.map(({ prop, propName, meta }) => (
<Property
key={propName}
propName={propName}
prop={prop}
meta={meta}
- component={selectedInstance.component}
+ component={component}
onChange={(value) =>
logic.handleChange({ prop, propName }, value)
}
onDelete={() => logic.handleDelete({ prop, propName })}
- setCssProperty={styleData.setProperty}
+ setCssProperty={setCssProperty}
/>
))}
@@ -216,16 +260,60 @@ export const PropsPanel = ({
}}
/>
)}
-
- <Button
- color="neutral"
- prefix={<PlusIcon />}
- onClick={() => setAddingProp(true)}
- >
- Add property
- </Button>
- </div>
+ </Flex>
</CollapsibleSection>
+ <Separator />
</Box>
);
};
+
+export const PropsPanelContainer = ({
+ selectedInstance: instance,
+ publish,
+}: {
+ publish: Publish;
+ selectedInstance: Instance;
+}) => {
+ const propsMeta = getComponentPropsMeta(instance.component);
+ if (propsMeta === undefined) {
+ throw new Error(`Could not get meta for compoent "${instance.component}"`);
+ }
+
+ const componentMeta = getComponentMeta(instance.component);
+ if (componentMeta === undefined) {
+ throw new Error(`Could not get meta for compoent "${instance.component}"`);
+ }
+
+ const { setProperty: setCssProperty } = useStyleData({
+ selectedInstance: instance,
+ publish,
+ });
+
+ const logic = usePropsLogic({
+ props: useInstanceProps(instance.id),
+ meta: propsMeta,
+ instanceId: instance.id,
+ updateProp: (update) => {
+ store.createTransaction([propsStore], (props) => {
+ props.set(update.id, update);
+ });
+ },
+ deleteProp: (id) => {
+ store.createTransaction([propsStore], (props) => {
+ props.delete(id);
+ });
+ },
+ });
+
+ const instanceLabel = getInstanceLabel(instance, componentMeta);
+
+ return (
+ <PropsPanel
+ propsLogic={logic}
+ component={instance.component}
+ instanceLabel={instanceLabel}
+ componentMeta={componentMeta}
+ setCssProperty={setCssProperty}
+ />
+ );
+};
diff --git a/apps/builder/app/builder/features/props-panel/shared.tsx b/apps/builder/app/builder/features/props-panel/shared.tsx
index f6b2c94dd2..b50d8001fe 100644
--- a/apps/builder/app/builder/features/props-panel/shared.tsx
+++ b/apps/builder/app/builder/features/props-panel/shared.tsx
@@ -1,6 +1,23 @@
+import {
+ type ComponentPropsWithoutRef,
+ type ReactNode,
+ useRef,
+ useState,
+ useEffect,
+} from "react";
import type { WsComponentPropsMeta } from "@webstudio-is/react-sdk";
import type { Prop } from "@webstudio-is/project-build";
import type { Asset } from "@webstudio-is/asset-uploader";
+import { SubtractIcon } from "@webstudio-is/icons";
+import {
+ SmallIconButton,
+ Label as BaseLabel,
+ useIsTruncated,
+ Tooltip,
+ Box,
+ Flex,
+ theme,
+} from "@webstudio-is/design-system";
import { humanizeString } from "~/shared/string-utils";
export type PropMeta = WsComponentPropsMeta["props"][string];
@@ -33,3 +50,110 @@ export type ControlProps<Control, PropType> = {
export const getLabel = (meta: { label?: string }, fallback: string) =>
meta.label || humanizeString(fallback);
+
+export const RemovePropButton = (props: { onClick: () => void }) => (
+ <SmallIconButton icon={<SubtractIcon />} variant="destructive" {...props} />
+);
+
+export const Label = ({
+ children,
+ ...rest
+}: ComponentPropsWithoutRef<typeof BaseLabel> & { children: string }) => {
+ const ref = useRef<HTMLLabelElement>(null);
+ const truncated = useIsTruncated(ref, children);
+
+ const label = (
+ <BaseLabel truncate {...rest} ref={ref}>
+ {children}
+ </BaseLabel>
+ );
+
+ return truncated ? <Tooltip content={children}>{label}</Tooltip> : label;
+};
+
+export const useLocalValue = <Type,>(
+ savedValue: Type,
+ onSave: (value: Type) => void
+) => {
+ const [localValue, setLocalValue] = useState(savedValue);
+
+ // Not using an effect to avoid re-rendering
+ // https://beta.reactjs.org/reference/react/useState#storing-information-from-previous-renders
+ const [previousSavedValue, setPreviousSavedValue] = useState(savedValue);
+ if (previousSavedValue !== savedValue) {
+ setLocalValue(savedValue);
+ setPreviousSavedValue(savedValue);
+ }
+
+ const save = () => {
+ if (localValue !== savedValue) {
+ onSave(localValue);
+ }
+ };
+
+ // onBlur will not trigger if control is unmounted when props panel is closed or similar.
+ // So we're saving at the unmount
+ const saveRef = useRef(save);
+ saveRef.current = save;
+ useEffect(() => () => saveRef.current(), []);
+
+ return {
+ /**
+ * Contains:
+ * - either the latest `savedValue`
+ * - or the latest value set via `set()`
+ * (whichever changed most recently)
+ */
+ value: localValue,
+ /**
+ * Should be called on onChange or similar event
+ */
+ set: setLocalValue,
+ /**
+ * Should be called on onBlur or similar event
+ */
+ save,
+ };
+};
+
+type LayoutProps = {
+ label: string;
+ id?: string;
+ onDelete?: () => void;
+ children: ReactNode;
+};
+
+export const VerticalLayout = ({
+ label,
+ id,
+ onDelete,
+ children,
+}: LayoutProps) => (
+ <Box>
+ <Flex align="center" gap="1" justify="between">
+ <Label htmlFor={id}>{label}</Label>
+ {onDelete && <RemovePropButton onClick={onDelete} />}
+ </Flex>
+ {children}
+ </Box>
+);
+
+export const HorizontalLayout = ({
+ label,
+ id,
+ onDelete,
+ children,
+}: LayoutProps) => (
+ <Flex
+ css={{ height: theme.spacing[13] }}
+ justify="between"
+ align="center"
+ gap="2"
+ >
+ <Label htmlFor={id}>{label}</Label>
+ <Flex align="center" gap="2">
+ {children}
+ {onDelete && <RemovePropButton onClick={onDelete} />}
+ </Flex>
+ </Flex>
+);
diff --git a/apps/builder/app/builder/features/props-panel/use-props-logic.ts b/apps/builder/app/builder/features/props-panel/use-props-logic.ts
index 1b8074dd73..c732278f8d 100644
--- a/apps/builder/app/builder/features/props-panel/use-props-logic.ts
+++ b/apps/builder/app/builder/features/props-panel/use-props-logic.ts
@@ -1,7 +1,7 @@
import { useState } from "react";
import { nanoid } from "nanoid";
import type { Instance, Prop } from "@webstudio-is/project-build";
-import { getComponentPropsMeta } from "@webstudio-is/react-sdk";
+import type { WsComponentPropsMeta } from "@webstudio-is/react-sdk";
import type { PropMeta, PropValue } from "./shared";
type PropOrName = { prop?: Prop; propName: string };
@@ -79,7 +79,8 @@ const getDefaultMetaForType = (type: Prop["type"]): PropMeta => {
type UsePropsLogicInput = {
props: Prop[];
- instance: Instance;
+ meta: WsComponentPropsMeta;
+ instanceId: Instance["id"];
updateProp: (update: Prop) => void;
deleteProp: (id: Prop["id"]) => void;
};
@@ -109,19 +110,15 @@ const getAndDelete = <Value>(map: Map<string, Value>, key: string) => {
};
/**
- * usePropsLogic expects that key={instance.id} is used on the ancestor component
+ * usePropsLogic expects that key={instanceId} is used on the ancestor component
*/
export const usePropsLogic = ({
props: savedProps,
- instance,
+ meta,
+ instanceId,
updateProp,
deleteProp,
}: UsePropsLogicInput): UsePropsLogicOutput => {
- const meta = getComponentPropsMeta(instance.component);
- if (meta === undefined) {
- throw new Error(`Could not get meta for compoent "${instance.component}"`);
- }
-
// we will delete items from these maps as we categorize the props
const unprocessedSaved = new Map(savedProps.map((prop) => [prop.name, prop]));
const unprocessedKnown = new Map(Object.entries(meta.props));
@@ -148,7 +145,7 @@ export const usePropsLogic = ({
// - they think that width is set to 0, but it's actually not set at all
//
if (prop === undefined && known.defaultValue !== undefined) {
- prop = getStartingProp(instance.id, known, name);
+ prop = getStartingProp(instanceId, known, name);
}
return { prop, propName: name, meta: known };
@@ -197,7 +194,7 @@ export const usePropsLogic = ({
if (propMeta === undefined) {
throw new Error(`Attempting to add a prop not lised in remainingProps`);
}
- const prop = getStartingProp(instance.id, propMeta, propName);
+ const prop = getStartingProp(instanceId, propMeta, propName);
if (prop) {
updateProp(prop);
}
@@ -207,7 +204,7 @@ export const usePropsLogic = ({
const handleChange = ({ prop, propName }: PropOrName, value: PropValue) => {
updateProp(
prop === undefined
- ? { id: nanoid(), instanceId: instance.id, name: propName, ...value }
+ ? { id: nanoid(), instanceId, name: propName, ...value }
: { ...prop, ...value }
);
};
diff --git a/packages/design-system/src/components/button.tsx b/packages/design-system/src/components/button.tsx
index e3d7a18434..e3ae7940de 100644
--- a/packages/design-system/src/components/button.tsx
+++ b/packages/design-system/src/components/button.tsx
@@ -21,7 +21,7 @@ const colors = [
"dark",
] as const;
-type ButtonColor = typeof colors[number];
+type ButtonColor = (typeof colors)[number];
type ButtonState = "auto" | "hover" | "focus" | "pressed" | "pending";
@@ -111,6 +111,8 @@ const StyledButton = styled("button", {
const TextContainer = styled("span", textVariants.labelsTitleCase, {
padding: `0 ${theme.spacing[2]}`,
+ overflow: "hidden",
+ textOverflow: "ellipsis",
});
type ButtonProps = {
diff --git a/packages/design-system/src/components/label.tsx b/packages/design-system/src/components/label.tsx
index fc409173b7..bf49a9fec6 100644
--- a/packages/design-system/src/components/label.tsx
+++ b/packages/design-system/src/components/label.tsx
@@ -56,6 +56,15 @@ const StyledLabel = styled("label", textVariants.labelsSentenceCase, {
},
},
},
+ truncate: {
+ true: {
+ whiteSpace: "nowrap",
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ flexBasis: 0,
+ flexGrow: 1,
+ },
+ },
},
defaultVariants: {
@@ -66,13 +75,14 @@ const StyledLabel = styled("label", textVariants.labelsSentenceCase, {
type Props = ComponentProps<typeof StyledLabel> & {
color?: "default" | "preset" | "local" | "remote";
disabled?: boolean;
+ truncate?: boolean;
children: ReactNode;
};
export const Label = forwardRef((props: Props, ref: Ref<HTMLLabelElement>) => {
- const { color, disabled, children, ...rest } = props;
+ const { disabled, children, ...rest } = props;
return (
- <StyledLabel ref={ref} color={color} aria-disabled={disabled} {...rest}>
+ <StyledLabel ref={ref} aria-disabled={disabled} {...rest}>
{children}
</StyledLabel>
);
diff --git a/packages/design-system/src/components/primitives/is-truncated.tsx b/packages/design-system/src/components/primitives/is-truncated.tsx
new file mode 100644
index 0000000000..951c1865ff
--- /dev/null
+++ b/packages/design-system/src/components/primitives/is-truncated.tsx
@@ -0,0 +1,20 @@
+import { type RefObject, useState, useEffect } from "react";
+
+/**
+ * Checks whether text was truncated in an element with `text-overflow: ellipsis`
+ */
+export const useIsTruncated = (ref: RefObject<HTMLElement>, text: string) => {
+ const [isTruncated, setIsTruncated] = useState(false);
+
+ useEffect(() => {
+ if (ref.current) {
+ const { offsetWidth, scrollWidth } = ref.current;
+ // https://stackoverflow.com/a/10017343/478603
+ setIsTruncated(offsetWidth < scrollWidth);
+ }
+
+ // ref is in dependencies just to make eslint happy
+ }, [ref, text]);
+
+ return isTruncated;
+};
diff --git a/packages/design-system/src/components/primitives/small-button.tsx b/packages/design-system/src/components/primitives/small-button.tsx
index d49ea8a774..5a4dd28da6 100644
--- a/packages/design-system/src/components/primitives/small-button.tsx
+++ b/packages/design-system/src/components/primitives/small-button.tsx
@@ -69,6 +69,9 @@ const styles = css({
destructive: perVariantStyle("destructive"),
},
},
+ defaultVariants: {
+ variant: "normal",
+ },
});
type Props = {
diff --git a/packages/design-system/src/components/text-area.tsx b/packages/design-system/src/components/text-area.tsx
index 52ede611ec..14d77b9f90 100644
--- a/packages/design-system/src/components/text-area.tsx
+++ b/packages/design-system/src/components/text-area.tsx
@@ -1,11 +1,10 @@
-import { styled } from "../stitches.config";
-import { theme } from "../stitches.config";
+import { styled, theme } from "../stitches.config";
+import { textVariants } from "./text";
export const TextArea = styled("textarea", {
// Reset
appearance: "none",
borderWidth: "0",
- fontFamily: "inherit",
margin: "0",
outline: "none",
padding: theme.spacing[3],
@@ -46,9 +45,8 @@ export const TextArea = styled("textarea", {
size: {
"1": {
borderRadius: theme.borderRadius[4],
- fontSize: theme.deprecatedFontSize[3],
- lineHeight: theme.deprecatedLineHeight[3],
- px: theme.spacing[3],
+ padding: theme.spacing[4],
+ ...textVariants.regular,
},
"2": {
borderRadius: theme.borderRadius[4],
diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.ts
index e8114abeaa..ba1ecd00a6 100644
--- a/packages/design-system/src/index.ts
+++ b/packages/design-system/src/index.ts
@@ -66,6 +66,7 @@ export { Flex } from "./components/flex";
export { Grid } from "./components/grid";
export * from "./components/primitives/dnd";
export * from "./components/primitives/numeric-gesture-control";
+export * from "./components/primitives/is-truncated";
// Deprecated