diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 945ccdd..0000000 --- a/.babelrc +++ /dev/null @@ -1,19 +0,0 @@ -{ - "env": { - "commonjs": { - "presets": [ ["env", { "loose": true }], "stage-0" ] - }, - "es": { - "presets": [ - [ - "env", - { - "loose": true, - "modules": false - } - ], - "stage-0" - ] - } - } -} diff --git a/.babelrc.js b/.babelrc.js new file mode 100644 index 0000000..384c6c5 --- /dev/null +++ b/.babelrc.js @@ -0,0 +1,11 @@ +const loose = true; + +module.exports = { + presets: [ + ['@babel/env', { loose, modules: false }], + ], + plugins: [ + ['@babel/proposal-class-properties', { loose }], + '@babel/proposal-object-rest-spread', + ], +}; diff --git a/dist/react-onclickoutside.js b/dist/react-onclickoutside.js index 4f33953..cd9ada6 100644 --- a/dist/react-onclickoutside.js +++ b/dist/react-onclickoutside.js @@ -4,51 +4,87 @@ (factory((global.onClickOutside = {}),global.React,global.ReactDOM)); }(this, (function (exports,react,reactDom) { 'use strict'; +function _inheritsLoose(subClass, superClass) { + subClass.prototype = Object.create(superClass.prototype); + subClass.prototype.constructor = subClass; + subClass.__proto__ = superClass; +} + +function _objectWithoutProperties(source, excluded) { + if (source == null) return {}; + var target = {}; + var sourceKeys = Object.keys(source); + var key, i; + + for (i = 0; i < sourceKeys.length; i++) { + key = sourceKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + target[key] = source[key]; + } + + if (Object.getOwnPropertySymbols) { + var sourceSymbolKeys = Object.getOwnPropertySymbols(source); + + for (i = 0; i < sourceSymbolKeys.length; i++) { + key = sourceSymbolKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; + target[key] = source[key]; + } + } + + return target; +} + /** * Check whether some DOM node is our Component's node. */ function isNodeFound(current, componentNode, ignoreClass) { if (current === componentNode) { return true; - } - // SVG elements do not technically reside in the rendered DOM, so + } // SVG elements do not technically reside in the rendered DOM, so // they do not have classList directly, but they offer a link to their // corresponding element, which can have classList. This extra check is for // that case. // See: http://www.w3.org/TR/SVG11/struct.html#InterfaceSVGUseElement // Discussion: https://github.com/Pomax/react-onclickoutside/pull/17 + + if (current.correspondingElement) { return current.correspondingElement.classList.contains(ignoreClass); } + return current.classList.contains(ignoreClass); } - /** * Try to find our node in a hierarchy of nodes, returning the document * node as highest node if our node is not found in the path up. */ + function findHighest(current, componentNode, ignoreClass) { if (current === componentNode) { return true; - } - - // If source=local then this event came from 'somewhere' + } // If source=local then this event came from 'somewhere' // inside and should be ignored. We could handle this with // a layered approach, too, but that requires going back to // thinking in terms of Dom node nesting, running counter // to React's 'you shouldn't care about the DOM' philosophy. + + while (current.parentNode) { if (isNodeFound(current, componentNode, ignoreClass)) { return true; } + current = current.parentNode; } + return current; } - /** * Check if the browser scrollbar was clicked */ + function clickedScrollbar(evt) { return document.documentElement.clientWidth <= evt.clientX || document.documentElement.clientHeight <= evt.clientY; } @@ -61,7 +97,6 @@ var testPassiveEventSupport = function testPassiveEventSupport() { } var passive = false; - var options = Object.defineProperty({}, 'passive', { get: function get() { passive = true; @@ -72,12 +107,13 @@ var testPassiveEventSupport = function testPassiveEventSupport() { window.addEventListener('testPassiveEventSupport', noop, options); window.removeEventListener('testPassiveEventSupport', noop, options); - return passive; }; -function autoInc() { - var seed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; +function autoInc(seed) { + if (seed === void 0) { + seed = 0; + } return function () { return ++seed; @@ -86,64 +122,11 @@ function autoInc() { var uid = autoInc(); -var classCallCheck = function (instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -}; - - - - - - - - - - - -var inherits = function (subClass, superClass) { - if (typeof superClass !== "function" && superClass !== null) { - throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); - } - - subClass.prototype = Object.create(superClass && superClass.prototype, { - constructor: { - value: subClass, - enumerable: false, - writable: true, - configurable: true - } - }); - if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; -}; - - - - - - - - - - - -var possibleConstructorReturn = function (self, call) { - if (!self) { - throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - } - - return call && (typeof call === "object" || typeof call === "function") ? call : self; -}; - -var passiveEventSupport = void 0; - +var passiveEventSupport; var handlersMap = {}; var enabledInstances = {}; - var touchEvents = ['touchstart', 'touchmove']; var IGNORE_CLASS_NAME = 'ignore-react-onclickoutside'; - /** * This function generates the HOC function that you'll use * in order to impart onOutsideClick listening to an @@ -151,20 +134,24 @@ var IGNORE_CLASS_NAME = 'ignore-react-onclickoutside'; * bootstrapping code to yield an instance of the * onClickOutsideHOC function defined inside setupHOC(). */ + function onClickOutsideHOC(WrappedComponent, config) { var _class, _temp; - return _temp = _class = function (_Component) { - inherits(onClickOutside, _Component); + return _temp = _class = + /*#__PURE__*/ + function (_Component) { + _inheritsLoose(onClickOutside, _Component); function onClickOutside(props) { - classCallCheck(this, onClickOutside); + var _this; - var _this = possibleConstructorReturn(this, _Component.call(this, props)); + _this = _Component.call(this, props) || this; _this.__outsideClickHandler = function (event) { if (typeof _this.__clickOutsideHandlerProp === 'function') { _this.__clickOutsideHandlerProp(event); + return; } @@ -193,8 +180,8 @@ function onClickOutsideHOC(WrappedComponent, config) { } enabledInstances[_this._uid] = true; - var events = _this.props.eventTypes; + if (!events.forEach) { events = [events]; } @@ -212,7 +199,6 @@ function onClickOutsideHOC(WrappedComponent, config) { } if (_this.props.excludeScrollbar && clickedScrollbar(event)) return; - var current = event.target; if (findHighest(current, _this.componentNode, _this.props.outsideClickIgnoreClass) !== document) { @@ -227,7 +213,9 @@ function onClickOutsideHOC(WrappedComponent, config) { var isTouchEvent = touchEvents.indexOf(eventName) !== -1; if (isTouchEvent && passiveEventSupport) { - handlerOptions = { passive: !_this.props.preventDefault }; + handlerOptions = { + passive: !_this.props.preventDefault + }; } document.addEventListener(eventName, handlersMap[_this._uid], handlerOptions); @@ -240,9 +228,11 @@ function onClickOutsideHOC(WrappedComponent, config) { if (fn && typeof document !== 'undefined') { var events = _this.props.eventTypes; + if (!events.forEach) { events = [events]; } + events.forEach(function (eventName) { return document.removeEventListener(eventName, fn); }); @@ -257,16 +247,18 @@ function onClickOutsideHOC(WrappedComponent, config) { _this._uid = uid(); return _this; } - /** * Access the WrappedComponent's instance. */ - onClickOutside.prototype.getInstance = function getInstance() { + var _proto = onClickOutside.prototype; + + _proto.getInstance = function getInstance() { if (!WrappedComponent.prototype.isReactComponent) { return this; } + var ref = this.instanceRef; return ref.getInstance ? ref.getInstance() : ref; }; @@ -275,7 +267,7 @@ function onClickOutsideHOC(WrappedComponent, config) { * Add click listeners to the current document, * linked to this component's state. */ - onClickOutside.prototype.componentDidMount = function componentDidMount() { + _proto.componentDidMount = function componentDidMount() { // If we are in an environment without a DOM such // as shallow rendering or snapshots then we exit // early to prevent any unhandled errors being thrown. @@ -287,6 +279,7 @@ function onClickOutsideHOC(WrappedComponent, config) { if (config && typeof config.handleClickOutside === 'function') { this.__clickOutsideHandlerProp = config.handleClickOutside(instance); + if (typeof this.__clickOutsideHandlerProp !== 'function') { throw new Error('WrappedComponent lacks a function for processing outside click events specified by the handleClickOutside config option.'); } @@ -296,43 +289,31 @@ function onClickOutsideHOC(WrappedComponent, config) { this.enableOnClickOutside(); }; - onClickOutside.prototype.componentDidUpdate = function componentDidUpdate() { + _proto.componentDidUpdate = function componentDidUpdate() { this.componentNode = reactDom.findDOMNode(this.getInstance()); }; - /** * Remove all document's event listeners for this component */ - onClickOutside.prototype.componentWillUnmount = function componentWillUnmount() { + _proto.componentWillUnmount = function componentWillUnmount() { this.disableOnClickOutside(); }; - /** * Can be called to explicitly enable event listening * for clicks and touches outside of this element. */ - /** - * Can be called to explicitly disable event listening - * for clicks and touches outside of this element. - */ - - /** * Pass-through render */ - onClickOutside.prototype.render = function render() { - var _this2 = this; - - var props = Object.keys(this.props).filter(function (prop) { - return prop !== 'excludeScrollbar'; - }).reduce(function (props, prop) { - props[prop] = _this2.props[prop]; - return props; - }, {}); + _proto.render = function render() { + // eslint-disable-next-line no-unused-vars + var _props = this.props, + excludeScrollbar = _props.excludeScrollbar, + props = _objectWithoutProperties(_props, ["excludeScrollbar"]); if (WrappedComponent.prototype.isReactComponent) { props.ref = this.getRef; @@ -342,12 +323,11 @@ function onClickOutsideHOC(WrappedComponent, config) { props.disableOnClickOutside = this.disableOnClickOutside; props.enableOnClickOutside = this.enableOnClickOutside; - return react.createElement(WrappedComponent, props); }; return onClickOutside; - }(react.Component), _class.displayName = 'OnClickOutside(' + (WrappedComponent.displayName || WrappedComponent.name || 'Component') + ')', _class.defaultProps = { + }(react.Component), _class.displayName = "OnClickOutside(" + (WrappedComponent.displayName || WrappedComponent.name || 'Component') + ")", _class.defaultProps = { eventTypes: ['mousedown', 'touchstart'], excludeScrollbar: config && config.excludeScrollbar || false, outsideClickIgnoreClass: IGNORE_CLASS_NAME, diff --git a/dist/react-onclickoutside.min.js b/dist/react-onclickoutside.min.js index 92816d8..3fd3dc3 100644 --- a/dist/react-onclickoutside.min.js +++ b/dist/react-onclickoutside.min.js @@ -1 +1 @@ -!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,t,n){"use strict";function o(e,t,n){return e===t||(e.correspondingElement?e.correspondingElement.classList.contains(n):e.classList.contains(n))}function i(e,t,n){if(e===t)return!0;for(;e.parentNode;){if(o(e,t,n))return!0;e=e.parentNode}return e}function r(e){return document.documentElement.clientWidth<=e.clientX||document.documentElement.clientHeight<=e.clientY}var c=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}},s=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return function(){return++e}}(),u=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},d=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},a=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},p=void 0,l={},f={},h=["touchstart","touchmove"],O="ignore-react-onclickoutside";e.IGNORE_CLASS_NAME=O,e.default=function(e,o){var m,v;return v=m=function(O){function m(e){u(this,m);var t=a(this,O.call(this,e));return t.__outsideClickHandler=function(e){if("function"!=typeof t.__clickOutsideHandlerProp){var n=t.getInstance();if("function"!=typeof n.props.handleClickOutside){if("function"!=typeof n.handleClickOutside)throw Error("WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.");n.handleClickOutside(e)}else n.props.handleClickOutside(e)}else t.__clickOutsideHandlerProp(e)},t.enableOnClickOutside=function(){if("undefined"!=typeof document&&!f[t._uid]){void 0===p&&(p=c()),f[t._uid]=!0;var e=t.props.eventTypes;e.forEach||(e=[e]),l[t._uid]=function(e){t.props.disableOnClickOutside||null!==t.componentNode&&(t.props.preventDefault&&e.preventDefault(),t.props.stopPropagation&&e.stopPropagation(),t.props.excludeScrollbar&&r(e)||i(e.target,t.componentNode,t.props.outsideClickIgnoreClass)===document&&t.__outsideClickHandler(e))},e.forEach(function(e){var n=null;-1!==h.indexOf(e)&&p&&(n={passive:!t.props.preventDefault}),document.addEventListener(e,l[t._uid],n)})}},t.disableOnClickOutside=function(){delete f[t._uid];var e=l[t._uid];if(e&&"undefined"!=typeof document){var n=t.props.eventTypes;n.forEach||(n=[n]),n.forEach(function(t){return document.removeEventListener(t,e)}),delete l[t._uid]}},t.getRef=function(e){return t.instanceRef=e},t._uid=s(),t}return d(m,O),m.prototype.getInstance=function(){if(!e.prototype.isReactComponent)return this;var t=this.instanceRef;return t.getInstance?t.getInstance():t},m.prototype.componentDidMount=function(){if("undefined"!=typeof document&&document.createElement){var e=this.getInstance();if(o&&"function"==typeof o.handleClickOutside&&(this.__clickOutsideHandlerProp=o.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=n.findDOMNode(this.getInstance()),this.enableOnClickOutside()}},m.prototype.componentDidUpdate=function(){this.componentNode=n.findDOMNode(this.getInstance())},m.prototype.componentWillUnmount=function(){this.disableOnClickOutside()},m.prototype.render=function(){var n=this,o=Object.keys(this.props).filter(function(e){return"excludeScrollbar"!==e}).reduce(function(e,t){return e[t]=n.props[t],e},{});return e.prototype.isReactComponent?o.ref=this.getRef:o.wrappedRef=this.getRef,o.disableOnClickOutside=this.disableOnClickOutside,o.enableOnClickOutside=this.enableOnClickOutside,t.createElement(e,o)},m}(t.Component),m.displayName="OnClickOutside("+(e.displayName||e.name||"Component")+")",m.defaultProps={eventTypes:["mousedown","touchstart"],excludeScrollbar:o&&o.excludeScrollbar||!1,outsideClickIgnoreClass:O,preventDefault:!1,stopPropagation:!1},m.getClass=function(){return e.getClass?e.getClass():e},v},Object.defineProperty(e,"__esModule",{value:!0})}); +!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,t,n){"use strict";var o,i=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}},r=function(e){return void 0===e&&(e=0),function(){return++e}}(),c={},s={},u=["touchstart","touchmove"],d="ignore-react-onclickoutside";e.IGNORE_CLASS_NAME=d,e.default=function(e,a){var l,p;return p=l=function(d){function l(e){var t;return t=d.call(this,e)||this,t.__outsideClickHandler=function(e){if("function"!=typeof t.__clickOutsideHandlerProp){var n=t.getInstance();if("function"!=typeof n.props.handleClickOutside){if("function"!=typeof n.handleClickOutside)throw Error("WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.");n.handleClickOutside(e)}else n.props.handleClickOutside(e)}else t.__clickOutsideHandlerProp(e)},t.enableOnClickOutside=function(){if("undefined"!=typeof document&&!s[t._uid]){void 0===o&&(o=i()),s[t._uid]=!0;var e=t.props.eventTypes;e.forEach||(e=[e]),c[t._uid]=function(e){t.props.disableOnClickOutside||null===t.componentNode||(t.props.preventDefault&&e.preventDefault(),t.props.stopPropagation&&e.stopPropagation(),t.props.excludeScrollbar&&function(e){return document.documentElement.clientWidth<=e.clientX||document.documentElement.clientHeight<=e.clientY}(e))||function(e,t,n){if(e===t)return!0;for(;e.parentNode;){if(function(e,t,n){return e===t||(e.correspondingElement?e.correspondingElement.classList.contains(n):e.classList.contains(n))}(e,t,n))return!0;e=e.parentNode}return e}(e.target,t.componentNode,t.props.outsideClickIgnoreClass)===document&&t.__outsideClickHandler(e)},e.forEach(function(e){var n=null;-1!==u.indexOf(e)&&o&&(n={passive:!t.props.preventDefault}),document.addEventListener(e,c[t._uid],n)})}},t.disableOnClickOutside=function(){delete s[t._uid];var e=c[t._uid];if(e&&"undefined"!=typeof document){var n=t.props.eventTypes;n.forEach||(n=[n]),n.forEach(function(t){return document.removeEventListener(t,e)}),delete c[t._uid]}},t.getRef=function(e){return t.instanceRef=e},t._uid=r(),t}!function(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}(l,d);var p=l.prototype;return p.getInstance=function(){if(!e.prototype.isReactComponent)return this;var t=this.instanceRef;return t.getInstance?t.getInstance():t},p.componentDidMount=function(){if("undefined"!=typeof document&&document.createElement){var e=this.getInstance();if(a&&"function"==typeof a.handleClickOutside&&(this.__clickOutsideHandlerProp=a.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=n.findDOMNode(this.getInstance()),this.enableOnClickOutside()}},p.componentDidUpdate=function(){this.componentNode=n.findDOMNode(this.getInstance())},p.componentWillUnmount=function(){this.disableOnClickOutside()},p.render=function(){var n=this.props,o=function(e,t){if(null==e)return{};var n,o,i={},r=Object.keys(e);for(o=0;r.length>o;o++)0>t.indexOf(n=r[o])&&(i[n]=e[n]);if(Object.getOwnPropertySymbols){var c=Object.getOwnPropertySymbols(e);for(o=0;c.length>o;o++)0>t.indexOf(n=c[o])&&Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}(n,["excludeScrollbar"]);return e.prototype.isReactComponent?o.ref=this.getRef:o.wrappedRef=this.getRef,o.disableOnClickOutside=this.disableOnClickOutside,o.enableOnClickOutside=this.enableOnClickOutside,t.createElement(e,o)},l}(t.Component),l.displayName="OnClickOutside("+(e.displayName||e.name||"Component")+")",l.defaultProps={eventTypes:["mousedown","touchstart"],excludeScrollbar:a&&a.excludeScrollbar||!1,outsideClickIgnoreClass:d,preventDefault:!1,stopPropagation:!1},l.getClass=function(){return e.getClass?e.getClass():e},p},Object.defineProperty(e,"__esModule",{value:!0})}); diff --git a/package.json b/package.json index 2a1ed98..7642240 100644 --- a/package.json +++ b/package.json @@ -34,13 +34,13 @@ "clean": "rimraf es lib dist", "prebuild": "npm run clean", "build": "run-p build:**", - "build:es": "cross-env BABEL_ENV=es babel src -d es", - "build:commonjs": "cross-env BABEL_ENV=commonjs babel src -d lib", - "build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c -i src/index.js -o dist/react-onclickoutside.js", - "build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c -i src/index.js -o dist/react-onclickoutside.min.js", + "build:es": "cross-env BABEL_ENV=es rollup -c -i src/index.js -o es/index.js", + "build:cjs": "cross-env BABEL_ENV=cjs rollup -c -i src/index.js -o lib/index.js", + "build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c rollup.umd.config.js -i src/index.js -o dist/react-onclickoutside.js", + "build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c rollup.umd.config.js -i src/index.js -o dist/react-onclickoutside.min.js", "lint": "eslint src/*.js ./test", "test": "run-s test:**", - "test:basic": "run-s lint build:commonjs", + "test:basic": "run-s lint build:cjs", "test:karma": "karma start test/karma.conf.js --single-run", "test:nodom": "mocha test/no-dom-test.js", "precommit": "npm test && lint-staged", @@ -52,12 +52,13 @@ "release:major": "npm run prerelease && npm version major && npm publish && git push --follow-tags" }, "devDependencies": { - "babel-cli": "^6.24.1", - "babel-eslint": "^7.2.3", - "babel-loader": "^6.x", - "babel-plugin-external-helpers": "^6.22.0", - "babel-preset-env": "^1.5.1", - "babel-preset-stage-0": "^6.24.1", + "@babel/core": "7.0.0-beta.31", + "@babel/plugin-proposal-class-properties": "7.0.0-beta.31", + "@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.31", + "@babel/preset-env": "7.0.0-beta.31", + "@babel/preset-stage-0": "7.0.0-beta.31", + "babel-eslint": "^8.0.2", + "babel-loader": "8.0.0-beta.0", "chai": "^3.5.0", "cross-env": "^5.0.0", "eslint": "^3.4.0", @@ -71,16 +72,16 @@ "lint-staged": "^3.4.2", "mocha": "^3.2.0", "npm-run-all": "^4.0.2", - "prettier": "^1.3.1", + "prettier": "^1.8.2", "react": "^15.5.x", "react-dom": "^15.5.x", "react-test-renderer": "^15.5.x", "require-hijack": "^1.2.1", "rimraf": "^2.6.2", - "rollup": "^0.50.0", - "rollup-plugin-babel": "^3.0.2", + "rollup": "^0.51.3", + "rollup-plugin-babel": "^4.0.0-beta.0", "rollup-plugin-uglify": "^2.0.1", - "webpack": "^1.15.0" + "webpack": "^3.8.1" }, "peerDependencies": { "react": "^15.5.x || ^16.x", @@ -91,6 +92,10 @@ "prettier --print-width=120 --single-quote --trailing-comma=all --write", "eslint --fix", "git add" + ], + "*.md": [ + "prettier --write", + "git add" ] } } diff --git a/rollup.config.js b/rollup.config.js index bd6fbc8..714927a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,38 +1,16 @@ import babel from 'rollup-plugin-babel'; -import { list as babelHelpersList } from 'babel-helpers'; -import uglify from 'rollup-plugin-uglify'; var config = { output: { - format: 'umd', - name: 'onClickOutside', + format: process.env.BABEL_ENV, exports: 'named', }, external: ['react', 'react-dom'], - globals: { - react: 'React', - 'react-dom': 'ReactDOM' - }, plugins: [ babel({ exclude: 'node_modules/**', - plugins: ['external-helpers'], - externalHelpersWhitelist: babelHelpersList.filter(helperName => helperName !== 'asyncGenerator'), }), ], }; -if (process.env.NODE_ENV === 'production') { - config.plugins.push( - uglify({ - compress: { - pure_getters: true, - unsafe: true, - unsafe_comps: true, - warnings: false, - }, - }) - ); -} - export default config; diff --git a/rollup.umd.config.js b/rollup.umd.config.js new file mode 100644 index 0000000..bf37d57 --- /dev/null +++ b/rollup.umd.config.js @@ -0,0 +1,31 @@ +import config from './rollup.config.js'; +import uglify from 'rollup-plugin-uglify'; + +const env = process.env.NODE_ENV; + +const umdConfig = Object.assign({}, config, { + output: Object.assign({}, config.output, { + format: 'umd', + name: 'onClickOutside', + }), + globals: { + react: 'React', + 'react-dom': 'ReactDOM', + 'prop-types': 'PropTypes' + }, +}); + +if (env === 'production') { + config.plugins.push( + uglify({ + compress: { + pure_getters: true, + unsafe: true, + unsafe_comps: true, + warnings: false, + }, + }) + ); +} + +export default umdConfig; diff --git a/src/index.js b/src/index.js index c62f913..1f66fee 100644 --- a/src/index.js +++ b/src/index.js @@ -189,12 +189,8 @@ export default function onClickOutsideHOC(WrappedComponent, config) { * Pass-through render */ render() { - var props = Object.keys(this.props) - .filter(prop => prop !== 'excludeScrollbar') - .reduce((props, prop) => { - props[prop] = this.props[prop]; - return props; - }, {}); + // eslint-disable-next-line no-unused-vars + let { excludeScrollbar, ...props } = this.props; if (WrappedComponent.prototype.isReactComponent) { props.ref = this.getRef; diff --git a/test/karma.conf.js b/test/karma.conf.js index 39e41a5..72cfee0 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -13,13 +13,10 @@ module.exports = function(config) { webpack: { devtool: 'inline-source-map', module: { - loaders: [ + rules: [ { test: /.js$/, - loader: 'babel', - query: { - presets: ['env', 'stage-0'], - }, + loader: 'babel-loader', }, ], },