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.

Automatically open tab content on select in navigator (#2073)

Bogdan Chadkina737e03c8f4c
diff --git a/apps/builder/app/builder/shared/navigator-tree.tsx b/apps/builder/app/builder/shared/navigator-tree.tsx
index 68577616d8..156eb56cbe 100644
--- a/apps/builder/app/builder/shared/navigator-tree.tsx
+++ b/apps/builder/app/builder/shared/navigator-tree.tsx
@@ -125,7 +125,7 @@ export const NavigatorTree = ({ publish }: { publish: Publish }) => {
payload: {
name: "onNavigatorUnselect",
data: {
- instanceSelection: previousInstanceSelector.flatMap((id) => {
+ instancePath: previousInstanceSelector.flatMap((id) => {
const instance = instances.get(id);
return instance ? [instance] : [];
}),
@@ -141,7 +141,7 @@ export const NavigatorTree = ({ publish }: { publish: Publish }) => {
payload: {
name: "onNavigatorSelect",
data: {
- instanceSelection: instanceSelector.flatMap((id) => {
+ instancePath: instanceSelector.flatMap((id) => {
const instance = instances.get(id);
return instance ? [instance] : [];
}),
diff --git a/apps/builder/app/shared/nano-states/components.ts b/apps/builder/app/shared/nano-states/components.ts
index b1a37df88b..7789cd8d44 100644
--- a/apps/builder/app/shared/nano-states/components.ts
+++ b/apps/builder/app/shared/nano-states/components.ts
@@ -1,16 +1,18 @@
import type { ExoticComponent } from "react";
import { atom } from "nanostores";
import {
- namespaceMeta,
type AnyComponent,
type WsComponentMeta,
type WsComponentPropsMeta,
type Hook,
type HookContext,
+ namespaceMeta,
+ getIndexesWithinAncestors,
} from "@webstudio-is/react-sdk";
import type { Instance } from "@webstudio-is/project-build";
import { subscribe } from "~/shared/pubsub";
import { dataSourceVariablesStore, propsStore } from "./nano-states";
+import { instancesStore, selectedPageStore } from ".";
type HookCommand = NonNullable<
{
@@ -33,8 +35,34 @@ declare module "~/shared/pubsub" {
export const subscribeComponentHooks = () => {
return subscribe("emitComponentHook", (data) => {
const hooks = registeredComponentHooksStore.get();
+ const metas = registeredComponentMetasStore.get();
+ const instances = instancesStore.get();
+ const page = selectedPageStore.get();
+ const indexesWithinAncestors = getIndexesWithinAncestors(
+ metas,
+ instances,
+ page ? [page.rootInstanceId] : []
+ );
for (const hook of hooks) {
const context: HookContext = {
+ indexesWithinAncestors,
+
+ getPropValue: (instanceId, propName) => {
+ const props = propsStore.get();
+ for (const prop of props.values()) {
+ if (prop.instanceId === instanceId && prop.name === propName) {
+ if (
+ prop.type === "string" ||
+ prop.type === "number" ||
+ prop.type === "boolean" ||
+ prop.type === "string[]"
+ ) {
+ return prop.value;
+ }
+ }
+ }
+ },
+
setPropVariable: (instanceId, propName, value) => {
const dataSourceVariables = new Map(dataSourceVariablesStore.get());
const props = propsStore.get();
diff --git a/packages/react-sdk/src/hook.test.ts b/packages/react-sdk/src/hook.test.ts
new file mode 100644
index 0000000000..c7abeb344e
--- /dev/null
+++ b/packages/react-sdk/src/hook.test.ts
@@ -0,0 +1,15 @@
+import { expect, test } from "@jest/globals";
+import { getClosestInstance, type InstancePath } from ".";
+
+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: [] },
+ ];
+ const [content2, tabs2, content1, tabs1, _body] = instancePath;
+ expect(getClosestInstance(instancePath, content2, "Tabs")).toBe(tabs2);
+ expect(getClosestInstance(instancePath, content1, "Tabs")).toBe(tabs1);
+});
diff --git a/packages/react-sdk/src/hook.ts b/packages/react-sdk/src/hook.ts
index 2c36eec68a..fbb8abfa61 100644
--- a/packages/react-sdk/src/hook.ts
+++ b/packages/react-sdk/src/hook.ts
@@ -1,4 +1,5 @@
import type { Instance, Prop } from "@webstudio-is/project-build";
+import type { IndexesWithinAncestors } from "./instance-utils";
/**
* Hooks are subscriptions to builder events
@@ -7,6 +8,8 @@ import type { Instance, Prop } from "@webstudio-is/project-build";
*/
export type HookContext = {
+ indexesWithinAncestors: IndexesWithinAncestors;
+ getPropValue: (instanceId: Instance["id"], propName: Prop["name"]) => unknown;
setPropVariable: (
instanceId: Instance["id"],
propName: Prop["name"],
@@ -14,10 +17,13 @@ export type HookContext = {
) => void;
};
-export type InstanceSelection = Instance[];
+export type InstancePath = Instance[];
type NavigatorEvent = {
- instanceSelection: InstanceSelection;
+ /**
+ * List of instances from selected to the root
+ */
+ instancePath: InstancePath;
};
export type Hook = {
@@ -25,13 +31,17 @@ export type Hook = {
onNavigatorUnselect?: (context: HookContext, event: NavigatorEvent) => void;
};
+/**
+ * Find closest matching instance by component name
+ * by lookup in instance path
+ */
export const getClosestInstance = (
- instanceSelection: InstanceSelection,
+ instancePath: InstancePath,
currentInstance: Instance,
closestComponent: Instance["component"]
) => {
let matched = false;
- for (const instance of instanceSelection) {
+ for (const instance of instancePath) {
if (currentInstance === instance) {
matched = true;
}
diff --git a/packages/sdk-components-react-radix/src/collapsible.tsx b/packages/sdk-components-react-radix/src/collapsible.tsx
index cc01784796..0521c5d48f 100644
--- a/packages/sdk-components-react-radix/src/collapsible.tsx
+++ b/packages/sdk-components-react-radix/src/collapsible.tsx
@@ -46,10 +46,10 @@ const namespace = "@webstudio-is/sdk-components-react-radix";
// and update its open prop bound to variable.
export const hooksCollapsible: Hook = {
onNavigatorUnselect: (context, event) => {
- for (const instance of event.instanceSelection) {
+ for (const instance of event.instancePath) {
if (instance.component === `${namespace}:CollapsibleContent`) {
const collapsible = getClosestInstance(
- event.instanceSelection,
+ event.instancePath,
instance,
`${namespace}:Collapsible`
);
@@ -60,10 +60,10 @@ export const hooksCollapsible: Hook = {
}
},
onNavigatorSelect: (context, event) => {
- for (const instance of event.instanceSelection) {
+ for (const instance of event.instancePath) {
if (instance.component === `${namespace}:CollapsibleContent`) {
const collapsible = getClosestInstance(
- event.instanceSelection,
+ event.instancePath,
instance,
`${namespace}:Collapsible`
);
diff --git a/packages/sdk-components-react-radix/src/hooks.ts b/packages/sdk-components-react-radix/src/hooks.ts
index 47e61c7ad7..b2bab1ab9e 100644
--- a/packages/sdk-components-react-radix/src/hooks.ts
+++ b/packages/sdk-components-react-radix/src/hooks.ts
@@ -1,4 +1,5 @@
import type { Hook } from "@webstudio-is/react-sdk";
import { hooksCollapsible } from "./collapsible";
+import { hooksTabs } from "./tabs";
-export const hooks: Hook[] = [hooksCollapsible];
+export const hooks: Hook[] = [hooksCollapsible, hooksTabs];
diff --git a/packages/sdk-components-react-radix/src/tabs.tsx b/packages/sdk-components-react-radix/src/tabs.tsx
index c6de7d67e7..3033eb2166 100644
--- a/packages/sdk-components-react-radix/src/tabs.tsx
+++ b/packages/sdk-components-react-radix/src/tabs.tsx
@@ -10,7 +10,11 @@ import {
Children,
} from "react";
import { Root, List, Trigger, Content } from "@radix-ui/react-tabs";
-import { getIndexWithinAncestorFromComponentProps } from "@webstudio-is/react-sdk";
+import {
+ getClosestInstance,
+ getIndexWithinAncestorFromComponentProps,
+ type Hook,
+} from "@webstudio-is/react-sdk";
export const Tabs: ForwardRefExoticComponent<
Omit<ComponentPropsWithRef<typeof Root>, "asChild" | "defaultValue">
@@ -44,3 +48,30 @@ export const TabsContent = forwardRef<
const index = getIndexWithinAncestorFromComponentProps(props);
return <Content ref={ref} value={value ?? index} {...props} />;
});
+
+/* BUILDER HOOKS */
+
+const namespace = "@webstudio-is/sdk-components-react-radix";
+
+// For each TabsContent component within the selection,
+// we identify its closest parent Tabs component
+// and update its open prop bound to variable.
+export const hooksTabs: Hook = {
+ onNavigatorSelect: (context, event) => {
+ for (const instance of event.instancePath) {
+ if (instance.component === `${namespace}:TabsContent`) {
+ const tabs = getClosestInstance(
+ event.instancePath,
+ instance,
+ `${namespace}:Tabs`
+ );
+ const contentValue =
+ context.getPropValue(instance.id, "value") ??
+ context.indexesWithinAncestors.get(instance.id)?.toString();
+ if (tabs && contentValue) {
+ context.setPropVariable(tabs.id, "value", contentValue);
+ }
+ }
+ }
+ },
+};