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

Commit

Permalink
Merge pull request #30 from github/tabindex
Browse files Browse the repository at this point in the history
Update query for focusable elements
  • Loading branch information
muan authored Apr 5, 2019
2 parents dbfcb1e + 166709c commit 52cc333
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,7 +32,7 @@ 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.tabIndex >= 0 && !el.disabled && !el.hidden && (!el.type || el.type !== 'hidden') && !el.closest('[hidden]')
}

function restrictTabBehavior(event: KeyboardEvent): void {
Expand All @@ -42,7 +41,7 @@ function restrictTabBehavior(event: KeyboardEvent): void {
if (!dialog) return
event.preventDefault()

const elements: Array<Focusable> = Array.from(dialog.querySelectorAll(INPUT_SELECTOR)).filter(focusable)
const elements: Array<Focusable> = Array.from(dialog.querySelectorAll('*')).filter(focusable)
if (elements.length === 0) return

const movement = event.shiftKey ? -1 : 1
Expand Down Expand Up @@ -158,9 +157,6 @@ class DetailsDialogElement extends HTMLElement {
static get CLOSE_SELECTOR() {
return CLOSE_SELECTOR
}
static get INPUT_SELECTOR() {
return INPUT_SELECTOR
}

constructor() {
super()
Expand Down

0 comments on commit 52cc333

Please sign in to comment.