UNPKG

@react-hook/window-size

Version:

React hooks for updating components when the size of the `window` changes.

3 lines (2 loc) 2.24 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).UseWindowSize={},e.React)}(this,(function(e,n){"use strict";function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:1,get:function(){return e[t]}})}})),n.default=e,Object.freeze(n)}function r(e,n,t,r){var i=u.useRef(t),o=u.useRef(r);u.useEffect((function(){i.current=t,o.current=r})),u.useEffect((function(){function t(){if(!u){for(var e=arguments.length,n=new Array(e),t=0;e>t;t++)n[t]=arguments[t];i.current.apply(this,n)}}var r=e&&"current"in e?e.current:e;if(r){var u=0;r.addEventListener(n,t);var c=o.current;return function(){u=1,r.removeEventListener(n,t),c&&c()}}}),[e,n])}var u=t(n),i=function(e,n,t){function r(){d.current&&clearTimeout(d.current),d.current=void 0}function i(){d.current=void 0}void 0===n&&(n=100),void 0===t&&(t=0);var o,c,f=(o=e,c=u.useRef(o),u.useEffect((function(){c.current=o})),c),d=u.useRef(),a=[n,t,f];return u.useEffect((function(){return r}),a),u.useCallback((function(){var e=arguments,r=d.current;if(void 0===r&&t)return d.current=setTimeout(i,n),f.current.apply(null,e);r&&clearTimeout(r),d.current=setTimeout((function(){d.current=void 0,f.current.apply(null,e)}),n)}),a)},o={},c="undefined"==typeof window?null:window,f=c&&void 0!==c.visualViewport?c.visualViewport:null,d=function(){return[document.documentElement.clientWidth,document.documentElement.clientHeight]},a=function(e){void 0===e&&(e=o);var n=e,t=n.wait,a=n.leading,l=n.initialWidth,s=void 0===l?0:l,v=n.initialHeight,p=function(e,n,t){var r=u.useState(e);return[r[0],i(r[1],n,t)]}("undefined"==typeof document?[s,void 0===v?0:v]:d,t,a),m=p[0],y=p[1],h=function(){return y(d)};return r(c,"resize",h),r(f,"resize",h),r(c,"orientationchange",h),m};e.useWindowHeight=function(e){return a(e)[1]},e.useWindowSize=a,e.useWindowWidth=function(e){return a(e)[0]},Object.defineProperty(e,"__esModule",{value:1})})); //# sourceMappingURL=use-window-size.js.map