Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-outside-click-handler

airbnb2.2mMIT1.3.0TypeScript support: definitely-typed

A React component for dealing with clicks outside its subtree

readme

react-outside-click-handler

A React component for handling outside clicks

Usage

import OutsideClickHandler from 'react-outside-click-handler';

function MyComponent() {
  return (
    <OutsideClickHandler
      onOutsideClick={() => {
        alert('You clicked outside of this component!!!');
      }}
    >
      Hello World
    </OutsideClickHandler>
  );
}

Props

children: PropTypes.node.isRequired

Since the OutsideClickHandler specifically handles clicks outside a specific subtree, children is expected to be defined. A consumer should also not render the OutsideClickHandler in the case that children are not defined.

Note that if you use a Portal (native or react-portal) of any sort in the children, the OutsideClickHandler will not behave as expected.

onOutsideClick: PropTypes.func.isRequired

The onOutsideClick prop is also required as without it, the OutsideClickHandler is basically a heavy-weight <div />. It takes the relevant clickevent as an arg and gets triggered when the user clicks anywhere outside of the subtree generated by the DOM node.

disabled: PropTypes.bool

If the disabled prop is true, outside clicks will not be registered. This can be utilized to temporarily disable interaction without unmounting/remounting the entire tree.

useCapture: PropTypes.bool

See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture for more information on event bubbling vs. capture.

If useCapture is true, the event will be registered in the capturing phase and thus, propagated top-down instead of bottom-up as is the default.

display: PropTypes.oneOf(['block', 'flex', 'inline-block', 'inline', 'contents'])

By default, the OutsideClickHandler renders a display: block <div /> to wrap the subtree defined by children. If desired, the display can be set to inline-block, inline, flex, or contents instead. There is no way not to render a wrapping <div />.

changelog

Change Log

v1.3.0

  • [New] display: add contents (#34)
  • [New] display: add inline (#28)
  • [Refactor] Replace componentWillReceiveProps with componentDidUpdate for a side effect (#36)
  • [Deps] update airbnb-prop-types
  • [Dev Deps] update enzyme-adapter-react-helper, eslint, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-react, eslint-plugin-react-with-styles, rimraf, safe-publish-latest, sinon-sandbox; add eslint-plugin-react-hooks

v1.2.4

  • [Fix] prevent memory leak if mousedown is fired, but mouseup isn’t (#20)
  • [Deps] update airbnb-prop-types, document.contains
  • [Dev Deps] update airbnb-js-shims, babel-preset-airbnb, enzyme, enzyme-adapter-react-helper, eslint, eslint-plugin-import, eslint-plugin-react, eslint-config-airbnb, eslint-plugin-jsx-a11y, sinon-sandbox

v1.2.3

  • [Fix] use document.contains instead of implicitly requiring polyfills
  • [Deps] update object.values, prop-types, airbnb-prop-types
  • [Dev Deps] update airbnb-js-shims, chai, enzyme, enzyme-adapter-react-helper, eslint, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react, eslint-plugin-react-with-styles, react, react-dom, rimraf, safe-publish-latest

v1.2.2

  • Add .npmignore, fixes (#6)

v1.2.1

  • [Fix] use object.values instead of Object.values

v1.2.0

  • Allow consolidated-events ^2.0.0 (#5)

v1.1.0

  • Add flex as a display prop option (#4)

v1.0.0

  • Initial commit