Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/taneba/react-click-outsider

ClickOutside component with hooks
https://github.com/taneba/react-click-outsider

Last synced: 2 months ago
JSON representation

ClickOutside component with hooks

Awesome Lists containing this project

README

        

# react-click-outsider
inspired by [tj/react-click-outside](https://github.com/tj/react-click-outside) but supported by TypeScript and React Hooks!

## install

```
yarn add 'react-click-outsider'
```

NOTE: Make sure your application is using react >= 16.8

## API

### `ClickOutside`
Wrapp your component that you want to enhance with click outside detection:

```js
import ClickOutside from 'react-click-outsider'
import React, { setState } from 'react'

function App() {
const [isOpen, setOpen] = useState(true)

return (
setOpen(false)}>

Something to close when clicking outside eg. modal, tooltip, etc.


)
}
```

### `useClickOutside(ref: React.RefObject, function: (e: MouseEvent | TouchEvent) => void`

A custom hook dealing with clicks outside of arbitrary dom. `ClickOutside` uses this custom hook internally.

```js
import {useClickOutside} from 'react-click-outsider'
import React, { setState, useRef } from 'react'

function App() {
const container = useRef(null)
const [isOpen, setOpen] = useState(true)

useClickOutside(container, () => setOpen(false))

return (

Something to close when clicking outside eg. modal, tooltip, etc.

)
}
```

## License
[MIT License](https://github.com/taneba/react-click-outsider/blob/master/LICENCE)