UNPKG

@studio-lumio/hooks

Version:

a compilation of react hooks we use to make our magic

3 lines (2 loc) 15.6 kB
import{useState as n,useEffect as e,useMemo as t,useLayoutEffect as r,useRef as o,useCallback as i}from"react";import{raf as u}from"@studio-freight/tempus";import c from"js-cookie";function a(n,e,t){void 0===t&&(t="mousedown"),b(t,function(t){var r=null==n?void 0:n.current;r&&!r.contains(t.target)&&e(t)})}function d(){var t=n(!1),r=t[0],o=t[1];return e(function(){o(!0)},[]),r}function f(){var n=d();return t(function(){return n&&(window.location.href.includes("#debug")||"development"===process.env.NODE_ENV)&&!window.location.href.includes("#production")},[n])}function v(){var t=n(function(){return"undefined"!=typeof document?document.readyState:"loading"}),r=t[0],o=t[1];return e(function(){if("undefined"!=typeof document)return o(document.readyState),document.addEventListener("readystatechange",n,!1),function(){return document.removeEventListener("readystatechange",n,!1)};function n(){o(document.readyState)}},[]),r}function s(n,t){void 0===t&&(t=0),e(function(){if(n)return u.add(n,t),function(){return u.remove(n)}},[n,t])}var l="undefined"!=typeof window?r:e;function m(n,t){var r=o(n);l(function(){r.current=n},[n]),e(function(){if(t||0===t){var n=setInterval(function(){return r.current()},t);return function(){return clearInterval(n)}}},[t])}var w=function(){var t=n(!1),r=t[0],o=t[1],u=i(function(){var n=!1;if("maxTouchPoints"in navigator)n=navigator.maxTouchPoints>0;else{var e=window.matchMedia("(pointer:coarse)");if(e&&"(pointer:coarse)"===e.media)n=!!e.matches;else{var t=window.navigator.userAgent;n=/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(t)||/\b(Android|Windows Phone|iPad|iPod)\b/i.test(t)}}o(!!n)},[]),c=i(function(){u()},[u]);return e(function(){return c(),window.addEventListener("resize",c,{passive:!0}),function(){window.removeEventListener("resize",c)}},[c]),r};function h(t){var r=t.root,u=void 0===r?null:r,c=t.rootMargin,a=void 0===c?"0px":c,d=t.threshold,f=void 0===d?1:d,v=t.once,s=void 0!==v&&v,l=o(null),m=o(null),w=n(!1),h=w[0],p=w[1],g=i(function(n){m.current||(m.current=n)},[]),y=i(function(n){0!==n.length&&p(n[0].isIntersecting)},[]);return e(function(){return m.current&&(l.current=new IntersectionObserver(y,{root:u,rootMargin:a,threshold:f}),l.current.observe(m.current)),function(){l.current&&l.current.disconnect()}},[y]),e(function(){s&&h&&l.current.disconnect()},[h]),{setRef:g,inView:h}}function p(r){var o=d(),u=t(function(){if(o)try{return window.matchMedia(r)}catch(n){"production"!==process.env.NODE_ENV&&console.error(n)}return null},[r,o]),c=n(void 0),a=c[0],f=c[1],v=i(function(n){f(n.matches)},[]);return e(function(){if(u)return v(u),u.addEventListener("change",v,{passive:!0}),function(){u.removeEventListener("change",v)}},[u,v,o]),a}var g=function(e){var t=n(y(e?e.current:null)),o=t[0],u=t[1],c=i(function(){e.current&&u(y(e.current))},[e]);return r(function(){var n=e.current;if(n){if(c(),"function"==typeof ResizeObserver){var t=new ResizeObserver(function(){return c()});return t.observe(n),function(){t&&(t.disconnect(),t=null)}}return window.addEventListener("resize",c),function(){window.removeEventListener("resize",c)}}},[e.current]),o};function y(n){return n?n.getBoundingClientRect():{bottom:0,height:0,left:0,right:0,top:0,width:0}}function E(n,e){void 0===n&&(n=[]),void 0===e&&(e=[]);var r=t(function(){return e&&[e].flat()},[e]),o=t(function(){return n&&[n].flat()},[n]);return t(function(){if(r&&o){var e=o.map(function(n){var e;return null==(e=r.find(function(e){return e.type===n}))?void 0:e.props.children});return n[0]?e:e[0]}},[r,o])}function b(n,t,r,i){var u=o(t);l(function(){u.current=t},[t]),e(function(){var e,t=null!=(e=null==r?void 0:r.current)?e:window;if(t&&t.addEventListener){var o=function(n){return u.current(n)};return t.addEventListener(n,o,i),function(){t.removeEventListener(n,o,i)}}},[n,r,i])}var L=function(){var e=n({width:0,height:0}),t=e[0],r=e[1],o=function(){r({width:window.innerWidth,height:window.innerHeight})};return b("resize",o),l(function(){o()},[]),t};function T(t,r){var u=void 0===t?{}:t,c=u.root,a=void 0===c?null:c,d=u.rootMargin,f=void 0===d?"0px":d,v=u.threshold,s=void 0===v?0:v,l=u.once,m=void 0!==l&&l,w=u.lazy,h=void 0!==w&&w,p=u.callback,g=void 0===p?function(){}:p;void 0===r&&(r=[]);var y=o(),E=n(),b=E[0],L=E[1],T=n(null),S=T[0],x=T[1];e(function(){if(S){var n=new IntersectionObserver(function(e){var t=e[0];h?y.current=t:L(t),g(t),m&&t.isIntersecting&&n.disconnect()},{root:a,rootMargin:f,threshold:s});return n.observe(S),function(){n.disconnect()}}},[S,a,f,s,h,m].concat(r));var k=i(function(){return y.current},[]);return[x,h?k:b]}function S(){var e=n(null),t=e[1];return[e[0],function(n,e){try{var r;return null!=(r=navigator)&&r.clipboard?Promise.resolve(function(r,o){try{var i=Promise.resolve(navigator.clipboard.writeText(n)).then(function(){return t(n),e(),!0})}catch(n){return o(n)}return i&&i.then?i.then(void 0,o):i}(0,function(n){return console.warn("Copy failed",n),t(null),!1})):(console.warn("Clipboard not supported"),Promise.resolve(!1))}catch(n){return Promise.reject(n)}}]}function x(t,r){var o=n(t),i=o[0],u=o[1];return e(function(){var n=setTimeout(function(){return u(t)},r||500);return function(){clearTimeout(n)}},[t,r]),i}var k=function(t){var r=o(t),i=o(),u=o(!1),c=o(!1),a=n(0)[1];u.current&&(c.current=!0),e(function(){return u.current||(i.current=r.current(),u.current=!0),a(function(n){return n+1}),function(){c.current&&i.current&&i.current()}},[])};function N(){var n=o(!0);return n.current?(n.current=!1,!0):n.current}var O=function(n,t){var r=N();e(function(){if(!r)return n()},t)};function P(){var n=o(!1),t=i(function(){return n.current},[]);return e(function(){return n.current=!0,function(){n.current=!1}},[]),t}function A(e,t){var r=n(function(){if("undefined"==typeof window)return t;try{var n=window.localStorage.getItem(e);return n?JSON.parse(n):t}catch(n){return console.log(n),t}}),o=r[0],i=r[1];return[o,function(n){try{var t=n instanceof Function?n(o):n;i(t),"undefined"!=typeof window&&window.localStorage.setItem(e,JSON.stringify(t))}catch(n){console.log(n)}},function(){try{"undefined"!=typeof window&&window.localStorage.removeItem(e)}catch(n){console.log(n)}}]}function z(e,t){var r=n(function(){if("undefined"==typeof window)return t;try{var n=window.sessionStorage.getItem(e);return n?JSON.parse(n):t}catch(n){return console.log(n),t}}),o=r[0],i=r[1];return[o,function(n){try{var t=n instanceof Function?n(o):n;i(t),"undefined"!=typeof window&&window.sessionStorage.setItem(e,JSON.stringify(t))}catch(n){console.log(n)}},function(){try{"undefined"!=typeof window&&window.sessionStorage.removeItem(e)}catch(n){console.log(n)}}]}var I=function(){return e(function(){console.debug("WARNING: Still using FOUC temp fix on route change. Has the Next.js bug not been fixed? See https://github.com/vercel/next.js/issues/17464");var n=Array.from(document.querySelectorAll('link[rel="stylesheet"][data-n-p]')).map(function(n){return{element:n,href:n.getAttribute("href")}});n.forEach(function(n){return n.element.removeAttribute("data-n-p")});var e=[],t=new MutationObserver(function(t){t.filter(function(n){var e=n.target;return"STYLE"===e.nodeName&&e.hasAttribute("data-n-href")}).map(function(n){var e=n.target;return{element:e,href:e.getAttribute("data-n-href")}}).forEach(function(n){var t=n.element,r=n.href;e.includes(r)?t.remove():(t.setAttribute("data-fouc-fix-n-href",r),t.removeAttribute("data-n-href"),e.push(r))}),n=n.reduce(function(n,t){var r=t.element;return e.includes(t.href)?r.remove():n.push(t),n},[])});return t.observe(document.head,{subtree:!0,attributeFilter:["media"]}),function(){return t.disconnect()}},[])},C=function(){var t=n(),r=t[0],o=t[1];return e(function(){var n=window.navigator?window.navigator.userAgent:"",e=n.match(/iPad/i)||n.match(/iPhone/i),t=n.match(/WebKit/i),r=e&&t&&!n.match(/CriOS/i),i=window.innerHeight;function u(){var n=window.innerHeight;o(!(n-50>i))}if(r){if(!("standalone"in window.navigator)||!window.navigator.standalone)return document.addEventListener("scroll",u),function(){document.removeEventListener("scroll",u)};o(!1)}},[]),{isVisible:r}},M=function(n){e(function(){var e=document.createElement("script");return e.src=n,e.async=!0,document.body.appendChild(e),function(){document.body.removeChild(e)}},[n])},D=function(n){var e,t,r,o,i,u=n.sequence,c=n.callback;return t=(e={eventType:"keydown",keystrokeDelay:1e3})&&e.eventType||"keydown",r=e&&e.keystrokeDelay||1e3,o=[],i=Date.now(),document.addEventListener(t,function(n){var e=n.key.toLowerCase(),t=Date.now();t-i>r&&(o=[]),o.push(e),i=t,u===o.join("")&&c()}),null};function H(n){e(function(){var e=document.querySelector("link[rel*='icon']")||document.createElement("link");e.type="image/x-icon",e.rel="shortcut icon",e.href=n,document.getElementsByTagName("head")[0].appendChild(e)},[n])}function R(t){void 0===t&&(t=6e4);var r=n(!1),o=r[0],i=r[1];return e(function(){var n,e,r,o=function(){i(!0)},u=(e=function(){i(!1),window.clearTimeout(n),n=window.setTimeout(o,t)},r=0,function(){var n=Date.now();n-r>=500&&(e(),r=n)}),c=function(){document.hidden||u()};return n=window.setTimeout(o,t),window.addEventListener("mousemove",u),window.addEventListener("mousedown",u),window.addEventListener("resize",u),window.addEventListener("keydown",u),window.addEventListener("touchstart",u),window.addEventListener("wheel",u),document.addEventListener("visibilitychange",c),function(){window.removeEventListener("mousemove",u),window.removeEventListener("mousedown",u),window.removeEventListener("resize",u),window.removeEventListener("keydown",u),window.removeEventListener("touchstart",u),window.removeEventListener("wheel",u),document.removeEventListener("visibilitychange",c),window.clearTimeout(n)}},[t]),o}function W(n){e(function(){document.title=n},[n])}function j(){var t=n(!1),r=t[0],i=t[1],u=o(null);return e(function(){var n=u.current;if(n){var e=function(){i(!0)},t=function(){i(!1)};return n.addEventListener("mouseenter",e),n.addEventListener("mouseleave",t),function(){n.removeEventListener("mouseenter",e),n.removeEventListener("mouseleave",t)}}},[]),[u,r]}var B=function(n,t){void 0===t&&(t=[]),e(function(){if(n){var e=function(e){var t=(e=e||window.event).relatedTarget||e.toElement;t&&"HTML"!==t.nodeName||n()};return function(n){n&&n.addEventListener&&n.addEventListener.apply(n,[].slice.call(arguments,1))}(document,"mouseout",e),function(){!function(n){n&&n.removeEventListener&&n.removeEventListener.apply(n,[].slice.call(arguments,1))}(document,"mouseout",e)}}},t)},F=function(){var t=n(!document.hidden),r=t[0],o=t[1];return e(function(){var n=function(){o(!document.hidden)};return document.addEventListener("visibilitychange",n),function(){document.removeEventListener("visibilitychange",n)}},[]),r};function J(){var e=n({angle:0,type:"landscape-primary"}),t=e[0],o=e[1];return r(function(){var n,e=function(){var n=window.screen.orientation;o({angle:n.angle,type:n.type})},t=function(){o({type:"UNKNOWN",angle:window.orientation})};return null!=(n=window.screen)&&n.orientation?(e(),window.screen.orientation.addEventListener("change",e)):(t(),window.addEventListener("orientationchange",t)),function(){var n;null!=(n=window.screen)&&n.orientation?window.screen.orientation.removeEventListener("change",e):window.removeEventListener("orientationchange",t)}},[]),t}function V(e){void 0===e&&(e=[]);var t=n(e),r=t[1];return[t[0],{set:i(function(n){r(n)},[]),push:i(function(n){r(function(e){return[].concat(e,[n])})},[]),removeAt:i(function(n){r(function(e){return[].concat(e.slice(0,n),e.slice(n+1))})},[]),insertAt:i(function(n,e){r(function(t){return[].concat(t.slice(0,n),[e],t.slice(n))})},[]),updateAt:i(function(n,e){r(function(t){return t.map(function(t,r){return r===n?e:t})})},[]),clear:i(function(){return r([])},[])}]}var q=function(e){var t=n(function(){return c.get(e)||null}),r=t[1];return[t[0],i(function(n,t){c.set(e,n,t),r(n)},[e]),i(function(){c.remove(e),r(null)},[e])]},K=function(n){var e=o(n);e.current=n,k(function(){return function(){return e.current()}})},U=function(t,r){void 0===r&&(r=200);var i=n(t),u=i[0],c=i[1],a=o(),d=o(null),f=o(0);return e(function(){a.current?(d.current=t,f.current=!0):(c(t),a.current=setTimeout(function n(){f.current?(f.current=!1,c(d.current),a.current=setTimeout(n,r)):a.current=void 0},r))},[t]),K(function(){a.current&&clearTimeout(a.current)}),u};function _(n,t){var r=o(null),i=o(n);return e(function(){i.current=n},[n]),function(){var n=arguments;r.current&&clearTimeout(r.current),r.current=setTimeout(function(){i.current.apply(i,[].slice.call(n))},t)}}function G(r){var u=void 0===r?{}:r,c=u.lazy,a=void 0!==c&&c,d=u.debounce,f=void 0===d?500:d,v=u.box,s=void 0===v?"border-box":v,l=u.callback,m=void 0===l?function(){}:l,w=o(null),h=n(null),p=h[0],g=h[1],y=n(null),E=y[0],b=y[1],L=o(!1),T=t(function(){return n=g,e=f,t=null,r=null,i=function(){var i=[].slice.call(arguments);if(!e)return n.apply(this,i);var u=this;return o(),r=function(){n.apply(u,i)},void(t=setTimeout(function(){t=null;var n=r;return r=null,n&&n()},e))},i.cancel=o=function(){t&&(clearTimeout(t),r=null,t=null)},i.flush=function(){var n=r;o(),n&&n()},i;var n,e,t,r,o,i},[f]),S=i(function(n){var e=n[0];w.current=e,m(e),a||(L.current?g(e):T(e)),L.current=!1},[a,T,m]);e(function(){if(E){L.current=!0;var n=new ResizeObserver(S);return n.observe(E,{box:s}),function(){n.disconnect(),T.cancel()}}},[E,f,s,S,T]);var x=i(function(){return w.current},[]);return[b,a?x:p]}function Y(){var n=i(function(){document.documentElement.style.setProperty("--vw",.01*document.documentElement.clientWidth+"px"),document.documentElement.style.setProperty("--dvh",.01*window.innerHeight+"px"),document.documentElement.style.setProperty("--svh",.01*document.documentElement.clientHeight+"px"),document.documentElement.style.setProperty("--lvh","1vh")},[]);return r(function(){return window.addEventListener("resize",n,!1),n(),function(){window.removeEventListener("resize",n,!1)}},[n]),null}function Q(n,t){var r=o(n);return e(function(){t(n,n)},[n,t]),[i(function(){return r.current},[]),function(n){if("function"==typeof n){var e=n(r.current);return t(e,r.current),void(r.current=e)}n!==r.current&&(t(n,r.current),r.current=n)}]}function X(n,t,r){void 0===r&&(r=[]);var i=o(n);e(function(){i.current=n},[n]),e(function(){var n=setTimeout(function(){return i.current()},t);return function(){clearTimeout(n)}},[t].concat(r))}function Z(n,e,r,o,i){void 0===n&&(n=1),void 0===e&&(e=1),void 0===r&&(r=1),void 0===o&&(o=1),void 0===i&&(i="cover");var u=t(function(){if(!(n&&e&&r&&o))return[1,1];var t,u=n/e,c=r/o;if("contain"===i)t=u>c?e*c:n;else{if("cover"!==i)return[1,1];t=u>c?n:e*c}return[n/t,e/(t/c)]},[n,e,o,r,i]);return[1/u[0],1/u[1]]}export{q as useCookie,S as useCopyToClipboard,x as useDebounce,_ as useDebounceFn,f as useDebug,v as useDocumentReadyState,W as useDocumentTitle,k as useEffectOnce,b as useEventListener,H as useFavicon,N as useFirstMountState,I as useFoucFix,s as useFrame,j as useHover,C as useIOSToolbarState,R as useIdle,T as useIntersectionObserver,m as useInterval,d as useIsClient,P as useIsMounted,w as useIsTouchDevice,h as useIsVisible,l as useIsomorphicLayoutEffect,D as useKeySequence,Q as useLazyState,V as useList,A as useLocalStorage,p as useMediaQuery,Z as useObjectFit,a as useOnClickOutside,J as useOrientation,B as usePageLeave,F as usePageVisibility,Y as useRealViewport,g as useRect,G as useResizeObserver,M as useScript,z as useSessionStorage,E as useSlots,U as useThrottle,X as useTimeout,K as useUnmount,O as useUpdateEffect,L as useWindowSize}; //# sourceMappingURL=hooks.mjs.map