1 | # React Clickout
|
2 |
|
3 | [![Build Status](https://travis-ci.org/danielmoi/react-clickout.svg?branch=master)](https://travis-ci.org/danielmoi/react-clickout)
|
4 | [![npm version](https://badge.fury.io/js/react-clickout.svg)](https://badge.fury.io/js/react-clickout)
|
5 |
|
6 | Higher Order Component providing clickout functionality for React components.
|
7 |
|
8 | ## Installation
|
9 | With Yarn:
|
10 | ```
|
11 | yarn add react-clickout
|
12 | ```
|
13 |
|
14 | With NPM:
|
15 | ```
|
16 | npm install react-clickout
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 | `react-clickout` returns a Higher Order Component that wraps a provided component with the ability to detect a `click` event outside of that component.
|
21 |
|
22 | Such a "`clickout`" event will call the wrapped component's `handleClickout` method. (Note the character casing.)
|
23 |
|
24 | See the test suite for more detailed example usage.
|
25 |
|
26 | ```js
|
27 | import React, { Component } from 'react';
|
28 | import wrapWithClickout from 'react-clickout';
|
29 |
|
30 | class ToWrap extends Component {
|
31 | constructor() {
|
32 | ...
|
33 | this.state = {
|
34 | isVisible: true,
|
35 | };
|
36 | }
|
37 | handleClickout() {
|
38 | this.setState({
|
39 | isVisible: false,
|
40 | });
|
41 | }
|
42 |
|
43 | toggleVisible() {
|
44 | this.setState({
|
45 | isVisible: !this.state.isVisible,
|
46 | });
|
47 | }
|
48 |
|
49 | render() {
|
50 | return (
|
51 | <div className="to-wrap__container">
|
52 |
|
53 | {this.state.isVisible
|
54 | ?
|
55 | <div className="box" />
|
56 | :
|
57 | null
|
58 | }
|
59 |
|
60 | <button onClick={this.toggleVisible} >
|
61 | Toggle Box
|
62 | </button>
|
63 |
|
64 | </div>
|
65 | );
|
66 | }
|
67 | }
|
68 |
|
69 | export default wrapWithClickout(ToWrap);
|
70 | ```
|
71 |
|
72 | ## Details
|
73 | - uses higher order functions (does not use mixins)
|
74 | - uses callback refs (does not use `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)))
|
75 |
|
76 |
|
77 |
|
78 | ## Tests
|
79 | With Yarn:
|
80 | ```
|
81 | yarn run test
|
82 | ```
|
83 |
|
84 | With NPM:
|
85 | ```
|
86 | npm run test
|
87 | ```
|
88 |
|
89 |
|
90 | ## Credits
|
91 | Initially a fork from [react-click-outside](https://github.com/kentor/react-click-outside).
|
92 | 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).
|
93 |
|
94 | ## Licence
|
95 |
|
96 | [Apache-2.0](LICENSE.txt) |
\ | No newline at end of file |