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.

refactor: remove instance selector from component hooks (#4469)

Bogdan Chadkin641619e4b430
diff --git a/apps/builder/app/builder/features/navigator/navigator-tree.tsx b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
index 2037770345..cde10d1212 100644
--- a/apps/builder/app/builder/features/navigator/navigator-tree.tsx
+++ b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
@@ -57,7 +57,11 @@ import {
} from "~/shared/instance-utils";
import { emitCommand } from "~/builder/shared/commands";
import { useContentEditable } from "~/shared/dom-hooks";
-import { $selectedPage, selectInstance } from "~/shared/awareness";
+import {
+ $selectedPage,
+ getInstanceKey,
+ selectInstance,
+} from "~/shared/awareness";
type TreeItem = {
level: number;
@@ -112,7 +116,7 @@ const $flatTree = computed(
return;
}
const propValues = propValuesByInstanceSelector.get(
- JSON.stringify(selector)
+ getInstanceKey(selector)
);
const isHidden =
isParentHidden ||
@@ -519,7 +523,7 @@ export const NavigatorTree = () => {
{flatTree.map((item) => {
const key = item.selector.join();
const propValues = propValuesByInstanceSelector.get(
- JSON.stringify(item.selector)
+ getInstanceKey(item.selector)
);
const show = Boolean(propValues?.get(showAttribute) ?? true);
const meta = metas.get(item.instance.component);
diff --git a/apps/builder/app/builder/features/pages/page-utils.ts b/apps/builder/app/builder/features/pages/page-utils.ts
index 0a1a176781..fc1e0f58d5 100644
--- a/apps/builder/app/builder/features/pages/page-utils.ts
+++ b/apps/builder/app/builder/features/pages/page-utils.ts
@@ -23,7 +23,7 @@ import {
$variableValuesByInstanceSelector,
} from "~/shared/nano-states";
import { insertPageCopyMutable } from "~/shared/page-utils";
-import { $selectedPage, selectPage } from "~/shared/awareness";
+import { $selectedPage, getInstanceKey, selectPage } from "~/shared/awareness";
/**
* When page or folder needs to be deleted or moved to a different parent,
@@ -247,7 +247,7 @@ export const $pageRootScope = computed(
}
const values =
variableValuesByInstanceSelector.get(
- JSON.stringify([page.rootInstanceId])
+ getInstanceKey([page.rootInstanceId])
) ?? new Map<string, unknown>();
for (const [dataSourceId, value] of values) {
const dataSource = dataSources.get(dataSourceId);
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index 53c230caf6..241cdb5a4c 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -46,7 +46,11 @@ import { setDataCollapsed } from "~/canvas/collapsed";
import { getIsVisuallyHidden } from "~/shared/visually-hidden";
import { serverSyncStore } from "~/shared/sync";
import { TextEditor } from "../text-editor";
-import { $selectedPage, selectInstance } from "~/shared/awareness";
+import {
+ $selectedPage,
+ getInstanceKey,
+ selectInstance,
+} from "~/shared/awareness";
import {
createInstanceChildrenElements,
type WebstudioComponentProps,
@@ -250,7 +254,7 @@ const $indexesWithinAncestors = computed(
);
const useInstanceProps = (instanceSelector: InstanceSelector) => {
- const instanceSelectorKey = JSON.stringify(instanceSelector);
+ const instanceKey = getInstanceKey(instanceSelector);
const [instanceId] = instanceSelector;
const $instancePropsObject = useMemo(() => {
return computed(
@@ -261,8 +265,7 @@ const useInstanceProps = (instanceSelector: InstanceSelector) => {
if (index !== undefined) {
instancePropsObject[indexAttribute] = index.toString();
}
- const instanceProps =
- propValuesByInstanceSelector.get(instanceSelectorKey);
+ const instanceProps = propValuesByInstanceSelector.get(instanceKey);
if (instanceProps) {
for (const [name, value] of instanceProps) {
instancePropsObject[name] = value;
@@ -271,7 +274,7 @@ const useInstanceProps = (instanceSelector: InstanceSelector) => {
return instancePropsObject;
}
);
- }, [instanceSelectorKey, instanceId]);
+ }, [instanceKey, instanceId]);
const instancePropsObject = useStore($instancePropsObject);
return instancePropsObject;
};
diff --git a/apps/builder/app/shared/awareness.ts b/apps/builder/app/shared/awareness.ts
index 95c79c018e..96e377a196 100644
--- a/apps/builder/app/shared/awareness.ts
+++ b/apps/builder/app/shared/awareness.ts
@@ -58,12 +58,16 @@ export const $selectedInstance = computed(
}
);
-export const $selectedInstanceKey = computed($awareness, (awareness) => {
- if (awareness === undefined) {
- return;
- }
- return JSON.stringify(awareness.instanceSelector);
-});
+export const getInstanceKey = <
+ InstanceSelector extends undefined | Instance["id"][],
+>(
+ instanceSelector: InstanceSelector
+): (InstanceSelector extends undefined ? undefined : never) | string =>
+ JSON.stringify(instanceSelector);
+
+export const $selectedInstanceKey = computed($awareness, (awareness) =>
+ getInstanceKey(awareness?.instanceSelector)
+);
export const selectPage = (pageId: Page["id"]) => {
const pages = $pages.get();
diff --git a/apps/builder/app/shared/nano-states/components.ts b/apps/builder/app/shared/nano-states/components.ts
index 29ee90ff7e..e9acbb2456 100644
--- a/apps/builder/app/shared/nano-states/components.ts
+++ b/apps/builder/app/shared/nano-states/components.ts
@@ -1,7 +1,7 @@
import { nanoid } from "nanoid";
import { shallowEqual } from "shallow-equal";
import type { ExoticComponent } from "react";
-import { atom } from "nanostores";
+import { atom, computed } from "nanostores";
import {
type AnyComponent,
type WsComponentMeta,
@@ -10,14 +10,13 @@ import {
type HookContext,
namespaceMeta,
getIndexesWithinAncestors,
+ type InstanceData,
} from "@webstudio-is/react-sdk";
import type { Instance } from "@webstudio-is/sdk";
-import { decodeDataSourceVariable } from "@webstudio-is/sdk";
import type { InstanceSelector } from "../tree-utils";
-import { $dataSources, $memoryProps, $props } from "./misc";
-import { $instances, $selectedInstanceSelector } from "./instances";
-import { $dataSourceVariables } from "./variables";
-import { $selectedPage } from "../awareness";
+import { $memoryProps, $props } from "./misc";
+import { $instances } from "./instances";
+import { $awareness, $selectedPage, getInstanceKey } from "../awareness";
const createHookContext = (): HookContext => {
const metas = $registeredComponentMetas.get();
@@ -32,10 +31,10 @@ const createHookContext = (): HookContext => {
return {
indexesWithinAncestors,
- getPropValue: (instanceId, propName) => {
+ getPropValue: ({ id }, propName) => {
const props = $props.get();
for (const prop of props.values()) {
- if (prop.instanceId === instanceId && prop.name === propName) {
+ if (prop.instanceId === id && prop.name === propName) {
if (
prop.type === "string" ||
prop.type === "number" ||
@@ -48,19 +47,15 @@ const createHookContext = (): HookContext => {
}
},
- setMemoryProp: (instanceSelector, propName, value) => {
- if (instanceSelector.length === 0) {
- console.error("instanceSelector is empty");
- return;
- }
-
+ setMemoryProp: (instanceData: InstanceData, propName, value) => {
+ const { instanceKey } = instanceData;
const props = new Map($memoryProps.get());
- const newProps = props.get(JSON.stringify(instanceSelector)) ?? new Map();
+ const newProps = props.get(instanceKey) ?? new Map();
const propBase = {
id: nanoid(),
- instanceId: instanceSelector[0]!,
+ instanceId: instanceData.id,
name: propName,
};
@@ -94,56 +89,43 @@ const createHookContext = (): HookContext => {
newProps.delete(propName);
}
- props.set(JSON.stringify(instanceSelector), newProps);
+ props.set(instanceKey, newProps);
$memoryProps.set(props);
},
-
- setPropVariable: (instanceId, propName, value) => {
- const dataSourceVariables = new Map($dataSourceVariables.get());
- const dataSources = new Map($dataSources.get());
- const props = $props.get();
- for (const prop of props.values()) {
- if (
- prop.instanceId === instanceId &&
- prop.name === propName &&
- prop.type === "expression"
- ) {
- // extract id without parsing expression
- const potentialVariableId = decodeDataSourceVariable(prop.value);
- if (
- potentialVariableId !== undefined &&
- dataSources.has(potentialVariableId)
- ) {
- const dataSourceId = potentialVariableId;
- dataSourceVariables.set(dataSourceId, value);
- }
- }
- }
- $dataSourceVariables.set(dataSourceVariables);
- },
};
};
+const $instanceSelector = computed(
+ $awareness,
+ (awareness) => awareness?.instanceSelector
+);
+
// subscribe builder events and invoke all component hooks
export const subscribeComponentHooks = () => {
- let lastSelectedInstanceSelector: undefined | InstanceSelector = undefined;
- const unsubscribeSelectedInstanceSelector =
- $selectedInstanceSelector.subscribe((instanceSelector) => {
+ let lastInstanceSelector: undefined | InstanceSelector = undefined;
+ const unsubscribeSelectedInstanceSelector = $instanceSelector.subscribe(
+ (instanceSelector) => {
// prevent executing hooks when selected instance is not changed
- if (shallowEqual(lastSelectedInstanceSelector, instanceSelector)) {
+ if (shallowEqual(lastInstanceSelector, instanceSelector)) {
return;
}
const hooks = $registeredComponentHooks.get();
const instances = $instances.get();
- if (lastSelectedInstanceSelector) {
+ if (lastInstanceSelector) {
for (const hook of hooks) {
hook.onNavigatorUnselect?.(createHookContext(), {
- instancePath: lastSelectedInstanceSelector.flatMap((id) => {
+ instancePath: lastInstanceSelector.flatMap((id, index, array) => {
const instance = instances.get(id);
- return instance ? [instance] : [];
+ if (instance === undefined) {
+ return [];
+ }
+ return {
+ id: instance.id,
+ instanceKey: getInstanceKey(array.slice(index)),
+ component: instance.component,
+ };
}),
- instanceSelector: lastSelectedInstanceSelector,
});
}
}
@@ -151,18 +133,25 @@ export const subscribeComponentHooks = () => {
if (instanceSelector) {
for (const hook of hooks) {
hook.onNavigatorSelect?.(createHookContext(), {
- instancePath: instanceSelector.flatMap((id) => {
+ instancePath: instanceSelector.flatMap((id, index, array) => {
const instance = instances.get(id);
- return instance ? [instance] : [];
+ if (instance === undefined) {
+ return [];
+ }
+ return {
+ id: instance.id,
+ instanceKey: getInstanceKey(array.slice(index)),
+ component: instance.component,
+ };
}),
- instanceSelector,
});
}
}
// store converts values to readonly
- lastSelectedInstanceSelector = instanceSelector as InstanceSelector;
- });
+ lastInstanceSelector = instanceSelector as InstanceSelector;
+ }
+ );
return () => {
unsubscribeSelectedInstanceSelector();
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index dc30c095a4..a4012a3008 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -47,7 +47,7 @@ import {
} from "./variables";
import { uploadingFileDataToAsset } from "~/builder/shared/assets/asset-utils";
import { fetch } from "~/shared/fetch.client";
-import { $selectedPage } from "../awareness";
+import { $selectedPage, getInstanceKey } from "../awareness";
export const getIndexedInstanceId = (
instanceId: Instance["id"],
@@ -393,7 +393,7 @@ export const $propValuesByInstanceSelector = computed(
}
propValuesByInstanceSelector.set(
- JSON.stringify(instanceSelector),
+ getInstanceKey(instanceSelector),
propValues
);
@@ -503,7 +503,7 @@ export const $variableValuesByInstanceSelector = computed(
let variableValues = new Map<string, unknown>(parentVariableValues);
variableValuesByInstanceSelector.set(
- JSON.stringify(instanceSelector),
+ getInstanceKey(instanceSelector),
variableValues
);
const variables = variablesByInstanceId.get(instanceId);
diff --git a/packages/react-sdk/src/hook.test.ts b/packages/react-sdk/src/hook.test.ts
index 8728b67fcb..82a9acd751 100644
--- a/packages/react-sdk/src/hook.test.ts
+++ b/packages/react-sdk/src/hook.test.ts
@@ -3,11 +3,31 @@ import { getClosestInstance, type InstancePath } from "./hook";
test("get closest instance", () => {
const instancePath: InstancePath = [
- { type: "instance", id: "4", component: "Content", children: [] },
- { type: "instance", id: "3", component: "Tabs", children: [] },
- { type: "instance", id: "2", component: "Content", children: [] },
- { type: "instance", id: "1", component: "Tabs", children: [] },
- { type: "instance", id: "0", component: "Body", children: [] },
+ {
+ id: "4",
+ instanceKey: JSON.stringify(["4", "3", "2", "1", "0"]),
+ component: "Content",
+ },
+ {
+ id: "3",
+ instanceKey: JSON.stringify(["3", "2", "1", "0"]),
+ component: "Tabs",
+ },
+ {
+ id: "2",
+ instanceKey: JSON.stringify(["2", "1", "0"]),
+ component: "Content",
+ },
+ {
+ id: "1",
+ instanceKey: JSON.stringify(["1", "0"]),
+ component: "Tabs",
+ },
+ {
+ id: "0",
+ instanceKey: JSON.stringify(["0"]),
+ component: "Body",
+ },
];
const [content2, tabs2, content1, tabs1, _body] = instancePath;
expect(getClosestInstance(instancePath, content2, "Tabs")).toBe(tabs2);
diff --git a/packages/react-sdk/src/hook.ts b/packages/react-sdk/src/hook.ts
index f94a430114..6c60580558 100644
--- a/packages/react-sdk/src/hook.ts
+++ b/packages/react-sdk/src/hook.ts
@@ -1,6 +1,12 @@
import type { Instance, Prop } from "@webstudio-is/sdk";
import type { IndexesWithinAncestors } from "./instance-utils";
+export type InstanceData = {
+ id: Instance["id"];
+ instanceKey: string;
+ component: Instance["component"];
+};
+
/**
* Hooks are subscriptions to builder events
* with limited way to interact with it.
@@ -9,27 +15,21 @@ import type { IndexesWithinAncestors } from "./instance-utils";
export type HookContext = {
indexesWithinAncestors: IndexesWithinAncestors;
- getPropValue: (instanceId: Instance["id"], propName: Prop["name"]) => unknown;
- setPropVariable: (
- instanceId: Instance["id"],
- propName: Prop["name"],
- value: unknown
- ) => void;
+ getPropValue: (instanceData: InstanceData, propName: Prop["name"]) => unknown;
setMemoryProp: (
- instanceId: readonly Instance["id"][],
+ instanceData: InstanceData,
propName: Prop["name"],
value: unknown
) => void;
};
-export type InstancePath = Instance[];
+export type InstancePath = InstanceData[];
type NavigatorEvent = {
/**
* List of instances from selected to the root
*/
instancePath: InstancePath;
- instanceSelector: readonly Instance["id"][];
};
export type Hook = {
@@ -43,8 +43,8 @@ export type Hook = {
*/
export const getClosestInstance = (
instancePath: InstancePath,
- currentInstance: Instance,
- closestComponent: Instance["component"]
+ currentInstance: InstanceData,
+ closestComponent: InstanceData["component"]
) => {
let matched = false;
for (const instance of instancePath) {
@@ -56,16 +56,3 @@ export const getClosestInstance = (
}
}
};
-
-export const getInstanceSelectorById = (
- instanceSelector: readonly Instance["id"][],
- instanceId: Instance["id"]
-) => {
- const index = instanceSelector.findIndex(
- (selector) => selector === instanceId
- );
- if (index === -1) {
- return [];
- }
- return instanceSelector.slice(index);
-};
diff --git a/packages/sdk-components-react-radix/src/accordion.tsx b/packages/sdk-components-react-radix/src/accordion.tsx
index 6daf8a82d3..dd339e64cd 100644
--- a/packages/sdk-components-react-radix/src/accordion.tsx
+++ b/packages/sdk-components-react-radix/src/accordion.tsx
@@ -15,7 +15,6 @@ import {
import {
getClosestInstance,
getIndexWithinAncestorFromComponentProps,
- getInstanceSelectorById,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
@@ -75,15 +74,11 @@ export const hooksAccordion: Hook = {
);
if (accordion && item) {
const itemValue =
- context.getPropValue(item.id, "value") ??
+ context.getPropValue(item, "value") ??
context.indexesWithinAncestors.get(item.id)?.toString();
if (itemValue) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- accordion.id
- );
- context.setMemoryProp(instanceSelector, "value", itemValue);
+ context.setMemoryProp(accordion, "value", itemValue);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/collapsible.tsx b/packages/sdk-components-react-radix/src/collapsible.tsx
index 82a6dae829..8999875a0c 100644
--- a/packages/sdk-components-react-radix/src/collapsible.tsx
+++ b/packages/sdk-components-react-radix/src/collapsible.tsx
@@ -7,11 +7,7 @@ import {
type RefAttributes,
} from "react";
import { Root, Trigger, Content } from "@radix-ui/react-collapsible";
-import {
- type Hook,
- getClosestInstance,
- getInstanceSelectorById,
-} from "@webstudio-is/react-sdk/runtime";
+import { type Hook, getClosestInstance } from "@webstudio-is/react-sdk/runtime";
export const Collapsible: ForwardRefExoticComponent<
Omit<ComponentProps<typeof Root>, "defaultOpen" | "asChild"> &
@@ -58,11 +54,7 @@ export const hooksCollapsible: Hook = {
`${namespace}:Collapsible`
);
if (collapsible) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- collapsible.id
- );
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(collapsible, "open", true);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/dialog.tsx b/packages/sdk-components-react-radix/src/dialog.tsx
index 9c93a71ecf..2541bcc822 100644
--- a/packages/sdk-components-react-radix/src/dialog.tsx
+++ b/packages/sdk-components-react-radix/src/dialog.tsx
@@ -12,7 +12,6 @@ import * as DialogPrimitive from "@radix-ui/react-dialog";
import {
ReactSdkContext,
getClosestInstance,
- getInstanceSelectorById,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
@@ -202,11 +201,7 @@ export const hooksDialog: Hook = {
`${namespace}:Dialog`
);
if (dialog) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- dialog.id
- );
- context.setMemoryProp(instanceSelector, "open", undefined);
+ context.setMemoryProp(dialog, "open", undefined);
}
}
}
@@ -219,14 +214,8 @@ export const hooksDialog: Hook = {
instance,
`${namespace}:Dialog`
);
-
if (dialog) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- dialog.id
- );
-
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(dialog, "open", true);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/navigation-menu.tsx b/packages/sdk-components-react-radix/src/navigation-menu.tsx
index 3bd34f351e..be5d7b019d 100644
--- a/packages/sdk-components-react-radix/src/navigation-menu.tsx
+++ b/packages/sdk-components-react-radix/src/navigation-menu.tsx
@@ -2,7 +2,6 @@ import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";
import {
getClosestInstance,
getIndexWithinAncestorFromComponentProps,
- getInstanceSelectorById,
ReactSdkContext,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
@@ -90,14 +89,8 @@ export const hooksNavigationMenu: Hook = {
instance,
`${namespace}:NavigationMenu`
);
-
if (menu) {
- const menuSelector = getInstanceSelectorById(
- event.instanceSelector,
- menu.id
- );
-
- context.setMemoryProp(menuSelector, "value", undefined);
+ context.setMemoryProp(menu, "value", undefined);
}
}
}
@@ -110,28 +103,19 @@ export const hooksNavigationMenu: Hook = {
instance,
`${namespace}:NavigationMenu`
);
-
const menuItem = getClosestInstance(
event.instancePath,
instance,
`${namespace}:NavigationMenuItem`
);
-
if (menuItem === undefined || menu === undefined) {
return;
}
-
const contentValue =
- context.getPropValue(menuItem.id, "value") ??
+ context.getPropValue(menuItem, "value") ??
context.indexesWithinAncestors.get(menuItem.id)?.toString();
-
if (contentValue) {
- const menuSelector = getInstanceSelectorById(
- event.instanceSelector,
- menu.id
- );
-
- context.setMemoryProp(menuSelector, "value", contentValue);
+ context.setMemoryProp(menu, "value", contentValue);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/popover.tsx b/packages/sdk-components-react-radix/src/popover.tsx
index e5194c41c3..7dd5a5453d 100644
--- a/packages/sdk-components-react-radix/src/popover.tsx
+++ b/packages/sdk-components-react-radix/src/popover.tsx
@@ -5,11 +5,7 @@ import {
Children,
} from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
-import {
- getClosestInstance,
- getInstanceSelectorById,
- type Hook,
-} from "@webstudio-is/react-sdk/runtime";
+import { getClosestInstance, type Hook } from "@webstudio-is/react-sdk/runtime";
// wrap in forwardRef because Root is functional component without ref
export const Popover = forwardRef<
@@ -75,11 +71,7 @@ export const hooksPopover: Hook = {
`${namespace}:Popover`
);
if (popover) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- popover.id
- );
- context.setMemoryProp(instanceSelector, "open", undefined);
+ context.setMemoryProp(popover, "open", undefined);
}
}
}
@@ -93,11 +85,7 @@ export const hooksPopover: Hook = {
`${namespace}:Popover`
);
if (popover) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- popover.id
- );
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(popover, "open", true);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/select.tsx b/packages/sdk-components-react-radix/src/select.tsx
index 313915ad64..351dc07eda 100644
--- a/packages/sdk-components-react-radix/src/select.tsx
+++ b/packages/sdk-components-react-radix/src/select.tsx
@@ -20,7 +20,6 @@ import {
import {
type Hook,
getClosestInstance,
- getInstanceSelectorById,
ReactSdkContext,
} from "@webstudio-is/react-sdk/runtime";
@@ -99,11 +98,7 @@ export const hooksSelect: Hook = {
`${namespace}:Select`
);
if (select) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- select.id
- );
- context.setMemoryProp(instanceSelector, "open", undefined);
+ context.setMemoryProp(select, "open", undefined);
}
}
}
@@ -117,11 +112,7 @@ export const hooksSelect: Hook = {
`${namespace}:Select`
);
if (select) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- select.id
- );
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(select, "open", true);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/sheet.tsx b/packages/sdk-components-react-radix/src/sheet.tsx
index 48c662a18f..dbc13b53ce 100644
--- a/packages/sdk-components-react-radix/src/sheet.tsx
+++ b/packages/sdk-components-react-radix/src/sheet.tsx
@@ -3,11 +3,7 @@ import {
type ElementRef,
type ComponentPropsWithoutRef,
} from "react";
-import {
- getClosestInstance,
- getInstanceSelectorById,
- type Hook,
-} from "@webstudio-is/react-sdk/runtime";
+import { getClosestInstance, type Hook } from "@webstudio-is/react-sdk/runtime";
import * as Dialog from "./dialog";
export const Sheet = Dialog.Dialog;
@@ -61,11 +57,7 @@ export const hooksSheet: Hook = {
);
if (sheet) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- sheet.id
- );
- context.setMemoryProp(instanceSelector, "open", undefined);
+ context.setMemoryProp(sheet, "open", undefined);
}
}
}
@@ -79,11 +71,7 @@ export const hooksSheet: Hook = {
`${namespace}:Sheet`
);
if (sheet) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- sheet.id
- );
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(sheet, "open", true);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/tabs.tsx b/packages/sdk-components-react-radix/src/tabs.tsx
index 5b355bb4c3..f08d1318b8 100644
--- a/packages/sdk-components-react-radix/src/tabs.tsx
+++ b/packages/sdk-components-react-radix/src/tabs.tsx
@@ -9,7 +9,6 @@ import { Root, List, Trigger, Content } from "@radix-ui/react-tabs";
import {
getClosestInstance,
getIndexWithinAncestorFromComponentProps,
- getInstanceSelectorById,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
@@ -52,14 +51,10 @@ export const hooksTabs: Hook = {
`${namespace}:Tabs`
);
const contentValue =
- context.getPropValue(instance.id, "value") ??
+ context.getPropValue(instance, "value") ??
context.indexesWithinAncestors.get(instance.id)?.toString();
if (tabs && contentValue) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- tabs.id
- );
- context.setMemoryProp(instanceSelector, "value", contentValue);
+ context.setMemoryProp(tabs, "value", contentValue);
}
}
}
diff --git a/packages/sdk-components-react-radix/src/tooltip.tsx b/packages/sdk-components-react-radix/src/tooltip.tsx
index cd0d20abe9..cddc87639c 100644
--- a/packages/sdk-components-react-radix/src/tooltip.tsx
+++ b/packages/sdk-components-react-radix/src/tooltip.tsx
@@ -1,9 +1,5 @@
import * as TooltipPrimitive from "@radix-ui/react-tooltip";
-import {
- getClosestInstance,
- getInstanceSelectorById,
- type Hook,
-} from "@webstudio-is/react-sdk/runtime";
+import { getClosestInstance, type Hook } from "@webstudio-is/react-sdk/runtime";
import {
forwardRef,
@@ -74,11 +70,7 @@ export const hooksTooltip: Hook = {
`${namespace}:Tooltip`
);
if (tooltip) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- tooltip.id
- );
- context.setMemoryProp(instanceSelector, "open", undefined);
+ context.setMemoryProp(tooltip, "open", undefined);
}
}
}
@@ -92,11 +84,7 @@ export const hooksTooltip: Hook = {
`${namespace}:Tooltip`
);
if (tooltip) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- tooltip.id
- );
- context.setMemoryProp(instanceSelector, "open", true);
+ context.setMemoryProp(tooltip, "open", true);
}
}
}
diff --git a/packages/sdk-components-react/src/select.tsx b/packages/sdk-components-react/src/select.tsx
index 1770fef00e..9ffa3f4055 100644
--- a/packages/sdk-components-react/src/select.tsx
+++ b/packages/sdk-components-react/src/select.tsx
@@ -1,8 +1,4 @@
-import {
- getClosestInstance,
- getInstanceSelectorById,
- type Hook,
-} from "@webstudio-is/react-sdk/runtime";
+import { type Hook } from "@webstudio-is/react-sdk/runtime";
import { forwardRef, type ElementRef, type ComponentProps } from "react";
export const defaultTag = "select";
@@ -23,36 +19,14 @@ export const hooksSelect: Hook = {
onNavigatorUnselect: (context, event) => {
for (const instance of event.instancePath) {
if (instance.component === `Option`) {
- const option = getClosestInstance(
- event.instancePath,
- instance,
- `Option`
- );
- if (option) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- option.id
- );
- context.setMemoryProp(instanceSelector, "selected", undefined);
- }
+ context.setMemoryProp(instance, "selected", undefined);
}
}
},
onNavigatorSelect: (context, event) => {
for (const instance of event.instancePath) {
if (instance.component === `Option`) {
- const option = getClosestInstance(
- event.instancePath,
- instance,
- `Option`
- );
- if (option) {
- const instanceSelector = getInstanceSelectorById(
- event.instanceSelector,
- option.id
- );
- context.setMemoryProp(instanceSelector, "selected", true);
- }
+ context.setMemoryProp(instance, "selected", true);
}
}
},