3.0.1 • Published 6 years ago

react-click-outside v3.0.1

Weekly downloads
111,668
License
MIT
Repository
github
Last release
6 years ago

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.

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:

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:

// 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:

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 for a discussion.

License

MIT

prosapient-uigamiphy-reactgamiphy-react-appreact-trello-sf@rrott/draft-js-markdown-plugin@hi-ui/hiui2creative-gallery-shonp@gamiphy/dashboard-corereact-trello-webthesoulkenshoo-sharedshaper-reactcria-design-systemshaman-online-meetingelement-react-bootstrapreact-trello-not-safereact-trello-unsafe@enjoytech/enjoy-integrationelement-react-rakkmulti-web-element-react@everything-registry/sub-chunk-2545drift-chrome-extensiondroplr-react-uiegeo-reactelement-raku-reactelement-reactelement-react-cocacolaelement-react-codishelement-react-kkelement-react-mikeeditelement-react-rakuelement-react-rakurakuelement-react-tselement-react-ui-componentselement-reactllelement-react-akelevate-uieha-cellsdoc-reactdz-cubefk-react-ui-components@tikoam/react-trelloformer-kit-mjtomko-chartskenshinko-rtwp-react-inline-searchjackhui-uiwuhaui@gamiphy/react-app@geistinteractive/react-trellografeno-uigitdocsgw-uigw-react-uijuststylekangcthwt-react-componentsishow-uiitalent-feed-14italent-portalitalent-priority-popitalent-tabswebhesab-element-reactmerle-demomaby-markdown-editor@impervaos/react-date-picker@lightspeed/cirrus-table@lightspeed/cirrus-datepicker@lightspeed/cirrus-popover@lyra/components@lyra/default-layout@lyra/desk-tool@lyra/form-buildernext-element-react@liquid-design/liquid-design-reactmy-magic-tablemy-table-component@intellyo/intellyo-componentsyii-steroids@gssfed/vital-ui-kit-react@gtoio/react-color-picker@ixinwu-ngp/web-framework@ixinwu-ngp/web-systemmessi-webappmengli-react-filemanagermetadata-js@kurtb/react-trellolihe-test-eelihe-test-ilihe-test-jlihe-test-klihe-test-mlihe-test-olihe-test-plihe-test-cclihe-test-dd@hoangtc/element-react@holism/core@holism/datepicker@holism/components
3.0.1

6 years ago

3.0.0

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago