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:
<button>elements<input>elements<select>elements<textarea>elements<a>elements with anhrefattribute<audio>and<video>elements withcontrolsattributes- the first
<summary>element directly under a<details>element <details>element without a<summary>element- elements with the
[contenteditable]attribute - anything with a non-negative
tabindexattribute
Any of the above will not be considered tabbable, though, if any of the following are also true about it:
- has a negative
tabindexattribute - has a
disabledattribute - either the node itself or an ancestor of it is hidden via
display: none(*see "Display check" below to modify this behavior) - has
visibility: hiddenstyle - is nested under a closed
<details>element (with the exception of the first<summary>element) - is an
<input type="radio">element and a different radio in its group ischecked - is a form field (button, input, select, textarea) inside a disabled
<fieldset>
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.
- Accurate (or, as accurate as possible & reasonable)
- No dependencies
- Small
- Fast
Basically IE9+.
Why? It uses Element.querySelectorAll() and Window.getComputedStyle().
Note: When used with any version of IE, CSS.escape needs a polyfill for tabbable to work properly with radio buttons that have name attributes containing special characters.
npm install tabbable
import { tabbable } from 'tabbable';
tabbable(rootNode, [options]);rootNode: Node(Required)options:- All the common options.
includeContainer: boolean(default: false)- If set to
true,rootNodewill be included in the returned tabbable node array, ifrootNodeis tabbable.
- If set to
Returns an array of ordered tabbable nodes (i.e. in tab order) within the rootNode.
Summary of ordering principles:
- First include any nodes with positive
tabindexattributes (1 or higher), ordered by ascendingtabindexand source order. - Then include any nodes with a zero
tabindexand any element that by default receives focus (listed above) and does not have a positivetabindexset, in source order.
import { isTabbable } from 'tabbable';
isTabbable(node, [options]);node: Node(Required)options:- All the common options.
Returns a boolean indicating whether the provided node is considered tabbable.
import { focusable } from 'tabbable';
focusable(rootNode, [options]);rootNode: Node: Requiredoptions:- All the common options.
includeContainer: boolean(default: false)- If set to
true,rootNodewill be included in the returned focusable node array, ifrootNodeis focusable.
- If set to
Returns an array of focusable nodes within the rootNode, in DOM order. This will not match the order in which tabbable() returns nodes.
import { isFocusable } from 'tabbable';
isFocusable(node, [options]);node: Node(Required)options:- All the common 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.
These options apply to all APIs.
Type: 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. The full process checks for computed display property of an element and each of the element ancestors. 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:
full: (default) Most reliably resembling browser behavior, this option checks that an element is displayed and all of his ancestors are displayed as well (notice that this doesn't excludevisibility: hiddenor elements with zero size). This option will cause layout reflow, however. If that is a concern, consider thenoneoption.non-zero-area: This option checks display under the assumption that elements that are not displayed have zero area (width AND height equals zero). While not keeping true to browser behavior, this option may enhance accessibility, as zero-size elements with focusable content are considered a strong accessibility anti-pattern. Like thefulloption, this option also causes layout reflow, and should have basically the same performance. Consider thenoneoption if reflow is a concern.none: This completely opts out of the display check. This option is not recommended, as it might return elements that are not displayed, and as such not tabbable/focusable and can break accessibility. Make sure you know which elements in your DOM are not displayed and can filter them out yourself before using this option.
β οΈ Testing in JSDom (e.g. with Jest): See notes about testing in JSDom.
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
boolean:truesimply enables shadow DOM support for any open shadow roots, but never presumes there is an undisclosed shadow. This is the equivalent of settinggetShadowRoot: () => falsefalse(default) disables shadow DOM support.
function:nodewill be a descendent of therootNodegiven totabbable(),isTabbable(),focusable(), orisFocusable().- Returns: The node's
ShadowRootif available,trueindicating aShadowRootis attached but not available (i.e. "undisclosed"), or a falsy value indicating there is no shadow attached to the node.
If set to a function, and if it returns
true, Tabbable assumes a closedShadowRootis 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
truefrom 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.
- Tabbable tries to identify elements that are reliably tabbable across (not dead) browsers. Browsers are inconsistent in their behavior, though βΒ especially for edge-case elements like
<object>and<iframe>βΒ so this means some elements that you can tab to in some browsers will be left out of the results. (To learn more about this inconsistency, see this amazing table). To provide better consistency across browsers and ensure the elements you want in your tabbables list show up there, try addingtabindex="0"to edge-case elements that Tabbable ignores. - (Exemplifying the above ^^:) The tabbability of
<iframe>,<embed>,<object>,<summary>, and<svg>nodes is inconsistent across browsers, so if you need an accurate read on one of these elements you should try giving it atabindex. (You'll also need to pay attention to thefocusableattribute on SVGs in IE & Edge.) But you also might not be able to get an accurate read βΒ so you should avoid relying on it. - Radio groups have some edge cases, which you can avoid by always having a
checkedone in each group (and that is what you should usually do anyway). If there is nocheckedradio in the radio group, all of the radios will be considered tabbable. (Some browsers do this, otherwise don't βΒ there's not consistency.) - If you're thinking, "Why not just use the right
querySelectorAll?", you may be on to something ... but, as with most "just" statements, you're probably not. For example, a simplequerySelectorAllapproach will not figure out whether an element is hidden, and therefore not actually tabbable. (That said, if you do think Tabbable can be simplified or otherwise improved, I'd love to hear your idea.) - jQuery UI's
:tabbableselector ignores elements with height and width of0. I'm not sure why β because I've found that I can still tab to those elements. So I kept them in. Only elements hidden withdisplay: noneorvisibility: hiddenare left out. See "Display check" below for other options. - Although Tabbable tries to deal with positive tabindexes, you should not use positive tabindexes. Accessibility experts seem to be in (rare) unanimous and clear consent about this: rely on the order of elements in the document.
- Safari on Mac OS X does not Tab to
<a>elements by default: you have to change a setting to get the standard behavior. Tabbable does not know whether you've changed that setting or not, so it will include<a>elements in its list.
β οΈ 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;Feedback and contributions more than welcome!
See CONTRIBUTING.
In alphabetical order: