@alessiofrittoli/react-hooks 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -885,7 +885,7 @@ interface UseInputOutput<I = unknown, O = I> extends InputState<I, O> {
885
885
  */
886
886
  reset: () => void;
887
887
  /**
888
- * Call `focus` method to focus the Input Element. A ref object must be provided.
888
+ * Call `focus` method to focus the Input Element. `inputRef` must be provided in the input options.
889
889
  *
890
890
  */
891
891
  focus: () => void;
package/dist/index.d.ts CHANGED
@@ -885,7 +885,7 @@ interface UseInputOutput<I = unknown, O = I> extends InputState<I, O> {
885
885
  */
886
886
  reset: () => void;
887
887
  /**
888
- * Call `focus` method to focus the Input Element. A ref object must be provided.
888
+ * Call `focus` method to focus the Input Element. `inputRef` must be provided in the input options.
889
889
  *
890
890
  */
891
891
  focus: () => void;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _react = require('react');var _LocalStorage = require('@alessiofrittoli/web-utils/storage/LocalStorage');var _SessionStorage = require('@alessiofrittoli/web-utils/storage/SessionStorage');var H=(e,n,t="local")=>{let r=_react.useCallback.call(void 0, ()=>_nullishCoalesce((t==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).get(e), () => (n)),[t,e,n]),[i,u]=_react.useState.call(void 0, n),s=_react.useCallback.call(void 0, o=>{u(c=>{let l=o instanceof Function?o(c):o;return(typeof window<"u"&&t==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).set(e,l),l})},[t,e]);return _react.useEffect.call(void 0, ()=>{u(r())},[r]),[i,s]};var $=(e,n)=>H(e,n,"local");var qe=(e,n)=>H(e,n,"session");var j=e=>e?"online":"offline",Ze= exports.useConnection =()=>{let[e,n]=_react.useState.call(void 0, j(!0)),t=e==="online",r=e==="offline",i=_react.useCallback.call(void 0, ()=>n(j(navigator.onLine)),[]);return P(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:t,isOffline:r}};var h=()=>{let e=_react.useRef.call(void 0, !0);return e.current?(e.current=!1,!0):e.current};var I=(e,n)=>{let t=h();_react.useEffect.call(void 0, ()=>{if(!t)return e()},n)};var _webutils = require('@alessiofrittoli/web-utils');var R=e=>typeof e=="string"?_webutils.isEmpty.call(void 0, e):!e,C={value:"",isTouched:!1,isValid:!0},B=(e,n)=>{switch(n.type){case"TOUCHED":return{...e,isTouched:!0};case"CHANGE":return{...e,value:n.value};case"BLUR":return{...e,value:e.value,isTouched:!R(_nullishCoalesce(e.value, () => ("")))};case"RESET":return C}};var ft=(e={})=>{let{inputRef:n}=e,{initialValue:t}=e,{touchTimeout:r=600}=e,{validate:i,parse:u}=e,{onChange:s}=e,[o,c]=_react.useReducer.call(void 0, B,{...C,value:t}),l=u?u(o.value):o.value,{isTouched:a}=o,d=R(l),p=typeof i=="function"?i(l):!0,f=!p&&a||!!t&&!p;I(()=>{let T=setTimeout(()=>{R(l)||c({type:"TOUCHED"})},r);return()=>clearTimeout(T)},[l,r]);let y=_react.useCallback.call(void 0, T=>{let{target:x}=T,{type:v}=x,M=v==="checkbox"?x.checked:x.value;c({type:"CHANGE",value:M}),_optionalChain([s, 'optionalCall', _2 => _2(u?u(M):M)])},[s,u]),S=_react.useCallback.call(void 0, ()=>{c({type:"BLUR"})},[]),O=_react.useCallback.call(void 0, ()=>{c({type:"TOUCHED"})},[]),E=_react.useCallback.call(void 0, T=>{c({type:"CHANGE",value:T})},[]),g=_react.useCallback.call(void 0, ()=>{_optionalChain([n, 'optionalAccess', _3 => _3.current, 'optionalAccess', _4 => _4.focus, 'call', _5 => _5()])},[n]),V=_react.useCallback.call(void 0, ()=>{c({type:"RESET"})},[]);return{value:l,isTouched:a,isValid:p,isEmpty:d,hasError:f,changeHandler:y,blurHandler:S,setValue:E,submit:O,focus:g,reset:V}};var xt=e=>{let n=h();_react.useEffect.call(void 0, ()=>{if(n)return e()},[])};var N=()=>{let[e,n]=_react.useState.call(void 0, !1);return _react.useEffect.call(void 0, ()=>n(!0),[]),e};var _helpers = require('@alessiofrittoli/math-utils/helpers');var ht=(e={})=>_react.useMemo.call(void 0, ()=>_helpers.paginate.call(void 0, e),[e]);var Vt=(e,n=[])=>{let[t,r]=_react.useState.call(void 0, n),i=t.length>0,u=a=>t.includes(a),s=_react.useCallback.call(void 0, a=>r(d=>{let p=new Set(d);return p.has(a)?p.delete(a):p.add(a),Array.from(p.values())}),[]),o=_react.useCallback.call(void 0, a=>{r(d=>{if(d.length===0)return[a];let p=[...e],f=p.indexOf(d.at(0)),y=p.indexOf(a);if(f>y){let S=[...p].reverse(),O=S.indexOf(d.at(0)),E=S.indexOf(a);return S.slice(O,E+1).reverse()}return p.slice(f,y+1)})},[e]),c=_react.useCallback.call(void 0, ()=>{r(e)},[e]),l=_react.useCallback.call(void 0, a=>r(a?n:[]),[n]);return{selection:t,hasSelection:i,isSelected:u,setSelection:r,select:s,groupSelect:o,selectAll:c,resetSelection:l}};var _browserapi = require('@alessiofrittoli/web-utils/browser-api');function k(e,n={}){let{updateState:t=!0,onChange:r}=n,[i,u]=_react.useState.call(void 0, _browserapi.getMediaMatches.call(void 0, e)),s=_react.useCallback.call(void 0, ()=>{let o=_browserapi.getMediaMatches.call(void 0, e);t&&u(o),_optionalChain([r, 'optionalCall', _6 => _6(o)])},[e,t,r]);if(_react.useEffect.call(void 0, ()=>{let o=window.matchMedia(e),{matches:c}=o;return t&&u(c),_optionalChain([r, 'optionalCall', _7 => _7(c)]),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,t,r,s]),!!t)return i}var jt=(e={})=>{let n=N(),t=k("(prefers-color-scheme: dark)"),{initial:r=t,docClassNames:i=[]}=e,[u,s]=$("dark-mode",r),o=_nullishCoalesce(u, () => (t)),[c,l]=i,a=_react.useRef.call(void 0, {light:"",dark:""});return I(()=>{s(t)},[t,s]),_react.useEffect.call(void 0, ()=>{c&&document.documentElement.classList.toggle(c,o),l&&document.documentElement.classList.toggle(l,!o)},[o,c,l]),_react.useEffect.call(void 0, ()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(d=>{let p=d.getAttribute("media"),f=d.getAttribute("content");if(f){if(!p||p==="(prefers-color-scheme: light)"){a.current.light=f;return}a.current.dark=f}})},[]),I(()=>{let d=a.current.dark,p=a.current.light;u&&!d||!u&&!p||document.head.querySelectorAll('meta[name="theme-color"]').forEach(f=>{f.setAttribute("content",u?d:p)})},[u]),{isDarkMode:n?o:!1,isDarkOS:n?t:!1,toggleDarkMode:_react.useCallback.call(void 0, ()=>s(d=>!d),[s]),enableDarkMode:_react.useCallback.call(void 0, ()=>s(!0),[s]),disableDarkMode:_react.useCallback.call(void 0, ()=>s(!1),[s])}};function P(e,n){let{target:t,query:r,options:i,listener:u,onLoad:s,onCleanUp:o}=n;_react.useEffect.call(void 0, ()=>{let c=Array.isArray(e)?e:[e],l=_nullishCoalesce((r?window.matchMedia(r):t&&"current"in t?t.current:t), () => (window));if(l.addEventListener)return _optionalChain([s, 'optionalCall', _8 => _8()]),c.map(a=>{l.addEventListener(a,u,i)}),()=>{c.map(a=>{l.removeEventListener(a,u,i)}),_optionalChain([o, 'optionalCall', _9 => _9()])}},[e,t,r,i,u,s,o])}var _device = require('@alessiofrittoli/web-utils/device');var Xt=()=>k(_device.portraitMediaQuery);var ge=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),en= exports.useFocusTrap =e=>{let[n,t]=_react.useState.call(void 0, !1),r=_react.useRef.call(void 0, null),i=_react.useCallback.call(void 0, s=>{r.current=document.activeElement;let o=s||_optionalChain([e, 'optionalAccess', _10 => _10.current])||!1;if(o)return t(o)},[e]),u=_react.useCallback.call(void 0, ()=>{_optionalChain([r, 'access', _11 => _11.current, 'optionalAccess', _12 => _12.focus, 'call', _13 => _13()]),t(!1)},[]);return _react.useEffect.call(void 0, ()=>{if(!n)return;let s=o=>{if(o.key!=="Tab")return;let c=Array.from(n.querySelectorAll(ge)),l=c.at(0),a=c.at(-1);if(!o.shiftKey){document.activeElement===a&&(o.preventDefault(),_optionalChain([l, 'optionalAccess', _14 => _14.focus, 'call', _15 => _15()]));return}document.activeElement===l&&(o.preventDefault(),_optionalChain([a, 'optionalAccess', _16 => _16.focus, 'call', _17 => _17()]))};return document.addEventListener("keydown",s),()=>{document.removeEventListener("keydown",s)}},[n]),[i,u]};var on=(e,n={})=>{let{initial:t=!1,once:r,amount:i,margin:u,root:s,enable:o=!0}=n,{onEnter:c,onExit:l,onIntersect:a}=n,d=_react.useRef.call(void 0, !0),[p,f]=_react.useState.call(void 0, t),[y,S]=_react.useState.call(void 0, o),O=_react.useRef.call(void 0, null),E=_react.useRef.call(void 0, !1),g=_react.useMemo.call(void 0, ()=>{if(!y||typeof IntersectionObserver>"u")return;let V=i==="all"?1:i==="some"?.5:i;try{return new IntersectionObserver(async([T],x)=>{if(!T)return;let v=T.isIntersecting;try{if(E.current=!v&&!!O.current,v&&c&&await c({entry:T,observer:x}),E.current&&l&&await l({entry:T,observer:x}),a&&(v||!v&&O.current!=null)){let M={isEntering:v,isExiting:E.current};await a({entry:T,observer:x,...M})}if(O.current=v,!d.current)return;f(v)}catch(M){console.error(M)}v&&r&&x.disconnect()},{root:s||void 0,rootMargin:u,threshold:V})}catch(T){console.error(T)}},[s,u,i,r,y,c,l,a]);return _react.useEffect.call(void 0, ()=>{if(d.current=!0,!(!y||!e.current||!g))return g.observe(e.current),()=>{d.current=!1,g.disconnect()}},[e,g,y]),{inView:p,enabled:y,observer:g,isExiting:E.current,setInView:f,setEnabled:S}};var _dom = require('@alessiofrittoli/web-utils/dom');var ln=e=>{let n=_react.useCallback.call(void 0, ()=>_dom.blockScroll.call(void 0, _optionalChain([e, 'optionalAccess', _18 => _18.current])||void 0),[e]),t=_react.useCallback.call(void 0, ()=>_dom.restoreScroll.call(void 0, _optionalChain([e, 'optionalAccess', _19 => _19.current])||void 0),[e]);return[n,t]};var Y=(e,n={})=>{let{delay:t=1,args:r}=n;_react.useEffect.call(void 0, ()=>{let i=setTimeout(e,t,...r||[]);return()=>clearTimeout(i)},[t,r,e])};var En=(e,n=500)=>{let[t,r]=_react.useState.call(void 0, e),i=_react.useMemo.call(void 0, ()=>[e],[e]);return Y(r,{delay:n,args:i}),t};function Z(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,l]=_react.useState.call(void 0, i),a=_react.useCallback.call(void 0, ()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),d=_react.useCallback.call(void 0, ()=>{let f=a();return u&&(r?e(...r):e()),o.current=setInterval(e,t,...r||[]),!f&&s&&l(!0),o.current},[t,r,s,u,e,a]),p=_react.useCallback.call(void 0, ()=>{a()&&s&&l(!1)},[s,a]);return _react.useEffect.call(void 0, ()=>{if(i)return d(),p},[i,d,p]),s?{isActive:c,start:d,stop:p}:{start:d,stop:p}}var In=(e,n={})=>{let{delay:t=1,args:r}=n;_react.useEffect.call(void 0, ()=>{let i=setInterval(e,t,...r||[]);return()=>clearInterval(i)},[t,r,e])};function kn(e,n={}){let{autoplay:t=!0}=n,r=Z(e,{autoplay:!1,...n}),{start:i,stop:u}=r,s=_react.useCallback.call(void 0, ()=>document.hidden?u():i(),[i,u]),o=_react.useCallback.call(void 0, ()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=_react.useCallback.call(void 0, ()=>{u(),document.removeEventListener("visibilitychange",s)},[u,s]);return _react.useEffect.call(void 0, ()=>{if(t)return o(),c},[t,o,c]),{...r,start:o,stop:c}}function Dn(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,l]=_react.useState.call(void 0, i),a=_react.useCallback.call(void 0, ()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),d=_react.useCallback.call(void 0, ()=>{let f=a();return u&&(r?e(...r):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&l(!1),r)return e(...r);e()},t),!f&&s&&l(!0),o.current},[t,r,s,u,e,a]),p=_react.useCallback.call(void 0, ()=>{a()&&s&&l(!1)},[s,a]);return _react.useEffect.call(void 0, ()=>{if(i)return d(),p},[i,d,p]),s?{isActive:c,start:d,stop:p}:{start:d,stop:p}}exports.getState = j; exports.useConnection = Ze; exports.useDarkMode = jt; exports.useDebounce = En; exports.useEffectOnce = xt; exports.useEventListener = P; exports.useFocusTrap = en; exports.useInView = on; exports.useInput = ft; exports.useInterval = Z; exports.useIntervalWhenVisible = kn; exports.useIsClient = N; exports.useIsFirstRender = h; exports.useIsPortrait = Xt; exports.useLightInterval = In; exports.useLightTimeout = Y; exports.useLocalStorage = $; exports.useMediaQuery = k; exports.usePagination = ht; exports.useScrollBlock = ln; exports.useSelection = Vt; exports.useSessionStorage = qe; exports.useStorage = H; exports.useTimeout = Dn; exports.useUpdateEffect = I;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _react = require('react');var _LocalStorage = require('@alessiofrittoli/web-utils/storage/LocalStorage');var _SessionStorage = require('@alessiofrittoli/web-utils/storage/SessionStorage');var H=(e,n,t="local")=>{let r=_react.useCallback.call(void 0, ()=>_nullishCoalesce((t==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).get(e), () => (n)),[t,e,n]),[i,u]=_react.useState.call(void 0, n),s=_react.useCallback.call(void 0, o=>{u(c=>{let l=o instanceof Function?o(c):o;return(typeof window<"u"&&t==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).set(e,l),l})},[t,e]);return _react.useEffect.call(void 0, ()=>{u(r())},[r]),[i,s]};var $=(e,n)=>H(e,n,"local");var ze=(e,n)=>H(e,n,"session");var j=e=>e?"online":"offline",_e= exports.useConnection =()=>{let[e,n]=_react.useState.call(void 0, j(!0)),t=e==="online",r=e==="offline",i=_react.useCallback.call(void 0, ()=>n(j(navigator.onLine)),[]);return P(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:t,isOffline:r}};var h=()=>{let e=_react.useRef.call(void 0, !0);return e.current?(e.current=!1,!0):e.current};var I=(e,n)=>{let t=h();_react.useEffect.call(void 0, ()=>{if(!t)return e()},n)};var _webutils = require('@alessiofrittoli/web-utils');var R=e=>typeof e=="string"?_webutils.isEmpty.call(void 0, e):!e,C={value:"",isTouched:!1,isValid:!0},B=(e,n)=>{switch(n.type){case"TOUCHED":return{...e,isTouched:!0};case"CHANGE":return{...e,value:n.value};case"BLUR":return{...e,value:e.value,isTouched:!R(_nullishCoalesce(e.value, () => ("")))};case"RESET":return C}};var Tt=(e={})=>{let{inputRef:n}=e,{initialValue:t}=e,{touchTimeout:r=600}=e,{validate:i,parse:u}=e,{onChange:s}=e,[o,c]=_react.useReducer.call(void 0, B,{...C,value:t}),l=_react.useMemo.call(void 0, ()=>u?u(o.value):o.value,[o.value,u]),a=_react.useMemo.call(void 0, ()=>typeof i=="function"?i(l):!0,[l,i]),{isTouched:p}=o,d=R(l),f=!a&&p||!!t&&!a;I(()=>{let T=setTimeout(()=>{R(l)||c({type:"TOUCHED"})},r);return()=>clearTimeout(T)},[l,r]);let y=_react.useCallback.call(void 0, T=>{let{target:x}=T,{type:v}=x,M=v==="checkbox"?x.checked:x.value;c({type:"CHANGE",value:M}),_optionalChain([s, 'optionalCall', _2 => _2(u?u(M):M)])},[s,u]),S=_react.useCallback.call(void 0, ()=>{c({type:"BLUR"})},[]),O=_react.useCallback.call(void 0, ()=>{c({type:"TOUCHED"})},[]),E=_react.useCallback.call(void 0, T=>{c({type:"CHANGE",value:T})},[]),g=_react.useCallback.call(void 0, ()=>{_optionalChain([n, 'optionalAccess', _3 => _3.current, 'optionalAccess', _4 => _4.focus, 'call', _5 => _5()])},[n]),V=_react.useCallback.call(void 0, ()=>{c({type:"RESET"})},[]);return{value:l,isTouched:p,isValid:a,isEmpty:d,hasError:f,changeHandler:y,blurHandler:S,setValue:E,submit:O,focus:g,reset:V}};var St=e=>{let n=h();_react.useEffect.call(void 0, ()=>{if(n)return e()},[])};var G=()=>{let[e,n]=_react.useState.call(void 0, !1);return _react.useEffect.call(void 0, ()=>n(!0),[]),e};var _helpers = require('@alessiofrittoli/math-utils/helpers');var Rt=(e={})=>_react.useMemo.call(void 0, ()=>_helpers.paginate.call(void 0, e),[e]);var Ct=(e,n=[])=>{let[t,r]=_react.useState.call(void 0, n),i=t.length>0,u=a=>t.includes(a),s=_react.useCallback.call(void 0, a=>r(p=>{let d=new Set(p);return d.has(a)?d.delete(a):d.add(a),Array.from(d.values())}),[]),o=_react.useCallback.call(void 0, a=>{r(p=>{if(p.length===0)return[a];let d=[...e],f=d.indexOf(p.at(0)),y=d.indexOf(a);if(f>y){let S=[...d].reverse(),O=S.indexOf(p.at(0)),E=S.indexOf(a);return S.slice(O,E+1).reverse()}return d.slice(f,y+1)})},[e]),c=_react.useCallback.call(void 0, ()=>{r(e)},[e]),l=_react.useCallback.call(void 0, a=>r(a?n:[]),[n]);return{selection:t,hasSelection:i,isSelected:u,setSelection:r,select:s,groupSelect:o,selectAll:c,resetSelection:l}};var _browserapi = require('@alessiofrittoli/web-utils/browser-api');function k(e,n={}){let{updateState:t=!0,onChange:r}=n,[i,u]=_react.useState.call(void 0, _browserapi.getMediaMatches.call(void 0, e)),s=_react.useCallback.call(void 0, ()=>{let o=_browserapi.getMediaMatches.call(void 0, e);t&&u(o),_optionalChain([r, 'optionalCall', _6 => _6(o)])},[e,t,r]);if(_react.useEffect.call(void 0, ()=>{let o=window.matchMedia(e),{matches:c}=o;return t&&u(c),_optionalChain([r, 'optionalCall', _7 => _7(c)]),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,t,r,s]),!!t)return i}var Pt=(e={})=>{let n=G(),t=k("(prefers-color-scheme: dark)"),{initial:r=t,docClassNames:i=[]}=e,[u,s]=$("dark-mode",r),o=_nullishCoalesce(u, () => (t)),[c,l]=i,a=_react.useRef.call(void 0, {light:"",dark:""});return I(()=>{s(t)},[t,s]),_react.useEffect.call(void 0, ()=>{c&&document.documentElement.classList.toggle(c,o),l&&document.documentElement.classList.toggle(l,!o)},[o,c,l]),_react.useEffect.call(void 0, ()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(p=>{let d=p.getAttribute("media"),f=p.getAttribute("content");if(f){if(!d||d==="(prefers-color-scheme: light)"){a.current.light=f;return}a.current.dark=f}})},[]),I(()=>{let p=a.current.dark,d=a.current.light;u&&!p||!u&&!d||document.head.querySelectorAll('meta[name="theme-color"]').forEach(f=>{f.setAttribute("content",u?p:d)})},[u]),{isDarkMode:n?o:!1,isDarkOS:n?t:!1,toggleDarkMode:_react.useCallback.call(void 0, ()=>s(p=>!p),[s]),enableDarkMode:_react.useCallback.call(void 0, ()=>s(!0),[s]),disableDarkMode:_react.useCallback.call(void 0, ()=>s(!1),[s])}};function P(e,n){let{target:t,query:r,options:i,listener:u,onLoad:s,onCleanUp:o}=n;_react.useEffect.call(void 0, ()=>{let c=Array.isArray(e)?e:[e],l=_nullishCoalesce((r?window.matchMedia(r):t&&"current"in t?t.current:t), () => (window));if(l.addEventListener)return _optionalChain([s, 'optionalCall', _8 => _8()]),c.map(a=>{l.addEventListener(a,u,i)}),()=>{c.map(a=>{l.removeEventListener(a,u,i)}),_optionalChain([o, 'optionalCall', _9 => _9()])}},[e,t,r,i,u,s,o])}var _device = require('@alessiofrittoli/web-utils/device');var Yt=()=>k(_device.portraitMediaQuery);var Me=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),tn= exports.useFocusTrap =e=>{let[n,t]=_react.useState.call(void 0, !1),r=_react.useRef.call(void 0, null),i=_react.useCallback.call(void 0, s=>{r.current=document.activeElement;let o=s||_optionalChain([e, 'optionalAccess', _10 => _10.current])||!1;if(o)return t(o)},[e]),u=_react.useCallback.call(void 0, ()=>{_optionalChain([r, 'access', _11 => _11.current, 'optionalAccess', _12 => _12.focus, 'call', _13 => _13()]),t(!1)},[]);return _react.useEffect.call(void 0, ()=>{if(!n)return;let s=o=>{if(o.key!=="Tab")return;let c=Array.from(n.querySelectorAll(Me)),l=c.at(0),a=c.at(-1);if(!o.shiftKey){document.activeElement===a&&(o.preventDefault(),_optionalChain([l, 'optionalAccess', _14 => _14.focus, 'call', _15 => _15()]));return}document.activeElement===l&&(o.preventDefault(),_optionalChain([a, 'optionalAccess', _16 => _16.focus, 'call', _17 => _17()]))};return document.addEventListener("keydown",s),()=>{document.removeEventListener("keydown",s)}},[n]),[i,u]};var sn=(e,n={})=>{let{initial:t=!1,once:r,amount:i,margin:u,root:s,enable:o=!0}=n,{onEnter:c,onExit:l,onIntersect:a}=n,p=_react.useRef.call(void 0, !0),[d,f]=_react.useState.call(void 0, t),[y,S]=_react.useState.call(void 0, o),O=_react.useRef.call(void 0, null),E=_react.useRef.call(void 0, !1),g=_react.useMemo.call(void 0, ()=>{if(!y||typeof IntersectionObserver>"u")return;let V=i==="all"?1:i==="some"?.5:i;try{return new IntersectionObserver(async([T],x)=>{if(!T)return;let v=T.isIntersecting;try{if(E.current=!v&&!!O.current,v&&c&&await c({entry:T,observer:x}),E.current&&l&&await l({entry:T,observer:x}),a&&(v||!v&&O.current!=null)){let M={isEntering:v,isExiting:E.current};await a({entry:T,observer:x,...M})}if(O.current=v,!p.current)return;f(v)}catch(M){console.error(M)}v&&r&&x.disconnect()},{root:s||void 0,rootMargin:u,threshold:V})}catch(T){console.error(T)}},[s,u,i,r,y,c,l,a]);return _react.useEffect.call(void 0, ()=>{if(p.current=!0,!(!y||!e.current||!g))return g.observe(e.current),()=>{p.current=!1,g.disconnect()}},[e,g,y]),{inView:d,enabled:y,observer:g,isExiting:E.current,setInView:f,setEnabled:S}};var _dom = require('@alessiofrittoli/web-utils/dom');var pn=e=>{let n=_react.useCallback.call(void 0, ()=>_dom.blockScroll.call(void 0, _optionalChain([e, 'optionalAccess', _18 => _18.current])||void 0),[e]),t=_react.useCallback.call(void 0, ()=>_dom.restoreScroll.call(void 0, _optionalChain([e, 'optionalAccess', _19 => _19.current])||void 0),[e]);return[n,t]};var Z=(e,n={})=>{let{delay:t=1,args:r}=n;_react.useEffect.call(void 0, ()=>{let i=setTimeout(e,t,...r||[]);return()=>clearTimeout(i)},[t,r,e])};var xn=(e,n=500)=>{let[t,r]=_react.useState.call(void 0, e),i=_react.useMemo.call(void 0, ()=>[e],[e]);return Z(r,{delay:n,args:i}),t};function _(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,l]=_react.useState.call(void 0, i),a=_react.useCallback.call(void 0, ()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),p=_react.useCallback.call(void 0, ()=>{let f=a();return u&&(r?e(...r):e()),o.current=setInterval(e,t,...r||[]),!f&&s&&l(!0),o.current},[t,r,s,u,e,a]),d=_react.useCallback.call(void 0, ()=>{a()&&s&&l(!1)},[s,a]);return _react.useEffect.call(void 0, ()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}var Hn=(e,n={})=>{let{delay:t=1,args:r}=n;_react.useEffect.call(void 0, ()=>{let i=setInterval(e,t,...r||[]);return()=>clearInterval(i)},[t,r,e])};function Vn(e,n={}){let{autoplay:t=!0}=n,r=_(e,{autoplay:!1,...n}),{start:i,stop:u}=r,s=_react.useCallback.call(void 0, ()=>document.hidden?u():i(),[i,u]),o=_react.useCallback.call(void 0, ()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=_react.useCallback.call(void 0, ()=>{u(),document.removeEventListener("visibilitychange",s)},[u,s]);return _react.useEffect.call(void 0, ()=>{if(t)return o(),c},[t,o,c]),{...r,start:o,stop:c}}function Un(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,l]=_react.useState.call(void 0, i),a=_react.useCallback.call(void 0, ()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),p=_react.useCallback.call(void 0, ()=>{let f=a();return u&&(r?e(...r):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&l(!1),r)return e(...r);e()},t),!f&&s&&l(!0),o.current},[t,r,s,u,e,a]),d=_react.useCallback.call(void 0, ()=>{a()&&s&&l(!1)},[s,a]);return _react.useEffect.call(void 0, ()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}exports.getState = j; exports.useConnection = _e; exports.useDarkMode = Pt; exports.useDebounce = xn; exports.useEffectOnce = St; exports.useEventListener = P; exports.useFocusTrap = tn; exports.useInView = sn; exports.useInput = Tt; exports.useInterval = _; exports.useIntervalWhenVisible = Vn; exports.useIsClient = G; exports.useIsFirstRender = h; exports.useIsPortrait = Yt; exports.useLightInterval = Hn; exports.useLightTimeout = Z; exports.useLocalStorage = $; exports.useMediaQuery = k; exports.usePagination = Rt; exports.useScrollBlock = pn; exports.useSelection = Ct; exports.useSessionStorage = ze; exports.useStorage = H; exports.useTimeout = Un; exports.useUpdateEffect = I;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{useCallback as Q,useEffect as ee,useState as te}from"react";import{LocalStorage as F}from"@alessiofrittoli/web-utils/storage/LocalStorage";import{SessionStorage as $}from"@alessiofrittoli/web-utils/storage/SessionStorage";var h=(e,n,t="local")=>{let r=Q(()=>(t==="local"?F:$).get(e)??n,[t,e,n]),[i,u]=te(n),s=Q(o=>{u(c=>{let l=o instanceof Function?o(c):o;return(typeof window<"u"&&t==="local"?F:$).set(e,l),l})},[t,e]);return ee(()=>{u(r())},[r]),[i,s]};var j=(e,n)=>h(e,n,"local");var ze=(e,n)=>h(e,n,"session");import{useCallback as ne,useState as re}from"react";var P=e=>e?"online":"offline",_e=()=>{let[e,n]=re(P(!0)),t=e==="online",r=e==="offline",i=ne(()=>n(P(navigator.onLine)),[]);return B(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:t,isOffline:r}};import{useCallback as D,useEffect as z,useRef as ve}from"react";import{useCallback as I,useReducer as ae}from"react";import{useEffect as se}from"react";import{useRef as oe}from"react";var R=()=>{let e=oe(!0);return e.current?(e.current=!1,!0):e.current};var H=(e,n)=>{let t=R();se(()=>{if(!t)return e()},n)};import{isEmpty as ie}from"@alessiofrittoli/web-utils";var L=e=>typeof e=="string"?ie(e):!e,w={value:"",isTouched:!1,isValid:!0},N=(e,n)=>{switch(n.type){case"TOUCHED":return{...e,isTouched:!0};case"CHANGE":return{...e,value:n.value};case"BLUR":return{...e,value:e.value,isTouched:!L(e.value??"")};case"RESET":return w}};var Tt=(e={})=>{let{inputRef:n}=e,{initialValue:t}=e,{touchTimeout:r=600}=e,{validate:i,parse:u}=e,{onChange:s}=e,[o,c]=ae(N,{...w,value:t}),l=u?u(o.value):o.value,{isTouched:a}=o,d=L(l),p=typeof i=="function"?i(l):!0,T=!p&&a||!!t&&!p;H(()=>{let y=setTimeout(()=>{L(l)||c({type:"TOUCHED"})},r);return()=>clearTimeout(y)},[l,r]);let v=I(y=>{let{target:S}=y,{type:E}=S,b=E==="checkbox"?S.checked:S.value;c({type:"CHANGE",value:b}),s?.(u?u(b):b)},[s,u]),O=I(()=>{c({type:"BLUR"})},[]),g=I(()=>{c({type:"TOUCHED"})},[]),x=I(y=>{c({type:"CHANGE",value:y})},[]),M=I(()=>{n?.current?.focus()},[n]),C=I(()=>{c({type:"RESET"})},[]);return{value:l,isTouched:a,isValid:p,isEmpty:d,hasError:T,changeHandler:v,blurHandler:O,setValue:x,submit:g,focus:M,reset:C}};import{useEffect as ue}from"react";var St=e=>{let n=R();ue(()=>{if(n)return e()},[])};import{useEffect as ce,useState as le}from"react";var G=()=>{let[e,n]=le(!1);return ce(()=>n(!0),[]),e};import{useMemo as pe}from"react";import{paginate as de}from"@alessiofrittoli/math-utils/helpers";var Rt=(e={})=>pe(()=>de(e),[e]);import{useCallback as k,useState as me}from"react";var Ct=(e,n=[])=>{let[t,r]=me(n),i=t.length>0,u=a=>t.includes(a),s=k(a=>r(d=>{let p=new Set(d);return p.has(a)?p.delete(a):p.add(a),Array.from(p.values())}),[]),o=k(a=>{r(d=>{if(d.length===0)return[a];let p=[...e],T=p.indexOf(d.at(0)),v=p.indexOf(a);if(T>v){let O=[...p].reverse(),g=O.indexOf(d.at(0)),x=O.indexOf(a);return O.slice(g,x+1).reverse()}return p.slice(T,v+1)})},[e]),c=k(()=>{r(e)},[e]),l=k(a=>r(a?n:[]),[n]);return{selection:t,hasSelection:i,isSelected:u,setSelection:r,select:s,groupSelect:o,selectAll:c,resetSelection:l}};import{useCallback as fe,useEffect as Te,useState as ye}from"react";import{getMediaMatches as q}from"@alessiofrittoli/web-utils/browser-api";function V(e,n={}){let{updateState:t=!0,onChange:r}=n,[i,u]=ye(q(e)),s=fe(()=>{let o=q(e);t&&u(o),r?.(o)},[e,t,r]);if(Te(()=>{let o=window.matchMedia(e),{matches:c}=o;return t&&u(c),r?.(c),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,t,r,s]),!!t)return i}var Pt=(e={})=>{let n=G(),t=V("(prefers-color-scheme: dark)"),{initial:r=t,docClassNames:i=[]}=e,[u,s]=j("dark-mode",r),o=u??t,[c,l]=i,a=ve({light:"",dark:""});return H(()=>{s(t)},[t,s]),z(()=>{c&&document.documentElement.classList.toggle(c,o),l&&document.documentElement.classList.toggle(l,!o)},[o,c,l]),z(()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(d=>{let p=d.getAttribute("media"),T=d.getAttribute("content");if(T){if(!p||p==="(prefers-color-scheme: light)"){a.current.light=T;return}a.current.dark=T}})},[]),H(()=>{let d=a.current.dark,p=a.current.light;u&&!d||!u&&!p||document.head.querySelectorAll('meta[name="theme-color"]').forEach(T=>{T.setAttribute("content",u?d:p)})},[u]),{isDarkMode:n?o:!1,isDarkOS:n?t:!1,toggleDarkMode:D(()=>s(d=>!d),[s]),enableDarkMode:D(()=>s(!0),[s]),disableDarkMode:D(()=>s(!1),[s])}};import{useEffect as Ee}from"react";function B(e,n){let{target:t,query:r,options:i,listener:u,onLoad:s,onCleanUp:o}=n;Ee(()=>{let c=Array.isArray(e)?e:[e],l=(r?window.matchMedia(r):t&&"current"in t?t.current:t)??window;if(l.addEventListener)return s?.(),c.map(a=>{l.addEventListener(a,u,i)}),()=>{c.map(a=>{l.removeEventListener(a,u,i)}),o?.()}},[e,t,r,i,u,s,o])}import{portraitMediaQuery as xe}from"@alessiofrittoli/web-utils/device";var Yt=()=>V(xe);import{useCallback as J,useEffect as Se,useRef as Oe,useState as ge}from"react";var Me=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),tn=e=>{let[n,t]=ge(!1),r=Oe(null),i=J(s=>{r.current=document.activeElement;let o=s||e?.current||!1;if(o)return t(o)},[e]),u=J(()=>{r.current?.focus(),t(!1)},[]);return Se(()=>{if(!n)return;let s=o=>{if(o.key!=="Tab")return;let c=Array.from(n.querySelectorAll(Me)),l=c.at(0),a=c.at(-1);if(!o.shiftKey){document.activeElement===a&&(o.preventDefault(),l?.focus());return}document.activeElement===l&&(o.preventDefault(),a?.focus())};return document.addEventListener("keydown",s),()=>{document.removeEventListener("keydown",s)}},[n]),[i,u]};import{useEffect as be,useMemo as Ie,useRef as U,useState as X}from"react";var sn=(e,n={})=>{let{initial:t=!1,once:r,amount:i,margin:u,root:s,enable:o=!0}=n,{onEnter:c,onExit:l,onIntersect:a}=n,d=U(!0),[p,T]=X(t),[v,O]=X(o),g=U(null),x=U(!1),M=Ie(()=>{if(!v||typeof IntersectionObserver>"u")return;let C=i==="all"?1:i==="some"?.5:i;try{return new IntersectionObserver(async([y],S)=>{if(!y)return;let E=y.isIntersecting;try{if(x.current=!E&&!!g.current,E&&c&&await c({entry:y,observer:S}),x.current&&l&&await l({entry:y,observer:S}),a&&(E||!E&&g.current!=null)){let b={isEntering:E,isExiting:x.current};await a({entry:y,observer:S,...b})}if(g.current=E,!d.current)return;T(E)}catch(b){console.error(b)}E&&r&&S.disconnect()},{root:s||void 0,rootMargin:u,threshold:C})}catch(y){console.error(y)}},[s,u,i,r,v,c,l,a]);return be(()=>{if(d.current=!0,!(!v||!e.current||!M))return M.observe(e.current),()=>{d.current=!1,M.disconnect()}},[e,M,v]),{inView:p,enabled:v,observer:M,isExiting:x.current,setInView:T,setEnabled:O}};import{useCallback as Y}from"react";import{blockScroll as He,restoreScroll as he}from"@alessiofrittoli/web-utils/dom";var pn=e=>{let n=Y(()=>He(e?.current||void 0),[e]),t=Y(()=>he(e?.current||void 0),[e]);return[n,t]};import{useMemo as Le,useState as ke}from"react";import{useEffect as Re}from"react";var Z=(e,n={})=>{let{delay:t=1,args:r}=n;Re(()=>{let i=setTimeout(e,t,...r||[]);return()=>clearTimeout(i)},[t,r,e])};var xn=(e,n=500)=>{let[t,r]=ke(e),i=Le(()=>[e],[e]);return Z(r,{delay:n,args:i}),t};import{useCallback as K,useEffect as Ve,useRef as Ce,useState as we}from"react";function _(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=Ce(void 0),[c,l]=we(i),a=K(()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),d=K(()=>{let T=a();return u&&(r?e(...r):e()),o.current=setInterval(e,t,...r||[]),!T&&s&&l(!0),o.current},[t,r,s,u,e,a]),p=K(()=>{a()&&s&&l(!1)},[s,a]);return Ve(()=>{if(i)return d(),p},[i,d,p]),s?{isActive:c,start:d,stop:p}:{start:d,stop:p}}import{useEffect as De}from"react";var Hn=(e,n={})=>{let{delay:t=1,args:r}=n;De(()=>{let i=setInterval(e,t,...r||[]);return()=>clearInterval(i)},[t,r,e])};import{useCallback as A,useEffect as Ue}from"react";function Vn(e,n={}){let{autoplay:t=!0}=n,r=_(e,{autoplay:!1,...n}),{start:i,stop:u}=r,s=A(()=>document.hidden?u():i(),[i,u]),o=A(()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=A(()=>{u(),document.removeEventListener("visibilitychange",s)},[u,s]);return Ue(()=>{if(t)return o(),c},[t,o,c]),{...r,start:o,stop:c}}import{useCallback as W,useEffect as Ke,useRef as Ae,useState as We}from"react";function Un(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=Ae(void 0),[c,l]=We(i),a=W(()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),d=W(()=>{let T=a();return u&&(r?e(...r):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&l(!1),r)return e(...r);e()},t),!T&&s&&l(!0),o.current},[t,r,s,u,e,a]),p=W(()=>{a()&&s&&l(!1)},[s,a]);return Ke(()=>{if(i)return d(),p},[i,d,p]),s?{isActive:c,start:d,stop:p}:{start:d,stop:p}}export{P as getState,_e as useConnection,Pt as useDarkMode,xn as useDebounce,St as useEffectOnce,B as useEventListener,tn as useFocusTrap,sn as useInView,Tt as useInput,_ as useInterval,Vn as useIntervalWhenVisible,G as useIsClient,R as useIsFirstRender,Yt as useIsPortrait,Hn as useLightInterval,Z as useLightTimeout,j as useLocalStorage,V as useMediaQuery,Rt as usePagination,pn as useScrollBlock,Ct as useSelection,ze as useSessionStorage,h as useStorage,Un as useTimeout,H as useUpdateEffect};
