0.8.4 • Published 8 months ago

focusable-selectors v0.8.4

Weekly downloads
1,576
License
MIT
Repository
github
Last release
8 months ago

Focusable Selectors

focusable-selectors is a micro-lib exporting an array of CSS selectors for focusable HTML elements. Figuring out which element can be focused is not always trivial and is sometimes necessary to build fully accessible widgets such as a11y-dialog.

It supports:

  • All natively focusable elements,
  • … and any element with the contenteditable attribute,
  • … provided they are not disabled,
  • … and do not have a negative tabindex attribute,
  • … and do not have the inert attribute.

For a more advanced solution using JavaScript and covering more edge cases, refer to focus-trap/tabbable.

Usage

npm install --save focusable-selectors
import focusableSelectors from 'focusable-selectors'

To have a single CSS selector out of it, join the array with commas:

const selector = selectors.join(',')
const focusableElements = document.querySelectorAll(selector)

Considering styles

The “focusability” of an element is not solely based on its type, but also on its styles. A relatively straightforward solution is to use JavaScript to figure out whether focusable elements are effectively visible by checking their dimensions.

function isVisible(element) {
  return Boolean(
    element.offsetWidth ||
      element.offsetHeight ||
      element.getClientRects().length
  )
}

const selector = selectors.join(',')
const focusableElements = Array.from(
  document.querySelectorAll(selector)
).filter(isVisible)
0.8.4

8 months ago

0.8.3

8 months ago

0.8.2

8 months ago

0.8.1

11 months ago

0.8.0

1 year ago

0.5.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.4.0

2 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago