Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 6a07747

Browse files
authored
Merge pull request #48 from github/visible
Ensure focusble targets are visible
2 parents 0869b97 + 85b802c commit 6a07747

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Focusable =
1212
| HTMLElement
1313

1414
function autofocus(el: DetailsDialogElement): void {
15-
let autofocusElement = el.querySelector('[autofocus]')
15+
let autofocusElement = Array.from(el.querySelectorAll('[autofocus]')).filter(focusable)[0]
1616
if (!autofocusElement) {
1717
autofocusElement = el
1818
el.setAttribute('tabindex', '-1')
@@ -32,7 +32,11 @@ function keydown(event: KeyboardEvent): void {
3232
}
3333

3434
function focusable(el: Focusable): boolean {
35-
return el.tabIndex >= 0 && !el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')
35+
return el.tabIndex >= 0 && !el.disabled && visible(el)
36+
}
37+
38+
function visible(el): boolean {
39+
return !el.hidden && (!el.type || el.type !== 'hidden') && (el.offsetWidth > 0 || el.offsetHeight > 0)
3640
}
3741

3842
function restrictTabBehavior(event: KeyboardEvent): void {

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('details-dialog-element', function() {
2929
<button data-button>Button</button>
3030
<button hidden>hidden</button>
3131
<div hidden><button>hidden</button></div>
32+
<details><button>Button in closed details</button></details>
3233
<button ${CLOSE_ATTR}>Goodbye</button>
3334
</details-dialog>
3435
</details>

0 commit comments

Comments
 (0)