
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
The tabbable npm package is used to identify DOM elements that are tabbable or focusable. This is useful for accessibility concerns, such as when creating keyboard navigation or managing focus within modal dialogs, dropdowns, and custom widgets. It helps ensure that keyboard users can navigate through all interactive elements on the page in a logical order.
Finding all tabbable elements
This feature allows you to get a list of all elements that are tabbable (can be focused using the Tab key) within a specified DOM node.
var tabbable = require('tabbable');
var tabbableElements = tabbable(document);
console.log(tabbableElements);
Finding all focusable elements
This feature provides a list of all elements that are focusable, including those that are not tabbable but can still receive focus, such as elements with a tabindex='-1'. The option `{ includeContainer: true }` includes the container itself if it is focusable.
var tabbable = require('tabbable');
var focusableElements = tabbable(document, { includeContainer: true });
console.log(focusableElements);
The focus-trap package is designed to trap focus within a DOM element, preventing the user from tabbing out of it. This is particularly useful for modal dialogs. It is similar to tabbable in that it deals with focus management, but it provides a higher-level API specifically for creating a focus trap rather than just identifying focusable elements.
ally.js is a JavaScript library that simplifies certain accessibility features, including focus management. It provides a broader range of accessibility utilities compared to tabbable, including the ability to find and manipulate focusable elements, but also extends to other areas such as accessible hiding of elements and maintaining disabled state across shadow DOM boundaries.
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 an href
attribute<audio>
and <video>
elements with controls
attributes<summary>
element directly under a <details>
element<details>
element without a <summary>
element[contenteditable]
attributetabindex
attributeAny of the above will not be considered tabbable, though, if any of the following are also true about it:
tabindex
attributedisabled
attributedisplay: none
(*see "Display check" below to modify this behavior)visibility: hidden
style<details>
element (with the exception of the first <summary>
element)<input type="radio">
element and a different radio in its group is checked
<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.
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
Dependencies: none.
import { tabbable } from 'tabbable';
tabbable(rootNode, [options]);
Returns an array of ordered tabbable nodes (i.e. in tab order) within the rootNode
.
Summary of ordering principles:
tabindex
attributes (1 or higher), ordered by ascending tabindex
and source order.tabindex
and any element that by default receives focus (listed above) and does not have a positive tabindex
set, in source order.Type: Node
. Required.
Type: boolean
. Default: false
.
If set to true
, rootNode
will be included in the returned tabbable node array, if rootNode
is tabbable.
Type: full
| non-zero-area
| none
. Default: full
.
Configures how to check if an element is displayed, see "Display check" below.
import { isTabbable } from 'tabbable';
isTabbable(node, [options]);
Returns a boolean indicating whether the provided node is considered tabbable.
Type: full
| non-zero-area
| none
. Default: full
.
Configures how to check if an element is displayed, see "Display check" below.
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.
Type: full
| non-zero-area
| none
. Default: full
.
Configures how to check if an element is displayed, see "Display check" below.
import { focusable } from 'tabbable';
focusable(rootNode, [options]);
Returns an array of focusable nodes within the rootNode
, in DOM order. This will not match the order in which tabbable()
returns nodes.
Type: Node
. Required.
Type: boolean
. Default: false
.
If set to true
, rootNode
will be included in the returned focusable node array, if rootNode
is focusable.
Type: full
| non-zero-area
| none
. Default: full
.
Configures how to check if an element is displayed, see "Display check" below.
<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 adding tabindex="0"
to edge-case elements that Tabbable ignores.<iframe>
s, <embed>
s, <object>
s, <summary>
s, and <svg>
s is inconsistent across browsers, so if you need an accurate read on one of these elements you should try giving it a tabindex
. (You'll also need to pay attention to the focusable
attribute on SVGs in IE & Edge.) But you also might not be able to get an accurate read — so you should avoid relying on it.checked
one in each group (and that is what you should usually do anyway). If there is no checked
radio in the radio group, all of the radios will be considered tabbable. (Some browsers do this, otherwise don't — there's not consistency.)querySelectorAll
?", you may be on to something ... but, as with most "just" statements, you're probably not. For example, a simple querySelectorAll
approach 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.):tabbable
selector ignores elements with height and width of 0
. 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 with display: none
or visibility: hidden
are left out. See "Display check" below for other options.<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.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 resemble browser behavior, this option checks that an element is displayed and all of his ancestors are displayed as well (Notice that this doesn't exclude visibility: hidden
or elements with zero size). This check is by far the slowest option as it might cause layout reflow.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 is much less intensive then the full
option and better for accessibility as zero-size elements with focusable content are considered a strong accessibility anti-pattern.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.Feedback and contributions more than welcome!
See CONTRIBUTING.
In alphabetical order:
Bryan Murphy 🐛 💻 | David Clark 💻 🐛 🚇 ⚠️ 📖 🚧 | Dependabot 🚧 | Ido Rosenthal 🐛 💻 👀 ⚠️ | Kristian Hamilton 🐛 | Mateusz Burzyński 💻 🐛 📖 | Stefan Cameron 💻 🐛 🚇 ⚠️ 📖 🚧 |
Tyler Hawkins 🔧 ⚠️ 🚇 📖 | pebble2050 🐛 |
5.2.1
FAQs
Returns an array of all tabbable DOM nodes within a containing node.
The npm package tabbable receives a total of 5,715,777 weekly downloads. As such, tabbable popularity was classified as popular.
We found that tabbable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.