From 6e51fb9dc1a5d35345e1d2c9a5e58925c9120633 Mon Sep 17 00:00:00 2001 From: Mu-An Chiou Date: Tue, 2 Apr 2019 17:15:18 -0400 Subject: [PATCH 1/3] Update query for focusable elements --- index.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index ac0debc..d2932c0 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ const CLOSE_ATTR = 'data-close-dialog' const CLOSE_SELECTOR = `[${CLOSE_ATTR}]` -const INPUT_SELECTOR = 'a, input, button, textarea, select, summary' type Focusable = | HTMLButtonElement @@ -33,7 +32,10 @@ function keydown(event: KeyboardEvent): void { } function focusable(el: Focusable): boolean { - return !el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]') + return ( + (el.hasAttribute('tabindex') || el.tabIndex >= 0) && + (!el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')) + ) } function restrictTabBehavior(event: KeyboardEvent): void { @@ -42,7 +44,7 @@ function restrictTabBehavior(event: KeyboardEvent): void { if (!dialog) return event.preventDefault() - const elements: Array = Array.from(dialog.querySelectorAll(INPUT_SELECTOR)).filter(focusable) + const elements: Array = Array.from(dialog.querySelectorAll('*')).filter(focusable) if (elements.length === 0) return const movement = event.shiftKey ? -1 : 1 @@ -141,9 +143,6 @@ class DetailsDialogElement extends HTMLElement { static get CLOSE_SELECTOR() { return CLOSE_SELECTOR } - static get INPUT_SELECTOR() { - return INPUT_SELECTOR - } constructor() { super() From 6866b962dbbaaba8e71596b0f8015dc167a70fd3 Mon Sep 17 00:00:00 2001 From: Mu-An Chiou Date: Tue, 2 Apr 2019 18:02:36 -0400 Subject: [PATCH 2/3] Fix tabindex='-1' elements being tabbable --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index d2932c0..cb5e37e 100644 --- a/index.js +++ b/index.js @@ -33,8 +33,7 @@ function keydown(event: KeyboardEvent): void { function focusable(el: Focusable): boolean { return ( - (el.hasAttribute('tabindex') || el.tabIndex >= 0) && - (!el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')) + el.tabIndex >= 0 && (!el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')) ) } From 166709c05128578f9e8a2129b333ecb4d8e23fa8 Mon Sep 17 00:00:00 2001 From: Mu-An Chiou Date: Tue, 2 Apr 2019 18:09:54 -0400 Subject: [PATCH 3/3] Remove unnecessary parentheses --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index cb5e37e..3bf8c21 100644 --- a/index.js +++ b/index.js @@ -32,9 +32,7 @@ function keydown(event: KeyboardEvent): void { } function focusable(el: Focusable): boolean { - return ( - el.tabIndex >= 0 && (!el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')) - ) + return el.tabIndex >= 0 && !el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]') } function restrictTabBehavior(event: KeyboardEvent): void {