Skip to content

Commit

Permalink
fix: race conditions with default focus
Browse files Browse the repository at this point in the history
  • Loading branch information
remilry committed Apr 11, 2024
1 parent 6871d70 commit 656738d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/lib/src/spatial-navigation/SpatialNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ export default class SpatialNavigator {
* because multiple elements might try to take the focus (DefaultFocus is a context, so all its children
* will try to grab it). We only want the first of these element to grab it.
*/
public queueDefaultFocus = (id: string) => {
public handleOrQueueDefaultFocus = (id: string) => {
if (this.getCurrentFocusNode()) return;
if (this.focusQueue) return;
if (this.lrud.getNode(id)) {
this.lrud.assignFocus(id);
return;
}
this.focusQueue = id;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/spatial-navigation/components/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const SpatialNavigationNode = forwardRef<SpatialNavigationNodeRef, Props>

useEffect(() => {
if (shouldHaveDefaultFocus && isFocusable && !spatialNavigator.hasOneNodeFocused()) {
spatialNavigator.queueDefaultFocus(id);
spatialNavigator.handleOrQueueDefaultFocus(id);
}
}, [id, isFocusable, shouldHaveDefaultFocus, spatialNavigator]);

Expand Down

0 comments on commit 656738d

Please sign in to comment.