UNPKG

@leafygreen-ui/hooks

Version:
3 lines (2 loc) 8.76 kB
import*as n from"react";import{useEffect as r,useState as t,useRef as e,useMemo as o,useCallback as i,useLayoutEffect as u}from"react";import c from"lodash/debounce";import{consoleOnce as a,createSyntheticEvent as l}from"@leafygreen-ui/lib";import f from"lodash/isEqual";import v from"lodash/isUndefined";var s=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 d(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?d(Object(t),!0).forEach((function(r){b(n,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(t)):d(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 m(n){return m="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},m(n)}function b(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 E(){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 P=function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,t=E();if(t&&n&&n.current){var e=n.current.getBoundingClientRect(),o=e.top,i=e.bottom;return Math.max(t.height-i,o)-r}};function A(n,t){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=o.options,u=o.enabled,c=void 0===u||u,a=o.dependencies,l=void 0===a?[c,n]:a,f=o.element,v=e((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(m(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}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 T=function(n,e,i){var u=o((function(){return!v(n)}),[]),c=h(t(i),2),f=c[0],s=c[1],d=function(n){null==e||e(n),u||s(n.target.value)};return r((function(){v(n)&&v(i)&&a.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:f,handleChange:d,setUncontrolledValue:s,updateValue:function(n,r){if(r.current){r.current.value=n;var t=l(new Event("change",{cancelable:!0,bubbles:!0}),r.current);d(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}a.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return r}),t?[t]:[])}var x=function(n,r){return A("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=e(t);return o((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 L(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 R=function(){return("undefined"==typeof window?r:u).apply(void 0,arguments)};function B(r){return n.useMemo((function(){return r.every((function(n){return null==n}))?null:function(n){r.forEach((function(r){"function"==typeof r?r(n):null!=r&&(r.current=n)}))}}),r)}function U(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 z(n){var r=e();return void 0!==r.current&&f(r.current,n)||(r.current=n),r.current}function H(n){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=o.interval,u=void 0===i?3e4:i,c=o.immediate,a=void 0===c||c,l=o.enabled,f=void 0===l||l,v=e(),s=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(s)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,s])}function W(n){var t=e();return r((function(){t.current=n})),t.current}function q(n){var r=h(t(n),2),o=r[0],u=r[1],c=e(o);return[o,i((function(n){u(n),c.current=n}),[u]),i((function(){return c.current}),[])]}function K(n){var r=h(t(!1),2),e=r[0],o=r[1];if(v(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{s as useAutoScroll,P as useAvailableSpace,C as useBackdropClick,T as useControlledValue,V as useDynamicRefs,x as useEscapeKey,A as useEventListener,I as useForceRerender,D as useForwardedRef,L as useIdAllocator,R as useIsomorphicLayoutEffect,B as useMergeRefs,U as useMutationObserver,z as useObjectDependency,k as useObservedRef,H as usePoller,W as usePrevious,j as useSsrCheck,q as useStateRef,K as useValidation,E as useViewportSize}; //# sourceMappingURL=index.js.map