Skip to content

Commit

Permalink
Merge branch 'master' into overlap-readme-update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningTrapez committed Jan 17, 2024
2 parents 68b2aa1 + b6605d4 commit dccfc3f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/lrud.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,23 @@ export const getNextFocus = (elem, keyOrKeyCode, scope) => {
const isNestedContainer = parentContainer?.contains(candidateContainer);
const isAnscestorContainer = candidateContainer?.contains(parentContainer);

if (elem.id) parentContainer?.setAttribute('data-focus', elem.id);

if (!isCurrentContainer && (!isNestedContainer || isBestCandidateAContainer)) {
const blockedExitDirs = getBlockedExitDirs(parentContainer, candidateContainer);
if (blockedExitDirs.indexOf(exitDir) > -1) return;

if (candidateContainer && !isAnscestorContainer) {
// Ignore active child behaviour when moving into a container that we
// are already nested in
if (elem.id) parentContainer?.setAttribute('data-focus', elem.id);

const lastActiveChild = document.getElementById(candidateContainer.getAttribute('data-focus'));

return lastActiveChild || getFocusables(candidateContainer)?.[0];
}
}
}

if (bestCandidate?.id) parentContainer?.setAttribute('data-focus', bestCandidate.id);
return bestCandidate;
};

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/tv-lrud-spatial",
"version": "0.0.8",
"version": "0.0.9",
"description": "Spatial navigation library",
"main": "lib/lrud.min.js",
"directories": {
Expand Down
11 changes: 11 additions & 0 deletions test/lrud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,5 +667,16 @@ describe('LRUD spatial', () => {
await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-1');
});

it('should still remember last active child if focus is manually moved from a container', async () => {
await page.evaluate(() => document.getElementById('item-1').focus());
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowRight');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-6');
await page.evaluate(() => document.getElementById('item-8').focus());

await page.keyboard.press('ArrowUp');
expect(await page.evaluate(() => document.activeElement.id)).toEqual('item-6');
});
});
});

0 comments on commit dccfc3f

Please sign in to comment.