Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/goofy-ways-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@noriginmedia/norigin-spatial-navigation-core": patch
---

Prevent preferredChildFocusKey from moving focus outside the current target’s subtree
21 changes: 20 additions & 1 deletion packages/core/src/SpatialNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@
this.layoutAdapter = new LayoutAdapterClass(this);
} else {
if (useGetBoundingClientRect) {
console.warn(

Check warning on line 790 in packages/core/src/SpatialNavigation.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
'useGetBoundingClientRect is deprecated. Please use layoutAdapter API instead.'
);
this.layoutAdapter = new GetBoundingClientRectAdapter(this);
Expand Down Expand Up @@ -1188,7 +1188,7 @@

if (this.debug) {
if (nextComponent != null && !siblings.includes(nextComponent)) {
console.warn(

Check warning on line 1191 in packages/core/src/SpatialNavigation.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
`nextFocusResolver returned an invalid component. This will result in lost focus. Check the "nextFocusResolver" implementation in component with focusKey: ${parentFocusKey}`
);
}
Expand All @@ -1208,7 +1208,7 @@
).length;

if (focusableSiblingCount >= 2) {
console.warn(

Check warning on line 1211 in packages/core/src/SpatialNavigation.ts

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
`measureChildrenLayout is false but no nextFocusResolver is set for component with focusKey: ${parentFocusKey}. Default coordinate-based navigation requires fresh layout data and may behave incorrectly.`
);
}
Expand Down Expand Up @@ -1432,7 +1432,8 @@
*/
if (
preferredChildFocusKey &&
this.isParticipatingFocusableComponent(preferredChildFocusKey)
this.isParticipatingFocusableComponent(preferredChildFocusKey) &&
this.isDescendantOf(preferredChildFocusKey, targetFocusKey)
) {
this.log(
'getNextFocusKey',
Expand Down Expand Up @@ -1770,6 +1771,24 @@
);
}

/**
* Checks whether the focusableComponent is a descendant of another component.
* @param focusKey The focus key of the component to check
* @param ancestorFocusKey The focus key of the ancestor component
* @returns Whether the focusableComponent is a descendant of the ancestor component
*/
isDescendantOf(focusKey: string, ancestorFocusKey: string): boolean {
let currentComponent = this.focusableComponents[focusKey];
while (currentComponent) {
if (currentComponent.parentFocusKey === ancestorFocusKey) {
return true;
}
currentComponent =
this.focusableComponents[currentComponent.parentFocusKey];
}
return false;
}

onIntermediateNodeBecameFocused(
focusKey: string,
focusDetails: FocusDetails
Expand Down
Loading