# react-onclickout

> An ES6-friendly on-click-outside React component.

Latest version **2.0.8** (published 2017-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-onclickout
pnpm add react-onclickout
yarn add react-onclickout
bun add react-onclickout
```

## 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 | 2.0.8 |
| Published | 2017-10-10 |
| First published | 2015-08-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 91 |
| Author | Bob Lauer |
| Maintainers | boblauer |
| Keywords | react, onclick, onclickout, onclickoutside |

## Links

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

## 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

- 2.0.8 (latest) — 2017-10-10
- 2.0.7 — 2017-06-05
- 2.0.6 — 2017-03-16
- 2.0.5 — 2017-01-03
- 2.0.4 — 2015-11-23
- 2.0.3 — 2015-11-23
- 2.0.2 — 2015-11-04
- 2.0.1 — 2015-11-03
- 2.0.0 — 2015-11-02
- 1.2.1 — 2015-10-03
- 1.2.0 — 2015-09-25
- 1.1.0 — 2015-08-07
- 1.0.1 — 2015-08-04
- 1.0.0 — 2015-08-04

## README

# An ES6-friendly on-click-outside React component.

This is a React component that can be used to listen for clicks outside of a given component.  As an example, you may need to hide a menu when a user clicks elsewhere on the page.

This component was created specifically to support ES6-style React components.  If you want to use a mixin instead, I would recommend the [react-onclickoutside](https://github.com/Pomax/react-onclickoutside) mixin.

## Installation

```
npm install react-onclickout --save
```

## React Version Support

For React `0.14` or later, use version `2.x` of this package.  For React `0.13` or earlier, use version `1.x` of this package.

## Usage

There are two ways to use this component.

### As a wrapper component

```jsx
const ClickOutHandler = require('react-onclickout');

class ExampleComponent extends React.Component {

  onClickOut(e) {
    alert('user clicked outside of the component!');
  }

  render() {
    return (
      <ClickOutHandler onClickOut={this.onClickOut}>
        <div>Click outside of me!</div>
      </ClickOutHandler>
    );
  }
}
```

### As a base component

```jsx
const ClickOutComponent = require('react-onclickout');

class ExampleComponent extends ClickOutComponent {

  onClickOut(e) {
    alert('user clicked outside of the component!');
  }

  render() {
    return (
      <div>Click outside of me!</div>
    );
  }
}
```

## Ignoring Elements

There are times when you may want to ignore certain elements that were clicked outside of the target component.  You can handle such a scenario by inspecting the event passed to your `onClickOut` method handler.

```jsx
const ClickOutHandler = require('react-onclickout');

class ExampleComponent extends React.Component {

  onClickOut(e) {
    if (hasClass(e.target, 'ignore-me')) return;
    alert('user clicked outside of the component!');
  }

  render() {
    return (
      <ClickOutHandler onClickOut={this.onClickOut}>
        <div>Click outside of me!</div>
      </ClickOutHandler>
    );
  }
}
```

That's pretty much it.  Pull requests are more than welcome!

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