1
+ import{useCallback as Q,useEffect as te,useState as ne}from"react";import{LocalStorage as F}from"@alessiofrittoli/web-utils/storage/LocalStorage";import{SessionStorage as $}from"@alessiofrittoli/web-utils/storage/SessionStorage";var h=(e,n,t="local")=>{let r=Q(()=>(t==="local"?F:$).get(e)??n,[t,e,n]),[i,u]=ne(n),s=Q(o=>{u(c=>{let l=o instanceof Function?o(c):o;return(typeof window<"u"&&t==="local"?F:$).set(e,l),l})},[t,e]);return te(()=>{u(r())},[r]),[i,s]};var j=(e,n)=>h(e,n,"local");var Je=(e,n)=>h(e,n,"session");import{useCallback as re,useState as oe}from"react";var P=e=>e?"online":"offline",et=()=>{let[e,n]=oe(P(!0)),t=e==="online",r=e==="offline",i=re(()=>n(P(navigator.onLine)),[]);return B(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:t,isOffline:r}};import{useCallback as D,useEffect as J,useRef as Ee}from"react";import{useCallback as I,useMemo as G,useReducer as ue}from"react";import{useEffect as ie}from"react";import{useRef as se}from"react";var R=()=>{let e=se(!0);return e.current?(e.current=!1,!0):e.current};var H=(e,n)=>{let t=R();ie(()=>{if(!t)return e()},n)};import{isEmpty as ae}from"@alessiofrittoli/web-utils";var L=e=>typeof e=="string"?ae(e):!e,w={value:"",isTouched:!1,isValid:!0},N=(e,n)=>{switch(n.type){case"TOUCHED":return{...e,isTouched:!0};case"CHANGE":return{...e,value:n.value};case"BLUR":return{...e,value:e.value,isTouched:!L(e.value??"")};case"RESET":return w}};var yt=(e={})=>{let{inputRef:n}=e,{initialValue:t}=e,{touchTimeout:r=600}=e,{validate:i,parse:u}=e,{onChange:s}=e,[o,c]=ue(N,{...w,value:t}),l=G(()=>u?u(o.value):o.value,[o.value,u]),a=G(()=>typeof i=="function"?i(l):!0,[l,i]),{isTouched:p}=o,d=L(l),T=!a&&p||!!t&&!a;H(()=>{let y=setTimeout(()=>{L(l)||c({type:"TOUCHED"})},r);return()=>clearTimeout(y)},[l,r]);let v=I(y=>{let{target:S}=y,{type:E}=S,b=E==="checkbox"?S.checked:S.value;c({type:"CHANGE",value:b}),s?.(u?u(b):b)},[s,u]),O=I(()=>{c({type:"BLUR"})},[]),g=I(()=>{c({type:"TOUCHED"})},[]),x=I(y=>{c({type:"CHANGE",value:y})},[]),M=I(()=>{n?.current?.focus()},[n]),C=I(()=>{c({type:"RESET"})},[]);return{value:l,isTouched:p,isValid:a,isEmpty:d,hasError:T,changeHandler:v,blurHandler:O,setValue:x,submit:g,focus:M,reset:C}};import{useEffect as ce}from"react";var Ot=e=>{let n=R();ce(()=>{if(n)return e()},[])};import{useEffect as le,useState as pe}from"react";var q=()=>{let[e,n]=pe(!1);return le(()=>n(!0),[]),e};import{useMemo as de}from"react";import{paginate as me}from"@alessiofrittoli/math-utils/helpers";var Lt=(e={})=>de(()=>me(e),[e]);import{useCallback as k,useState as fe}from"react";var wt=(e,n=[])=>{let[t,r]=fe(n),i=t.length>0,u=a=>t.includes(a),s=k(a=>r(p=>{let d=new Set(p);return d.has(a)?d.delete(a):d.add(a),Array.from(d.values())}),[]),o=k(a=>{r(p=>{if(p.length===0)return[a];let d=[...e],T=d.indexOf(p.at(0)),v=d.indexOf(a);if(T>v){let O=[...d].reverse(),g=O.indexOf(p.at(0)),x=O.indexOf(a);return O.slice(g,x+1).reverse()}return d.slice(T,v+1)})},[e]),c=k(()=>{r(e)},[e]),l=k(a=>r(a?n:[]),[n]);return{selection:t,hasSelection:i,isSelected:u,setSelection:r,select:s,groupSelect:o,selectAll:c,resetSelection:l}};import{useCallback as Te,useEffect as ye,useState as ve}from"react";import{getMediaMatches as z}from"@alessiofrittoli/web-utils/browser-api";function V(e,n={}){let{updateState:t=!0,onChange:r}=n,[i,u]=ve(z(e)),s=Te(()=>{let o=z(e);t&&u(o),r?.(o)},[e,t,r]);if(ye(()=>{let o=window.matchMedia(e),{matches:c}=o;return t&&u(c),r?.(c),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,t,r,s]),!!t)return i}var Bt=(e={})=>{let n=q(),t=V("(prefers-color-scheme: dark)"),{initial:r=t,docClassNames:i=[]}=e,[u,s]=j("dark-mode",r),o=u??t,[c,l]=i,a=Ee({light:"",dark:""});return H(()=>{s(t)},[t,s]),J(()=>{c&&document.documentElement.classList.toggle(c,o),l&&document.documentElement.classList.toggle(l,!o)},[o,c,l]),J(()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(p=>{let d=p.getAttribute("media"),T=p.getAttribute("content");if(T){if(!d||d==="(prefers-color-scheme: light)"){a.current.light=T;return}a.current.dark=T}})},[]),H(()=>{let p=a.current.dark,d=a.current.light;u&&!p||!u&&!d||document.head.querySelectorAll('meta[name="theme-color"]').forEach(T=>{T.setAttribute("content",u?p:d)})},[u]),{isDarkMode:n?o:!1,isDarkOS:n?t:!1,toggleDarkMode:D(()=>s(p=>!p),[s]),enableDarkMode:D(()=>s(!0),[s]),disableDarkMode:D(()=>s(!1),[s])}};import{useEffect as xe}from"react";function B(e,n){let{target:t,query:r,options:i,listener:u,onLoad:s,onCleanUp:o}=n;xe(()=>{let c=Array.isArray(e)?e:[e],l=(r?window.matchMedia(r):t&&"current"in t?t.current:t)??window;if(l.addEventListener)return s?.(),c.map(a=>{l.addEventListener(a,u,i)}),()=>{c.map(a=>{l.removeEventListener(a,u,i)}),o?.()}},[e,t,r,i,u,s,o])}import{portraitMediaQuery as Se}from"@alessiofrittoli/web-utils/device";var Zt=()=>V(Se);import{useCallback as X,useEffect as Oe,useRef as ge,useState as Me}from"react";var be=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),nn=e=>{let[n,t]=Me(!1),r=ge(null),i=X(s=>{r.current=document.activeElement;let o=s||e?.current||!1;if(o)return t(o)},[e]),u=X(()=>{r.current?.focus(),t(!1)},[]);return Oe(()=>{if(!n)return;let s=o=>{if(o.key!=="Tab")return;let c=Array.from(n.querySelectorAll(be)),l=c.at(0),a=c.at(-1);if(!o.shiftKey){document.activeElement===a&&(o.preventDefault(),l?.focus());return}document.activeElement===l&&(o.preventDefault(),a?.focus())};return document.addEventListener("keydown",s),()=>{document.removeEventListener("keydown",s)}},[n]),[i,u]};import{useEffect as Ie,useMemo as He,useRef as U,useState as Y}from"react";var an=(e,n={})=>{let{initial:t=!1,once:r,amount:i,margin:u,root:s,enable:o=!0}=n,{onEnter:c,onExit:l,onIntersect:a}=n,p=U(!0),[d,T]=Y(t),[v,O]=Y(o),g=U(null),x=U(!1),M=He(()=>{if(!v||typeof IntersectionObserver>"u")return;let C=i==="all"?1:i==="some"?.5:i;try{return new IntersectionObserver(async([y],S)=>{if(!y)return;let E=y.isIntersecting;try{if(x.current=!E&&!!g.current,E&&c&&await c({entry:y,observer:S}),x.current&&l&&await l({entry:y,observer:S}),a&&(E||!E&&g.current!=null)){let b={isEntering:E,isExiting:x.current};await a({entry:y,observer:S,...b})}if(g.current=E,!p.current)return;T(E)}catch(b){console.error(b)}E&&r&&S.disconnect()},{root:s||void 0,rootMargin:u,threshold:C})}catch(y){console.error(y)}},[s,u,i,r,v,c,l,a]);return Ie(()=>{if(p.current=!0,!(!v||!e.current||!M))return M.observe(e.current),()=>{p.current=!1,M.disconnect()}},[e,M,v]),{inView:d,enabled:v,observer:M,isExiting:x.current,setInView:T,setEnabled:O}};import{useCallback as Z}from"react";import{blockScroll as he,restoreScroll as Re}from"@alessiofrittoli/web-utils/dom";var dn=e=>{let n=Z(()=>he(e?.current||void 0),[e]),t=Z(()=>Re(e?.current||void 0),[e]);return[n,t]};import{useMemo as ke,useState as Ve}from"react";import{useEffect as Le}from"react";var _=(e,n={})=>{let{delay:t=1,args:r}=n;Le(()=>{let i=setTimeout(e,t,...r||[]);return()=>clearTimeout(i)},[t,r,e])};var Sn=(e,n=500)=>{let[t,r]=Ve(e),i=ke(()=>[e],[e]);return _(r,{delay:n,args:i}),t};import{useCallback as K,useEffect as Ce,useRef as we,useState as De}from"react";function ee(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=we(void 0),[c,l]=De(i),a=K(()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),p=K(()=>{let T=a();return u&&(r?e(...r):e()),o.current=setInterval(e,t,...r||[]),!T&&s&&l(!0),o.current},[t,r,s,u,e,a]),d=K(()=>{a()&&s&&l(!1)},[s,a]);return Ce(()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}import{useEffect as Ue}from"react";var hn=(e,n={})=>{let{delay:t=1,args:r}=n;Ue(()=>{let i=setInterval(e,t,...r||[]);return()=>clearInterval(i)},[t,r,e])};import{useCallback as A,useEffect as Ke}from"react";function Cn(e,n={}){let{autoplay:t=!0}=n,r=ee(e,{autoplay:!1,...n}),{start:i,stop:u}=r,s=A(()=>document.hidden?u():i(),[i,u]),o=A(()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=A(()=>{u(),document.removeEventListener("visibilitychange",s)},[u,s]);return Ke(()=>{if(t)return o(),c},[t,o,c]),{...r,start:o,stop:c}}import{useCallback as W,useEffect as Ae,useRef as We,useState as Qe}from"react";function Kn(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=We(void 0),[c,l]=Qe(i),a=W(()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),p=W(()=>{let T=a();return u&&(r?e(...r):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&l(!1),r)return e(...r);e()},t),!T&&s&&l(!0),o.current},[t,r,s,u,e,a]),d=W(()=>{a()&&s&&l(!1)},[s,a]);return Ae(()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}export{P as getState,et as useConnection,Bt as useDarkMode,Sn as useDebounce,Ot as useEffectOnce,B as useEventListener,nn as useFocusTrap,an as useInView,yt as useInput,ee as useInterval,Cn as useIntervalWhenVisible,q as useIsClient,R as useIsFirstRender,Zt as useIsPortrait,hn as useLightInterval,_ as useLightTimeout,j as useLocalStorage,V as useMediaQuery,Lt as usePagination,dn as useScrollBlock,wt as useSelection,Je as useSessionStorage,h as useStorage,Kn as useTimeout,H as useUpdateEffect};
package/license.md CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2024-2025 Alessio Frittoli.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024-2025 Alessio Frittoli.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/package.json CHANGED
@@ -1,134 +1,134 @@
1
- {
2
- "name": "@alessiofrittoli/react-hooks",
3
- "version": "3.2.0",
4
- "description": "TypeScript React utility Hooks",
5
- "author": {
6
- "name": "Alessio Frittoli",
7
- "email": "info@alessiofrittoli.it",
8
- "url": "https://alessiofrittoli.it"
9
- },
10
- "license": "MIT",
11
- "funding": [
12
- {
13
- "type": "github",
14
- "url": "https://github.com/sponsors/alessiofrittoli"
15
- }
16
- ],
17
- "keywords": [
18
- "react",
19
- "react-hooks"
20
- ],
21
- "homepage": "https://github.com/alessiofrittoli/react-hooks#readme",
22
- "bugs": {
23
- "url": "https://github.com/alessiofrittoli/react-hooks/issues",
24
- "email": "info@alessiofrittoli.it"
25
- },
26
- "repository": {
27
- "type": "git",
28
- "url": "git+https://github.com/alessiofrittoli/react-hooks.git"
29
- },
30
- "main": "./dist/index.js",
31
- "module": "./dist/index.mjs",
32
- "types": "./dist/index.d.ts",
33
- "files": [
34
- "dist"
35
- ],
36
- "exports": {
37
- ".": {
38
- "import": {
39
- "types": "./dist/index.d.mts",
40
- "default": "./dist/index.mjs"
41
- },
42
- "require": {
43
- "types": "./dist/index.d.ts",
44
- "default": "./dist/index.js"
45
- }
46
- },
47
- "./eslint": {
48
- "import": {
49
- "types": "./dist/eslint.d.mts",
50
- "default": "./dist/eslint.mjs"
51
- },
52
- "require": {
53
- "types": "./dist/eslint.d.ts",
54
- "default": "./dist/eslint.js"
55
- }
56
- }
57
- },
58
- "sideEffects": false,
59
- "scripts": {
60
- "//1a": "*********************************************************************",
61
- "//1b": "******************** DEV - BUILD - LINT - RELEASE *******************",
62
- "//1c": "*********************************************************************",
63
- "dev": "cross-env NODE_ENV=development tsup --watch",
64
- "build:prod": "cross-env NODE_ENV=production tsup",
65
- "build": "pnpm lint && pnpm test:ci && pnpm build:prod",
66
- "lint": "eslint",
67
- "release": "node scripts/publish.js --verbose --npm",
68
- "//2a": "*********************************************************************",
69
- "//2b": "***************************** UNIT TESTS ****************************",
70
- "//2c": "*********************************************************************",
71
- "test": "jest --verbose",
72
- "test:watch": "jest --watchAll --verbose",
73
- "test:ci": "jest --ci --verbose",
74
- "//3a": "*********************************************************************",
75
- "//3b": "************************ UNIT TESTS COVERAGE ************************",
76
- "//3c": "*********************************************************************",
77
- "test:coverage": "pnpm test:watch --coverage",
78
- "test:coverage:ci": "pnpm test:ci --coverage",
79
- "test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
80
- "test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\"",
81
- "//4a": "*********************************************************************",
82
- "//4b": "************************ TARGETED UNIT TESTS ************************",
83
- "//4c": "*********************************************************************",
84
- "test:storage": "pnpm test:watch browser-api/storage/*",
85
- "test:browser-api": "pnpm test:watch --testPathPattern='browser-api/(?!storage/).*\\.test\\.(js|jsx|ts|tsx)'",
86
- "test:dom-api": "pnpm test:watch dom-api/*",
87
- "test:misc": "pnpm test:watch misc/*",
88
- "test:timers": "pnpm test:watch timers/*"
89
- },
90
- "devDependencies": {
91
- "@alessiofrittoli/event-emitter": "^1.5.0",
92
- "@alessiofrittoli/node-scripts": "^2.6.0",
93
- "@eslint/compat": "^1.3.1",
94
- "@eslint/eslintrc": "^3.3.1",
95
- "@eslint/js": "^9.30.1",
96
- "@jest/globals": "^30.0.4",
97
- "@testing-library/dom": "^10.4.0",
98
- "@testing-library/jest-dom": "^6.6.3",
99
- "@testing-library/react": "^16.3.0",
100
- "@testing-library/user-event": "^14.6.1",
101
- "@types/jest": "^30.0.0",
102
- "@types/node": "^24.0.10",
103
- "@types/react": "^19.1.8",
104
- "@types/react-dom": "^19.1.6",
105
- "concurrently": "^9.2.0",
106
- "cross-env": "^7.0.3",
107
- "dotenv": "^17.1.0",
108
- "eslint": "^9.30.1",
109
- "eslint-plugin-react": "^7.37.5",
110
- "eslint-plugin-react-hooks": "^5.2.0",
111
- "globals": "^16.3.0",
112
- "http-server": "^14.1.1",
113
- "jest": "^30.0.4",
114
- "jest-environment-jsdom": "^30.0.4",
115
- "react": "^19.1.0",
116
- "react-dom": "^19.1.0",
117
- "ts-jest": "^29.4.0",
118
- "ts-node": "^10.9.2",
119
- "tsup": "^8.5.0",
120
- "typescript": "^5.8.3",
121
- "typescript-eslint": "^8.36.0"
122
- },
123
- "dependencies": {
124
- "@alessiofrittoli/math-utils": "^1.14.0",
125
- "@alessiofrittoli/type-utils": "^1.8.0",
126
- "@alessiofrittoli/web-utils": "^1.12.0"
127
- },
128
- "peerDependencies": {
129
- "@types/react": "^19",
130
- "@types/react-dom": "^19",
131
- "react": "^19",
132
- "react-dom": "^19"
133
- }
134
- }
1
+ {
2
+ "name": "@alessiofrittoli/react-hooks",
3
+ "version": "3.2.1",
4
+ "description": "TypeScript React utility Hooks",
5
+ "author": {
6
+ "name": "Alessio Frittoli",
7
+ "email": "info@alessiofrittoli.it",
8
+ "url": "https://alessiofrittoli.it"
9
+ },
10
+ "license": "MIT",
11
+ "funding": [
12
+ {
13
+ "type": "github",
14
+ "url": "https://github.com/sponsors/alessiofrittoli"
15
+ }
16
+ ],
17
+ "keywords": [
18
+ "react",
19
+ "react-hooks"
20
+ ],
21
+ "homepage": "https://github.com/alessiofrittoli/react-hooks#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/alessiofrittoli/react-hooks/issues",
24
+ "email": "info@alessiofrittoli.it"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/alessiofrittoli/react-hooks.git"
29
+ },
30
+ "main": "./dist/index.js",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.ts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "exports": {
37
+ ".": {
38
+ "import": {
39
+ "types": "./dist/index.d.mts",
40
+ "default": "./dist/index.mjs"
41
+ },
42
+ "require": {
43
+ "types": "./dist/index.d.ts",
44
+ "default": "./dist/index.js"
45
+ }
46
+ },
47
+ "./eslint": {
48
+ "import": {
49
+ "types": "./dist/eslint.d.mts",
50
+ "default": "./dist/eslint.mjs"
51
+ },
52
+ "require": {
53
+ "types": "./dist/eslint.d.ts",
54
+ "default": "./dist/eslint.js"
55
+ }
56
+ }
57
+ },
58
+ "sideEffects": false,
59
+ "scripts": {
60
+ "//1a": "*********************************************************************",
61
+ "//1b": "******************** DEV - BUILD - LINT - RELEASE *******************",
62
+ "//1c": "*********************************************************************",
63
+ "dev": "cross-env NODE_ENV=development tsup --watch",
64
+ "build:prod": "cross-env NODE_ENV=production tsup",
65
+ "build": "pnpm lint && pnpm test:ci && pnpm build:prod",
66
+ "lint": "eslint",
67
+ "release": "node scripts/publish.js --verbose --npm",
68
+ "//2a": "*********************************************************************",
69
+ "//2b": "***************************** UNIT TESTS ****************************",
70
+ "//2c": "*********************************************************************",
71
+ "test": "jest --verbose",
72
+ "test:watch": "jest --watchAll --verbose",
73
+ "test:ci": "jest --ci --verbose",
74
+ "//3a": "*********************************************************************",
75
+ "//3b": "************************ UNIT TESTS COVERAGE ************************",
76
+ "//3c": "*********************************************************************",
77
+ "test:coverage": "pnpm test:watch --coverage",
78
+ "test:coverage:ci": "pnpm test:ci --coverage",
79
+ "test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
80
+ "test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\"",
81
+ "//4a": "*********************************************************************",
82
+ "//4b": "************************ TARGETED UNIT TESTS ************************",
83
+ "//4c": "*********************************************************************",
84
+ "test:storage": "pnpm test:watch browser-api/storage/*",
85
+ "test:browser-api": "pnpm test:watch --testPathPattern='browser-api/(?!storage/).*\\.test\\.(js|jsx|ts|tsx)'",
86
+ "test:dom-api": "pnpm test:watch dom-api/*",
87
+ "test:misc": "pnpm test:watch misc/*",
88
+ "test:timers": "pnpm test:watch timers/*"
89
+ },
90
+ "devDependencies": {
91
+ "@alessiofrittoli/event-emitter": "^1.5.0",
92
+ "@alessiofrittoli/node-scripts": "^2.6.0",
93
+ "@eslint/compat": "^1.3.1",
94
+ "@eslint/eslintrc": "^3.3.1",
95
+ "@eslint/js": "^9.30.1",
96
+ "@jest/globals": "^30.0.4",
97
+ "@testing-library/dom": "^10.4.0",
98
+ "@testing-library/jest-dom": "^6.6.3",
99
+ "@testing-library/react": "^16.3.0",
100
+ "@testing-library/user-event": "^14.6.1",
101
+ "@types/jest": "^30.0.0",
102
+ "@types/node": "^24.0.10",
103
+ "@types/react": "^19.1.8",
104
+ "@types/react-dom": "^19.1.6",
105
+ "concurrently": "^9.2.0",
106
+ "cross-env": "^7.0.3",
107
+ "dotenv": "^17.1.0",
108
+ "eslint": "^9.30.1",
109
+ "eslint-plugin-react": "^7.37.5",
110
+ "eslint-plugin-react-hooks": "^5.2.0",
111
+ "globals": "^16.3.0",
112
+ "http-server": "^14.1.1",
113
+ "jest": "^30.0.4",
114
+ "jest-environment-jsdom": "^30.0.4",
115
+ "react": "^19.1.0",
116
+ "react-dom": "^19.1.0",
117
+ "ts-jest": "^29.4.0",
118
+ "ts-node": "^10.9.2",
119
+ "tsup": "^8.5.0",
120
+ "typescript": "^5.8.3",
121
+ "typescript-eslint": "^8.36.0"
122
+ },
123
+ "dependencies": {
124
+ "@alessiofrittoli/math-utils": "^1.14.0",
125
+ "@alessiofrittoli/type-utils": "^1.8.0",
126
+ "@alessiofrittoli/web-utils": "^1.12.0"
127
+ },
128
+ "peerDependencies": {
129
+ "@types/react": "^19",
130
+ "@types/react-dom": "^19",
131
+ "react": "^19",
132
+ "react-dom": "^19"
133
+ }
134
+ }