From 452698240b8659c8cf2410eb0c5384b2d884c718 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 31 Jan 2024 10:06:35 +0100 Subject: [PATCH] chore: enhance focusout handler using relatedTarget --- .changeset/unlucky-students-complain.md | 5 +++++ src/focusEnterBehavior.ts | 13 +++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 .changeset/unlucky-students-complain.md diff --git a/.changeset/unlucky-students-complain.md b/.changeset/unlucky-students-complain.md new file mode 100644 index 0000000..31bdeaf --- /dev/null +++ b/.changeset/unlucky-students-complain.md @@ -0,0 +1,5 @@ +--- +"@chialab/loock": patch +--- + +Enhance focusout handler using relatedTarget diff --git a/src/focusEnterBehavior.ts b/src/focusEnterBehavior.ts index d3b3ebe..4798bc6 100644 --- a/src/focusEnterBehavior.ts +++ b/src/focusEnterBehavior.ts @@ -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 {