UNPKG

@leafygreen-ui/hooks

Version:
3 lines (2 loc) 9.6 kB
import*as n from"react";import{useEffect as e,useState as t,useRef as r,useMemo as o,useCallback as i,useLayoutEffect as u}from"react";import a from"lodash/debounce";import{consoleOnce as c,createSyntheticEvent as l}from"@leafygreen-ui/lib";import f from"lodash/isEqual";import v from"lodash/isUndefined";import{breakpoints as d}from"@leafygreen-ui/tokens";var s=function(n,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;e(function(){if(n&&n.current&&t&&t.current){var e=n.current.offsetTop,o=t.current,i=o.scrollTop;(e>o.offsetHeight||e<i)&&t.current.scrollTo({top:e-r,behavior:"smooth"})}},[t,n,r])};function p(n,e){(null==e||e>n.length)&&(e=n.length);for(var t=0,r=Array(e);t<e;t++)r[t]=n[t];return r}function y(n,e,t){return(e=function(n){var e=function(n,e){if("object"!=typeof n||!n)return n;var t=n[Symbol.toPrimitive];if(void 0!==t){var r=t.call(n,e);if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(n)}(n,"string");return"symbol"==typeof e?e:e+""}(e))in n?Object.defineProperty(n,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[e]=t,n}function b(n,e){var t=Object.keys(n);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(n);e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable})),t.push.apply(t,r)}return t}function m(n){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{};e%2?b(Object(t),!0).forEach(function(e){y(n,e,t[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(t)):b(Object(t)).forEach(function(e){Object.defineProperty(n,e,Object.getOwnPropertyDescriptor(t,e))})}return n}function g(n,e){return function(n){if(Array.isArray(n))return n}(n)||function(n,e){var t=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=t){var r,o,i,u,a=[],c=!0,l=!1;try{if(i=(t=t.call(n)).next,0===e);else for(;!(c=(r=i.call(t)).done)&&(a.push(r.value),a.length!==e);c=!0);}catch(n){l=!0,o=n}finally{try{if(!c&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(l)throw o}}return a}}(n,e)||O(n,e)||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 h(n){return function(n){if(Array.isArray(n))return p(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||O(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){return w="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},w(n)}function O(n,e){if(n){if("string"==typeof n)return p(n,e);var t={}.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)?p(n,e):void 0}}function P(){var n=g(t("undefined"==typeof window),2),r=n[0],o=n[1];return e(function(){o(!1)},[]),r}function S(){return{width:window.innerWidth,height:window.innerHeight}}function j(){var n=P(),r=g(t(n?null:S()),2),o=r[0],i=r[1];return e(function(){var n=a(function(){return i(S())},100);return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}},[]),o}var E=function(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,t=j();if(t&&n&&n.current){var r=n.current.getBoundingClientRect(),o=r.top,i=r.bottom;return Math.max(t.height-i,o)-e}};function A(n,t){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=o.options,u=o.enabled,a=void 0===u||u,c=o.dependencies,l=void 0===c?[a,n]:c,f=o.element,v=r(function(){});e(function(){v.current=t},[t]),e(function(){if(!1!==a){if("once"===a||!0===a){var e=function(n){v.current(n)},t=m(m({},i),{},{once:"once"===a});return(null!=f?f:document).addEventListener(n,e,t),function(){(null!=f?f:document).removeEventListener(n,e,t)}}console.error("Received value of type ".concat(w(a)," for property `enabled`. Expected a boolean."))}},l)}function C(n,e){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{enabled:!0,allowPropagation:!1},r="boolean"==typeof t?{enabled:t,allowPropagation:!1}:t,o=r.enabled,i=r.allowPropagation;function u(n){var t,r=n.target;return Array.isArray(e)?e.some(function(n){var e;return null===(e=n.current)||void 0===e?void 0:e.contains(r)}):(null===(t=e.current)||void 0===t?void 0:t.contains(r))||!1}"boolean"==typeof t&&c.warn("useBackdropClick: The 'enabled' boolean argument is deprecated. Please use the 'options' object argument instead."),A("mousedown",function(n){u(n)||i||(n.preventDefault(),n.stopPropagation())},{enabled:o}),A("click",function(e){u(e)||(i||e.stopPropagation(),n(e))},{options:{capture:!0},enabled:o})}var T=function(n,r,i){var u=o(function(){return!v(n)},[]),a=g(t(v(n)?i:n),2),l=a[0],f=a[1],d=o(function(){return u?n:l},[u,l,n]);return e(function(){v(n)&&v(i)&&c.error("Warning: `useControlled` hook is being used without a value or initialValue. If using an input, this will cause a React warning when an input changes. Please decide between using a controlled or uncontrolled element, and provide either a controlledValue or initialValue to `useControlled`")},[n,i]),{isControlled:u,value:d,updateValue:function(n){if(u){var e="function"==typeof n?n(d):n;null==r||r(e)}else"function"==typeof n?f(function(e){var t=n(e);return null==r||r(t),t}):(f(n),null==r||r(n))},setUncontrolledValue:f}},V=function(n,e,t){var r=T(n,void 0,t),o=r.isControlled,i=r.value,u=r.setUncontrolledValue,a=function(n){null==e||e(n),o||u(n.target.value)};return{isControlled:o,value:i,handleChange:a,setUncontrolledValue:u,updateValue:function(n,e){if(e.current){e.current.value=n;var t=l(new Event("change",{cancelable:!0,bubbles:!0}),e.current);a(t)}}}};function k(e){var t=null==e?void 0:e.prefix;return n.useMemo(function(){var e=function(e){return function(t){if(t){if(e.get(t))return e.get(t);var r=n.createRef();return e.set(t,r),r}c.error("`useDynamicRefs`: Cannot get ref without key")}}(new Map);return e},t?[t]:[])}var I=function(n,e){return A("keydown",function(e){return function(n,e){27===n.keyCode&&(n.stopImmediatePropagation(),e())}(e,n)},e)},x=function(n){var t=r(!0);return e(function(){t.current&&(t.current=!1,null==n||n())},[n]),t.current};function M(){var n=g(t({}),2)[1];return i(function(){return n({})},[])}function D(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=e.initialValue,i=e.deps,u=void 0===i?[]:i,a=r(t);return o(function(){return{get current(){return a.current},set current(e){a.current=e,n(e)}}},[n,a].concat(h(u)))}function L(n,e){var t=i(function(n,e){Array.isArray(n)?n.forEach(t):"function"==typeof n?n(e):n&&(n.current=e)},[]);return D(i(function(e){return t(n,e)},[n,t]),{initialValue:e})}var R=0;function U(n){var r=n.prefix;return function(n){var r=n.id,o=n.prefix,i=g(t(r),2),u=i[0],a=i[1];return e(function(){null==u&&a(R+=1)},[u,o]),r||"".concat(null!=o?o:"lg","-").concat(u)}({id:n.id,prefix:r})}var B=function(){return("undefined"==typeof window?e:u).apply(void 0,arguments)};function z(e){return n.useMemo(function(){return e.every(function(n){return null==n})?null:function(n){e.forEach(function(e){"function"==typeof e?e(n):null!=e&&(e.current=n)})}},e)}var H=function(){var n=j();return{isMobileSize:!(null==n||!n.width)&&n.width<=d.Tablet}};function W(n,r,o){var i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=g(t(),2),a=u[0],c=u[1];return e(function(){if(i){var e=new MutationObserver(function(){c(o.apply(void 0,arguments))});return n&&e.observe(n,r),function(){return e.disconnect()}}},[n,r,o,i]),a}function q(n){var e=r();return void 0!==e.current&&f(e.current,n)||(e.current=n),e.current}function K(n){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=o.interval,u=void 0===i?3e4:i,a=o.immediate,c=void 0===a||a,l=o.enabled,f=void 0===l||l,v=r(),d=function(){var n=function(){return"visible"===document.visibilityState},r=g(t(!0),2),o=r[0],i=r[1];return e(function(){i(n)},[]),A("visibilitychange",function(){i(n)}),o}()&&f;e(function(){v.current=n}),e(function(){var n;if(d)return c?r():e(),t;function e(){t(),n=setTimeout(r,u)}function t(){clearTimeout(n)}function r(){var n;Promise.resolve(null===(n=v.current)||void 0===n?void 0:n.call(v)).finally(e)}},[u,c,d])}function $(n){var t=r();return e(function(){t.current=n}),t.current}function F(n){var e=g(t(n),2),o=e[0],u=e[1],a=r(o);return[o,i(function(n){u(n),a.current=n},[u]),i(function(){return a.current},[])]}function G(n){var e=g(t(!1),2),r=e[0],o=e[1];if(v(n)||"function"!=typeof n)return{onBlur:function(){},onChange:function(){}};return{onBlur:function(e){o(!0),null==n||n(e.target.value)},onChange:function(e){r&&(null==n||n(e.target.value))}}}export{s as useAutoScroll,E as useAvailableSpace,C as useBackdropClick,T as useControlled,V as useControlledValue,k as useDynamicRefs,I as useEscapeKey,A as useEventListener,x as useFirstRender,M as useForceRerender,L as useForwardedRef,U as useIdAllocator,B as useIsomorphicLayoutEffect,z as useMergeRefs,H as useMobile,W as useMutationObserver,q as useObjectDependency,D as useObservedRef,K as usePoller,$ as usePrevious,P as useSsrCheck,F as useStateRef,G as useValidation,j as useViewportSize}; //# sourceMappingURL=index.js.map