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.

Scroll navigator into view when selecting instances on canvas/navigator (#1162)

Oleg Isonen0eaa57caaeaf
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 072ca803b2..d259dde614 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -1,4 +1,4 @@
-import type { MouseEvent, FormEvent } from "react";
+import { MouseEvent, FormEvent, useEffect } from "react";
import { Suspense, lazy, useCallback, useMemo, useRef } from "react";
import { useStore } from "@nanostores/react";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
@@ -82,6 +82,18 @@ export const WebstudioComponentDev = ({
return result;
}, [instanceProps]);
+ const isSelected = selectedInstanceId === instanceId;
+
+ // Scroll the selected instance into view when selected from navigator.
+ useEffect(() => {
+ if (isSelected) {
+ instanceElementRef.current?.scrollIntoView({
+ behavior: "smooth",
+ block: "nearest",
+ });
+ }
+ }, [isSelected]);
+
const readonlyProps =
instance.component === "Input" ? { readOnly: true } : undefined;
@@ -116,7 +128,7 @@ export const WebstudioComponentDev = ({
const instanceElement = (
<>
- {selectedInstanceId === instance.id && (
+ {isSelected && (
<SelectedInstanceConnector
instanceElementRef={instanceElementRef}
instance={instance}
diff --git a/packages/design-system/src/components/tree/tree-node.tsx b/packages/design-system/src/components/tree/tree-node.tsx
index d8121c5fdb..049d98e023 100644
--- a/packages/design-system/src/components/tree/tree-node.tsx
+++ b/packages/design-system/src/components/tree/tree-node.tsx
@@ -282,6 +282,16 @@ export const TreeItemBody = <Data extends { id: string }>({
const isDragging = dropTargetItemId !== undefined;
const isDropTarget = dropTargetItemId === itemData.id;
+ // Scroll the selected button into view when selected from canvas.
+ useEffect(() => {
+ if (isSelected && isDragging === false && isDropTarget === false) {
+ itemButtonRef.current?.scrollIntoView({
+ behavior: "smooth",
+ block: "nearest",
+ });
+ }
+ }, [isSelected, isDragging, isDropTarget]);
+
return (
<ItemContainer
onMouseEnter={onMouseEnter && (() => onMouseEnter(itemData))}