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: hide variables outside of slots (#2751)

Bogdan Chadkin427e40dd725b
diff --git a/apps/builder/app/shared/nano-states/props.test.ts b/apps/builder/app/shared/nano-states/props.test.ts
index ce224c6c25..19a1ffa138 100644
--- a/apps/builder/app/shared/nano-states/props.test.ts
+++ b/apps/builder/app/shared/nano-states/props.test.ts
@@ -639,3 +639,72 @@ test("compute resource variable values", () => {
cleanStores($variableValuesByInstanceSelector);
});
+
+test("stop variables lookup outside of slots", () => {
+ $instances.set(
+ toMap([
+ {
+ id: "body",
+ type: "instance",
+ component: "Body",
+ children: [{ type: "id", value: "slot" }],
+ },
+ {
+ id: "slot",
+ type: "instance",
+ component: "Slot",
+ children: [{ type: "id", value: "box" }],
+ },
+ { id: "box", type: "instance", component: "Box", children: [] },
+ ])
+ );
+ selectPageRoot("body");
+ $dataSources.set(
+ toMap([
+ {
+ id: "bodyVariable",
+ scopeInstanceId: "body",
+ type: "variable",
+ name: "",
+ value: { type: "string", value: "body" },
+ },
+ {
+ id: "slotVariable",
+ scopeInstanceId: "slot",
+ type: "variable",
+ name: "",
+ value: { type: "string", value: "slot" },
+ },
+ {
+ id: "boxVariable",
+ scopeInstanceId: "box",
+ type: "variable",
+ name: "",
+ value: { type: "string", value: "box" },
+ },
+ ])
+ );
+ $props.set(new Map());
+
+ expect($variableValuesByInstanceSelector.get()).toEqual(
+ new Map([
+ [
+ JSON.stringify(["body"]),
+ new Map<string, unknown>([["bodyVariable", "body"]]),
+ ],
+ [
+ JSON.stringify(["slot", "body"]),
+ new Map<string, unknown>([
+ ["bodyVariable", "body"],
+ ["slotVariable", "slot"],
+ ]),
+ ],
+ [
+ JSON.stringify(["box", "slot", "body"]),
+ new Map<string, unknown>([["boxVariable", "box"]]),
+ ],
+ ])
+ );
+
+ cleanStores($variableValuesByInstanceSelector);
+});
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index db677a0fb2..aaa22488c8 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -12,6 +12,7 @@ import {
encodeDataSourceVariable,
generateDataSources,
normalizeProps,
+ portalComponent,
} from "@webstudio-is/react-sdk";
import { $instances } from "./instances";
import {
@@ -292,7 +293,7 @@ export const $variableValuesByInstanceSelector = computed(
return;
}
- const variableValues = new Map<string, unknown>(parentVariableValues);
+ let variableValues = new Map<string, unknown>(parentVariableValues);
variableValuesByInstanceSelector.set(
JSON.stringify(instanceSelector),
variableValues
@@ -366,6 +367,10 @@ export const $variableValuesByInstanceSelector = computed(
}
return;
}
+ // reset values for slot children to let slots behave as isolated components
+ if (instance.component === portalComponent) {
+ variableValues = new Map();
+ }
for (const child of instance.children) {
if (child.type === "id") {
traverseInstances([child.value, ...instanceSelector], variableValues);