# react-trap-focus

> A react component to trap focus for accessibility

Latest version **1.0.3** (published 2022-06-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-06-13 |
| First published | 2022-06-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Darshan Satya |
| Maintainers | dashsatya |
| Keywords | focus, trap, focusTrap, react, focus-trap |

## Links

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

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-06-13
- 1.0.2 — 2022-06-13
- 1.0.1 — 2022-06-13
- 1.0.0 — 2022-06-09

## README

# React Focus Trap ![npm](https://img.shields.io/npm/v/react-trap-focus?color=lightgreen&label=react-trap-focus&logo=react)

A react component to trap your focus for keyboard events.

It is as simple as wrapping your component with the FocusTrap and rest will be be taken care of.

-----

## Installation

Using npm:

```
$ npm i react-trap-focus
```

Using yarn:

```
$ yarn add react-trap-focus
```
----
## Usage

There are 2 ways in which you can use it.

### <b>1) Wrapping any component in it.</b>


>The default query selector is `[role="button"]` but you can pass whatever your application sees fit.

```javascript
import { FocusTrap } from 'react-trap-focus'

<FocusTrap>
    <Modal.Body>
        // Do Something
    </Modal.Body>
</FocusTrap>
```

```javascript
import { FocusTrap } from 'react-trap-focus'

<FocusTrap querySelector={'div.highlighted > p'}>
 // Do something / wrap any component
</FocusTrap>
```


### <b>2) Directly calling the method on keyboard events.</b>

The package also exposes `focusTrap = (event: KeyboardEvent, elements: HTMLElement[])` function which can be used directly on keyboard events.

>Pass the event and the elements list you want to be focus trapped.

```javascript
import { focusTrap } from 'react-trap-focus'

  const allButtonEls = document.querySelectorAll('[role="button"]')
  const onKeyDown = event => focusTrap(event, allButtonEls)
  return (
    <div onKeyDown={onKeyDown}>
      // Do something
    </div>
  )
```

---------

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