An open API service indexing awesome lists of open source software.

https://github.com/brandonpittman/use-dismiss

The useDismiss() hook for React takes a callback, returns a ref, and invokes the callback when a click event occurs outside the ref's bounds, when the escape key is pressed, or both.
https://github.com/brandonpittman/use-dismiss

callback clickaway dismiss escape escape-key hook react

Last synced: 2 months ago
JSON representation

The useDismiss() hook for React takes a callback, returns a ref, and invokes the callback when a click event occurs outside the ref's bounds, when the escape key is pressed, or both.

Awesome Lists containing this project

README

          

# useDismiss ๐Ÿงน

The `useDismiss()` hook for React takes a callback, returns a ref, and invokes the callback when a click event occurs outside the ref's bounds, when the escape key is pressed, **or both**.

## Setup โŒจ

```sh
npm i use-dismiss #or yarn add use-dismiss
```

## Usage โš›

```javascript
import { useState } from "react";
import useDismiss from "use-dismiss";
// or import { useClickAway, useEscape } from "use-dismiss" for just click away or escape listeners

export default () => {
const [modal, setModal] = useState(false);
const ref = useDismiss(() => setModal(false));

return (


setModal(true)}>Show Modal
{modal ?
Modal Content
: null}

);
};
```

## API ๐Ÿ“„

- `callback: (event) => any` - The callback that runs after user click