react-with-dimension
Version:
Debounced React high order component to expose container width and height.
1 lines • 6.33 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.WithDimension=t(require("react")):e.WithDimension=t(e.react)}(this,function(e){return function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{},id:i,loaded:!1};return e[i].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var o=n(1),r=i(o);t.default=r.default},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(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}function u(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)}Object.defineProperty(t,"__esModule",{value:!0});var a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},c=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),f=n(5),d=i(f),s=n(4),l=i(s),p=n(2),v=i(p),h=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e&&e.clientHeight?e.clientHeight:e&&e.getBoundingClientRect?e.getBoundingClientRect().height:void 0},y=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e&&e.clientWidth?e.clientWidth:e&&e.getBoundingClientRect?e.getBoundingClientRect().width:void 0},m=function(e,t){return{containerWidth:e,containerHeight:t}},b={width:"100%",height:"100%",padding:0,border:0},w={leading:!1,trailing:!0,maxWait:800},g=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.transform,n=void 0===t?m:t,i=e.containerStyle,s=void 0===i?b:i,p=e.getHeight,g=void 0===p?h:p,j=e.getWidth,_=void 0===j?y:j,O=e.wait,x=void 0===O?200:O,W=e.debounceOptions,D=void 0===W?w:W;return function(e){var t,i;return i=t=function(t){function i(){var e,t,n,u;o(this,i);for(var a=arguments.length,c=Array(a),f=0;f<a;f++)c[f]=arguments[f];return t=n=r(this,(e=i.__proto__||Object.getPrototypeOf(i)).call.apply(e,[this].concat(c))),n.state={containerWidth:null,containerHeight:null},n.update=function(e){n.setState({containerWidth:_(e),containerHeight:g(e)})},n.updateDimensions=function(){var e=n.withDimensionContainer;n.canUpdate&&(n.state.containerWidth===_(e)&&n.state.containerHeight===g(e)||("undefined"!=typeof window&&null!==window&&window.requestAnimationFrame?window.requestAnimationFrame(function(){return n.update(e)}):n.update(e)))},n.updateWithDimensionContainerRef=function(e){n.withDimensionContainer=e},u=t,r(n,u)}return u(i,t),c(i,[{key:"componentDidMount",value:function(){"undefined"!=typeof window&&null!==window&&window.addEventListener&&(this.canUpdate=!0,this.debouncedUpdate=(0,v.default)(this.updateDimensions,x,D),window.addEventListener("resize",this.debouncedUpdate,{passive:!0}),this.debouncedUpdate())}},{key:"componentDidUpdate",value:function(){this.debouncedUpdate&&this.debouncedUpdate()}},{key:"componentWillUnmount",value:function(){"undefined"!=typeof window&&null!==window&&window.removeEventListener&&(this.canUpdate=!1,window.removeEventListener("resize",this.debouncedUpdate,{passive:!0}))}},{key:"render",value:function(){return d.default.createElement("div",{ref:this.updateWithDimensionContainerRef,style:s},d.default.createElement(e,a({},n(this.state.containerWidth,this.state.containerHeight),this.props)))}}]),i}(f.Component),t.displayName=(0,l.default)(e,"withDimension"),i}};t.default=g},function(e,t){(function(t){function n(e,t,n){function o(t){var n=h,i=y;return h=y=void 0,x=t,b=e.apply(i,n)}function r(e){return x=e,w=setTimeout(d,t),W?o(e):b}function c(e){var n=e-O,i=e-x,o=t-n;return D?j(o,m-i):o}function f(e){var n=e-O,i=e-x;return void 0===O||n>=t||n<0||D&&i>=m}function d(){var e=_();return f(e)?s(e):void(w=setTimeout(d,c(e)))}function s(e){return w=void 0,C&&h?o(e):(h=y=void 0,b)}function l(){void 0!==w&&clearTimeout(w),x=0,h=O=y=w=void 0}function p(){return void 0===w?b:s(_())}function v(){var e=_(),n=f(e);if(h=arguments,y=this,O=e,n){if(void 0===w)return r(O);if(D)return w=setTimeout(d,t),o(O)}return void 0===w&&(w=setTimeout(d,t)),b}var h,y,m,b,w,O,x=0,W=!1,D=!1,C=!0;if("function"!=typeof e)throw new TypeError(a);return t=u(t)||0,i(n)&&(W=!!n.leading,D="maxWait"in n,m=D?g(u(n.maxWait)||0,t):m,C="trailing"in n?!!n.trailing:C),v.cancel=l,v.flush=p,v}function i(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}function o(e){return!!e&&"object"==typeof e}function r(e){return"symbol"==typeof e||o(e)&&w.call(e)==f}function u(e){if("number"==typeof e)return e;if(r(e))return c;if(i(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=i(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=e.replace(d,"");var n=l.test(e);return n||p.test(e)?v(e.slice(2),n?2:8):s.test(e)?c:+e}var a="Expected a function",c=NaN,f="[object Symbol]",d=/^\s+|\s+$/g,s=/^[-+]0x[0-9a-f]+$/i,l=/^0b[01]+$/i,p=/^0o[0-7]+$/i,v=parseInt,h="object"==typeof t&&t&&t.Object===Object&&t,y="object"==typeof self&&self&&self.Object===Object&&self,m=h||y||Function("return this")(),b=Object.prototype,w=b.toString,g=Math.max,j=Math.min,_=function(){return m.Date.now()};e.exports=n}).call(t,function(){return this}())},function(e,t){"use strict";t.__esModule=!0;var n=function(e){if("string"==typeof e)return e;if(e)return e.displayName||e.name||"Component"};t.default=n},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0;var o=n(3),r=i(o),u=function(e,t){return t+"("+(0,r.default)(e)+")"};t.default=u},function(t,n){t.exports=e}])});