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 variables to settings panel (#2573)

Bogdan Chadkin4f5883834e99
diff --git a/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx b/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx
index e7c72dd702..e8fa773643 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx
@@ -1,10 +1,11 @@
-import { Grid, Switch, theme, useId } from "@webstudio-is/design-system";
+import { Box, Grid, Switch, theme, useId } from "@webstudio-is/design-system";
import {
type ControlProps,
getLabel,
Label,
RemovePropButton,
} from "../shared";
+import { VariablesButton } from "../variables";
export const BooleanControl = ({
meta,
@@ -28,9 +29,12 @@ export const BooleanControl = ({
align="center"
gap="2"
>
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
<Switch
id={id}
checked={prop?.value ?? false}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/check.tsx b/apps/builder/app/builder/features/settings-panel/controls/check.tsx
index 8e59bfc582..cc576d5059 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/check.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/check.tsx
@@ -7,6 +7,7 @@ import {
} from "@webstudio-is/design-system";
import { humanizeString } from "~/shared/string-utils";
import { type ControlProps, getLabel, VerticalLayout, Label } from "../shared";
+import { VariablesButton } from "../variables";
const add = (array: string[], item: string) => {
if (array.includes(item)) {
@@ -43,9 +44,12 @@ export const CheckControl = ({
return (
<VerticalLayout
label={
- <Label htmlFor={`${id}:${options[0]}`} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={`${id}:${options[0]}`} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/color.tsx b/apps/builder/app/builder/features/settings-panel/controls/color.tsx
index 9f2bbaca96..f30605e358 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/color.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/color.tsx
@@ -1,4 +1,4 @@
-import { InputField, useId } from "@webstudio-is/design-system";
+import { Box, InputField, useId } from "@webstudio-is/design-system";
import {
type ControlProps,
getLabel,
@@ -6,6 +6,7 @@ import {
ResponsiveLayout,
Label,
} from "../shared";
+import { VariablesButton } from "../variables";
// @todo:
// use ColorPicker (need to refactor it first,
@@ -28,9 +29,12 @@ export const ColorControl = ({
return (
<ResponsiveLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
index d055f94618..edd21332e3 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
@@ -183,6 +183,12 @@ export const renderControl = ({
);
}
+ if (prop.type === "json") {
+ throw new Error(
+ `Cannot render a fallback control for prop "${rest.propName}" with type json, because we don't know the available options for a multiselect control`
+ );
+ }
+
prop satisfies never;
}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/file.tsx b/apps/builder/app/builder/features/settings-panel/controls/file.tsx
index b3cdae653c..045c0bd9f3 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/file.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/file.tsx
@@ -1,5 +1,11 @@
import { type ReactNode } from "react";
-import { Flex, InputField, theme, useId } from "@webstudio-is/design-system";
+import {
+ Box,
+ Flex,
+ InputField,
+ theme,
+ useId,
+} from "@webstudio-is/design-system";
import {
type ControlProps,
getLabel,
@@ -8,6 +14,7 @@ import {
Label,
} from "../shared";
import { SelectAsset } from "./select-asset";
+import { VariablesButton } from "../variables";
type FileControlProps = ControlProps<"file", "asset" | "string">;
@@ -67,9 +74,12 @@ export const FileControl = ({
return (
<VerticalLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/number.tsx b/apps/builder/app/builder/features/settings-panel/controls/number.tsx
index 4704136260..20f5fe9543 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/number.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/number.tsx
@@ -1,4 +1,4 @@
-import { InputField, useId } from "@webstudio-is/design-system";
+import { Box, InputField, useId } from "@webstudio-is/design-system";
import {
type ControlProps,
getLabel,
@@ -7,6 +7,7 @@ import {
Label,
} from "../shared";
import { useState } from "react";
+import { VariablesButton } from "../variables";
export const NumberControl = ({
meta,
@@ -31,9 +32,12 @@ export const NumberControl = ({
return (
<ResponsiveLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/radio.tsx b/apps/builder/app/builder/features/settings-panel/controls/radio.tsx
index 624f53c4a0..798936fc61 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/radio.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/radio.tsx
@@ -8,6 +8,7 @@ import {
} from "@webstudio-is/design-system";
import { humanizeString } from "~/shared/string-utils";
import { type ControlProps, getLabel, VerticalLayout, Label } from "../shared";
+import { VariablesButton } from "../variables";
export const RadioControl = ({
meta,
@@ -28,9 +29,12 @@ export const RadioControl = ({
return (
<VerticalLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/select.tsx b/apps/builder/app/builder/features/settings-panel/controls/select.tsx
index ffe06a877a..4f45a87859 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/select.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/select.tsx
@@ -1,6 +1,7 @@
-import { Flex, theme, useId, Select } from "@webstudio-is/design-system";
+import { Flex, theme, useId, Select, Box } from "@webstudio-is/design-system";
import { humanizeString } from "~/shared/string-utils";
import { type ControlProps, getLabel, VerticalLayout, Label } from "../shared";
+import { VariablesButton } from "../variables";
export const SelectControl = ({
meta,
@@ -21,9 +22,12 @@ export const SelectControl = ({
return (
<VerticalLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/controls/text.tsx b/apps/builder/app/builder/features/settings-panel/controls/text.tsx
index 83393e2aaf..001e67b0a2 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/text.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/text.tsx
@@ -1,4 +1,4 @@
-import { Flex, theme, useId, TextArea } from "@webstudio-is/design-system";
+import { Flex, theme, useId, TextArea, Box } from "@webstudio-is/design-system";
import {
type ControlProps,
getLabel,
@@ -7,6 +7,7 @@ import {
ResponsiveLayout,
Label,
} from "../shared";
+import { VariablesButton } from "../variables";
export const TextControl = ({
meta,
@@ -38,14 +39,19 @@ export const TextControl = ({
/>
);
+ const labelElement = (
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {label}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
+ );
+
if (isTwoColumnLayout) {
return (
<ResponsiveLayout
- label={
- <Label htmlFor={id} description={meta.description}>
- {label}
- </Label>
- }
+ label={labelElement}
deletable={deletable}
onDelete={onDelete}
>
@@ -56,11 +62,7 @@ export const TextControl = ({
return (
<VerticalLayout
- label={
- <Label htmlFor={id} description={meta.description}>
- {label}
- </Label>
- }
+ label={labelElement}
deletable={deletable}
onDelete={onDelete}
>
diff --git a/apps/builder/app/builder/features/settings-panel/controls/url.tsx b/apps/builder/app/builder/features/settings-panel/controls/url.tsx
index fd435bcb98..b511e82391 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/url.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/url.tsx
@@ -10,6 +10,7 @@ import {
ToggleGroupButton,
Select,
Tooltip,
+ Box,
} from "@webstudio-is/design-system";
import {
AttachmentIcon,
@@ -29,6 +30,7 @@ import {
Label,
} from "../shared";
import { SelectAsset } from "./select-asset";
+import { VariablesButton } from "../variables";
type UrlControlProps = ControlProps<"url", "string" | "page" | "asset">;
@@ -400,9 +402,12 @@ export const UrlControl = ({
return (
<VerticalLayout
label={
- <Label htmlFor={id} description={meta.description}>
- {getLabel(meta, propName)}
- </Label>
+ <Box css={{ position: "relative" }}>
+ <Label htmlFor={id} description={meta.description}>
+ {getLabel(meta, propName)}
+ </Label>
+ <VariablesButton prop={prop} propMeta={meta} onChange={onChange} />
+ </Box>
}
deletable={deletable}
onDelete={onDelete}
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
index 8fd30a565c..21dea09cfa 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx
@@ -218,7 +218,8 @@ export const PropsSectionContainer = ({
const props = propsStore.get();
const prop = props.get(update.id);
// update data source instead when real prop has data source type
- if (prop?.type === "dataSource") {
+ // update the prop when new binding is added
+ if (prop?.type === "dataSource" && update.type !== "dataSource") {
let dataSourceId = prop.value;
const dataSources = dataSourcesStore.get();
const dataSource = dataSources.get(dataSourceId);
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
index a62cf11150..06c4039178 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
@@ -88,6 +88,10 @@ const getDefaultMetaForType = (type: Prop["type"]): PropMeta => {
throw new Error(
"A prop with type string[] must have a meta, we can't provide a default one because we need a list of options"
);
+ case "json":
+ throw new Error(
+ "A prop with type json must have a meta, we can't provide a default one because we need a list of options"
+ );
case "dataSource":
throw new Error(
"A prop with type dataSource must have a meta, we can't provide a default one because we need a list of options"
@@ -136,7 +140,8 @@ const getPropTypeAndValue = (value: unknown) => {
if (Array.isArray(value)) {
return { type: "string[]", value } as const;
}
- throw Error(`Unexpected prop value ${value}`);
+ // fallback to empty string to not break UI
+ return { type: "string", value: "" } as const;
};
/** usePropsLogic expects that key={instanceId} is used on the ancestor component */
diff --git a/apps/builder/app/builder/features/settings-panel/variables.stories.tsx b/apps/builder/app/builder/features/settings-panel/variables.stories.tsx
index d07da83b63..f68aacbb19 100644
--- a/apps/builder/app/builder/features/settings-panel/variables.stories.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables.stories.tsx
@@ -1,6 +1,5 @@
import type { Meta, StoryObj } from "@storybook/react";
import { useState } from "react";
-import { useStore } from "@nanostores/react";
import { CodeEditor as CodeEditorComponent } from "./code-editor";
import {
Button,
@@ -34,8 +33,6 @@ propsStore.set(new Map([[initialProp.id, initialProp]]));
const VariablesPopover = () => {
const [isOpen, setIsOpen] = useState(true);
- const props = useStore(propsStore);
- const myProp = props.get("my-prop");
return (
<FloatingPanelPopover open={isOpen} onOpenChange={setIsOpen}>
<FloatingPanelPopoverTrigger asChild>
@@ -43,7 +40,7 @@ const VariablesPopover = () => {
</FloatingPanelPopoverTrigger>
<FloatingPanelPopoverContent align="start">
<VariablesPanel
- prop={myProp}
+ propId="my-prop"
propMeta={{
required: false,
control: "text",
diff --git a/apps/builder/app/builder/features/settings-panel/variables.tsx b/apps/builder/app/builder/features/settings-panel/variables.tsx
index 5dbbb08657..623cf638e3 100644
--- a/apps/builder/app/builder/features/settings-panel/variables.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables.tsx
@@ -7,8 +7,11 @@ import {
CssValueListArrowFocus,
CssValueListItem,
Flex,
+ FloatingPanelPopover,
FloatingPanelPopoverClose,
+ FloatingPanelPopoverContent,
FloatingPanelPopoverTitle,
+ FloatingPanelPopoverTrigger,
InputErrorsTooltip,
InputField,
Label,
@@ -19,7 +22,13 @@ import {
Tooltip,
theme,
} from "@webstudio-is/design-system";
-import { MenuIcon, MinusIcon, PlusIcon, TrashIcon } from "@webstudio-is/icons";
+import {
+ DotIcon,
+ MenuIcon,
+ MinusIcon,
+ PlusIcon,
+ TrashIcon,
+} from "@webstudio-is/icons";
import type { DataSource, Prop } from "@webstudio-is/sdk";
import {
PropMeta,
@@ -35,6 +44,7 @@ import {
import { serverSyncStore } from "~/shared/sync";
import { CodeEditor } from "./code-editor";
import type { PropValue } from "./shared";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
type VariableDataSource = Extract<DataSource, { type: "variable" }>;
@@ -482,7 +492,7 @@ const ListPanel = ({
gap: theme.spacing[3],
}}
>
- <Label>Name</Label>
+ <Label>Expression</Label>
<CodeEditor
// reset uncontrolled editor when saved expression is changed
key={propExpression}
@@ -521,14 +531,28 @@ const ListPanel = ({
};
export const VariablesPanel = ({
- prop,
+ propId,
propMeta,
onChange,
}: {
- prop: undefined | Prop;
+ propId: undefined | Prop["id"];
propMeta: PropMeta;
onChange: (value: PropValue) => void;
}) => {
+ // compute prop instead of using passed one
+ // because data source props are converted into values
+ const prop = useStore(
+ useMemo(
+ () =>
+ computed(propsStore, (props) => {
+ if (propId) {
+ return props.get(propId);
+ }
+ }),
+ [propId]
+ )
+ );
+
const [view, setView] = useState<
| { name: "list" }
| { name: "add" }
@@ -621,3 +645,38 @@ export const VariablesPanel = ({
</>
);
};
+
+export const VariablesButton = ({
+ prop,
+ propMeta,
+ onChange,
+}: {
+ prop: undefined | Prop;
+ propMeta: PropMeta;
+ onChange: (value: PropValue) => void;
+}) => {
+ if (isFeatureEnabled("bindings") === false) {
+ return;
+ }
+ return (
+ <FloatingPanelPopover modal>
+ <FloatingPanelPopoverTrigger asChild>
+ <SmallIconButton
+ css={{
+ position: "absolute",
+ top: -10,
+ left: -10,
+ }}
+ icon={<DotIcon />}
+ />
+ </FloatingPanelPopoverTrigger>
+ <FloatingPanelPopoverContent side="left" align="start">
+ <VariablesPanel
+ propId={prop?.id}
+ propMeta={propMeta}
+ onChange={onChange}
+ />
+ </FloatingPanelPopoverContent>
+ </FloatingPanelPopover>
+ );
+};
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index a94f8fd2a7..8e5f04d208 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -6,3 +6,4 @@ export const adminRole = false;
export const ai = true;
export const aiRadixComponents = false;
export const transitions = false;
+export const bindings = false;
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index db810cdc55..76bf57d2e1 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -74,7 +74,8 @@ export const generateJsxElement = ({
prop.type === "string" ||
prop.type === "number" ||
prop.type === "boolean" ||
- prop.type === "string[]"
+ prop.type === "string[]" ||
+ prop.type === "json"
) {
generatedProps += `\n${prop.name}={${JSON.stringify(prop.value)}}`;
continue;
diff --git a/packages/sdk/src/schema/props.ts b/packages/sdk/src/schema/props.ts
index f6cb59a4ae..5829e490bc 100644
--- a/packages/sdk/src/schema/props.ts
+++ b/packages/sdk/src/schema/props.ts
@@ -25,6 +25,11 @@ export const Prop = z.union([
type: z.literal("boolean"),
value: z.boolean(),
}),
+ z.object({
+ ...baseProp,
+ type: z.literal("json"),
+ value: z.unknown(),
+ }),
z.object({
...baseProp,
type: z.literal("asset"),