UNPKG

react-goodies

Version:

[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![downloads][downloads-badge]][npmcharts] [![version][version-badge]][package] [![MIT License][license-badge]][license]

3 lines (2 loc) 8.34 kB
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],n):n(t.ReactGoodies={},t.preact)}(this,function(t,n){"use strict";"default"in n&&n.default;function e(){}function r(t,n){var e=Array.isArray(t)?t[0]:t;return!e&&n?n:e}var o=function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")},i=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},s=function(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Super expression must either be null or a function, not "+typeof n);t.prototype=Object.create(n&&n.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),n&&(Object.setPrototypeOf?Object.setPrototypeOf(t,n):t.__proto__=n)},u=function(t,n){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!n||"object"!=typeof n&&"function"!=typeof n?t:n},c=function(t){function n(){var e,r;o(this,n);for(var i=arguments.length,s=Array(i),c=0;c<i;c++)s[c]=arguments[c];return e=r=u(this,t.call.apply(t,[this].concat(s))),r.isTouch=!1,r.setRef=function(t){r.container=t},r.handle=function(t){if("touchend"===t.type&&(r.isTouch=!0),"click"!==t.type||!r.isTouch){var n=r.props.onClickOutside;r.container.contains(t.target)||n(t)}},u(r,e)}return s(n,t),n.prototype.addListener=function(){document.addEventListener("touchend",this.handle,!0),document.addEventListener("click",this.handle,!0)},n.prototype.removeListener=function(){document.removeEventListener("touchend",this.handle,!0),document.removeEventListener("click",this.handle,!0)},n.prototype.componentDidMount=function(){this.props.disabled||this.addListener()},n.prototype.componentDidUpdate=function(t){var n=this.props.disabled;t.disabled!==n&&(n?this.removeListener():this.addListener())},n.prototype.componentWillUnmount=function(){this.removeListener()},n.prototype.render=function(){return r(this.props.children,e)({setRef:this.setRef})},n}(n.Component),a=function(t){function n(){var e,r;o(this,n);for(var s=arguments.length,c=Array(s),a=0;a<s;a++)c[a]=arguments[a];return e=r=u(this,t.call.apply(t,[this].concat(c))),r.state={},r.setRef=function(t){r.domNode=t},r.onObserve=function(t){var n=r.props.thresholds;r.setState(Object.keys(n).reduce(function(e,r){return i({},e,t.reduce(function(t,e){var o;return i({},t,((o={})[r]=e.isIntersecting&&e.intersectionRatio>=n[r],o))},{}))},{}))},u(r,e)}return s(n,t),n.prototype.componentDidMount=function(){var t=this.props,n=t.thresholds,e=t.options;this.observer=new global.IntersectionObserver(this.onObserve,i({},e,{threshold:Object.keys(n).map(function(t){return n[t]})})),this.observer.observe(this.domNode)},n.prototype.componentWillUnmount=function(){this.observer&&this.observer.disconnect&&this.observer.disconnect()},n.prototype.render=function(){return r(this.props.children,e)(i({setRef:this.setRef},this.state))},n}(n.Component);a.defaultProps={thresholds:{},options:{}};var p,f,l=(function(t,n){Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t){return t.displayName||t.name||("string"==typeof t&&t.length>0?t:"Unknown")}}(p={exports:{}},p.exports),p.exports);(f=l)&&f.__esModule&&Object.prototype.hasOwnProperty.call(f,"default")&&f.default;var d="function"==typeof global.IntersectionObserver,v=function(t){return t.replace(/[A-Z]/g,function(t){return"-"+t.toLowerCase()}).toLowerCase()},y=function(t){var n="",e=Object.keys(t);return e.forEach(function(r,o){var i=t[r];(function(t){return/[height|width]$/.test(t)})(r=v(r))&&"number"==typeof i&&(i+="px"),n+=!0===i?r:!1===i?"not "+r:"("+r+": "+i+")",o<e.length-1&&(n+=" and ")}),n},b=function(t){var n="";return"string"==typeof t?t:t instanceof Array?(t.forEach(function(e,r){n+=y(e),r<t.length-1&&(n+=", ")}),n):y(t)},m=function(t){var n=function(t){return global.matchMedia(b(t))}(t);return{matches:n.matches,subscribe:function(t){return n.addListener(t),function(){return n.removeListener(t)}}}},g=function(t){function n(e,r){o(this,n);var s=u(this,t.call(this,e,r)),c=e.query;return s.propsMatchers=Object.keys(c).map(function(t){return i({prop:t},m(c[t]))}),s.state=s.propsMatchers.reduce(function(t,n){var e;return i({},t,((e={})[n.prop]=n.matches,e))},{}),s}return s(n,t),n.prototype.componentDidMount=function(){var t=this;this.unsubscribers=this.propsMatchers.map(function(n){return n.subscribe(function(e){var r;t.setState(((r={})[n.prop]=e.matches,r))})})},n.prototype.componentWillUnmount=function(){this.unsubscribers.forEach(function(t){return t()})},n.prototype.render=function(){return r(this.props.children,e)(i({},this.state))},n}(n.Component);g.defaultProps={query:{}};var O="function"==typeof global.matchMedia,L=function(t){function n(e,r){o(this,n);var i=u(this,t.call(this,e,r));i.handleOnline=function(){i.setState({isOnline:!0,isOffline:!1})},i.handleOffline=function(){i.setState({isOnline:!1,isOffline:!0})};var s=global.navigator.onLine;return i.state={isOnline:s,isOffline:!s},i}return s(n,t),n.prototype.componentDidMount=function(){global.addEventListener("online",this.handleOnline,!1),global.addEventListener("offline",this.handleOffline,!1)},n.prototype.componentWillUnmount=function(){global.removeEventListener("online",this.handleOnline),global.removeEventListener("offline",this.handleOffline)},n.prototype.render=function(){return r(this.props.children,e)(i({},this.state))},n}(n.Component),w=global.navigator&&void 0!==global.navigator.onLine,C=function(t){return t},M=function(t){return{isVisible:"visible"===t,isHidden:"hidden"===t,isPrerendered:"prerender"===t,isUnloaded:"unloaded"===t}},E=function(t){function n(e,r){o(this,n);var i=u(this,t.call(this,e,r));return i.onVibisilityChange=function(){i.setState(M(document.visibilityState))},i.state=M(document.visibilityState),i}return s(n,t),n.prototype.componentDidMount=function(){document.addEventListener("visibilitychange",this.onVibisilityChange,!1)},n.prototype.componentWillUnmount=function(){document.removeEventListener("visibilitychange",this.onVibisilityChange)},n.prototype.render=function(){return r(this.props.children,e)(i({},this.state))},n}(n.Component),j=global.document&&void 0!==global.document.visibilityState,R=function(t){return t};t.ClickOutside=c,t.IntersectionObserverComp=a,t.withIntersectionObserver=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.thresholds,r=t.options,c=t.refKey,p=void 0===c?"setRef":c;return function(t){return d?function(n){function c(){var t,e;o(this,c);for(var r=arguments.length,i=Array(r),s=0;s<r;s++)i[s]=arguments[s];return t=e=u(this,n.call.apply(n,[this].concat(i))),e.setRef=function(t,n){t(n),"function"==typeof e.props[p]&&e.props[p](n)},u(e,t)}return s(c,n),c.prototype.render=function(){var n=this;return h(a,{thresholds:e,options:r},function(e){var r,o=e.setRef,s=function(t,n){var e={};for(var r in t)n.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}(e,["setRef"]),u=((r={})[p]=n.setRef.bind(n,o),r);return h(t,i({},n.props,u,s))})},c}(n.Component):t}},t.MatchMedia=g,t.withMatchMedia=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(e){return O?function(n){function r(){return o(this,r),u(this,n.apply(this,arguments))}return s(r,n),r.prototype.render=function(){var n=this;return h(g,{query:t},function(t){return h(e,i({},n.props,t))})},r}(n.Component):e}},t.OnlineStatus=L,t.withOnlineStatus=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:C;return function(e){return w?function(n){function r(){return o(this,r),u(this,n.apply(this,arguments))}return s(r,n),r.prototype.render=function(){var n=this;return h(L,null,function(r){return h(e,i({},n.props,t(r)))})},r}(n.Component):e}},t.PageVisibility=E,t.withPageVisibility=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:R;return function(e){return j?function(n){function r(){return o(this,r),u(this,n.apply(this,arguments))}return s(r,n),r.prototype.render=function(){var n=this;return h(E,null,function(r){return h(e,i({},n.props,t(r)))})},r}(n.Component):e}},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=react-goodies.umd.min.js.map