Skip to content

Commit

Permalink
fix(lists): fix imperative focus not working properly on android
Browse files Browse the repository at this point in the history
  • Loading branch information
pierpo committed Sep 11, 2024
1 parent d9509dc commit deea47f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/lib/src/spatial-navigation/SpatialNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export default class SpatialNavigator {
*/
private focusQueue: string | null = null;

private parentFocusQueue: string | null = null;

/**
* To handle the default focus, we want to queue the element to be focused.
* We queue it because it might not be registered yet when it asks for focus.
Expand All @@ -111,12 +113,15 @@ export default class SpatialNavigator {
* Still, I want to queue it, because the element might not be registered yet (example: in the case of virtualized lists)
*/
public deferredFocus = (id: string) => {
setTimeout(() => {
try {
if (this.lrud.getNode(id)) {
this.lrud.assignFocus(id);
return;
}
}, 0);
} catch (error) {
// TODO: check that error is that element is not focusable
this.parentFocusQueue = id;
}
};

/**
Expand All @@ -135,6 +140,15 @@ export default class SpatialNavigator {
// pass
}
}

if (this.parentFocusQueue && this.lrud.getNode(this.parentFocusQueue)?.children?.length !== 0) {
try {
this.lrud.assignFocus(this.parentFocusQueue);
this.parentFocusQueue = null;
} catch (e) {
// pass
}
}
};

public grabFocus = (id: string) => {
Expand Down

0 comments on commit deea47f

Please sign in to comment.