# @curiousmedia/keyboard-trap

> Create a keyboard trap to enhance accessibility

Latest version **1.1.2** (published 2025-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @curiousmedia/keyboard-trap
pnpm add @curiousmedia/keyboard-trap
yarn add @curiousmedia/keyboard-trap
bun add @curiousmedia/keyboard-trap
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2025-08-05 |
| First published | 2023-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | cm-gregory, kyfoote, curiousjason, eaglstun, ishoa |

## Links

- npm: https://www.npmjs.com/package/@curiousmedia/keyboard-trap
- npm.io page: https://npm.io/package/@curiousmedia/keyboard-trap

## Recent versions

- 1.1.2 (latest) — 2025-08-05
- 1.1.1 — 2023-05-24
- 1.1.0 — 2023-05-24
- 1.0.0 — 2023-05-23

## README

# Keyboard Trap
Create a keyboard trap to enhance accessibility on UI components such as modals and toggled navigation.

## Example
### HTML
```html
<div class="modal">
  <button class="close">Close</button>
  
  <form>
    <label>Name<input type="text" /></label>
    <button>Submit</button>
  </form>
</div>
<button class="toggle">Open modal</button>
```

### Javascript
```javascript
const trap = new KeyboardTrap(document.querySelector('.modal'));

document.querySelector('.toggle').addEventListener('click', (e) => {
  trap.setFocusVisibleByPointerType(e); // Determines visibility of focus based on event pointer type
  trap.enable = !trap.enable;
});

document.querySelector('.close').addEventListener('click', () => {
  trap.setFocusVisibleByPointerType(e); // Determines visibility of focus based on event pointer type
  trap.enable = false;
});
```

---
_Source: https://npm.io/package/@curiousmedia/keyboard-trap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
