UNPKG

@rexora/hooks

Version:

A modern, lightweight collection of reusable React hooks for everyday frontend development. Built to be flexible, composable, and framework-friendly.

2 lines (1 loc) 6.58 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react");function S(e,r){switch(r.type){case"SET_STATE":return{...e,...r.payload};case"RESET_STATE":return{};case"REMOVE_STATE":const{[r.key]:n,...u}=e;return u;default:throw new Error(`Unhandled action type: ${r.type}`)}}function h(e={}){const[r,n]=t.useReducer(S,e);return[r,(o,f)=>{if(typeof o=="string")n({type:"SET_STATE",payload:{[o]:f}});else if(typeof o=="object")n({type:"SET_STATE",payload:o});else throw new Error("Invalid key type. Key must be a string or an object.")},o=>{n({type:"REMOVE_STATE",key:o})},()=>{n({type:"RESET_STATE"})}]}function T(e,r){const[n,u]=t.useState(e);return t.useEffect(()=>{const s=setTimeout(()=>{u(e)},r);return()=>{clearTimeout(s)}},[e,r]),n}function x(e,r){let n=!1,u,s,a=null;const o=t.useCallback((...f)=>(s=f,n||(n=!0,u=e(...s),a=setTimeout(()=>{n=!1},r)),u),[e,r]);return o.cancel=()=>{a&&clearTimeout(a),n=!1},o}function y(e,r){const[n,u]=t.useState(e),s=typeof e=="function",a=t.useRef(null);return t.useEffect(()=>{if(s){const o=x(e,r);return a.current=o,()=>{a.current&&a.current.cancel&&a.current.cancel()}}else{const o=setTimeout(()=>{u(e)},r);return()=>{clearTimeout(o)}}},[e,r,s]),s?a.current:n}function L(e,r){const n=t.useRef(e),u=t.useRef(null);return t.useEffect(()=>{n.current=e},[e]),t.useEffect(()=>{if(r!==null){const a=setTimeout(()=>n.current(),r);return u.current=a,()=>clearTimeout(a)}return()=>{}},[r]),()=>{u.current&&(clearTimeout(u.current),u.current=null)}}function g(e,r){const n=t.useRef(e);t.useEffect(()=>{n.current=e},[e]),t.useEffect(()=>{function s(){n.current()}if(r!==null){const a=setInterval(s,r);return()=>clearInterval(a)}return()=>{}},[r]);const u=t.useRef(()=>{});return t.useEffect(()=>{if(r!==null){const s=setInterval(n.current,r);return u.current=()=>clearInterval(s),()=>clearInterval(s)}return u.current=()=>{},()=>{}},[r]),u.current}function I(e=!1){const[r,n]=t.useState(e),u=t.useCallback(()=>{n(s=>!s)},[]);return[r,u]}function b(e=!1){const[r,n]=t.useState(e);return[r,{on:()=>n(!0),off:()=>n(!1),toggle:()=>n(o=>!o)}]}function p(){const e=t.useRef(!1);return t.useEffect(()=>(e.current=!0,()=>{e.current=!1}),[]),e.current}const C=(e,r)=>{const n=t.useRef(!0);t.useEffect(()=>{if(n.current){n.current=!1;return}return e()},r)};function D(){const e=t.useRef(!0);return e.current?(e.current=!1,!0):!1}function A(){const[,e]=t.useState(0);return t.useCallback(()=>{e(n=>n+1)},[])}function M(e){const r=t.useRef(e);return r.current=e,r}const V=typeof window<"u"?t.useLayoutEffect:t.useEffect;function _(e){const[r,n]=t.useState(!1);return t.useEffect(()=>{const u=e.current;if(!u)return;const s=()=>n(!0),a=()=>n(!1);return u.addEventListener("mouseenter",s),u.addEventListener("mouseleave",a),()=>{u.removeEventListener("mouseenter",s),u.removeEventListener("mouseleave",a)}},[e]),r}function k(){const[e,r]=t.useState(navigator.onLine);return t.useEffect(()=>{const n=()=>r(!0),u=()=>r(!1);return window.addEventListener("online",n),window.addEventListener("offline",u),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",u)}},[]),e}const U=6e4;function P(e=U){const[r,n]=t.useState(!1),u=t.useRef(null),s=()=>{n(!1),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{n(!0)},e)};return t.useEffect(()=>(s(),window.addEventListener("mousemove",s),window.addEventListener("keydown",s),window.addEventListener("scroll",s),window.addEventListener("click",s),window.addEventListener("touchstart",s),window.addEventListener("touchmove",s),()=>{u.current&&clearTimeout(u.current),window.removeEventListener("mousemove",s),window.removeEventListener("keydown",s),window.removeEventListener("scroll",s),window.removeEventListener("click",s),window.removeEventListener("touchstart",s),window.removeEventListener("touchmove",s)}),[e]),r}function O(){const[e,r]=t.useState(()=>document.visibilityState);return t.useEffect(()=>{const n=()=>{r(document.visibilityState)};return document.addEventListener("visibilitychange",n),()=>{document.removeEventListener("visibilitychange",n)}},[]),e}function F(){const e=t.useRef(0);return e.current++,e.current}function H(e,r){const[n,u]=t.useState(null),[s,a]=t.useState(!0),[o,f]=t.useState(null);return t.useEffect(()=>{(async()=>{try{const c=await fetch(e,r);if(!c.ok)throw new Error(`HTTP error! status: ${c.status}`);const d=await c.json();u(d)}catch(c){f(c)}finally{a(!1)}})()},[e,r]),{data:n,loading:s,error:o}}function j(e){const[r,n]=t.useState({data:null,loading:!0,error:null}),u=t.useCallback(()=>{n({data:null,loading:!0,error:null}),e().then(s=>{n({data:s,loading:!1,error:null})}).catch(s=>{n({data:null,loading:!1,error:s})})},[e]);return t.useEffect(()=>{u()},[u]),r}function B(e,r={retries:3,delay:1e3},n=[]){const[u,s]=t.useState(),[a,o]=t.useState(!0),[f,R]=t.useState(null),[c,d]=t.useState(0),l=t.useRef(!0),E=t.useRef(e);t.useEffect(()=>{E.current=e},[e]),t.useCallback(async()=>{o(!0),R(null);try{const i=await E.current();l.current&&(s(i),o(!1))}catch(i){l.current&&(R(i),o(!1))}},[c]),t.useEffect(()=>{l.current=!0;let i=0;return(async()=>{for(;i<=r.retries;)try{const w=await e();l.current&&(s(w),o(!1),R(null));return}catch(w){l.current&&R(w),i++,i<=r.retries?await new Promise(m=>setTimeout(m,r.delay)):l.current&&o(!1)}})(),()=>{l.current=!1}},[...n,r.retries,r.delay,c]);const v=t.useCallback(()=>{d(i=>i+1)},[]);return{data:u,loading:a,error:f,retry:v}}function W(e,r,n=[]){const[u,s]=t.useState(null),[a,o]=t.useState(!1),[f,R]=t.useState(null),c=t.useRef(null),d=t.useRef(!1),l=async()=>{o(!0),R(null);try{const i=await e();d.current&&s(i)}catch(i){d.current&&R(i)}finally{d.current&&o(!1)}},E=()=>{c.current===null&&(l(),c.current=window.setInterval(l,r))},v=()=>{c.current!==null&&(window.clearInterval(c.current),c.current=null)};return t.useEffect(()=>(d.current=!0,E(),()=>{d.current=!1,v()}),[r,...n]),{data:u,loading:a,error:f,startPolling:E,stopPolling:v}}exports.useRexoraAsync=j;exports.useRexoraBoolean=b;exports.useRexoraDebounce=T;exports.useRexoraDocumentVisibility=O;exports.useRexoraFetch=H;exports.useRexoraFirstRender=D;exports.useRexoraForceUpdate=A;exports.useRexoraHover=_;exports.useRexoraIdle=P;exports.useRexoraInterval=g;exports.useRexoraIsomorphicLayoutEffect=V;exports.useRexoraLatest=M;exports.useRexoraMounted=p;exports.useRexoraOnline=k;exports.useRexoraPolling=W;exports.useRexoraRenderCount=F;exports.useRexoraRetry=B;exports.useRexoraState=h;exports.useRexoraThrottle=y;exports.useRexoraTimeout=L;exports.useRexoraToggle=I;exports.useRexoraUpdateEffect=C;