Skip to content

Commit

Permalink
chore: enhance focusout handler using relatedTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
DarioSacco committed Jan 31, 2024
1 parent cbc438d commit 4526982
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-students-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/loock": patch
---

Enhance focusout handler using relatedTarget
13 changes: 5 additions & 8 deletions src/focusEnterBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ export function focusEnterBehavior(node: HTMLElement, options: FocusEnterOptions
onEnter?.(activeElement);
};

const onFocusOut = () => {
const onFocusOut = (event: FocusEvent) => {
if (!focused) {
return;
}

setTimeout(() => {
const activeElement = document.activeElement;
if (node !== activeElement && !node.contains(activeElement)) {
focused = false;
onExit?.();
}
});
if (!node.contains(event.relatedTarget as HTMLElement)) {
focused = false;
onExit?.();
}
};

return {
Expand Down

0 comments on commit 4526982

Please sign in to comment.