Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/sebinsua/react-detect-event-outside

🎪🍃 `EventOutside` component for React.
https://github.com/sebinsua/react-detect-event-outside

blur click component event focus forms modals outside react

Last synced: about 3 hours ago
JSON representation

🎪🍃 `EventOutside` component for React.

Awesome Lists containing this project

README

        

# `react-detect-event-outside`
> `EventOutside` component for React.

A component which detects `click`, `focus` and `touch` events outside from it and calls a function to handle the event.

> **NOTE**: We capture events at the `document` level and wish to support some events which [do not bubble](https://en.wikipedia.org/wiki/DOM_events#Events), for example to detect when elements outside a component have gained `focus`. In these situations we: (a) use the [`useCapture` argument of `addEventListener`](https://stackoverflow.com/questions/7398290/unable-to-understand-usecapture-attribute-in-addeventlistener), or (b) find a similar event which does bubble ([sometimes difficult](https://bugzilla.mozilla.org/show_bug.cgi?id=687787)).

## Example

### Using the `EventOutside` component

```js
import React from 'react'
import EventOutside from 'react-detect-event-outside'

const onEvent = (evt, el) => console.log('event happened outside', evt, el)

const SomeComponent = () => (




Inside






Outside





)

export default SomeComponent
```

### Wrapping your own component with the `withEventOutside` HOC

> *NOTE*: This is useful if you wish to cause some state change within a component, since
> the `handleEventOutside` handler is defined within your component.

```js
import React, { Component } from 'react'
import { withEventOutside } from 'react-detect-event-outside'

class Handler extends Component {
handleEventOutside = (evt, el) =>
console.log('Handler: event happened outside', evt, el)

render() {
const { children } = this.props
return

{children}

}
}

const WrappedHandler = withEventOutside()(Handler)

const SomeComponent = () => (



Inside





Outside





)

export default SomeComponent
```

## Install

```sh
yarn add react-detect-event-outside
```