From b6228d703502f6aad5fbb88b4af6b80d77c83273 Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Fri, 24 Nov 2023 13:02:39 +0100 Subject: [PATCH 1/2] Check slotted content for autofocus delegate --- src/popover-helpers.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/popover-helpers.ts b/src/popover-helpers.ts index 6e91a49..dd7131d 100644 --- a/src/popover-helpers.ts +++ b/src/popover-helpers.ts @@ -196,9 +196,20 @@ function focusDelegate(focusTarget: HTMLElement) { if (whereToLook.shadowRoot) { whereToLook = whereToLook.shadowRoot; } - const autoFocusDelegate = whereToLook.querySelector('[autofocus]'); + let autoFocusDelegate: Element | null | undefined = + whereToLook.querySelector('[autofocus]'); if (autoFocusDelegate) { return autoFocusDelegate; + } else { + const slots = whereToLook.querySelectorAll('slot'); + for (const slot of slots) { + autoFocusDelegate = slot + .assignedElements({ flatten: true }) + .find((el) => el.hasAttribute('autofocus')); + if (autoFocusDelegate) { + return autoFocusDelegate; + } + } } const walker = focusTarget.ownerDocument.createTreeWalker( whereToLook, From dda43c0b29f46ff10e2f6512abcb16c0e6aa1c16 Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Fri, 24 Nov 2023 13:08:58 +0100 Subject: [PATCH 2/2] Fix logic --- src/popover-helpers.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/popover-helpers.ts b/src/popover-helpers.ts index dd7131d..2fda3a7 100644 --- a/src/popover-helpers.ts +++ b/src/popover-helpers.ts @@ -196,18 +196,22 @@ function focusDelegate(focusTarget: HTMLElement) { if (whereToLook.shadowRoot) { whereToLook = whereToLook.shadowRoot; } - let autoFocusDelegate: Element | null | undefined = - whereToLook.querySelector('[autofocus]'); + let autoFocusDelegate = whereToLook.querySelector('[autofocus]'); if (autoFocusDelegate) { return autoFocusDelegate; } else { const slots = whereToLook.querySelectorAll('slot'); for (const slot of slots) { - autoFocusDelegate = slot - .assignedElements({ flatten: true }) - .find((el) => el.hasAttribute('autofocus')); - if (autoFocusDelegate) { - return autoFocusDelegate; + const assignedElements = slot.assignedElements({ flatten: true }); + for (const el of assignedElements) { + if (el.hasAttribute('autofocus')) { + return el; + } else { + autoFocusDelegate = el.querySelector('[autofocus]'); + if (autoFocusDelegate) { + return autoFocusDelegate; + } + } } } }