@daniel.husar/focus-trap

中文文档
1.0.3发布于 2018/11/09MITCommonJS零依赖近 30 天下载 3万

Add focus traps to the elements

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Constructor API
  4. Interface
  5. License

focus-trap

Add focus traps around elements.

Adding a focus traps is important part of making your site accessible. The most common example where you want to set up a focus trap is a modal. While modal is open, the user shouldn't be able to tab outside of it, and focus should be locked within the modal elements. You have to remember to make sure there is always a way how to exit this focus trap. In modal example it's usually the escape key, or close button in the corner.

Demo

Install

Using npm

npm install @daniel.husar/focus-trap

Using yarn

yarn add @daniel.husar/focus-trap

Usage

import FocusTrap from '@daniel.husar/focus-trap';
// Setup trap
const trap = new FocusTrap({ node: document.querySelector('#my-trap') });
// Release trap
trap.restore();

Constructor API

Constructor accept object with those properties:

new FocusTrap({
  node: document.querySelector('#my-trap') }),
  firstElement: () => document.querySelector('#first-element'),
  lastElement: () => document.querySelector('#last-element'),
  disableStartingTrap: () => false,
  disableEndingTrap: () => false,
});

node

Type: DOM element

Default: none

Required: true

This is the DOM node you want to set-up focus trap around

firstElement

Type: Function

Default: () => null

Required: none

Function that returns element that will replace first element when focus will jump back at the start. Usefull when you want to connect 2 focus traps together. Will fallback to firstElement in focus trap if function returns falsy value.

lastElement

Type: Function

Default: () => null

Required: none

Function that returns element that will replace last element when focus will jump back at the end. Usefull when you want to connect 2 focus traps together. Will fallback to lastElement in focus trap if function returns falsy value.

disableStartingTrap

Type: Function

Default: () => false

Required: none

Function that returns boolean if the starting trap should be disabled.

disableEndingTrap

Type: Function

Default: () => false

Required: none

Function that can returns boolean if the ending trap should be disabled.

Interface

recalculateFocusableElements

Type: Function

Recalculate focusable elements inside of your trap. Usefull if content of the node changes over time.

restore

Type: Function

Remove the focus trap from element.

License

MIT © Daniel Husar

版本与发布记录

@daniel.husar/focus-trap 共发布 4 个版本,首次发布于 2018/11/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2018/11/098 年前
1.0.22018/11/098 年前
1.0.12018/11/098 年前
1.0.02018/11/098 年前

关于 @daniel.husar/focus-trap 的常见问题

@daniel.husar/focus-trap 是什么?

Add focus traps to the elements

如何安装 @daniel.husar/focus-trap?

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

@daniel.husar/focus-trap 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/daniel.husar__focus-trap 是否存在。

@daniel.husar/focus-trap 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@daniel.husar/focus-trap 可以商用吗?

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

@daniel.husar/focus-trap 的下载量有多少?

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

@daniel.husar/focus-trap 还在维护吗?

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