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: inspect variable value (#3182)

Bogdan Chadkin16861c02edf7
diff --git a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
index cf60069354..37f0282874 100644
--- a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
@@ -59,6 +59,7 @@ import { BindingPopoverProvider } from "~/builder/shared/binding-popover";
import { useSideOffset } from "~/builder/shared/floating-panel";
import {
EditorDialog,
+ EditorDialogButton,
EditorDialogControl,
} from "~/builder/shared/code-editor";
import { ResourceForm } from "./resource-panel";
@@ -209,13 +210,18 @@ const StringForm = forwardRef<
value={value}
onChange={setValue}
/>
- <EditorDialog title={`Variable "${nameField.value || "Unnamed"}"`}>
- <TextArea
- grow={true}
- id={valueId}
- value={value}
- onChange={setValue}
- />
+ <EditorDialog
+ title={`Variable "${nameField.value || "Unnamed"}"`}
+ content={
+ <TextArea
+ grow={true}
+ id={valueId}
+ value={value}
+ onChange={setValue}
+ />
+ }
+ >
+ <EditorDialogButton />
</EditorDialog>
</EditorDialogControl>
</Flex>
diff --git a/apps/builder/app/builder/features/settings-panel/variables-section.tsx b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
index 4ad4db387e..033ad4448d 100644
--- a/apps/builder/app/builder/features/settings-panel/variables-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
@@ -1,10 +1,15 @@
-import { useRef } from "react";
+import { useRef, useState } from "react";
import { computed } from "nanostores";
import { useStore } from "@nanostores/react";
import {
Button,
CssValueListArrowFocus,
CssValueListItem,
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuPortal,
+ DropdownMenuTrigger,
Flex,
Label,
SectionTitle,
@@ -12,10 +17,9 @@ import {
SectionTitleLabel,
SmallIconButton,
Text,
- Tooltip,
theme,
} from "@webstudio-is/design-system";
-import { MinusIcon, PlusIcon } from "@webstudio-is/icons";
+import { EllipsesIcon, PlusIcon } from "@webstudio-is/icons";
import type { DataSource } from "@webstudio-is/sdk";
import {
decodeDataSourceVariable,
@@ -34,7 +38,10 @@ import {
CollapsibleSectionRoot,
useOpenState,
} from "~/builder/shared/collapsible-section";
-import { formatValuePreview } from "~/builder/shared/expression-editor";
+import {
+ ValuePreviewDialog,
+ formatValuePreview,
+} from "~/builder/shared/expression-editor";
import {
VariablePopoverProvider,
VariablePopoverTrigger,
@@ -155,6 +162,76 @@ const EmptyVariables = () => {
);
};
+const VariablesItem = ({
+ variable,
+ index,
+ value,
+ isUsedVariable,
+}: {
+ variable: DataSource;
+ index: number;
+ value: unknown;
+ isUsedVariable: boolean;
+}) => {
+ const label =
+ value === undefined
+ ? variable.name
+ : `${variable.name}: ${formatValuePreview(value)}`;
+ const [inspectDialogOpen, setInspectDialogOpen] = useState(false);
+ return (
+ <VariablePopoverTrigger key={variable.id} variable={variable}>
+ <CssValueListItem
+ id={variable.id}
+ index={index}
+ label={<Label truncate>{label}</Label>}
+ buttons={
+ <>
+ <ValuePreviewDialog
+ open={inspectDialogOpen}
+ onOpenChange={setInspectDialogOpen}
+ title={`Inspect "${variable.name}" value`}
+ value={value}
+ >
+ {undefined}
+ </ValuePreviewDialog>
+ <DropdownMenu modal>
+ <DropdownMenuTrigger asChild>
+ {/* a11y is completely broken here
+ focus is not restored to button invoker
+ @todo fix it eventually and consider restoring from closed value preview dialog
+ */}
+ <SmallIconButton
+ tabIndex={-1}
+ aria-label="Open variable menu"
+ icon={<EllipsesIcon />}
+ onClick={() => {}}
+ />
+ </DropdownMenuTrigger>
+ <DropdownMenuPortal>
+ <DropdownMenuContent
+ css={{ width: theme.spacing[28] }}
+ onCloseAutoFocus={(event) => event.preventDefault()}
+ >
+ <DropdownMenuItem onSelect={() => setInspectDialogOpen(true)}>
+ Inspect
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ // allow to delete only unused variables
+ disabled={variable.type === "parameter" || isUsedVariable}
+ onSelect={() => deleteVariable(variable.id)}
+ >
+ Delete
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenuPortal>
+ </DropdownMenu>
+ </>
+ }
+ />
+ </VariablePopoverTrigger>
+ );
+};
+
const VariablesList = () => {
const availableVariables = useStore($instanceVariables);
const variableValues = useStore($instanceVariableValues);
@@ -168,43 +245,14 @@ const VariablesList = () => {
<CssValueListArrowFocus>
{availableVariables.map((variable, index) => {
const value = variableValues.get(variable.id);
- const label =
- value === undefined
- ? variable.name
- : `${variable.name}: ${formatValuePreview(value)}`;
return (
- <VariablePopoverTrigger key={variable.id} variable={variable}>
- <CssValueListItem
- id={variable.id}
- index={index}
- label={<Label truncate>{label}</Label>}
- buttons={
- <Tooltip
- content={
- variable.type === "parameter"
- ? "Variable is managed by the component and cannot be deleted"
- : usedVariables.has(variable.id)
- ? "Variable is used in bindings and cannot be deleted"
- : "Delete variable"
- }
- side="bottom"
- >
- <SmallIconButton
- tabIndex={-1}
- // allow to delete only unused variables
- disabled={
- variable.type === "parameter" ||
- usedVariables.has(variable.id)
- }
- aria-label="Delete variable"
- variant="destructive"
- icon={<MinusIcon />}
- onClick={() => deleteVariable(variable.id)}
- />
- </Tooltip>
- }
- />
- </VariablePopoverTrigger>
+ <VariablesItem
+ key={variable.id}
+ value={value}
+ variable={variable}
+ index={index}
+ isUsedVariable={usedVariables.has(variable.id)}
+ />
);
})}
</CssValueListArrowFocus>
diff --git a/apps/builder/app/builder/shared/code-editor.tsx b/apps/builder/app/builder/shared/code-editor.tsx
index b055a30426..00ade9421d 100644
--- a/apps/builder/app/builder/shared/code-editor.tsx
+++ b/apps/builder/app/builder/shared/code-editor.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, type ReactNode, useState } from "react";
+import { useEffect, useRef, type ReactNode, useState, forwardRef } from "react";
import {
Annotation,
EditorState,
@@ -140,7 +140,7 @@ type EditorContentProps = {
onBlur?: (event: FocusEvent) => void;
};
-const EditorContent = ({
+export const BaseCodeEditor = ({
extensions,
readOnly = false,
autoFocus = false,
@@ -259,11 +259,35 @@ export const EditorDialogControl = ({ children }: { children: ReactNode }) => {
return <div className={editorDialogControlStyle()}>{children}</div>;
};
+export const EditorDialogButton = forwardRef<HTMLButtonElement>(
+ (_props, ref) => {
+ return (
+ <SmallIconButton
+ ref={ref}
+ icon={<MaximizeIcon />}
+ css={{
+ position: "absolute",
+ top: 6,
+ right: 4,
+ visibility: `var(--ws-code-editor-maximize-icon-visibility, hidden)`,
+ }}
+ />
+ );
+ }
+);
+EditorDialogButton.displayName = "EditorDialogButton";
+
export const EditorDialog = ({
+ open,
+ onOpenChange,
title,
+ content,
children,
}: {
+ open?: boolean;
+ onOpenChange?: (newOpen: boolean) => void;
title?: ReactNode;
+ content: ReactNode;
children: ReactNode;
}) => {
const [isExpanded, setIsExpanded] = useState(false);
@@ -271,18 +295,8 @@ export const EditorDialog = ({
const height = isExpanded ? "80vh" : "480px";
const padding = rawTheme.spacing[7];
return (
- <Dialog>
- <DialogTrigger asChild>
- <SmallIconButton
- icon={<MaximizeIcon />}
- css={{
- position: "absolute",
- top: 6,
- right: 4,
- visibility: `var(--ws-code-editor-maximize-icon-visibility, hidden)`,
- }}
- />
- </DialogTrigger>
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ <DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent
// Left Aside panels (e.g., Pages, Components) use zIndex: theme.zIndices[1].
// For a dialog to appear above these panels, both overlay and content should also have zIndex: theme.zIndices[1].
@@ -303,7 +317,7 @@ export const EditorDialog = ({
boxSizing: "content-box",
}}
>
- {children}
+ {content}
</Grid>
{/* Title is at the end intentionally,
* to make the close button last in the tab order
@@ -336,15 +350,26 @@ export const EditorDialog = ({
export const CodeEditor = ({
title,
+ open,
+ onOpenChange,
...editorContentProps
}: EditorContentProps & {
title?: ReactNode;
+ open?: boolean;
+ onOpenChange?: (newOpen: boolean) => void;
}) => {
- const content = <EditorContent {...editorContentProps} />;
+ const content = <BaseCodeEditor {...editorContentProps} />;
return (
<EditorDialogControl>
{content}
- <EditorDialog title={title}>{content}</EditorDialog>
+ <EditorDialog
+ open={open}
+ onOpenChange={onOpenChange}
+ title={title}
+ content={content}
+ >
+ <EditorDialogButton />
+ </EditorDialog>
</EditorDialogControl>
);
};
diff --git a/apps/builder/app/builder/shared/expression-editor.tsx b/apps/builder/app/builder/shared/expression-editor.tsx
index 37173364d9..2b115cb262 100644
--- a/apps/builder/app/builder/shared/expression-editor.tsx
+++ b/apps/builder/app/builder/shared/expression-editor.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useMemo } from "react";
+import { useEffect, useMemo, type ReactNode } from "react";
import { matchSorter } from "match-sorter";
import type { SyntaxNode } from "@lezer/common";
import { Facet } from "@codemirror/state";
@@ -31,7 +31,7 @@ import {
decodeDataSourceVariable,
transpileExpression,
} from "@webstudio-is/sdk";
-import { CodeEditor } from "./code-editor";
+import { BaseCodeEditor, CodeEditor, EditorDialog } from "./code-editor";
export const formatValue = (value: unknown) => {
if (Array.isArray(value)) {
@@ -433,3 +433,43 @@ export const ExpressionEditor = ({
</div>
);
};
+
+// compute value as json lazily only when dialog is open
+// by spliting into separate component which is invoked
+// only when dialog content is rendered
+const ValuePreviewEditor = ({ value }: { value: unknown }) => {
+ const extensions = useMemo(() => [javascript({})], []);
+ return (
+ <BaseCodeEditor
+ readOnly={true}
+ extensions={extensions}
+ value={JSON.stringify(value, null, 2)}
+ onChange={() => {}}
+ />
+ );
+};
+
+export const ValuePreviewDialog = ({
+ title,
+ value,
+ children,
+ open,
+ onOpenChange,
+}: {
+ title?: ReactNode;
+ value: unknown;
+ open?: boolean;
+ onOpenChange?: (newOpen: boolean) => void;
+ children: ReactNode;
+}) => {
+ return (
+ <EditorDialog
+ open={open}
+ onOpenChange={onOpenChange}
+ title={title}
+ content={<ValuePreviewEditor value={value} />}
+ >
+ {children}
+ </EditorDialog>
+ );
+};