@olasearch/react-onclickoutside
Version:
An onClickOutside wrapper for React components
2 lines (1 loc) • 4.38 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","react-dom"],t):t(e.onClickOutside={},e.React,e.ReactDOM)}(this,function(e,s,d){"use strict";var u=function(){if("undefined"!=typeof window&&"function"==typeof window.addEventListener){var e=!1,t=Object.defineProperty({},"passive",{get:function(){e=!0}}),n=function(){};return window.addEventListener("testPassiveEventSupport",n,t),window.removeEventListener("testPassiveEventSupport",n,t),e}};var t,p,a=(void 0===t&&(t=0),function(){return++t}),l={},f={},o=["touchstart","touchmove"],n="ignore-react-onclickoutside";function v(e,t){var n=null;return-1!==o.indexOf(t)&&p&&(n={passive:!e.props.preventDefault}),n}var i="undefined"!=typeof document?document:void 0;e.IGNORE_CLASS_NAME=n,e.default=function(r,c){var e,t;return t=e=function(t){var e,n;function o(e){var o;return(o=t.call(this,e)||this).__outsideClickHandler=function(e){if("function"!=typeof o.__clickOutsideHandlerProp){var t=o.getInstance();if("function"!=typeof t.props.handleClickOutside){if("function"!=typeof t.handleClickOutside)throw Error("WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.");t.handleClickOutside(e)}else t.props.handleClickOutside(e)}else o.__clickOutsideHandlerProp(e)},o.enableOnClickOutside=function(){if(void 0!==o.props.document&&!f[o._uid]){void 0===p&&(p=u()),f[o._uid]=!0;var e=o.props.eventTypes;e.forEach||(e=[e]),l[o._uid]=function(e){var t,n;o.props.disableOnClickOutside||null!==o.componentNode&&(o.props.preventDefault&&e.preventDefault(),o.props.stopPropagation&&e.stopPropagation(),(!o.props.excludeScrollbar||(t=e).clientX<(n=o.props.document).documentElement.clientWidth&&t.clientY<n.documentElement.clientHeight)&&function(e,t,n){if(e===t)return!0;for(;e.parentNode;){if(i=n,(o=e)===t||(o.correspondingElement?o.correspondingElement.classList.contains(i):o.classList.contains(i)))return!0;e=e.parentNode}var o,i;return e}(e.target,o.componentNode,o.props.outsideClickIgnoreClass)===o.props.document&&o.__outsideClickHandler(e))},e.forEach(function(e){o.props.document.addEventListener(e,l[o._uid],v(o,e))})}},o.disableOnClickOutside=function(){delete f[o._uid];var t=l[o._uid];if(t&&void 0!==o.props.document){var e=o.props.eventTypes;e.forEach||(e=[e]),e.forEach(function(e){return o.props.document.removeEventListener(e,t,v(o,e))}),delete l[o._uid]}},o.getRef=function(e){return o.instanceRef=e},o._uid=a(),o}(e=o).prototype=Object.create((n=t).prototype),(e.prototype.constructor=e).__proto__=n;var i=o.prototype;return i.getInstance=function(){if(!r.prototype.isReactComponent)return this;var e=this.instanceRef;return e.getInstance?e.getInstance():e},i.componentDidMount=function(){if(void 0!==this.props.document&&this.props.document.createElement){var e=this.getInstance();if(c&&"function"==typeof c.handleClickOutside&&(this.__clickOutsideHandlerProp=c.handleClickOutside(e),"function"!=typeof this.__clickOutsideHandlerProp))throw Error("WrappedComponent lacks a function for processing outside click events specified by the handleClickOutside config option.");this.componentNode=d.findDOMNode(this.getInstance()),this.enableOnClickOutside()}},i.componentDidUpdate=function(){this.componentNode=d.findDOMNode(this.getInstance())},i.componentWillUnmount=function(){this.disableOnClickOutside()},i.render=function(){var e=this.props,t=function(e,t){if(null==e)return{};var n,o,i={},r=Object.keys(e);for(o=0;o<r.length;o++)t.indexOf(n=r[o])<0&&(i[n]=e[n]);if(Object.getOwnPropertySymbols){var c=Object.getOwnPropertySymbols(e);for(o=0;o<c.length;o++)t.indexOf(n=c[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}(e,["excludeScrollbar"]);return r.prototype.isReactComponent?t.ref=this.getRef:t.wrappedRef=this.getRef,t.disableOnClickOutside=this.disableOnClickOutside,t.enableOnClickOutside=this.enableOnClickOutside,s.createElement(r,t)},o}(s.Component),e.displayName="OnClickOutside("+(r.displayName||r.name||"Component")+")",e.defaultProps={eventTypes:["mousedown","touchstart"],excludeScrollbar:c&&c.excludeScrollbar||!1,outsideClickIgnoreClass:n,preventDefault:!1,stopPropagation:!1,document:i},e.getClass=function(){return r.getClass?r.getClass():r},t},Object.defineProperty(e,"__esModule",{value:!0})});