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.

another measure to avoid loosing focus

Roman Pominovbbdcd3f63c65
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index 505fa3638b..c0cd6bfbd1 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -389,6 +389,7 @@ export const Tree = ({
<Box
ref={keyboardNavigation.rootRef}
onClick={keyboardNavigation.handleClick}
+ onBlur={keyboardNavigation.handleBlur}
>
<TreeNode
animate={animate}
@@ -511,6 +512,34 @@ const useKeyboardNavigation = ({
[rootRef]
);
+ const hadFocus = useRef(false);
+ const prevRoot = useRef(root);
+ useEffect(() => {
+ const haveFocus =
+ rootRef.current?.contains(document.activeElement) === true;
+
+ const isRootChanged = prevRoot.current !== root;
+ prevRoot.current = root;
+
+ // If we've lost focus due to a root update, we want to get it back.
+ // This can happen when we delete an item or on drag-end.
+ if (
+ isRootChanged &&
+ haveFocus === false &&
+ hadFocus.current === true &&
+ selectedInstanceId !== undefined
+ ) {
+ setFocus(selectedInstanceId);
+ }
+ }, [root, rootRef, selectedInstanceId, setFocus]);
+
+ // onBlur doesn't fire when the activeElement is removed from the DOM
+ useEffect(() => {
+ const haveFocus =
+ rootRef.current?.contains(document.activeElement) === true;
+ hadFocus.current = haveFocus;
+ });
+
return {
rootRef(element: HTMLElement | null) {
rootRef.current = element;
@@ -532,6 +561,9 @@ const useKeyboardNavigation = ({
setFocus(selectedInstanceId);
}
},
+ handleBlur() {
+ hadFocus.current = false;
+ },
};
};