Skip to content

Commit

Permalink
fix: set current target on enter
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Oct 20, 2022
1 parent 75e259a commit 3b813ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-worms-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/loock": patch
---

Fix current target on enter.
8 changes: 6 additions & 2 deletions src/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ export class Context {

/**
* Entering the context.
*
* @param {HTMLElement} [target] The target element to focus.
* @returns {Promise<void>}
*/
async enter() {
async enter(target) {
if (this.disabled || this.active) {
return;
}
Expand All @@ -298,6 +298,10 @@ export class Context {
console.warn('created a Context without aria-label', this);
}
await dispatchAsyncEvent(element, 'focusenter', this);
const children = this.findFocusableChildren();
if (children.indexOf(target) !== -1) {
this.setCurrentElement(target);
}
this.restore();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ export class Manager {
* @private
*/
this.onFocusIn = ({ target }) => {
const context = this.contexts.find(({ element }) => element === target);
const context = this.contexts
.filter(({ element }) => element === target || element.contains(target))
.sort(({ element: match1 }, { element: match2 }) => (match1.contains(match2) ? 1 : -1))[0];

if (context && !context.active && !context.disabled) {
context.enter();
context.enter(target);
return;
}

Expand Down

0 comments on commit 3b813ae

Please sign in to comment.