# react-click-outside

> A component wrapper that provides click outside detection.

Latest version **3.0.1** (published 2018-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-click-outside
pnpm add react-click-outside
yarn add react-click-outside
bun add react-click-outside
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2018-02-15 |
| First published | 2015-10-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-click-outside) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 200.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 272 |
| Author | Kenneth Chung |
| Maintainers | kentor |
| Keywords | click outside, higher order component, onclickoutside, react |

## Links

- npm: https://www.npmjs.com/package/react-click-outside
- Repository: https://github.com/kentor/react-click-outside
- Issues: https://github.com/kentor/react-click-outside/issues
- npm.io page: https://npm.io/package/react-click-outside

## Dependencies (1)

- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^2.1.1

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

- 3.0.1 (latest) — 2018-02-15
- 3.0.0 — 2017-09-08
- 2.3.1 — 2017-05-09
- 2.3.0 — 2017-04-12
- 2.2.0 — 2016-10-08
- 2.1.0 — 2015-11-29
- 2.0.1 — 2015-11-26
- 2.0.0 — 2015-10-25
- 1.0.1 — 2015-10-15
- 1.0.0 — 2015-10-05

## README

# React Click Outside

[![Build Status](https://travis-ci.org/kentor/react-click-outside.svg)](https://travis-ci.org/kentor/react-click-outside) [![npm](https://img.shields.io/npm/v/react-click-outside.svg)](https://www.npmjs.com/package/react-click-outside)

Enhance a React component with a Higher Order Component that provides click
outside detection.

**Note:** React 0.14 required for version >= 2.x. This assumes `react` and
`react-dom` is installed in your project. Continue using version 1.x for React
0.13 support.

**Note:** Use version >= 2.3.0 to get rid of `React.createClass`
warnings in React 15.5.

## Usage

Installation:

```
npm install react-click-outside
```

Some component that you wish to enhance with click outside detection:

```js
const createReactClass = require('create-react-class');
const enhanceWithClickOutside = require('react-click-outside');
const React = require('react');

const Dropdown = createReactClass({
  getInitialState() {
    return {
      isOpened: false,
    };
  },

  handleClickOutside() {
    this.toggle();
  },

  toggle() {
    this.setState({ isOpened: !this.state.isOpened });
  },

  render() {
    ...
  },
});

module.exports = enhanceWithClickOutside(Dropdown);
```

**Note:** There will be no error thrown if `handleClickOutside` is not
implemented.

### `wrappedRef` prop

Use the `wrappedRef` prop to get access to the wrapped component instance. For
example:

```js
// Inside a component's render method
<Dropdown
  wrappedRef={instance => { this.toggle = instance.toggle; }}
/>

// Now you can call toggle externally
this.toggle();
```

## Details

The `enhanceWithClickOutside` function wraps the provided component in another
component that registers a click handler on `document` for the event capturing
phase. Using the event capturing phase prevents elements with a click handler
that calls `stopPropagation` from cancelling the click event that would
eventually trigger the component's `handleClickOutside` function.

## Why not a mixin?

There are some mixins that provide click outside detection functionality, but
they prevent the component from implementing the  `componentDidMount` and
`componentWillUnmount` life cycle hooks. I recommend not using a mixin for this
case.

## Limitations

- IE9+ due to the usage of the event capturing phase.

## Not working on iOS?

If the `handleClickOutside` handler is not firing on iOS, try adding the
`cursor: pointer` css style to the `<body>` element. There are many ways to
achieve this, here is just one example:

```js
if ('ontouchstart' in document.documentElement) {
  document.body.style.cursor = 'pointer';
}
```

If your app already has a way for mobile detection (e.g. Modernizr), you may
want to use that instead.


See issue [#4][i] for a discussion.

## License

[MIT](LICENSE.txt)

[i]: https://github.com/kentor/react-click-outside/issues/4

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