# react-clickout

> Higher Order Component providing clickout functionality for React components.

Latest version **3.0.8** (published 2018-12-28) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.8 |
| Published | 2018-12-28 |
| First published | 2017-01-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 146.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Daniel Moi |
| Maintainers | danielmoi |

## Links

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

## Recent versions

- 3.0.8 (latest) — 2018-12-28
- 3.0.7 — 2018-12-20
- 3.0.6 — 2018-12-18
- 3.0.5 — 2018-12-17
- 3.0.4 — 2018-12-16
- 3.0.3 — 2018-12-16
- 3.0.2 — 2018-12-15
- 3.0.0 — 2018-12-15
- 2.4.3 — 2018-12-15
- 2.4.2 — 2018-12-15
- 2.4.1 — 2018-12-15
- 2.4.0 — 2018-12-15
- 2.3.0 — 2017-10-30
- 2.2.0 — 2017-07-04
- 2.1.2 — 2017-01-24
- … 16 more at https://npm.io/package/react-clickout/versions

## README

# React Clickout

Higher Order Component providing clickout functionality for React components.

[![Build Status](https://travis-ci.org/danielmoi/react-clickout.svg?branch=master)](https://travis-ci.org/danielmoi/react-clickout)
[![npm version](https://badge.fury.io/js/react-clickout.svg)](https://badge.fury.io/js/react-clickout)


## Installation
With Yarn:
```
yarn add react-clickout
```

With NPM:
```
npm install react-clickout
```

## Usage
`react-clickout` returns a Higher Order Component that wraps a provided component with the ability to detect a `click` event outside of that component.

Such a "`clickout`" event will call the wrapped component's `handleClickout` method. (Note the character casing.)

See the test suite for more detailed example usage.

```js
import React, { Component } from 'react';
import wrapWithClickout from 'react-clickout';

class ToWrap extends Component {
  constructor() {
    ...
    this.state = {
      isVisible: true,
    };
  }
  handleClickout() {
    this.setState({
      isVisible: false,
    });
  }

  toggleVisible() {
    this.setState({
      isVisible: !this.state.isVisible,
    });
  }

  render() {
    return (
      <div className="to-wrap__container">

        {this.state.isVisible
          ?
            <div className="box" />
          :
            null
        }

        <button onClick={this.toggleVisible} >
          Toggle Box
        </button>

      </div>
    );
  }
}

export default wrapWithClickout(ToWrap);
```

### Excluding elements from calling `handleClickout`
- it is possible to prevent the `handleClickout` method from being called
- the elements (outside the component wrapped with `react-clickout`) to be excluded should be marked with the `data-react-clickout` attribute with the value `exclude`
```js
<div data-react-clickout="exclude" />
```


## Details
- React Clickout uses higher order functions (instead of using mixins)
- React Clickout also uses callback refs (instead of `ReactDOM.findDOMNode)` (which will eventually be deprecated (see [here](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-find-dom-node.md) and [here](https://github.com/yannickcr/eslint-plugin-react/issues/678#issue-165177220)))



## Tests
With Yarn:
```
yarn run test
```

With NPM:
```
npm run test
```


## Credits
Initially a fork from [react-click-outside](https://github.com/kentor/react-click-outside).

Thanks to [Dan Abramov](https://github.com/gaearon) for the solution of [using callback refs](https://github.com/yannickcr/eslint-plugin-react/issues/678#issuecomment-232293175).

## Licence

[Apache-2.0](LICENSE.txt)

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