JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 73123
  • Score
    100M100P100Q155640F
  • License MIT

A component wrapper that provides click outside detection.

Package Exports

  • react-click-outside

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-click-outside) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React Click Outside Build Status npm

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.

Usage

Installation:

npm install react-click-outside

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

const enhanceWithClickOutside = require('react-click-outside');
const React = require('react');

const Dropdown = React.createClass({
  getInitialState() {
    return {
      isOpened: false,
    };
  },

  handleClickOutside() {
    this.setState({ isOpened: false });
  },

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

module.exports = enhanceWithClickOutside(Dropdown);

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.

License

MIT