UNPKG

react-recipes

Version:

A React Hooks utility library containing popular customized hooks

2 lines (1 loc) 16.7 kB
import{useMemo as n,useState as e,useCallback as t,useEffect as r,useLayoutEffect as o,useRef as i}from"react";var u=function(e,t,r,o){return n(function(){return function(n,e,t,r){var o,i,u,c,a,d,l,s=parseInt,f=Math.round,v="string"==typeof t;if("number"!=typeof n||n<-1||n>1||"string"!=typeof e||"r"!=e[0]&&"#"!=e[0]||t&&!v)return null;var m=function(n){var e=n.length,t={};if(e>9){var r;if(r=n=n.split(","),o=r[0],i=r[1],u=r[2],v=r[3],(e=n.length)<3||e>4)return null;t.r=s(o.slice("a"==o[3]?5:4)),t.g=s(i),t.b=s(u),t.a=v?parseFloat(v):-1}else{if(8==e||6==e||e<4)return null;e<6&&(n="#"+n[1]+n[1]+n[2]+n[2]+n[3]+n[3]+(e>4?n[4]+n[4]:"")),n=s(n.slice(1),16),9==e||5==e?(t.r=n>>24&255,t.g=n>>16&255,t.b=n>>8&255,t.a=f((255&n)/.255)/1e3):(t.r=n>>16,t.g=n>>8&255,t.b=255&n,t.a=-1)}return t};return l=e.length>9,l=v?t.length>9||"c"==t&&!l:l,a=m(e),c=n<0,d=t&&"c"!=t?m(t):c?{r:0,g:0,b:0,a:-1}:{r:255,g:255,b:255,a:-1},c=1-(n=c?-1*n:n),a&&d?(r?(o=f(c*a.r+n*d.r),i=f(c*a.g+n*d.g),u=f(c*a.b+n*d.b)):(o=f(Math.pow(c*Math.pow(a.r,2)+n*Math.pow(d.r,2),.5)),i=f(Math.pow(c*Math.pow(a.g,2)+n*Math.pow(d.g,2),.5)),u=f(Math.pow(c*Math.pow(a.b,2)+n*Math.pow(d.b,2),.5))),d=d.a,v=(a=(v=a.a)>=0||d>=0)?v<0?d:d<0?v:v*c+d*n:0,l?"rgb"+(a?"a(":"(")+o+","+i+","+u+(a?","+f(1e3*v)/1e3:"")+")":"#"+(4294967296+16777216*o+65536*i+256*u+(a?f(255*v):0)).toString(16).slice(1,a?void 0:-2)):null}(e,t,r,o)},[e,t,r,o])},c=function(n){var r=e(n),o=r[1];return{value:r[0],setValue:o,add:t(function(n){return o(function(e){return[].concat(e,[n])})},[]),clear:t(function(){return o(function(){return[]})},[]),removeById:t(function(n){return o(function(e){return e.filter(function(e){return e&&e.id!==n})})},[]),removeIndex:t(function(n){return o(function(e){return e.filter(function(e,t){return t!==n})})},[])}},a=function(n,o){void 0===o&&(o=!0);var i=e(!1),u=i[0],c=i[1],a=e(null),d=a[0],l=a[1],s=e(null),f=s[0],v=s[1],m=t(function(){return v(null),c(!0),l(null),n().then(function(n){return l(n)}).catch(function(n){return v(n)}).finally(function(){return c(!1)})},[n]);return r(function(){o&&m()},[m,o]),{error:f,execute:m,pending:u,value:d}},d=function(n,t){var r=e(function(){return e=n,document.cookie.split("; ").reduce(function(n,t){var r=t.split("=");return r[0]===e?decodeURIComponent(r[1]):n},"")||t;var e}),o=r[1],i=function(e,t,r){void 0===t&&(t=365),void 0===r&&(r="/"),o(e),function(n,e,t,r){var o=new Date(Date.now()+864e5*t).toUTCString();document.cookie=n+"="+encodeURIComponent(e)+"; expires="+o+"; path="+r}(n,e,t,r)};return[r[0],i,function(n){void 0===n&&(n="/"),i("",-1,n),o(null)}]};function l(n){void 0===n&&(n=2e3);var t=e(!1),o=t[0],i=t[1];return r(function(){if(o){var e=setTimeout(function(){i(!1)},n);return function(){return clearTimeout(e)}}},[o,n]),[o,function(n){var e=function(n){var e=document.createElement("textarea"),t=window.navigator.userAgent.match(/ipad|iphone/i),r=window.pageYOffset||document.documentElement.scrollTop;if(e.contentEditable=!0,e.readOnly=!1,e.value=n,e.style.border="0",e.style.padding="0",e.style.margin="0",e.style.position="absolute",e.style.top=r+"px",document.body.appendChild(e),t){var o=document.createRange();o.selectNodeContents(e);var i=window.getSelection();i.removeAllRanges(),i.addRange(o),e.setSelectionRange(0,999999)}else e.select();var u=!1;try{u=document.execCommand("copy")}catch(e){try{window.clipboardData.setData("text",n),u=!0}catch(n){console.error("unable to copy using clipboardData: ",n)}}return document.body.removeChild(e),u}(n);i(e)}]}function s(n,t){var r=e(function(){try{if("undefined"==typeof window)return t;var e=window.localStorage.getItem(n);return null!==e?JSON.parse(e):t}catch(n){return console.log(n),t}}),o=r[0],i=r[1];return[o,function(e){try{var t=e instanceof Function?e(o):e;i(t),window.localStorage.setItem(n,JSON.stringify(t))}catch(n){console.log(n)}}]}function f(n,t,o){var i=n.map(function(n){return window.matchMedia(n)}),u=function(){var n=i.findIndex(function(n){return n.matches});return void 0!==t[n]?t[n]:o},c=e(u),a=c[0],d=c[1];return r(function(){var n=function(){return d(u)};return i.forEach(function(e){return e.addListener(n)}),function(){return i.forEach(function(e){return e.removeListener(n)})}},[]),a}function v(){var n=s("dark-mode-enabled"),e=n[0],t=n[1],o=f(["(prefers-color-scheme: dark)"],[!0],!1),i=void 0!==e?e:o;return r(function(){var n=window.document.body;i?n.classList.add("dark-mode"):n.classList.remove("dark-mode")},[i]),[i,t]}function m(n,t){var o=e(n),i=o[0],u=o[1];return r(function(){var e=setTimeout(function(){u(n)},t);return function(){clearTimeout(e)}},[n,t]),i}function w(n,r,i,u){void 0===n&&(n=!0),void 0===r&&(r=250),void 0===i&&(i={}),void 0===u&&(u=[]);var c=e(i),a=c[0],d=c[1],l=e(null),s=l[0],f=l[1],v=t(function(n){f(n)},[]);return o(function(){if(s){var e,t,o,i=function(){window.requestAnimationFrame(function(){var n=s.getBoundingClientRect();d(n)})};if(i(),n){var u=(e=i,t=r,function(){var n=[].slice.call(arguments),r=this,i=function(){o=null,e.apply(r,n)},u=void 0;clearTimeout(o),o=setTimeout(i,t),u&&e.apply(r,n)});if("ResizeObserver"in window){var c=new ResizeObserver(u);return c.observe(s),window.addEventListener("scroll",u),function(){c.disconnect(),window.removeEventListener("scroll",u)}}return window.addEventListener("resize",u),window.addEventListener("scroll",u),function(){window.removeEventListener("resize",u),window.removeEventListener("scroll",u)}}}},[s,n].concat(u)),[v,a,s]}var p=!("undefined"==typeof window||!window.document||!window.document.createElement);function h(n,e,t){void 0===t&&(t=p?window:void 0);var o=i();r(function(){o.current=e},[e]),r(function(){if(t&&t.addEventListener){var e=function(n){return o.current(n)};return t.addEventListener(n,e),function(){t.removeEventListener(n,e)}}},[n,t])}function g(n){return n&&n.current?Boolean(document.fullscreenElement===n.current||document.mozFullScreenElement===n.current||document.webkitFullscreenElement===n.current||document.msFullscreenElement===n.current):Boolean(document.fullscreenElement||document.mozFullScreenElement||document.webkitFullscreenElement||document.msFullscreenElement||document.fullscreen||document.mozFullScreen||document.webkitIsFullScreen||document.fullScreenMode)}function y(n){var t=!!p&&g(n),o=e(t),i=o[0],u=o[1];function c(){var e=n&&n.current||document.documentElement;return e.requestFullscreen?e.requestFullscreen():e.mozRequestFullScreen?e.mozRequestFullScreen():e.webkitRequestFullscreen?e.webkitRequestFullscreen():e.msRequestFullscreen?e.msRequestFullscreen():void 0}function a(){return document.exitFullscreen?document.exitFullscreen():document.mozCancelFullScreen?document.mozCancelFullScreen():document.webkitExitFullscreen?document.webkitExitFullscreen():document.msExitFullscreen?document.msExitFullscreen():void 0}return r(function(){function e(){u(g(n))}return document.addEventListener("webkitfullscreenchange",e,!1),document.addEventListener("mozfullscreenchange",e,!1),document.addEventListener("msfullscreenchange",e,!1),document.addEventListener("MSFullscreenChange",e,!1),document.addEventListener("fullscreenchange",e,!1),function(){document.removeEventListener("webkitfullscreenchange",e),document.removeEventListener("mozfullscreenchange",e),document.removeEventListener("msfullscreenchange",e),document.removeEventListener("MSFullscreenChange",e),document.removeEventListener("fullscreenchange",e)}},[n]),{fullScreen:i,open:c,close:a,toggle:i?a:c}}function E(){return(E=Object.assign||function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n}).apply(this,arguments)}var b={enableHighAccuracy:!1,timeout:Infinity,maximumAge:0},L=function(n,t){void 0===n&&(n=!1),void 0===t&&(t=b);var o=e({}),i=o[0],u=o[1],c=e(null),a=c[0],d=c[1],l=function(n){var e=n.coords;u({latitude:e.latitude,longitude:e.longitude,accuracy:e.accuracy,timestamp:n.timestamp})},s=function(n){d(n.message)};return r(function(){var e=navigator.geolocation;if(e){var r=null;return n?r=e.watchPosition(l,s,t):e.getCurrentPosition(l,s,t),function(){return r&&e.clearWatch(r)}}d("Geolocation is not supported")},[t]),E({},i,{error:a})};function S(){var n=e(!1),r=n[0],o=n[1],u=t(function(){return o(!0)},[]),c=t(function(){return o(!1)},[]),a=i();return[t(function(n){a.current&&(a.current.removeEventListener("mouseover",u),a.current.removeEventListener("mouseout",c)),a.current=n,a.current&&(a.current.addEventListener("mouseover",u),a.current.addEventListener("mouseout",c))},[u,c]),r]}function k(n,e,t,o){void 0===t&&(t=!1),void 0===o&&(o=[]);var u=i();r(function(){t&&n()},[].concat(o)),r(function(){u.current=n},[n]),r(function(){if(null!==e){var n=setInterval(function(){return u.current()},e);return function(){return clearInterval(n)}}},[e].concat(o))}var F=function(){return p};function R(n){var t=e(!1),r=t[0],o=t[1];return h("keydown",function(e){e.key===n&&o(!0)}),h("keyup",function(e){e.key===n&&o(!1)}),r}function x(){return{pathname:window.location.pathname,search:window.location.search}}var C=[];function O(){var n=e(x()),t=n[0],o=t.pathname,i=t.search,u=n[1];function c(){C.forEach(function(n){return n()})}function a(){u(x())}return r(function(){return C.push(a),window.addEventListener("popstate",a),function(){C.splice(C.indexOf(a),1),window.removeEventListener("popstate",a)}},[]),{push:function(n){window.history.pushState(null,null,n),c()},replace:function(n){window.history.replaceState(null,null,n),c()},pathname:o,search:i}}function z(){o(function(){var n=window.getComputedStyle(document.body).overflow;return document.body.style.overflow="hidden",function(){document.body.style.overflow=n}},[])}function M(){var n=e(new Set([])),t=n[0],r=n[1];return h("keydown",function(n){r(t.add(n.key))}),h("keyup",function(n){t.delete(n.key),r(t)}),t}var I=function(n,e){var r=t(function(){"Notification"in window&&("granted"!==Notification.permission?Notification.requestPermission().then(function(t){"granted"===t&&Notification(n,e)}):Notification(n,e))},[]);if("Notification"in window)return r};function N(n,e){r(function(){var t=function(t){n.current&&!n.current.contains(t.target)&&e(t)};return document.addEventListener("mousedown",t),document.addEventListener("touchstart",t),function(){document.removeEventListener("mousedown",t),document.removeEventListener("touchstart",t)}},[n,e])}function T(){var n=e("undefined"==typeof navigator||"boolean"!=typeof navigator.onLine||navigator.onLine),t=n[0],r=n[1];return h("online",function(){return r(!0)}),h("offline",function(){return r(!1)}),t}function q(n){var e=i();return r(function(){e.current=n},[n]),e.current}var P={position:"absolute",zIndex:999999,backgroundColor:"white",top:0,bottom:0,left:0,right:0},j=function(n){void 0===n&&(n={});var e=i(null);return r(function(){var t,r=function(n){return Object.entries(n).map(function(n){var e=n[1];return[n[0].split(/(?=[A-Z])/).join("-").toLowerCase(),e].join(":")}).join(";")}(E({},P,n)),o=function(){console.log("hi"),(t=document.createElement("div")).style=r;var n=e.current.cloneNode(!0);t.appendChild(n),document.body.appendChild(t)},i=function(){t.parentNode.removeChild(t)};return window.addEventListener("beforeprint",o),window.addEventListener("afterprint",i),function(){window.removeEventListener("beforeprint",o),window.removeEventListener("afterprint",i)}},[e,n]),{ref:e,handlePrint:function(){window.print()}}};function A(){var n=O(),e=n.replace,t=n.search;return{getParams:function(){var n=new URLSearchParams(t);return Object.fromEntries(n.entries())},setParams:function(n){var t=new URLSearchParams(n).toString();e("?"+t)}}}var U=[];function B(n){var t=e({loaded:!1,error:!1}),o=t[0],i=t[1];return r(function(){if(!U.includes(n)){U.push(n);var e=document.createElement("script");e.src=n,e.async=!0;var t=function(){i({loaded:!0,error:!1})},r=function(){var t=U.indexOf(n);t>=0&&U.splice(t,1),e.remove(),i({loaded:!0,error:!0})};return e.addEventListener("load",t),e.addEventListener("error",r),document.body.appendChild(e),function(){e.removeEventListener("load",t),e.removeEventListener("error",r)}}i({loaded:!0,error:!1})},[n]),[o.loaded,o.error]}var D=function(n,e){var o=i(function(){throw new Error("Cannot call an event handler while rendering.")});return r(function(){o.current=n},[n].concat(e)),t(function(n){return(0,o.current)(n)},[o])};p&&(window.SpeechRecognition=window.SpeechRecognition||window.webkitSpeechRecognition);var W=function(){},H=function(n){void 0===n&&(n={});var t=n.onEnd,o=void 0===t?W:t,u=n.onResult,c=void 0===u?W:u,a=n.onError,d=void 0===a?W:a,l=i(null),s=e(!1),f=s[0],v=s[1],m=!!window.SpeechRecognition||!!window.webkitSpeechRecognition,w=function(n){var e=Array.from(n.results).map(function(n){return n[0]}).map(function(n){return n.transcript});c(e)},p=function(n){"not-allowed"===n.error&&(l.current.onend=function(){},v(!1)),d(n)},h=D(function(n){if(void 0===n&&(n={}),!f&&m){var e=n.lang,t=void 0===e?"":e,r=n.interimResults,o=void 0===r||r,i=n.continuous,u=void 0!==i&&i,c=n.maxAlternatives,a=void 0===c?1:c,d=n.grammars;v(!0),l.current.lang=t,l.current.interimResults=o,l.current.onresult=w,l.current.onerror=p,l.current.continuous=u,l.current.maxAlternatives=a,d&&(l.current.grammars=d),l.current.onend=function(){return l.current.start()},l.current.start()}},[f,m,l]),g=D(function(){f&&m&&(l.current.onresult=function(){},l.current.onend=function(){},l.current.onerror=function(){},v(!1),l.current.stop(),o())},[f,m,l,o]);return r(function(){m&&(l.current=new window.SpeechRecognition)},[]),{listen:h,listening:f,stop:g,supported:m}},V=function(){},Y=function(n){void 0===n&&(n={});var t=n.onBoundary,o=n.onEnd,i=void 0===o?V:o,u=n.onError,c=void 0===u?V:u,a=n.onPause,d=void 0===a?V:a,l=n.onResume,s=void 0===l?V:l,f=e([]),v=f[0],m=f[1],w=e(!1),p=w[0],h=w[1],g=!!window.speechSynthesis,y=function(n){m(n)},E=function(){h(!1),i()},b=function(n){h(!1),c(n)};return r(function(){var n;return g&&((n=window.speechSynthesis.getVoices()).length>0?y(n):window.speechSynthesis.onvoiceschanged=function(e){n=e.target.getVoices(),y(n)}),function(){g&&window.speechSynthesis.cancel()}},[]),{supported:g,speak:function(n){void 0===n&&(n={});var e=n.voice,r=void 0===e?null:e,o=n.text,i=void 0===o?"":o,u=n.rate,c=void 0===u?1:u,a=n.pitch,l=void 0===a?1:a,f=n.volume,v=void 0===f?1:f,m=n.lang,w=void 0===m?"en-US":m,p=n.continuous,y=void 0!==p&&p;if(g){window.speechSynthesis.cancel();var L=new window.SpeechSynthesisUtterance;L.lang=w,L.text=i,L.voice=r,L.rate=c,L.pitch=l,L.volume=v,L.continuous=y,L.onend=E,L.onerror=b,L.onpause=d,L.onresume=s,t&&(L.onboundary=t),window.speechSynthesis.speak(L),h(!0)}},speaking:p,voices:v,cancel:function(){g&&(h(!1),window.speechSynthesis.cancel())},pause:function(){p&&g&&(window.speechSynthesis.pause(),h(!1))},resume:function(){!p&&g&&(window.speechSynthesis.resume(),h(!0))}}},J=function(n,t){void 0===t&&(t=200);var o=e(n),u=o[0],c=o[1],a=i(),d=i(null),l=i(0);return r(function(){a.current?(d.current=n,l.current=!0):(c(n),a.current=setTimeout(function n(){l.current?(l.current=!1,c(d.current),a.current=setTimeout(n,t)):a.current=void 0},t))},[n]),r(function(){return function(){return a.current&&clearTimeout(a.current)}}),u};function X(n,e){var t=i();r(function(){if(t.current){var r=Object.keys(E({},t.current,e)),o={};r.forEach(function(n){t.current[n]!==e[n]&&(o[n]={from:t.current[n],to:e[n]})}),Object.keys(o).length&&console.log("[why-did-you-update]",n,o)}t.current=e})}var G=function(){var n=e({x:p?window.pageXOffset:0,y:p?window.pageYOffset:0}),t=n[0],o=n[1];return r(function(){var n=function(){o({x:window.pageXOffset,y:window.pageYOffset})};return window.addEventListener("scroll",n,{capture:!1,passive:!0}),function(){window.removeEventListener("scroll",n)}},[]),t};function Z(n,t){var r=e({width:p?window.innerWidth:n,height:p?window.innerHeight:t}),o=r[0],i=r[1];return h("resize",function(){i({width:window.innerWidth,height:window.innerHeight})}),o}function K(n,t,o){var i=e(void 0),u=i[0],c=i[1];return r(function(){var e=new Worker(n,t);if(o)for(var r in o)e[r]=o[r];return c(e),function(){e.terminate(),c(void 0)}},[n]),u}export{u as useAdjustColor,c as useArray,a as useAsync,d as useCookie,l as useCopyClipboard,v as useDarkMode,m as useDebounce,w as useDimensions,h as useEventListener,y as useFullScreen,L as useGeolocation,S as useHover,k as useInterval,F as useIsClient,R as useKeyPress,s as useLocalStorage,O as useLocation,z as useLockBodyScroll,f as useMedia,M as useMultiKeyPress,I as useNotification,N as useOnClickOutside,T as useOnlineStatus,q as usePrevious,j as usePrint,A as useQueryParams,B as useScript,H as useSpeechRecognition,Y as useSpeechSynthesis,J as useThrottle,X as useWhyDidYouUpdate,G as useWindowScroll,Z as useWindowSize,K as useWorker};