@leafygreen-ui/hooks
Advanced tools
Comparing version 7.1.1 to 7.2.0
# @leafygreen-ui/hooks | ||
## 7.2.0 | ||
### Minor Changes | ||
- 63ddf39: - Adds useForwardedRef hook | ||
- Adds useDynamicRefs hook | ||
## 7.1.1 | ||
@@ -4,0 +11,0 @@ |
@@ -1,2 +0,2 @@ | ||
import{useRef as n,useEffect as t,useState as e,useLayoutEffect as r}from"react";import o from"lodash/debounce";import i from"lodash/isEqual";import u from"lodash/isUndefined";function c(n,t){var e=Object.keys(n);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(n);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),e.push.apply(e,r)}return e}function f(n){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?c(Object(e),!0).forEach((function(t){l(n,t,e[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(e)):c(Object(e)).forEach((function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(e,t))}))}return n}function a(n){return(a="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})(n)}function l(n,t,e){return t in n?Object.defineProperty(n,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):n[t]=e,n}function v(n,t){return function(n){if(Array.isArray(n))return n}(n)||function(n,t){var e=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null==e)return;var r,o,i=[],u=!0,c=!1;try{for(e=e.call(n);!(u=(r=e.next()).done)&&(i.push(r.value),!t||i.length!==t);u=!0);}catch(n){c=!0,o=n}finally{try{u||null==e.return||e.return()}finally{if(c)throw o}}return i}(n,t)||function(n,t){if(!n)return;if("string"==typeof n)return d(n,t);var e=Object.prototype.toString.call(n).slice(8,-1);"Object"===e&&n.constructor&&(e=n.constructor.name);if("Map"===e||"Set"===e)return Array.from(n);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return d(n,t)}(n,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 d(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}function p(e,r){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=o.options,u=o.enabled,c=void 0===u||u,l=o.dependencies,v=void 0===l?[c,e]:l,d=o.element,p=n((function(){}));t((function(){p.current=r}),[r]),t((function(){if(!1!==c){if("once"===c||!0===c){var n=function(n){return p.current(n)},t=f(f({},i),{},{once:"once"===c});return(null!=d?d:document).addEventListener(e,n,t),function(){(null!=d?d:document).removeEventListener(e,n,t)}}console.error("Received value of type ".concat(a(c)," for property `enabled`. Expected a boolean."))}}),v)}function s(n,r,o){var i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=e(),c=v(u,2),f=c[0],a=c[1];return t((function(){if(i){var t=new MutationObserver((function(){a(o.apply(void 0,arguments))}));return n&&t.observe(n,r),function(){return t.disconnect()}}}),[n,r,o,i]),f}function y(){return{width:window.innerWidth,height:window.innerHeight}}function b(){var n=v(e(null),2),r=n[0],i=n[1];return t((function(){i(y());var n=o((function(){return i(y())}),100);return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}}),[]),r}var m=function(n,t){return p("keydown",(function(t){return function(n,t){27===n.keyCode&&(n.stopImmediatePropagation(),t())}(t,n)}),t)};function h(){var n=function(){return"visible"===document.visibilityState},r=v(e(!0),2),o=r[0],i=r[1];return t((function(){i(n)}),[]),p("visibilitychange",(function(){i(n)})),o}function g(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.interval,i=void 0===o?3e4:o,u=r.immediate,c=void 0===u||u,f=r.enabled,a=void 0===f||f,l=n(),v=h(),d=v&&a;t((function(){l.current=e})),t((function(){var n;if(d)return c?r():t(),e;function t(){e(),n=setTimeout(r,i)}function e(){clearTimeout(n)}function r(){var n;Promise.resolve(null===(n=l.current)||void 0===n?void 0:n.call(l)).finally(t)}}),[i,c,d])}function w(e){var r=n();return t((function(){r.current=e})),r.current}function O(t){var e=n();return void 0!==e.current&&i(e.current,t)||(e.current=t),e.current}var j=function(){var n="undefined"==typeof window?t:r;return n.apply(void 0,arguments)},S=!1,P=new Map,E=function(n){if(P.has(n)){var t=P.get(n),e=t?t+1:1;return P.set(n,e),e}return P.set(n,1),1};function A(n){var r=n.prefix,o=n.id,i=r&&(S?E(r):null),u=v(e(i),2),c=u[0],f=u[1];return j((function(){r&&null===c&&f(E(r))}),[]),t((function(){!1===S&&(S=!0)}),[]),o||"".concat(r,"-").concat(c)}function x(n){var t=v(e(!1),2),r=t[0],o=t[1];if(u(n)||"function"!=typeof n)return{onBlur:function(){},onChange:function(){}};return{onBlur:function(t){o(!0),null==n||n(t.target.value)},onChange:function(t){r&&(null==n||n(t.target.value))}}}export{m as useEscapeKey,p as useEventListener,A as useIdAllocator,j as useIsomorphicLayoutEffect,s as useMutationObserver,O as useObjectDependency,g as usePoller,w as usePrevious,x as useValidation,b as useViewportSize}; | ||
import{useRef as n,useEffect as t,useState as r,useLayoutEffect as e,createRef as o,useMemo as i,useCallback as u}from"react";import c from"lodash/debounce";import a from"lodash/isEqual";import f from"lodash/isUndefined";function l(n,t){var r=Object.keys(n);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(n);t&&(e=e.filter((function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),r.push.apply(r,e)}return r}function v(n){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?l(Object(r),!0).forEach((function(t){y(n,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach((function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(r,t))}))}return n}function d(n){return(d="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})(n)}function y(n,t,r){return t in n?Object.defineProperty(n,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):n[t]=r,n}function s(n,t){return function(n){if(Array.isArray(n))return n}(n)||function(n,t){var r=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null==r)return;var e,o,i=[],u=!0,c=!1;try{for(r=r.call(n);!(u=(e=r.next()).done)&&(i.push(e.value),!t||i.length!==t);u=!0);}catch(n){c=!0,o=n}finally{try{u||null==r.return||r.return()}finally{if(c)throw o}}return i}(n,t)||b(n,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 p(n){return function(n){if(Array.isArray(n))return m(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||b(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 b(n,t){if(n){if("string"==typeof n)return m(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?m(n,t):void 0}}function m(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function h(r,e){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,f=void 0===a?[c,r]:a,l=o.element,y=n((function(){}));t((function(){y.current=e}),[e]),t((function(){if(!1!==c){if("once"===c||!0===c){var n=function(n){return y.current(n)},t=v(v({},i),{},{once:"once"===c});return(null!=l?l:document).addEventListener(r,n,t),function(){(null!=l?l:document).removeEventListener(r,n,t)}}console.error("Received value of type ".concat(d(c)," for property `enabled`. Expected a boolean."))}}),f)}function g(n,e,o){var i=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=r(),c=s(u,2),a=c[0],f=c[1];return t((function(){if(i){var t=new MutationObserver((function(){f(o.apply(void 0,arguments))}));return n&&t.observe(n,e),function(){return t.disconnect()}}}),[n,e,o,i]),a}function w(){return{width:window.innerWidth,height:window.innerHeight}}function O(){var n=s(r(null),2),e=n[0],o=n[1];return t((function(){o(w());var n=c((function(){return o(w())}),100);return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}}),[]),e}var j=function(n,t){return h("keydown",(function(t){return function(n,t){27===n.keyCode&&(n.stopImmediatePropagation(),t())}(t,n)}),t)};function S(){var n=function(){return"visible"===document.visibilityState},e=s(r(!0),2),o=e[0],i=e[1];return t((function(){i(n)}),[]),h("visibilitychange",(function(){i(n)})),o}function A(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=e.interval,i=void 0===o?3e4:o,u=e.immediate,c=void 0===u||u,a=e.enabled,f=void 0===a||a,l=n(),v=S(),d=v&&f;t((function(){l.current=r})),t((function(){var n;if(d)return c?e():t(),r;function t(){r(),n=setTimeout(e,i)}function r(){clearTimeout(n)}function e(){var n;Promise.resolve(null===(n=l.current)||void 0===n?void 0:n.call(l)).finally(t)}}),[i,c,d])}function E(r){var e=n();return t((function(){e.current=r})),e.current}function P(t){var r=n();return void 0!==r.current&&a(r.current,t)||(r.current=t),r.current}var I=function(){var n="undefined"==typeof window?t:e;return n.apply(void 0,arguments)},x=!1,C=new Map,D=function(n){if(C.has(n)){var t=C.get(n),r=t?t+1:1;return C.set(n,r),r}return C.set(n,1),1};function k(n){var e=n.prefix,o=n.id,i=e&&(x?D(e):null),u=s(r(i),2),c=u[0],a=u[1];return I((function(){e&&null===c&&a(D(e))}),[]),t((function(){!1===x&&(x=!0)}),[]),o||"".concat(e,"-").concat(c)}function L(n){var t=s(r(!1),2),e=t[0],o=t[1];if(f(n)||"function"!=typeof n)return{onBlur:function(){},onChange:function(){}};return{onBlur:function(t){o(!0),null==n||n(t.target.value)},onChange:function(t){e&&(null==n||n(t.target.value))}}}var M=new Map;function T(n){return function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";function t(t){if(t){var r=n+"-"+t;if(M.get(r))return M.get(r);var e=o();return M.set(r,e),e}console.error("useDynamicRefs: Cannot get ref without key")}return t}(n.prefix)}function z(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=r.initialValue,o=r.deps,u=void 0===o?[]:o,c=n(e);return i((function(){return{get current(){return c.current},set current(n){c.current=n,t(n)}}}),[t,c].concat(p(u)))}function B(n,t){var r=u((function(n,t){Array.isArray(n)?n.forEach(r):"function"==typeof n?n(t):n&&(n.current=t)}),[]);return z(u((function(t){return r(n,t)}),[n,r]),{initialValue:t})}export{T as useDynamicRefs,j as useEscapeKey,h as useEventListener,B as useForwardedRef,k as useIdAllocator,I as useIsomorphicLayoutEffect,g as useMutationObserver,P as useObjectDependency,z as useObservedRef,A as usePoller,E as usePrevious,L as useValidation,O as useViewportSize}; | ||
//# sourceMappingURL=index.js.map |
@@ -11,2 +11,4 @@ export { default as useEventListener } from './useEventListener'; | ||
export { default as useValidation } from './useValidation'; | ||
export { default as useDynamicRefs } from './useDynamicRefs'; | ||
export { useObservedRef, useForwardedRef } from './useForwardedRef'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("lodash/debounce"),require("lodash/isEqual"),require("lodash/isUndefined")):"function"==typeof define&&define.amd?define(["exports","react","lodash/debounce","lodash/isEqual","lodash/isUndefined"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/hooks"]={},e.React,e.debounce,e.isEqual,e.isUndefined)}(this,(function(e,n,t,r,o){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=u(t),c=u(r),f=u(o);function a(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 l(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?a(Object(t),!0).forEach((function(n){d(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):a(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function s(e){return(s="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})(e)}function d(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function v(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)return;var r,o,u=[],i=!0,c=!1;try{for(t=t.call(e);!(i=(r=t.next()).done)&&(u.push(r.value),!n||u.length!==n);i=!0);}catch(e){c=!0,o=e}finally{try{i||null==t.return||t.return()}finally{if(c)throw o}}return u}(e,n)||function(e,n){if(!e)return;if("string"==typeof e)return y(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return y(e,n)}(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 y(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 p(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,f=void 0===c?[i,e]:c,a=r.element,d=n.useRef((function(){}));n.useEffect((function(){d.current=t}),[t]),n.useEffect((function(){if(!1!==i){if("once"===i||!0===i){var n=function(e){return d.current(e)},t=l(l({},o),{},{once:"once"===i});return(null!=a?a:document).addEventListener(e,n,t),function(){(null!=a?a:document).removeEventListener(e,n,t)}}console.error("Received value of type ".concat(s(i)," for property `enabled`. Expected a boolean."))}}),f)}function b(){return{width:window.innerWidth,height:window.innerHeight}}function m(){var e=function(){return"visible"===document.visibilityState},t=v(n.useState(!0),2),r=t[0],o=t[1];return n.useEffect((function(){o(e)}),[]),p("visibilitychange",(function(){o(e)})),r}var h=function(){var e="undefined"==typeof window?n.useEffect:n.useLayoutEffect;return e.apply(void 0,arguments)},g=!1,w=new Map,E=function(e){if(w.has(e)){var n=w.get(e),t=n?n+1:1;return w.set(e,t),t}return w.set(e,1),1};e.useEscapeKey=function(e,n){return p("keydown",(function(n){return function(e,n){27===e.keyCode&&(e.stopImmediatePropagation(),n())}(n,e)}),n)},e.useEventListener=p,e.useIdAllocator=function(e){var t=e.prefix,r=e.id,o=t&&(g?E(t):null),u=v(n.useState(o),2),i=u[0],c=u[1];return h((function(){t&&null===i&&c(E(t))}),[]),n.useEffect((function(){!1===g&&(g=!0)}),[]),r||"".concat(t,"-").concat(i)},e.useIsomorphicLayoutEffect=h,e.useMutationObserver=function(e,t,r){var o=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=n.useState(),i=v(u,2),c=i[0],f=i[1];return n.useEffect((function(){if(o){var n=new MutationObserver((function(){f(r.apply(void 0,arguments))}));return e&&n.observe(e,t),function(){return n.disconnect()}}}),[e,t,r,o]),c},e.useObjectDependency=function(e){var t=n.useRef();return void 0!==t.current&&c.default(t.current,e)||(t.current=e),t.current},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,f=void 0===c||c,a=n.useRef(),l=m(),s=l&&f;n.useEffect((function(){a.current=e})),n.useEffect((function(){var e;if(s)return i?r():n(),t;function n(){t(),e=setTimeout(r,o)}function t(){clearTimeout(e)}function r(){var e;Promise.resolve(null===(e=a.current)||void 0===e?void 0:e.call(a)).finally(n)}}),[o,i,s])},e.usePrevious=function(e){var t=n.useRef();return n.useEffect((function(){t.current=e})),t.current},e.useValidation=function(e){var t=v(n.useState(!1),2),r=t[0],o=t[1];return f.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=function(){var e=v(n.useState(null),2),t=e[0],r=e[1];return n.useEffect((function(){r(b());var e=i.default((function(){return r(b())}),100);return window.addEventListener("resize",e),function(){return window.removeEventListener("resize",e)}}),[]),t},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("lodash/debounce"),require("lodash/isEqual"),require("lodash/isUndefined")):"function"==typeof define&&define.amd?define(["exports","react","lodash/debounce","lodash/isEqual","lodash/isUndefined"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@leafygreen-ui/hooks"]={},e.React,e.debounce,e.isEqual,e.isUndefined)}(this,(function(e,n,t,r,o){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=u(t),c=u(r),f=u(o);function a(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 l(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?a(Object(t),!0).forEach((function(n){d(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):a(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function s(e){return(s="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})(e)}function d(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function y(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)return;var r,o,u=[],i=!0,c=!1;try{for(t=t.call(e);!(i=(r=t.next()).done)&&(u.push(r.value),!n||u.length!==n);i=!0);}catch(e){c=!0,o=e}finally{try{i||null==t.return||t.return()}finally{if(c)throw o}}return u}(e,n)||p(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 v(e){return function(e){if(Array.isArray(e))return b(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||p(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 p(e,n){if(e){if("string"==typeof e)return b(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)?b(e,n):void 0}}function b(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 m(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,f=void 0===c?[i,e]:c,a=r.element,d=n.useRef((function(){}));n.useEffect((function(){d.current=t}),[t]),n.useEffect((function(){if(!1!==i){if("once"===i||!0===i){var n=function(e){return d.current(e)},t=l(l({},o),{},{once:"once"===i});return(null!=a?a:document).addEventListener(e,n,t),function(){(null!=a?a:document).removeEventListener(e,n,t)}}console.error("Received value of type ".concat(s(i)," for property `enabled`. Expected a boolean."))}}),f)}function h(){return{width:window.innerWidth,height:window.innerHeight}}function g(){var e=function(){return"visible"===document.visibilityState},t=y(n.useState(!0),2),r=t[0],o=t[1];return n.useEffect((function(){o(e)}),[]),m("visibilitychange",(function(){o(e)})),r}var w=function(){var e="undefined"==typeof window?n.useEffect:n.useLayoutEffect;return e.apply(void 0,arguments)},E=!1,O=new Map,S=function(e){if(O.has(e)){var n=O.get(e),t=n?n+1:1;return O.set(e,t),t}return O.set(e,1),1};var j=new Map;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(v(u)))}e.useDynamicRefs=function(e){return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";function t(t){if(t){var r=e+"-"+t;if(j.get(r))return j.get(r);var o=n.createRef();return j.set(r,o),o}console.error("useDynamicRefs: Cannot get ref without key")}return t}(e.prefix)},e.useEscapeKey=function(e,n){return m("keydown",(function(n){return function(e,n){27===e.keyCode&&(e.stopImmediatePropagation(),n())}(n,e)}),n)},e.useEventListener=m,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,r=e.id,o=t&&(E?S(t):null),u=y(n.useState(o),2),i=u[0],c=u[1];return w((function(){t&&null===i&&c(S(t))}),[]),n.useEffect((function(){!1===E&&(E=!0)}),[]),r||"".concat(t,"-").concat(i)},e.useIsomorphicLayoutEffect=w,e.useMutationObserver=function(e,t,r){var o=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],u=n.useState(),i=y(u,2),c=i[0],f=i[1];return n.useEffect((function(){if(o){var n=new MutationObserver((function(){f(r.apply(void 0,arguments))}));return e&&n.observe(e,t),function(){return n.disconnect()}}}),[e,t,r,o]),c},e.useObjectDependency=function(e){var t=n.useRef();return void 0!==t.current&&c.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,f=void 0===c||c,a=n.useRef(),l=g(),s=l&&f;n.useEffect((function(){a.current=e})),n.useEffect((function(){var e;if(s)return i?r():n(),t;function n(){t(),e=setTimeout(r,o)}function t(){clearTimeout(e)}function r(){var e;Promise.resolve(null===(e=a.current)||void 0===e?void 0:e.call(a)).finally(n)}}),[o,i,s])},e.usePrevious=function(e){var t=n.useRef();return n.useEffect((function(){t.current=e})),t.current},e.useValidation=function(e){var t=y(n.useState(!1),2),r=t[0],o=t[1];return f.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=function(){var e=y(n.useState(null),2),t=e[0],r=e[1];return n.useEffect((function(){r(h());var e=i.default((function(){return r(h())}),100);return window.addEventListener("resize",e),function(){return window.removeEventListener("resize",e)}}),[]),t},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@leafygreen-ui/hooks", | ||
"version": "7.1.1", | ||
"version": "7.2.0", | ||
"description": "LeafyGreen UI Kit Custom Hooks", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -34,5 +34,6 @@ # Hooks | ||
| Prop | Type | Description | Default | | ||
| ----------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | --- | ---------- | -------- | ------------------------------------------------------------------------------ | --- | | ||
| ----------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | | ||
| `type` | `Global Event Handler`, `string` | Type of event to listen for. | | | ||
| `eventCallback` | `function` | Callback executed when event is triggered. | | | `optional` | `object` | Optional third argument passed to function with implementation specifications. | | | ||
| `eventCallback` | `function` | Callback executed when event is triggered. | | | ||
| `optional` | `object` | Optional third argument passed to function with implementation specifications. | | | ||
| `optional.options` | `AddEventListenerOptions` excluding `once` | Parameter to specify options passed to the eventListener. To enable the `once` option, see `optional.enabled`. | | | ||
@@ -176,1 +177,46 @@ | `optional.enabled` | `boolean`, `'once'` | Determines whether the event handler is attached or not. Setting this to `"once"` will ensure the event handler will be detached after the initial time an event is triggered. | `true` | | ||
``` | ||
## useForwardedRef | ||
Returns a MutableRefObject from the forwarded ref. | ||
### Example | ||
```jsx | ||
import { useForwardedRef } from '@leafygreen-ui/hooks'; | ||
const MyComponent = React.forwardRef((props, forwardedRef) => { | ||
const ref = useForwawrdedRef(forwardedRef, null); | ||
}); | ||
``` | ||
### Properties | ||
| Prop | Type | Description | Default | | ||
| -------------------- | ------------------------------------------- | ------------------------ | ------- | | ||
| `forwardedRefOrRefs` | `SettableRef`, `Array<SettableRef>`, `null` | The forwarded ref | | | ||
| `initialValue` | `T` | Initial value of the ref | | | ||
## useDynamicRefs | ||
useDynamicRefs is a hook that allows you to create multiple refs for dynamically created elements. Use the `prefix` property to create multiple sets of dynamic refs. | ||
### Example | ||
```jsx | ||
import { useDynamicRefs } from '@leafygreen-ui/hooks'; | ||
const getRef = useDynamicRefs(); | ||
... | ||
return ['a','b','c'].map(x => { | ||
const divRef = getRef(x) | ||
return <div ref={divRef}>{x}</div> | ||
}) | ||
``` | ||
### Properties | ||
| Prop | Type | Description | Default | | ||
| -------- | -------- | --------------------------------------------------- | ------- | | ||
| `prefix` | `string` | Optional prefix to that genterates a ref name space | | |
@@ -11,1 +11,3 @@ export { default as useEventListener } from './useEventListener'; | ||
export { default as useValidation } from './useValidation'; | ||
export { default as useDynamicRefs } from './useDynamicRefs'; | ||
export { useObservedRef, useForwardedRef } from './useForwardedRef'; |
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
264574
49
907
221