focus-trap / tabbable

Find descendants of a DOM node that are in the tab order
MIT License
534 stars 58 forks source link
a11y dependabot focus-trap focusable

tabbable CI Codecov license

All Contributors

Small utility that returns an array of all* tabbable DOM nodes within a containing node.

*all has some necessary caveats, which you'll learn about by reading below.

The following are considered tabbable:

Any of the above will not be considered tabbable, though, if any of the following are also true about it:

If you think a node should be included in your array of tabbables but it's not, all you need to do is add tabindex="0" to deliberately include it. (Or if it is in your array but you don't want it, you can add tabindex="-1" to deliberately exclude it.) This will also result in more consistent cross-browser behavior. For information about why your special node might not be included, see "More details", below.

Goals

Browser Support

As old and as broad as reasonably possible, excluding browsers that are out of support or have nearly no user base.

Focused on desktop browsers, particularly Chrome, Edge, FireFox, Safari, and Opera.

Tabbable is not officially tested on any mobile browsers or devices.

⚠️ Microsoft no longer supports any version of IE, so IE is no longer supported by this library.

💬 Keep in mind that performance optimization and old browser support are often at odds, so tabbable may not always be able to use the most optimal (typically modern) APIs in all cases.

Installation

npm install tabbable

💬 Some very old browsers may need a polyfill for the CSS.escape API for tabbable to work properly with radio buttons that have name attributes containing special characters.

API

tabbable

import { tabbable } from 'tabbable';

tabbable(container, [options]);

Returns an array of ordered tabbable nodes (i.e. in tab order) within the container.

Summary of ordering principles:

isTabbable

import { isTabbable } from 'tabbable';

isTabbable(node, [options]);

Returns a boolean indicating whether the provided node is considered tabbable.

💬 If the node has an inert ancestor, it will not be tabbable.

focusable

import { focusable } from 'tabbable';

focusable(container, [options]);

Returns an array of focusable nodes within the container, in DOM order. This will not match the order in which tabbable() returns nodes.

isFocusable

import { isFocusable } from 'tabbable';

isFocusable(node, [options]);

Returns a boolean indicating whether the provided node is considered focusable.

💬 All tabbable elements are focusable, but not all focusable elements are tabbable. For example, elements with tabindex="-1" are focusable but not tabbable. Also note that if the node has aninert ancestor, it will not be focusable.

getTabIndex

import { getTabIndex } from 'tabbable';

getTabIndex(node);

Returns a negative, 0, or positive number that expresses the node's tab index in the DOM, with exceptions made where there are browser inconsistencies related to <audio>, <video>, <details>, and elements with the contenteditable="true" attribute.

The specific exceptions may change over time. See the implementation for specific behavior.

Common Options

These options apply to all APIs.

displayCheck option

Type: full | legacy-full | non-zero-area | none . Default: full.

Configures how to check if an element is displayed.

To reliably check if an element is tabbable/focusable, Tabbable defaults to the most reliable option to keep consistent with browser behavior, however this comes at a cost since every node needs to be validated as displayed using Web APIs that cause layout reflow.

For this reason Tabbable offers the ability of an alternative way to check if an element is displayed (or completely opt out of the check).

The displayCheck configuration accepts the following options:

⚠️ Testing in JSDom (e.g. with Jest): See notes about testing in JSDom.

getShadowRoot option

By default, tabbable overlooks (i.e. does not consider) all elements contained in shadow DOMs (whether open or closed). This has been the behavior since the beginning.

Setting this option to a truthy value enables Shadow DOM support, which means tabbable will consider elements inside web components as candidates, both open (automatically) and closed (provided this function returns the shadow root).

Type: boolean | (node: FocusableElement) => ShadowRoot | boolean | undefined

If set to a function, and if it returns true, Tabbable assumes a closed ShadowRoot is attached and will treat the node as a scope, iterating its children for additional tabbable/focusable candidates as though it was looking inside the shadow, but not. This will get tabbing order closer to -- but not necessarily the same as -- browser order.

Returning true from a function will also inform how the node's visibility check is done, causing tabbable to use the non-zero-area Display Check when determining if it's visible, and so tabbable/focusable.

More details

Help

Testing in JSDom

⚠️ JSDom is not officially supported. Your mileage may vary, and tests may break from one release to the next (even a patch or minor release).

This topic is just here to help with what we know may affect your tests.

Tabbable uses some DOM APIs such as Element.getClientRects() in order to determine node visibility, which helps in deciding whether a node is tabbable, focusable, or neither.

When using test engines such as Jest that use JSDom under the hood in order to run tests in Node.js (as opposed to using an automated browser testing tool like Cypress, Playwright, or Nightwatch where a full DOM is available), it is highly recommended (if not essential) to set the displayCheck option to none when calling any of the APIs in this library that accept it.

Using any other displayCheck setting will likely lead to failed tests due to nodes expected to be tabbable/focusable being determined to be the opposite because JSDom doesn't fully support some of the DOM APIs being used (even old ones that have been around for a long time).

You can globally overwrite the diplayCheck property by including this file in your __mocks__ folder:

// __mocks__/tabbable.js

const lib = jest.requireActual('tabbable');

const tabbable = {
   ...lib,
   tabbable: (node, options) => lib.tabbable(node, { ...options, displayCheck: 'none' }),
   focusable: (node, options) => lib.focusable(node, { ...options, displayCheck: 'none' }),
   isFocusable: (node, options) => lib.isFocusable(node, { ...options, displayCheck: 'none' }),
   isTabbable: (node, options) => lib.isTabbable(node, { ...options, displayCheck: 'none' }),
};

module.exports = tabbable;

Contributing

Feedback and contributions more than welcome!

See CONTRIBUTING.

Contributors

In alphabetical order:

Bryan Murphy
Bryan Murphy

🐛 💻
Craig Kovatch
Craig Kovatch

🐛
DaviDevMod
DaviDevMod

🐛 💻 ⚠️ 📖
David Clark
David Clark

💻 🐛 🚇 ⚠️ 📖 🚧
Dependabot
Dependabot

🚧
Erica Pramer
Erica Pramer

⚠️
Ido Rosenthal
Ido Rosenthal

🐛 💻 👀 ⚠️
Kristian Hamilton
Kristian Hamilton

🐛
Les Lim
Les Lim

🐛
Mateusz Burzyński
Mateusz Burzyński

💻 🐛 📖
Richard Všianský
Richard Všianský

📖
Stefan Cameron
Stefan Cameron

💻 🐛 🚇 ⚠️ 📖 🚧
Tyler Hawkins
Tyler Hawkins

🔧 ⚠️ 🚇 📖
bfrost
bfrost

🐛
pebble2050
pebble2050

🐛