# focus-trap-js

> Trap focus inside specified HTML element. Vanilla JS with size

Latest version **1.1.0** (published 2020-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install focus-trap-js
pnpm add focus-trap-js
yarn add focus-trap-js
bun add focus-trap-js
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-09-15 |
| First published | 2019-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Alexandr Zavalii |
| Maintainers | alexandrzavalii |
| Keywords | trap, vanilla, javascript |

## Links

- npm: https://www.npmjs.com/package/focus-trap-js
- Repository: https://github.com/alexandrzavalii/focus-trap-js
- Homepage: https://github.com/alexandrzavalii/focus-trap-js#readme
- Issues: https://github.com/alexandrzavalii/focus-trap-js/issues
- npm.io page: https://npm.io/package/focus-trap-js

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@atlaskit/theme](https://npm.io/package/@atlaskit/theme.md) — 402.0K weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-09-15
- 1.0.9 — 2020-03-07
- 1.0.8 — 2019-11-28
- 1.0.7 — 2019-09-09
- 1.0.6 — 2019-09-02
- 1.0.5 — 2019-08-22
- 1.0.4 — 2019-08-20
- 1.0.3 — 2019-08-17
- 1.0.2 — 2019-08-17
- 1.0.1 — 2019-08-17
- 1.0.0 — 2019-08-17

## README

# focus-trap-js

Trap focus inside specified HTML element. Vanilla JS with size <0.5kb. No dependencies.

Browser Support: IE 10, Firefox, Chrome.

## Note

No event listeners, it is just handling the current event, and traps the focus if it is neccessary.

## Instalation

```js
npm i focus-trap-js
```

## Usage

```js
import focusTrap from "focus-trap-js";

const popupContainer = document.getElementById("popupId");

document.addEventListener("keydown", event => {
  focusTrap(event, popupContainer);
});
```

The method `focusTrap` accepts two parameters, the `event` and `HTML element` container in which you want to trap your focus.

## Usage in React

```js
import React from "react";
import focusTrap from "focus-trap-js";

const Container = () => {
  const contRef = React.useRef();

  React.useEffect(() => {
    const handleKeyEvent = event => {
      focusTrap(event, contRef.current);
    };
    document.addEventListener("keydown", handleKeyEvent);
    return () => {
      document.removeEventListener("keydown", handleKeyEvent);
    };
  }, [contRef]);

  return <div ref={contRef}></div>;
};
```

## List of Tabbable Elements

```js
const tabbableQuery = [
  "input",
  "select",
  "textarea",
  "a[href]",
  "button",
  "[tabindex]", //tabIndex > 0
  "audio[controls]",
  "video[controls]",
  '[contenteditable]:not([contenteditable="false"])'
];
```

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