@leafygreen-ui/hooks
Advanced tools
Comparing version 8.1.4 to 8.2.0
# @leafygreen-ui/hooks | ||
## 8.2.0 | ||
### Minor Changes | ||
- 9776f5f42: Adds `useSsrCheck` and adds it to viewport check in `useViewportSize`. | ||
When server side rendering is used, `window` is not defined. This is causing build issues on the server where we access `window` in `useViewportSize`. To fix this, this change adds a hook, `useSsrCheck`, that checks the rendering environment and can be used before attempting to access `window`. It adds a check of this to `useViewportSize` to fix the current build issue. | ||
## 8.1.4 | ||
@@ -4,0 +12,0 @@ |
@@ -1,2 +0,2 @@ | ||
import*as n from"react";import{useEffect as r,useState as t,useMemo as e,useRef as o,useCallback as i,useLayoutEffect as u}from"react";import c from"lodash/debounce";import a from"lodash/isUndefined";import{consoleOnce as l,createSyntheticEvent as f}from"@leafygreen-ui/lib";import v from"lodash/isEqual";var d=function(n,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;r((function(){if(n&&n.current&&t&&t.current){var r=n.current.offsetTop,o=t.current,i=o.scrollTop;(r>o.offsetHeight||r<i)&&t.current.scrollTo({top:r-e,behavior:"smooth"})}}),[t,n,e])};function s(n,r){var t=Object.keys(n);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(n);r&&(e=e.filter((function(r){return Object.getOwnPropertyDescriptor(n,r).enumerable}))),t.push.apply(t,e)}return t}function p(n){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?s(Object(t),!0).forEach((function(r){m(n,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(t)):s(Object(t)).forEach((function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(t,r))}))}return n}function y(n){var r=function(n,r){if("object"!=typeof n||!n)return n;var t=n[Symbol.toPrimitive];if(void 0!==t){var e=t.call(n,r);if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(n)}(n,"string");return"symbol"==typeof r?r:r+""}function b(n){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},b(n)}function m(n,r,t){return(r=y(r))in n?Object.defineProperty(n,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[r]=t,n}function h(n,r){return function(n){if(Array.isArray(n))return n}(n)||function(n,r){var t=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=t){var e,o,i,u,c=[],a=!0,l=!1;try{if(i=(t=t.call(n)).next,0===r);else for(;!(a=(e=i.call(t)).done)&&(c.push(e.value),c.length!==r);a=!0);}catch(n){l=!0,o=n}finally{try{if(!a&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(l)throw o}}return c}}(n,r)||w(n,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(n){return function(n){if(Array.isArray(n))return O(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||w(n)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function w(n,r){if(n){if("string"==typeof n)return O(n,r);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?O(n,r):void 0}}function O(n,r){(null==r||r>n.length)&&(r=n.length);for(var t=0,e=new Array(r);t<r;t++)e[t]=n[t];return e}function j(){return{width:window.innerWidth,height:window.innerHeight}}function S(){var n=h(t(j()),2),e=n[0],o=n[1];return r((function(){var n=c((function(){return o(j())}),100);return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}}),[]),e}var P=function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,t=S();return e((function(){if(t&&n&&n.current){var e=n.current.getBoundingClientRect(),o=e.top,i=e.bottom;return Math.max(t.height-i,o)-r}}),[t,n,r])};function A(n,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=e.options,u=e.enabled,c=void 0===u||u,a=e.dependencies,l=void 0===a?[c,n]:a,f=e.element,v=o((function(){}));r((function(){v.current=t}),[t]),r((function(){if(!1!==c){if("once"===c||!0===c){var r=function(n){v.current(n)},t=p(p({},i),{},{once:"once"===c});return(null!=f?f:document).addEventListener(n,r,t),function(){(null!=f?f:document).removeEventListener(n,r,t)}}console.error("Received value of type ".concat(b(c)," for property `enabled`. Expected a boolean."))}}),l)}function E(n,r){var t=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];function e(n){var t,e=n.target;return Array.isArray(r)?r.some((function(n){var r;return null===(r=n.current)||void 0===r?void 0:r.contains(e)})):(null===(t=r.current)||void 0===t?void 0:t.contains(e))||!1}A("mousedown",(function(n){e(n)||(n.preventDefault(),n.stopPropagation())}),{enabled:t}),A("click",(function(r){e(r)||(r.stopPropagation(),n())}),{options:{capture:!0},enabled:t})}var C=function(n,o,i){var u=e((function(){return!a(n)}),[]),c=h(t(i),2),v=c[0],d=c[1],s=function(n){null==o||o(n),u||d(n.target.value)};return r((function(){a(n)&&a(i)&&l.error("Warning: `useControlledValue` hook is being used without a value or initialValue. This will cause a React warning when the input changes. Please decide between using a controlled or uncontrolled input element, and provide either a controlledValue or initialValue to `useControlledValue`")}),[n,i]),{isControlled:u,value:u?n:v,handleChange:s,setUncontrolledValue:d,updateValue:function(n,r){if(r.current){r.current.value=n;var t=f(new Event("change",{cancelable:!0,bubbles:!0}),r.current);s(t)}}}};function T(r){var t=null==r?void 0:r.prefix;return n.useMemo((function(){var r=function(r){return function(t){if(t){if(r.get(t))return r.get(t);var e=n.createRef();return r.set(t,e),e}l.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return r}),t?[t]:[])}var V=function(n,r){return A("keydown",(function(r){return function(n,r){27===n.keyCode&&(n.stopImmediatePropagation(),r())}(r,n)}),r)};function x(){var n=h(t({}),2)[1];return i((function(){return n({})}),[])}function I(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=r.initialValue,i=r.deps,u=void 0===i?[]:i,c=o(t);return e((function(){return{get current(){return c.current},set current(r){c.current=r,n(r)}}}),[n,c].concat(g(u)))}function k(n,r){var t=i((function(n,r){Array.isArray(n)?n.forEach(t):"function"==typeof n?n(r):n&&(n.current=r)}),[]);return I(i((function(r){return t(n,r)}),[n,t]),{initialValue:r})}var D=0;function M(n){var e=n.prefix;return function(n){var e=n.id,o=n.prefix,i=h(t(e),2),u=i[0],c=i[1];return r((function(){null==u&&c(D+=1)}),[u,o]),e||"".concat(null!=o?o:"lg","-").concat(u)}({id:n.id,prefix:e})}var R=function(){return("undefined"==typeof window?r:u).apply(void 0,arguments)};function L(n,e,o){var i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=h(t(),2),c=u[0],a=u[1];return r((function(){if(i){var r=new MutationObserver((function(){a(o.apply(void 0,arguments))}));return n&&r.observe(n,e),function(){return r.disconnect()}}}),[n,e,o,i]),c}function B(n){var r=o();return void 0!==r.current&&v(r.current,n)||(r.current=n),r.current}function U(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=e.interval,u=void 0===i?3e4:i,c=e.immediate,a=void 0===c||c,l=e.enabled,f=void 0===l||l,v=o(),d=function(){var n=function(){return"visible"===document.visibilityState},e=h(t(!0),2),o=e[0],i=e[1];return r((function(){i(n)}),[]),A("visibilitychange",(function(){i(n)})),o}()&&f;r((function(){v.current=n})),r((function(){var n;if(d)return a?e():r(),t;function r(){t(),n=setTimeout(e,u)}function t(){clearTimeout(n)}function e(){var n;Promise.resolve(null===(n=v.current)||void 0===n?void 0:n.call(v)).finally(r)}}),[u,a,d])}function z(n){var t=o();return r((function(){t.current=n})),t.current}function H(n){var r=h(t(n),2),e=r[0],u=r[1],c=o(e);return[e,i((function(n){u(n),c.current=n}),[u]),i((function(){return c.current}),[])]}function W(n){var r=h(t(!1),2),e=r[0],o=r[1];if(a(n)||"function"!=typeof n)return{onBlur:function(){},onChange:function(){}};return{onBlur:function(r){o(!0),null==n||n(r.target.value)},onChange:function(r){e&&(null==n||n(r.target.value))}}}export{d as useAutoScroll,P as useAvailableSpace,E as useBackdropClick,C as useControlledValue,T as useDynamicRefs,V as useEscapeKey,A as useEventListener,x as useForceRerender,k as useForwardedRef,M as useIdAllocator,R as useIsomorphicLayoutEffect,L as useMutationObserver,B as useObjectDependency,I as useObservedRef,U as usePoller,z as usePrevious,H as useStateRef,W as useValidation,S as useViewportSize}; | ||
import*as n from"react";import{useEffect as r,useState as t,useMemo as e,useRef as o,useCallback as i,useLayoutEffect as u}from"react";import c from"lodash/debounce";import a from"lodash/isUndefined";import{consoleOnce as l,createSyntheticEvent as f}from"@leafygreen-ui/lib";import v from"lodash/isEqual";var d=function(n,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;r((function(){if(n&&n.current&&t&&t.current){var r=n.current.offsetTop,o=t.current,i=o.scrollTop;(r>o.offsetHeight||r<i)&&t.current.scrollTo({top:r-e,behavior:"smooth"})}}),[t,n,e])};function s(n,r){var t=Object.keys(n);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(n);r&&(e=e.filter((function(r){return Object.getOwnPropertyDescriptor(n,r).enumerable}))),t.push.apply(t,e)}return t}function p(n){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?s(Object(t),!0).forEach((function(r){m(n,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(t)):s(Object(t)).forEach((function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(t,r))}))}return n}function y(n){var r=function(n,r){if("object"!=typeof n||!n)return n;var t=n[Symbol.toPrimitive];if(void 0!==t){var e=t.call(n,r);if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(n)}(n,"string");return"symbol"==typeof r?r:r+""}function b(n){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},b(n)}function m(n,r,t){return(r=y(r))in n?Object.defineProperty(n,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[r]=t,n}function h(n,r){return function(n){if(Array.isArray(n))return n}(n)||function(n,r){var t=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=t){var e,o,i,u,c=[],a=!0,l=!1;try{if(i=(t=t.call(n)).next,0===r);else for(;!(a=(e=i.call(t)).done)&&(c.push(e.value),c.length!==r);a=!0);}catch(n){l=!0,o=n}finally{try{if(!a&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(l)throw o}}return c}}(n,r)||w(n,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(n){return function(n){if(Array.isArray(n))return O(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||w(n)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function w(n,r){if(n){if("string"==typeof n)return O(n,r);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?O(n,r):void 0}}function O(n,r){(null==r||r>n.length)&&(r=n.length);for(var t=0,e=new Array(r);t<r;t++)e[t]=n[t];return e}function j(){var n=h(t("undefined"==typeof window),2),e=n[0],o=n[1];return r((function(){o(!1)}),[]),e}function S(){return{width:window.innerWidth,height:window.innerHeight}}function P(){var n=j(),e=h(t(n?null:S()),2),o=e[0],i=e[1];return r((function(){var n=c((function(){return i(S())}),100);return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}}),[]),o}var A=function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,t=P();return e((function(){if(t&&n&&n.current){var e=n.current.getBoundingClientRect(),o=e.top,i=e.bottom;return Math.max(t.height-i,o)-r}}),[t,n,r])};function E(n,t){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=e.options,u=e.enabled,c=void 0===u||u,a=e.dependencies,l=void 0===a?[c,n]:a,f=e.element,v=o((function(){}));r((function(){v.current=t}),[t]),r((function(){if(!1!==c){if("once"===c||!0===c){var r=function(n){v.current(n)},t=p(p({},i),{},{once:"once"===c});return(null!=f?f:document).addEventListener(n,r,t),function(){(null!=f?f:document).removeEventListener(n,r,t)}}console.error("Received value of type ".concat(b(c)," for property `enabled`. Expected a boolean."))}}),l)}function C(n,r){var t=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];function e(n){var t,e=n.target;return Array.isArray(r)?r.some((function(n){var r;return null===(r=n.current)||void 0===r?void 0:r.contains(e)})):(null===(t=r.current)||void 0===t?void 0:t.contains(e))||!1}E("mousedown",(function(n){e(n)||(n.preventDefault(),n.stopPropagation())}),{enabled:t}),E("click",(function(r){e(r)||(r.stopPropagation(),n())}),{options:{capture:!0},enabled:t})}var T=function(n,o,i){var u=e((function(){return!a(n)}),[]),c=h(t(i),2),v=c[0],d=c[1],s=function(n){null==o||o(n),u||d(n.target.value)};return r((function(){a(n)&&a(i)&&l.error("Warning: `useControlledValue` hook is being used without a value or initialValue. This will cause a React warning when the input changes. Please decide between using a controlled or uncontrolled input element, and provide either a controlledValue or initialValue to `useControlledValue`")}),[n,i]),{isControlled:u,value:u?n:v,handleChange:s,setUncontrolledValue:d,updateValue:function(n,r){if(r.current){r.current.value=n;var t=f(new Event("change",{cancelable:!0,bubbles:!0}),r.current);s(t)}}}};function V(r){var t=null==r?void 0:r.prefix;return n.useMemo((function(){var r=function(r){return function(t){if(t){if(r.get(t))return r.get(t);var e=n.createRef();return r.set(t,e),e}l.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return r}),t?[t]:[])}var x=function(n,r){return E("keydown",(function(r){return function(n,r){27===n.keyCode&&(n.stopImmediatePropagation(),r())}(r,n)}),r)};function I(){var n=h(t({}),2)[1];return i((function(){return n({})}),[])}function k(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=r.initialValue,i=r.deps,u=void 0===i?[]:i,c=o(t);return e((function(){return{get current(){return c.current},set current(r){c.current=r,n(r)}}}),[n,c].concat(g(u)))}function D(n,r){var t=i((function(n,r){Array.isArray(n)?n.forEach(t):"function"==typeof n?n(r):n&&(n.current=r)}),[]);return k(i((function(r){return t(n,r)}),[n,t]),{initialValue:r})}var M=0;function R(n){var e=n.prefix;return function(n){var e=n.id,o=n.prefix,i=h(t(e),2),u=i[0],c=i[1];return r((function(){null==u&&c(M+=1)}),[u,o]),e||"".concat(null!=o?o:"lg","-").concat(u)}({id:n.id,prefix:e})}var L=function(){return("undefined"==typeof window?r:u).apply(void 0,arguments)};function B(n,e,o){var i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=h(t(),2),c=u[0],a=u[1];return r((function(){if(i){var r=new MutationObserver((function(){a(o.apply(void 0,arguments))}));return n&&r.observe(n,e),function(){return r.disconnect()}}}),[n,e,o,i]),c}function U(n){var r=o();return void 0!==r.current&&v(r.current,n)||(r.current=n),r.current}function z(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=e.interval,u=void 0===i?3e4:i,c=e.immediate,a=void 0===c||c,l=e.enabled,f=void 0===l||l,v=o(),d=function(){var n=function(){return"visible"===document.visibilityState},e=h(t(!0),2),o=e[0],i=e[1];return r((function(){i(n)}),[]),E("visibilitychange",(function(){i(n)})),o}()&&f;r((function(){v.current=n})),r((function(){var n;if(d)return a?e():r(),t;function r(){t(),n=setTimeout(e,u)}function t(){clearTimeout(n)}function e(){var n;Promise.resolve(null===(n=v.current)||void 0===n?void 0:n.call(v)).finally(r)}}),[u,a,d])}function H(n){var t=o();return r((function(){t.current=n})),t.current}function W(n){var r=h(t(n),2),e=r[0],u=r[1],c=o(e);return[e,i((function(n){u(n),c.current=n}),[u]),i((function(){return c.current}),[])]}function q(n){var r=h(t(!1),2),e=r[0],o=r[1];if(a(n)||"function"!=typeof n)return{onBlur:function(){},onChange:function(){}};return{onBlur:function(r){o(!0),null==n||n(r.target.value)},onChange:function(r){e&&(null==n||n(r.target.value))}}}export{d as useAutoScroll,A as useAvailableSpace,C as useBackdropClick,T as useControlledValue,V as useDynamicRefs,x as useEscapeKey,E as useEventListener,I as useForceRerender,D as useForwardedRef,R as useIdAllocator,L as useIsomorphicLayoutEffect,B as useMutationObserver,U as useObjectDependency,k as useObservedRef,z as usePoller,H as usePrevious,j as useSsrCheck,W as useStateRef,q as useValidation,P as useViewportSize}; | ||
//# sourceMappingURL=index.js.map |
@@ -16,2 +16,3 @@ export { useAutoScroll } from './useAutoScroll'; | ||
export { default as usePrevious } from './usePrevious'; | ||
export { default as useSsrCheck } from './useSsrCheck'; | ||
export { useStateRef } from './useStateRef'; | ||
@@ -18,0 +19,0 @@ export { default as useValidation } from './useValidation'; |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("lodash/debounce"),require("lodash/isUndefined"),require("@leafygreen-ui/lib"),require("lodash/isEqual")):"function"==typeof define&&define.amd?define(["exports","react","lodash/debounce","lodash/isUndefined","@leafygreen-ui/lib","lodash/isEqual"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/hooks"]={},e.React,e["/debounce"],e["/isUndefined"],e["@leafygreen-ui/lib"],e["/isEqual"])}(this,(function(e,t,n,r,o,u){"use strict";function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function c(e){if(e&&"object"==typeof e&&"default"in e)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var a=c(t),f=i(n),l=i(r),s=i(u);function d(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function v(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?d(Object(n),!0).forEach((function(t){b(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):d(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function p(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t);if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function b(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function h(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,u,i,c=[],a=!0,f=!1;try{if(u=(n=n.call(e)).next,0===t);else for(;!(a=(r=u.call(n)).done)&&(c.push(r.value),c.length!==t);a=!0);}catch(e){f=!0,o=e}finally{try{if(!a&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(f)throw o}}return c}}(e,t)||g(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function m(e){return function(e){if(Array.isArray(e))return w(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||g(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(e,t){if(e){if("string"==typeof e)return w(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?w(e,t):void 0}}function w(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function E(){return{width:window.innerWidth,height:window.innerHeight}}function O(){var e=h(t.useState(E()),2),n=e[0],r=e[1];return t.useEffect((function(){var e=f.default((function(){return r(E())}),100);return window.addEventListener("resize",e),function(){return window.removeEventListener("resize",e)}}),[]),n}function S(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=r.options,u=r.enabled,i=void 0===u||u,c=r.dependencies,a=void 0===c?[i,e]:c,f=r.element,l=t.useRef((function(){}));t.useEffect((function(){l.current=n}),[n]),t.useEffect((function(){if(!1!==i){if("once"===i||!0===i){var t=function(e){l.current(e)},n=v(v({},o),{},{once:"once"===i});return(null!=f?f:document).addEventListener(e,t,n),function(){(null!=f?f:document).removeEventListener(e,t,n)}}console.error("Received value of type ".concat(y(i)," for property `enabled`. Expected a boolean."))}}),a)}function j(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.initialValue,o=n.deps,u=void 0===o?[]:o,i=t.useRef(r);return t.useMemo((function(){return{get current(){return i.current},set current(t){i.current=t,e(t)}}}),[e,i].concat(m(u)))}var P=0;e.useAutoScroll=function(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;t.useEffect((function(){if(e&&e.current&&n&&n.current){var t=e.current.offsetTop,o=n.current,u=o.scrollTop;(t>o.offsetHeight||t<u)&&n.current.scrollTo({top:t-r,behavior:"smooth"})}}),[n,e,r])},e.useAvailableSpace=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,r=O();return t.useMemo((function(){if(r&&e&&e.current){var t=e.current.getBoundingClientRect(),o=t.top,u=t.bottom;return Math.max(r.height-u,o)-n}}),[r,e,n])},e.useBackdropClick=function(e,t){var n=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];function r(e){var n,r=e.target;return Array.isArray(t)?t.some((function(e){var t;return null===(t=e.current)||void 0===t?void 0:t.contains(r)})):(null===(n=t.current)||void 0===n?void 0:n.contains(r))||!1}S("mousedown",(function(e){r(e)||(e.preventDefault(),e.stopPropagation())}),{enabled:n}),S("click",(function(t){r(t)||(t.stopPropagation(),e())}),{options:{capture:!0},enabled:n})},e.useControlledValue=function(e,n,r){var u=t.useMemo((function(){return!l.default(e)}),[]),i=h(t.useState(r),2),c=i[0],a=i[1],f=function(e){null==n||n(e),u||a(e.target.value)};return t.useEffect((function(){l.default(e)&&l.default(r)&&o.consoleOnce.error("Warning: `useControlledValue` hook is being used without a value or initialValue. This will cause a React warning when the input changes. Please decide between using a controlled or uncontrolled input element, and provide either a controlledValue or initialValue to `useControlledValue`")}),[e,r]),{isControlled:u,value:u?e:c,handleChange:f,setUncontrolledValue:a,updateValue:function(e,t){if(t.current){t.current.value=e;var n=o.createSyntheticEvent(new Event("change",{cancelable:!0,bubbles:!0}),t.current);f(n)}}}},e.useDynamicRefs=function(e){var t=null==e?void 0:e.prefix;return a.useMemo((function(){var e=function(e){return function(t){if(t){if(e.get(t))return e.get(t);var n=a.createRef();return e.set(t,n),n}o.consoleOnce.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return e}),t?[t]:[])},e.useEscapeKey=function(e,t){return S("keydown",(function(t){return function(e,t){27===e.keyCode&&(e.stopImmediatePropagation(),t())}(t,e)}),t)},e.useEventListener=S,e.useForceRerender=function(){var e=h(t.useState({}),2)[1];return t.useCallback((function(){return e({})}),[])},e.useForwardedRef=function(e,n){var r=t.useCallback((function(e,t){Array.isArray(e)?e.forEach(r):"function"==typeof e?e(t):e&&(e.current=t)}),[]);return j(t.useCallback((function(t){return r(e,t)}),[e,r]),{initialValue:n})},e.useIdAllocator=function(e){var n=e.prefix;return function(e){var n=e.id,r=e.prefix,o=h(t.useState(n),2),u=o[0],i=o[1];return t.useEffect((function(){null==u&&i(P+=1)}),[u,r]),n||"".concat(null!=r?r:"lg","-").concat(u)}({id:e.id,prefix:n})},e.useIsomorphicLayoutEffect=function(){return("undefined"==typeof window?t.useEffect:t.useLayoutEffect).apply(void 0,arguments)},e.useMutationObserver=function(e,n,r){var o=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=h(t.useState(),2),i=u[0],c=u[1];return t.useEffect((function(){if(o){var t=new MutationObserver((function(){c(r.apply(void 0,arguments))}));return e&&t.observe(e,n),function(){return t.disconnect()}}}),[e,n,r,o]),i},e.useObjectDependency=function(e){var n=t.useRef();return void 0!==n.current&&s.default(n.current,e)||(n.current=e),n.current},e.useObservedRef=j,e.usePoller=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.interval,o=void 0===r?3e4:r,u=n.immediate,i=void 0===u||u,c=n.enabled,a=void 0===c||c,f=t.useRef(),l=function(){var e=function(){return"visible"===document.visibilityState},n=h(t.useState(!0),2),r=n[0],o=n[1];return t.useEffect((function(){o(e)}),[]),S("visibilitychange",(function(){o(e)})),r}()&&a;t.useEffect((function(){f.current=e})),t.useEffect((function(){var e;if(l)return i?r():t(),n;function t(){n(),e=setTimeout(r,o)}function n(){clearTimeout(e)}function r(){var e;Promise.resolve(null===(e=f.current)||void 0===e?void 0:e.call(f)).finally(t)}}),[o,i,l])},e.usePrevious=function(e){var n=t.useRef();return t.useEffect((function(){n.current=e})),n.current},e.useStateRef=function(e){var n=h(t.useState(e),2),r=n[0],o=n[1],u=t.useRef(r);return[r,t.useCallback((function(e){o(e),u.current=e}),[o]),t.useCallback((function(){return u.current}),[])]},e.useValidation=function(e){var n=h(t.useState(!1),2),r=n[0],o=n[1];return l.default(e)||"function"!=typeof e?{onBlur:function(){},onChange:function(){}}:{onBlur:function(t){o(!0),null==e||e(t.target.value)},onChange:function(t){r&&(null==e||e(t.target.value))}}},e.useViewportSize=O})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("lodash/debounce"),require("lodash/isUndefined"),require("@leafygreen-ui/lib"),require("lodash/isEqual")):"function"==typeof define&&define.amd?define(["exports","react","lodash/debounce","lodash/isUndefined","@leafygreen-ui/lib","lodash/isEqual"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/hooks"]={},e.React,e["/debounce"],e["/isUndefined"],e["@leafygreen-ui/lib"],e["/isEqual"])}(this,(function(e,n,t,r,o,u){"use strict";function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function c(e){if(e&&"object"==typeof e&&"default"in e)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:!0,get:function(){return e[t]}})}})),n.default=e,Object.freeze(n)}var a=c(n),f=i(t),l=i(r),s=i(u);function d(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function v(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?d(Object(t),!0).forEach((function(n){b(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):d(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function p(e){var n=function(e,n){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n);if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof n?n:n+""}function y(e){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},y(e)}function b(e,n,t){return(n=p(n))in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function h(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var r,o,u,i,c=[],a=!0,f=!1;try{if(u=(t=t.call(e)).next,0===n);else for(;!(a=(r=u.call(t)).done)&&(c.push(r.value),c.length!==n);a=!0);}catch(e){f=!0,o=e}finally{try{if(!a&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(f)throw o}}return c}}(e,n)||g(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function m(e){return function(e){if(Array.isArray(e))return w(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||g(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(e,n){if(e){if("string"==typeof e)return w(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?w(e,n):void 0}}function w(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}function E(){var e=h(n.useState("undefined"==typeof window),2),t=e[0],r=e[1];return n.useEffect((function(){r(!1)}),[]),t}function O(){return{width:window.innerWidth,height:window.innerHeight}}function S(){var e=E(),t=h(n.useState(e?null:O()),2),r=t[0],o=t[1];return n.useEffect((function(){var e=f.default((function(){return o(O())}),100);return window.addEventListener("resize",e),function(){return window.removeEventListener("resize",e)}}),[]),r}function j(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=r.options,u=r.enabled,i=void 0===u||u,c=r.dependencies,a=void 0===c?[i,e]:c,f=r.element,l=n.useRef((function(){}));n.useEffect((function(){l.current=t}),[t]),n.useEffect((function(){if(!1!==i){if("once"===i||!0===i){var n=function(e){l.current(e)},t=v(v({},o),{},{once:"once"===i});return(null!=f?f:document).addEventListener(e,n,t),function(){(null!=f?f:document).removeEventListener(e,n,t)}}console.error("Received value of type ".concat(y(i)," for property `enabled`. Expected a boolean."))}}),a)}function P(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.initialValue,o=t.deps,u=void 0===o?[]:o,i=n.useRef(r);return n.useMemo((function(){return{get current(){return i.current},set current(n){i.current=n,e(n)}}}),[e,i].concat(m(u)))}var C=0;e.useAutoScroll=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;n.useEffect((function(){if(e&&e.current&&t&&t.current){var n=e.current.offsetTop,o=t.current,u=o.scrollTop;(n>o.offsetHeight||n<u)&&t.current.scrollTo({top:n-r,behavior:"smooth"})}}),[t,e,r])},e.useAvailableSpace=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,r=S();return n.useMemo((function(){if(r&&e&&e.current){var n=e.current.getBoundingClientRect(),o=n.top,u=n.bottom;return Math.max(r.height-u,o)-t}}),[r,e,t])},e.useBackdropClick=function(e,n){var t=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];function r(e){var t,r=e.target;return Array.isArray(n)?n.some((function(e){var n;return null===(n=e.current)||void 0===n?void 0:n.contains(r)})):(null===(t=n.current)||void 0===t?void 0:t.contains(r))||!1}j("mousedown",(function(e){r(e)||(e.preventDefault(),e.stopPropagation())}),{enabled:t}),j("click",(function(n){r(n)||(n.stopPropagation(),e())}),{options:{capture:!0},enabled:t})},e.useControlledValue=function(e,t,r){var u=n.useMemo((function(){return!l.default(e)}),[]),i=h(n.useState(r),2),c=i[0],a=i[1],f=function(e){null==t||t(e),u||a(e.target.value)};return n.useEffect((function(){l.default(e)&&l.default(r)&&o.consoleOnce.error("Warning: `useControlledValue` hook is being used without a value or initialValue. This will cause a React warning when the input changes. Please decide between using a controlled or uncontrolled input element, and provide either a controlledValue or initialValue to `useControlledValue`")}),[e,r]),{isControlled:u,value:u?e:c,handleChange:f,setUncontrolledValue:a,updateValue:function(e,n){if(n.current){n.current.value=e;var t=o.createSyntheticEvent(new Event("change",{cancelable:!0,bubbles:!0}),n.current);f(t)}}}},e.useDynamicRefs=function(e){var n=null==e?void 0:e.prefix;return a.useMemo((function(){var e=function(e){return function(n){if(n){if(e.get(n))return e.get(n);var t=a.createRef();return e.set(n,t),t}o.consoleOnce.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return e}),n?[n]:[])},e.useEscapeKey=function(e,n){return j("keydown",(function(n){return function(e,n){27===e.keyCode&&(e.stopImmediatePropagation(),n())}(n,e)}),n)},e.useEventListener=j,e.useForceRerender=function(){var e=h(n.useState({}),2)[1];return n.useCallback((function(){return e({})}),[])},e.useForwardedRef=function(e,t){var r=n.useCallback((function(e,n){Array.isArray(e)?e.forEach(r):"function"==typeof e?e(n):e&&(e.current=n)}),[]);return P(n.useCallback((function(n){return r(e,n)}),[e,r]),{initialValue:t})},e.useIdAllocator=function(e){var t=e.prefix;return function(e){var t=e.id,r=e.prefix,o=h(n.useState(t),2),u=o[0],i=o[1];return n.useEffect((function(){null==u&&i(C+=1)}),[u,r]),t||"".concat(null!=r?r:"lg","-").concat(u)}({id:e.id,prefix:t})},e.useIsomorphicLayoutEffect=function(){return("undefined"==typeof window?n.useEffect:n.useLayoutEffect).apply(void 0,arguments)},e.useMutationObserver=function(e,t,r){var o=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=h(n.useState(),2),i=u[0],c=u[1];return n.useEffect((function(){if(o){var n=new MutationObserver((function(){c(r.apply(void 0,arguments))}));return e&&n.observe(e,t),function(){return n.disconnect()}}}),[e,t,r,o]),i},e.useObjectDependency=function(e){var t=n.useRef();return void 0!==t.current&&s.default(t.current,e)||(t.current=e),t.current},e.useObservedRef=P,e.usePoller=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.interval,o=void 0===r?3e4:r,u=t.immediate,i=void 0===u||u,c=t.enabled,a=void 0===c||c,f=n.useRef(),l=function(){var e=function(){return"visible"===document.visibilityState},t=h(n.useState(!0),2),r=t[0],o=t[1];return n.useEffect((function(){o(e)}),[]),j("visibilitychange",(function(){o(e)})),r}()&&a;n.useEffect((function(){f.current=e})),n.useEffect((function(){var e;if(l)return i?r():n(),t;function n(){t(),e=setTimeout(r,o)}function t(){clearTimeout(e)}function r(){var e;Promise.resolve(null===(e=f.current)||void 0===e?void 0:e.call(f)).finally(n)}}),[o,i,l])},e.usePrevious=function(e){var t=n.useRef();return n.useEffect((function(){t.current=e})),t.current},e.useSsrCheck=E,e.useStateRef=function(e){var t=h(n.useState(e),2),r=t[0],o=t[1],u=n.useRef(r);return[r,n.useCallback((function(e){o(e),u.current=e}),[o]),n.useCallback((function(){return u.current}),[])]},e.useValidation=function(e){var t=h(n.useState(!1),2),r=t[0],o=t[1];return l.default(e)||"function"!=typeof e?{onBlur:function(){},onChange:function(){}}:{onBlur:function(n){o(!0),null==e||e(n.target.value)},onChange:function(n){r&&(null==e||e(n.target.value))}}},e.useViewportSize=S})); | ||
//# sourceMappingURL=index.js.map |
@@ -5,4 +5,4 @@ interface ViewportSize { | ||
} | ||
export default function useViewportSize(): ViewportSize; | ||
export default function useViewportSize(): ViewportSize | null; | ||
export {}; | ||
//# sourceMappingURL=useViewportSize.d.ts.map |
{ | ||
"name": "@leafygreen-ui/hooks", | ||
"version": "8.1.4", | ||
"version": "8.2.0", | ||
"description": "LeafyGreen UI Kit Custom Hooks", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -16,4 +16,5 @@ export { useAutoScroll } from './useAutoScroll'; | ||
export { default as usePrevious } from './usePrevious'; | ||
export { default as useSsrCheck } from './useSsrCheck'; | ||
export { useStateRef } from './useStateRef'; | ||
export { default as useValidation } from './useValidation'; | ||
export { default as useViewportSize } from './useViewportSize'; |
import { useEffect, useState } from 'react'; | ||
import debounce from 'lodash/debounce'; | ||
import useSsrCheck from './useSsrCheck'; | ||
interface ViewportSize { | ||
@@ -16,5 +18,7 @@ width: number; | ||
export default function useViewportSize(): ViewportSize { | ||
const [viewportSize, setViewportUpdateVal] = useState<ViewportSize>( | ||
getViewportSize(), | ||
export default function useViewportSize(): ViewportSize | null { | ||
const isSsr = useSsrCheck(); | ||
const [viewportSize, setViewportUpdateVal] = useState<ViewportSize | null>( | ||
isSsr ? null : getViewportSize(), // window undefined on server | ||
); | ||
@@ -28,4 +32,4 @@ | ||
// useEffect callback only runs on client, so safe to assume window is defined here | ||
window.addEventListener('resize', calcResize); | ||
return () => window.removeEventListener('resize', calcResize); | ||
@@ -32,0 +36,0 @@ }, []); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
182029
86
1901