@accessible/tabbable

中文文档
1.1.2发布于 2020/07/07MITTypeScriptESM + CJS零依赖近 30 天下载 5946

A function that returns an array of all tabbable DOM nodes within a containing node.

原始使用文档
文档目录6 节
  1. API
  2. tabbable(rootNode: HTMLElement, includeRootNode: boolean = …
  3. More details
  4. Why fork?
  5. Credit
  6. LICENSE

tabbable()

Bundlephobia Build status NPM Version MIT License

npm i @accessible/tabbable

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>
  • <input>
  • <select>
  • <textarea>
  • <a> with href or xlink:href attributes
  • <audio> and <videos> with controls attributes
  • [contenteditable] elements
  • anything with a non-negative tabindex

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

  • negative tabindex
  • disabled
  • either the node itself or an ancestor of it is hidden via display: none or visibility: hidden
  • it's an <input type="radio"> and a different radio in its group is checked

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.

API

tabbable(rootNode: HTMLElement, includeRootNode: boolean = false): HTMLElement[]

Returns an array of ordered tabbable node within the rootNode.

Summary of ordering principles:

  • First include any nodes with positive tabindex attributes (1 or higher), ordered by ascending tabindex and source order.
  • Then include any nodes with a zero tabindex and any element that by default receives focus (listed above) and does not have a positive tabindex set, in source order.

More details

  • Tabbable tries to identify elements that are reliably tabbable across (not dead) browsers. Browsers are stupidly 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 that stupid 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.
  • (Exemplifying the above ^^:) The tabbability of <iframe>, <embed>, <object>, <summary>, and <svg> 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.
  • Radio groups have some edge cases, which you can avoid by always having a 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.)
  • 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.

Why fork?

The creator is looking for a new maintainer and @accessibile packages don't need some of the features in the original, so I can make a smaller package with TypeScript types by forking :P

Credit

This library is forked from tabbable

LICENSE

MIT

版本与发布记录

@accessible/tabbable 共发布 8 个版本,首次发布于 2019/12/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2020/07/076 年前
1.1.12020/05/316 年前
1.1.02020/05/316 年前
1.0.42019/12/166 年前
1.0.32019/12/166 年前
1.0.22019/12/166 年前

关于 @accessible/tabbable 的常见问题

@accessible/tabbable 是什么?

A function that returns an array of all tabbable DOM nodes within a containing node.

如何安装 @accessible/tabbable?

在项目目录运行 npm install @accessible/tabbable,或使用 pnpm add / yarn add / bun add。

@accessible/tabbable 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@accessible/tabbable 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@accessible/tabbable 可以商用吗?

@accessible/tabbable 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@accessible/tabbable 的下载量有多少?

根据 npm 官方下载统计,@accessible/tabbable 在 2026/10/05 – 2026/11/03 这 30 天内被下载 5,946 次,日均约 198 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@accessible/tabbable 还在维护吗?

最新版本 1.1.2 发布于 2020/07/07(6 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/12/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。