@a11y/focus-trap

中文文档
1.0.5发布于 2019/11/10MITTypeScriptESM + CJS零依赖近 30 天下载 5.9万

A lightweight web component that traps focus within a DOM node

原始使用文档
文档目录4 节
  1. ➤ Installation
  2. ➤ Usage
  3. ➤ API
  4. ➤ License

@a11y/focus-trap

Downloads per month NPM Version Dependencies Contributors Published on webcomponents.org

A lightweight web component that traps focus within a DOM node
A focus trap ensures that tab and shift + tab keys will cycle through the focus trap's tabbable elements but not leave the focus trap. This is great for making accessible modals. Go here to see a demo https://appnest-demo.firebaseapp.com/focus-trap/.


  • Does one things very very well - it traps the focus!
  • Pierces through the shadow roots when looking for focusable elements.
  • Works right out of the box (just add it to your markup)
  • Created using only vanilla js - no dependencies and framework agnostic!

-----------------------------------------------------

➤ Installation

npm i @a11y/focus-trap

-----------------------------------------------------

➤ Usage

Import @a11y/focus-trap somewhere in your code and you're ready to go! Simply add the focus trap to your html and it'll be working without any more effort from your part.

<focus-trap>
  <button>Focus 1</button>
  <button>Focus 2</button>
  <button>Focus 3</button>
  <button>Focus 4</button>
  <button>Focus 5</button>
</focus-trap>

-----------------------------------------------------

➤ API

The focus-trap element implements the following interface.

interface IFocusTrap {
  // Returns whether or not the focus trap is inactive.
  inactive: boolean;

  // Returns whether the focus trap currently has focus.
  readonly focused: boolean;

  // Focuses the first focusable element in the focus trap.
  focusFirstElement: (() => void);

  // Focuses the last focusable element in the focus trap.
  focusLastElement: (() => void);

  // Returns a list of the focusable children found within the element.
  getFocusableElements: (() => HTMLElement[]);
}

-----------------------------------------------------

➤ License

Licensed under MIT.

版本与发布记录

@a11y/focus-trap 共发布 5 个版本,首次发布于 2019/03/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2019/11/106 年前
1.0.42019/10/217 年前
1.0.32019/10/117 年前
1.0.22019/03/287 年前
1.0.12019/03/237 年前

关于 @a11y/focus-trap 的常见问题

@a11y/focus-trap 是什么?

A lightweight web component that traps focus within a DOM node

如何安装 @a11y/focus-trap?

在项目目录运行 npm install @a11y/focus-trap,或使用 pnpm add / yarn add / bun add。

@a11y/focus-trap 支持 TypeScript 吗?

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

@a11y/focus-trap 支持 ESM 还是 CommonJS?

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

@a11y/focus-trap 可以商用吗?

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

@a11y/focus-trap 的下载量有多少?

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

@a11y/focus-trap 还在维护吗?

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