Giter Site home page Giter Site logo

react-onclickoutside's Introduction

An onClickOutside mixin for React components

This is a React mixin that you can add to your React components if you want to have them listen for clicks that occur somewhere in the document, outside of the element itself (for instance, if you need to hide a menu when people click anywhere else on your page).

Note that this mixin relies on the .classList property, which is supported by all modern browsers, but not by no longer supported browsers like IE9 or even older. For setups that need to support deprecated browsers, using something like the MDN classlist-polyfill will be necessary.

installation

There are two ways to install this mixin, depending on your development process.

NPM

If you have Node.js needs, you can install this mixin via npm, using:

npm install react-onclickoutside --save

(or --save-dev depending on your needs). You then use it in your components as:

var Component = React.createClass({
  mixins: [
    require('react-onclickoutside')
  ],

  handleClickOutside: function(evt) {
    // ...handling code goes here...
  }
});

For the browser (not recommended)

If you have plain-old-browser needs and for some reason are unable to use the modern browserify/webpack approach to building your JS payloads, you can install this mixin via bower, using:

bower install react-onclickoutside

and then include it as script via:

<script src="bower_components/react-onclickoutside/index.js"></script>

Then use it as:

var Component = React.createClass({
  mixins: [
    OnClickOutside
  ],

  handleClickOutside: function(evt) {
    // ...handling code goes here...
  }
});

Regulate whether or not to listen for outside clicks

When using this mixin, a component has two functions that can be used to explicitly listen for, or do nothing with, outside clicks

  • enableOnClickOutside() - Enables outside click listening by setting up the event listening bindings.
  • disableOnClickOutside() - Disables outside click listening by explicitly removing the event listening bindings.

In addition, you can create a component that uses this mixin such that it has the code set up and ready to go, but not listening for outside click events until you explicitly issue its enableOnClickOutside(), by passing in a properly called disableOnClickOutside:

var Component = React.createClass({
  mixins: [ ... ],
  handleClickOutside: function(evt) {
    // ...
  }
});

var Container = React.createClass({
  render: function(evt) {
    return <Component disableOnClickOutside={true} />
  }
});

Marking elements as "skip over this one" during the event loop

If you want the mixin to ignore certain elements, then add the class ignore-react-onclickoutside to that element and the callback won't be invoked when the click happens inside elements with that class.

ES6/2015 class support via HOC / ES7 decorators

Since mixins can't be used with ES6/2015 class React components a Higher-Order Component (HOC) and ES7 decorator are bundled with the mixin:

import listensToClickOutside from 'react-onclickoutside/decorator';

class Component extends React.Component {
  handleClickOutside = (event) => {
    // ...
  }
}

export default listensToClickOutside(Component);

// OR

import listensToClickOutside from 'react-onclickoutside/decorator';

@listensToClickOutside()
class Component extends React.Component {
  handleClickOutside = (event) => {
    // ...
  }
}

export default Component;

One difference when using the HOC/decorator compared to the mixin is that the enableOnClickOutside() and disableOnClickOutside() methods are not available as class methods, but rather on the props; so instead of this.enableOnClickOutside() you would call this.props.enableOnClickOutside().

In every other respect the the mixin and HOC/decorator provides the same functionality.

For bugs and enhancements hit up https://github.com/Pomax/react-onclickoutside/issues

Version compatibility

If you still use React 0.13 or 0.12, any version up to and including 2.4 will work. Any version v4.* or above will not work due to relying on modules not introduced until React 0.14.

If you use React 0.14 or above, use v2.5 or higher, as that specifically uses react-DOM for the necessary DOM event bindings.

react-onclickoutside's People

Contributors

pomax avatar chentsulin avatar stevewillard avatar arunthampi avatar davidtheclark avatar hahahana avatar jslauthor avatar jergason avatar jeffkole avatar marnusw avatar martijnrusschen avatar sverrejoh avatar vinnymac avatar

Watchers

Joel Moss avatar Max Kraev avatar Friedel Ziegelmayer avatar James Cloos avatar Freddy May avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.