@alessiofrittoli/react-hooks 3.3.0 → 3.5.0

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
@@ -115,6 +115,60 @@ interface UseDarkModeOptions {
115
115
  */
116
116
  declare const useDarkMode: (options?: UseDarkModeOptions) => UseDarkModeOutput;
117
117
 
118
+ /**
119
+ * Handle Document visibility changes.
120
+ *
121
+ * @param isVisible Indicates whether the Document is visible or not.
122
+ */
123
+ type VisibilityChangeHandler = (isVisible: boolean) => void;
124
+ interface UseDocumentVisibilityOptions {
125
+ /**
126
+ * Whether to update React state about Document visibility state or not.
127
+ *
128
+ * @default true
129
+ */
130
+ updateState?: boolean;
131
+ /**
132
+ * A custom callback executed when Document visiblity sate changes.
133
+ *
134
+ */
135
+ onVisibilityChange?: VisibilityChangeHandler;
136
+ }
137
+ interface StateDisabledUseDocumentVisibilityOptions extends UseDocumentVisibilityOptions {
138
+ /**
139
+ * React state updates will be disabled.
140
+ *
141
+ * @default true
142
+ */
143
+ updateState: false;
144
+ }
145
+ /**
146
+ * Track the visibility state of the Document (i.e., whether the page is visible or hidden).
147
+ *
148
+ * @param options (Optional) Configuration options for the hook.
149
+ * @param options.updateState If `true` (default), the hook manages and returns the visibility state. If `false`, the hook does not manage state and only calls `onVisibilityChange`.
150
+ * @param options.onVisibilityChange Optional callback invoked whenever the Document's visibility changes. Receives the new visibility state as a boolean.
151
+ *
152
+ * @remarks
153
+ * - Uses the Page Visibility API (`document.hidden` and `visibilitychange` event).
154
+ * - The hook will not trigger re-renders on visibility changes since `updateState` has been set to `false`.
155
+ */
156
+ declare function useDocumentVisibility(otpions: StateDisabledUseDocumentVisibilityOptions): void;
157
+ /**
158
+ * Track the visibility state of the Document (i.e., whether the page is visible or hidden).
159
+ *
160
+ * @param options (Optional) Configuration options for the hook.
161
+ * @param options.updateState The hook will manage and return the visibility state.
162
+ * @param options.onVisibilityChange Optional callback invoked whenever the Document's visibility changes. Receives the new visibility state as a boolean.
163
+ *
164
+ * @returns Returns `true` if the Document is visible, `false` otherwise.
165
+ *
166
+ * @remarks
167
+ * - Uses the Page Visibility API (`document.hidden` and `visibilitychange` event).
168
+ * - The hook will trigger re-renders on visibility changes since `updateState` is `true` by default.
169
+ */
170
+ declare function useDocumentVisibility(otpions?: UseDocumentVisibilityOptions): boolean;
171
+
118
172
  /**
119
173
  * Specifies characteristics about the event listener.
120
174
  *
@@ -555,6 +609,93 @@ declare function useMediaQuery(query: string, options?: UseMediaQueryStateOption
555
609
  */
556
610
  declare function useMediaQuery(query: string, options?: UseMediaQueryOptions): void;
557
611
 
612
+ /**
613
+ * Defines a callback executed when a screen Wake Lock request fails.
614
+ *
615
+ * @param error The `DOMException` thrown by [`WakeLock.request()`](https://developer.mozilla.org/en-US/docs/Web/API/WakeLock/request).
616
+ */
617
+ type OnWakeLockRequestError = (error: DOMException) => void;
618
+ interface UseWakeLockOptions {
619
+ /**
620
+ * Indicates whether to request the screen Wake Lock on mount.
621
+ *
622
+ * @default true
623
+ */
624
+ onMount?: boolean;
625
+ /**
626
+ * A custom callback executed when a screen Wake Lock request fails.
627
+ *
628
+ * @param error The `DOMException` thrown by [`WakeLock.request()`](https://developer.mozilla.org/en-US/docs/Web/API/WakeLock/request).
629
+ */
630
+ onError?: OnWakeLockRequestError;
631
+ }
632
+ interface UseWakeLockBase {
633
+ /**
634
+ * Manually request the Wake Lock.
635
+ *
636
+ */
637
+ requestWakeLock: () => Promise<void>;
638
+ /**
639
+ * Manually release the Wake Lock.
640
+ *
641
+ */
642
+ releaseWakeLock: () => Promise<void>;
643
+ }
644
+ type UseWakeLock = UseWakeLockBase & ({
645
+ /**
646
+ * The **`WakeLockSentinel`** interface of the Screen Wake Lock API can be used to monitor the status of the platform screen wake lock, and manually release the lock when needed.
647
+ * Available only in secure contexts.
648
+ *
649
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WakeLockSentinel)
650
+ */
651
+ wakeLock: WakeLockSentinel;
652
+ /**
653
+ * Indicates whether the Wake Lock is enabled.
654
+ *
655
+ */
656
+ enabled: true;
657
+ } | {
658
+ /**
659
+ * The **`WakeLockSentinel`** interface of the Screen Wake Lock API returned by the `WakeLock` instance once has been enabled.
660
+ * Available only in secure contexts.
661
+ *
662
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WakeLockSentinel)
663
+ */
664
+ wakeLock: null;
665
+ /**
666
+ * Indicates whether the Wake Lock is enabled.
667
+ *
668
+ */
669
+ enabled: false;
670
+ });
671
+ /**
672
+ * React hook to manage the Screen Wake Lock API.
673
+ *
674
+ * This hook allows you to request and release a screen wake lock, preventing the device from dimming or locking the screen.
675
+ * It automatically handles wake lock requests based on document visibility and user interaction.
676
+ *
677
+ * @param options An object defining hook options. See {@link UseWakeLockOptions} for more info.
678
+ * @returns An object returning The current `WakeLockSentinel` instance or `null` if not enabled and utility functions. See {@link UseWakeLock} for more info.
679
+ *
680
+ * @example
681
+ * ```tsx
682
+ * const { enabled, requestWakeLock, releaseWakeLock } = useWakeLock()
683
+ *
684
+ * if ( ! enabled ) {
685
+ * // Request wake lock on button click
686
+ * <button onClick={ requestWakeLock }>Enable Wake Lock</button>
687
+ * }
688
+ *
689
+ * if ( enabled ) {
690
+ * // Release wake lock on button click
691
+ * <button onClick={ releaseWakeLock }>Disable Wake Lock</button>
692
+ * }
693
+ * ```
694
+ *
695
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API
696
+ */
697
+ declare const useWakeLock: (options?: UseWakeLockOptions) => UseWakeLock;
698
+
558
699
  type SetFocusTrap = (target?: HTMLElement) => void;
559
700
  type RestoreFocusTrap = () => void;
560
701
  /**
@@ -1359,4 +1500,4 @@ declare function useTimeout<T extends readonly unknown[]>(callback: TimerHandler
1359
1500
  */
1360
1501
  declare const useLightTimeout: <T extends readonly unknown[]>(callback: TimerHandler<T>, options?: BasicTimerOptions<T>) => void;
1361
1502
 
1362
- export { type AddEventListenerOptions, type BasicTimerOptions, type ChangeHandler, type CommonListenerOptions, type Connection, type CustomEventListenerOptions, type DocumentEventListener, type DocumentListenerOptions, type ElementEventListener, type ElementListenerOptions, type GroupSelectHandler, type InputState, type InputType, type IntersectionState, type IsSelectedHandler, type ListenerOptions, type MarginType, type MarginValue, type MediaQueryChangeListener, type MediaQueryEventListener, type MediaQueryListenerOptions, type OnChangeHandler, type OnIntersectHandler, type OnIntersectStateHandler, type ParseValueHandler, type ResetSelectionHandler, type SelectAllHandler, type SelectHandler, type SetSelectionHandler, type StartTimer, type StateTimerOptions, type StateTimerReturnType, type StopTimer, type TimerHandler, type TimerId, type TimerOptions, type TimerReturnType, type UseConnectionReturnType, type UseDarkModeOptions, type UseDarkModeOutput, type UseInViewOptions, type UseInViewReturnType, type UseInputOptions, type UseInputOutput, type UseIntervalWhenVisibleReturnType, type UseIntervalWhenVisibleStateReturnType, type UseMediaQueryOptions, type UseMediaQueryStateOptions, type UseSelectionReturnType, type ValidateValueHandler, type WindowEventListener, type WindowListenerOptions, getState, useConnection, useDarkMode, useDebounce, useDeferCallback, useEffectOnce, useEventListener, useFocusTrap, useInView, useInput, useInterval, useIntervalWhenVisible, useIsClient, useIsFirstRender, useIsPortrait, useLightInterval, useLightTimeout, useLocalStorage, useMediaQuery, usePagination, useScrollBlock, useSelection, useSessionStorage, useStorage, useTimeout, useUpdateEffect };
1503
+ export { type AddEventListenerOptions, type BasicTimerOptions, type ChangeHandler, type CommonListenerOptions, type Connection, type CustomEventListenerOptions, type DocumentEventListener, type DocumentListenerOptions, type ElementEventListener, type ElementListenerOptions, type GroupSelectHandler, type InputState, type InputType, type IntersectionState, type IsSelectedHandler, type ListenerOptions, type MarginType, type MarginValue, type MediaQueryChangeListener, type MediaQueryEventListener, type MediaQueryListenerOptions, type OnChangeHandler, type OnIntersectHandler, type OnIntersectStateHandler, type OnWakeLockRequestError, type ParseValueHandler, type ResetSelectionHandler, type SelectAllHandler, type SelectHandler, type SetSelectionHandler, type StartTimer, type StateDisabledUseDocumentVisibilityOptions, type StateTimerOptions, type StateTimerReturnType, type StopTimer, type TimerHandler, type TimerId, type TimerOptions, type TimerReturnType, type UseConnectionReturnType, type UseDarkModeOptions, type UseDarkModeOutput, type UseDocumentVisibilityOptions, type UseInViewOptions, type UseInViewReturnType, type UseInputOptions, type UseInputOutput, type UseIntervalWhenVisibleReturnType, type UseIntervalWhenVisibleStateReturnType, type UseMediaQueryOptions, type UseMediaQueryStateOptions, type UseSelectionReturnType, type UseWakeLock, type UseWakeLockBase, type UseWakeLockOptions, type ValidateValueHandler, type VisibilityChangeHandler, type WindowEventListener, type WindowListenerOptions, getState, useConnection, useDarkMode, useDebounce, useDeferCallback, useDocumentVisibility, useEffectOnce, useEventListener, useFocusTrap, useInView, useInput, useInterval, useIntervalWhenVisible, useIsClient, useIsFirstRender, useIsPortrait, useLightInterval, useLightTimeout, useLocalStorage, useMediaQuery, usePagination, useScrollBlock, useSelection, useSessionStorage, useStorage, useTimeout, useUpdateEffect, useWakeLock };
package/dist/index.d.ts CHANGED
@@ -115,6 +115,60 @@ interface UseDarkModeOptions {
115
115
  */
116
116
  declare const useDarkMode: (options?: UseDarkModeOptions) => UseDarkModeOutput;
117
117
 
118
+ /**
119
+ * Handle Document visibility changes.
120
+ *
121
+ * @param isVisible Indicates whether the Document is visible or not.
122
+ */
123
+ type VisibilityChangeHandler = (isVisible: boolean) => void;
124
+ interface UseDocumentVisibilityOptions {
125
+ /**
126
+ * Whether to update React state about Document visibility state or not.
127
+ *
128
+ * @default true
129
+ */
130
+ updateState?: boolean;
131
+ /**
132
+ * A custom callback executed when Document visiblity sate changes.
133
+ *
134
+ */
135
+ onVisibilityChange?: VisibilityChangeHandler;
136
+ }
137
+ interface StateDisabledUseDocumentVisibilityOptions extends UseDocumentVisibilityOptions {
138
+ /**
139
+ * React state updates will be disabled.
140
+ *
141
+ * @default true
142
+ */
143
+ updateState: false;
144
+ }
145
+ /**
146
+ * Track the visibility state of the Document (i.e., whether the page is visible or hidden).
147
+ *
148
+ * @param options (Optional) Configuration options for the hook.
149
+ * @param options.updateState If `true` (default), the hook manages and returns the visibility state. If `false`, the hook does not manage state and only calls `onVisibilityChange`.
150
+ * @param options.onVisibilityChange Optional callback invoked whenever the Document's visibility changes. Receives the new visibility state as a boolean.
151
+ *
152
+ * @remarks
153
+ * - Uses the Page Visibility API (`document.hidden` and `visibilitychange` event).
154
+ * - The hook will not trigger re-renders on visibility changes since `updateState` has been set to `false`.
155
+ */
156
+ declare function useDocumentVisibility(otpions: StateDisabledUseDocumentVisibilityOptions): void;
157
+ /**
158
+ * Track the visibility state of the Document (i.e., whether the page is visible or hidden).
159
+ *
160
+ * @param options (Optional) Configuration options for the hook.
161
+ * @param options.updateState The hook will manage and return the visibility state.
162
+ * @param options.onVisibilityChange Optional callback invoked whenever the Document's visibility changes. Receives the new visibility state as a boolean.
163
+ *
164
+ * @returns Returns `true` if the Document is visible, `false` otherwise.
165
+ *
166
+ * @remarks
167
+ * - Uses the Page Visibility API (`document.hidden` and `visibilitychange` event).
168
+ * - The hook will trigger re-renders on visibility changes since `updateState` is `true` by default.
169
+ */
170
+ declare function useDocumentVisibility(otpions?: UseDocumentVisibilityOptions): boolean;
171
+
118
172
  /**
119
173
  * Specifies characteristics about the event listener.
120
174
  *
@@ -555,6 +609,93 @@ declare function useMediaQuery(query: string, options?: UseMediaQueryStateOption
555
609
  */
556
610
  declare function useMediaQuery(query: string, options?: UseMediaQueryOptions): void;
557
611
 
612
+ /**
613
+ * Defines a callback executed when a screen Wake Lock request fails.
614
+ *
615
+ * @param error The `DOMException` thrown by [`WakeLock.request()`](https://developer.mozilla.org/en-US/docs/Web/API/WakeLock/request).
616
+ */
617
+ type OnWakeLockRequestError = (error: DOMException) => void;
618
+ interface UseWakeLockOptions {
619
+ /**
620
+ * Indicates whether to request the screen Wake Lock on mount.
621
+ *
622
+ * @default true
623
+ */
624
+ onMount?: boolean;
625
+ /**
626
+ * A custom callback executed when a screen Wake Lock request fails.
627
+ *
628
+ * @param error The `DOMException` thrown by [`WakeLock.request()`](https://developer.mozilla.org/en-US/docs/Web/API/WakeLock/request).
629
+ */
630
+ onError?: OnWakeLockRequestError;
631
+ }
632
+ interface UseWakeLockBase {
633
+ /**
634
+ * Manually request the Wake Lock.
635
+ *
636
+ */
637
+ requestWakeLock: () => Promise<void>;
638
+ /**
639
+ * Manually release the Wake Lock.
640
+ *
641
+ */
642
+ releaseWakeLock: () => Promise<void>;
643
+ }
644
+ type UseWakeLock = UseWakeLockBase & ({
645
+ /**
646
+ * The **`WakeLockSentinel`** interface of the Screen Wake Lock API can be used to monitor the status of the platform screen wake lock, and manually release the lock when needed.
647
+ * Available only in secure contexts.
648
+ *
649
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WakeLockSentinel)
650
+ */
651
+ wakeLock: WakeLockSentinel;
652
+ /**
653
+ * Indicates whether the Wake Lock is enabled.
654
+ *
655
+ */
656
+ enabled: true;
657
+ } | {
658
+ /**
659
+ * The **`WakeLockSentinel`** interface of the Screen Wake Lock API returned by the `WakeLock` instance once has been enabled.
660
+ * Available only in secure contexts.
661
+ *
662
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WakeLockSentinel)
663
+ */
664
+ wakeLock: null;
665
+ /**
666
+ * Indicates whether the Wake Lock is enabled.
667
+ *
668
+ */
669
+ enabled: false;
670
+ });
671
+ /**
672
+ * React hook to manage the Screen Wake Lock API.
673
+ *
674
+ * This hook allows you to request and release a screen wake lock, preventing the device from dimming or locking the screen.
675
+ * It automatically handles wake lock requests based on document visibility and user interaction.
676
+ *
677
+ * @param options An object defining hook options. See {@link UseWakeLockOptions} for more info.
678
+ * @returns An object returning The current `WakeLockSentinel` instance or `null` if not enabled and utility functions. See {@link UseWakeLock} for more info.
679
+ *
680
+ * @example
681
+ * ```tsx
682
+ * const { enabled, requestWakeLock, releaseWakeLock } = useWakeLock()
683
+ *
684
+ * if ( ! enabled ) {
685
+ * // Request wake lock on button click
686
+ * <button onClick={ requestWakeLock }>Enable Wake Lock</button>
687
+ * }
688
+ *
689
+ * if ( enabled ) {
690
+ * // Release wake lock on button click
691
+ * <button onClick={ releaseWakeLock }>Disable Wake Lock</button>
692
+ * }
693
+ * ```
694
+ *
695
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API
696
+ */
697
+ declare const useWakeLock: (options?: UseWakeLockOptions) => UseWakeLock;
698
+
558
699
  type SetFocusTrap = (target?: HTMLElement) => void;
559
700
  type RestoreFocusTrap = () => void;
560
701
  /**
@@ -1359,4 +1500,4 @@ declare function useTimeout<T extends readonly unknown[]>(callback: TimerHandler
1359
1500
  */
1360
1501
  declare const useLightTimeout: <T extends readonly unknown[]>(callback: TimerHandler<T>, options?: BasicTimerOptions<T>) => void;
1361
1502
 
1362
- export { type AddEventListenerOptions, type BasicTimerOptions, type ChangeHandler, type CommonListenerOptions, type Connection, type CustomEventListenerOptions, type DocumentEventListener, type DocumentListenerOptions, type ElementEventListener, type ElementListenerOptions, type GroupSelectHandler, type InputState, type InputType, type IntersectionState, type IsSelectedHandler, type ListenerOptions, type MarginType, type MarginValue, type MediaQueryChangeListener, type MediaQueryEventListener, type MediaQueryListenerOptions, type OnChangeHandler, type OnIntersectHandler, type OnIntersectStateHandler, type ParseValueHandler, type ResetSelectionHandler, type SelectAllHandler, type SelectHandler, type SetSelectionHandler, type StartTimer, type StateTimerOptions, type StateTimerReturnType, type StopTimer, type TimerHandler, type TimerId, type TimerOptions, type TimerReturnType, type UseConnectionReturnType, type UseDarkModeOptions, type UseDarkModeOutput, type UseInViewOptions, type UseInViewReturnType, type UseInputOptions, type UseInputOutput, type UseIntervalWhenVisibleReturnType, type UseIntervalWhenVisibleStateReturnType, type UseMediaQueryOptions, type UseMediaQueryStateOptions, type UseSelectionReturnType, type ValidateValueHandler, type WindowEventListener, type WindowListenerOptions, getState, useConnection, useDarkMode, useDebounce, useDeferCallback, useEffectOnce, useEventListener, useFocusTrap, useInView, useInput, useInterval, useIntervalWhenVisible, useIsClient, useIsFirstRender, useIsPortrait, useLightInterval, useLightTimeout, useLocalStorage, useMediaQuery, usePagination, useScrollBlock, useSelection, useSessionStorage, useStorage, useTimeout, useUpdateEffect };
1503
+ export { type AddEventListenerOptions, type BasicTimerOptions, type ChangeHandler, type CommonListenerOptions, type Connection, type CustomEventListenerOptions, type DocumentEventListener, type DocumentListenerOptions, type ElementEventListener, type ElementListenerOptions, type GroupSelectHandler, type InputState, type InputType, type IntersectionState, type IsSelectedHandler, type ListenerOptions, type MarginType, type MarginValue, type MediaQueryChangeListener, type MediaQueryEventListener, type MediaQueryListenerOptions, type OnChangeHandler, type OnIntersectHandler, type OnIntersectStateHandler, type OnWakeLockRequestError, type ParseValueHandler, type ResetSelectionHandler, type SelectAllHandler, type SelectHandler, type SetSelectionHandler, type StartTimer, type StateDisabledUseDocumentVisibilityOptions, type StateTimerOptions, type StateTimerReturnType, type StopTimer, type TimerHandler, type TimerId, type TimerOptions, type TimerReturnType, type UseConnectionReturnType, type UseDarkModeOptions, type UseDarkModeOutput, type UseDocumentVisibilityOptions, type UseInViewOptions, type UseInViewReturnType, type UseInputOptions, type UseInputOutput, type UseIntervalWhenVisibleReturnType, type UseIntervalWhenVisibleStateReturnType, type UseMediaQueryOptions, type UseMediaQueryStateOptions, type UseSelectionReturnType, type UseWakeLock, type UseWakeLockBase, type UseWakeLockOptions, type ValidateValueHandler, type VisibilityChangeHandler, type WindowEventListener, type WindowListenerOptions, getState, useConnection, useDarkMode, useDebounce, useDeferCallback, useDocumentVisibility, useEffectOnce, useEventListener, useFocusTrap, useInView, useInput, useInterval, useIntervalWhenVisible, useIsClient, useIsFirstRender, useIsPortrait, useLightInterval, useLightTimeout, useLocalStorage, useMediaQuery, usePagination, useScrollBlock, useSelection, useSessionStorage, useStorage, useTimeout, useUpdateEffect, useWakeLock };
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 Xe=(e,n)=>H(e,n,"session");var j=e=>e?"online":"offline",tt= 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 vt=(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 gt=(e,n)=>_react.useCallback.call(void 0, _webutils.deferCallback.call(void 0, e),n);var ht=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 Ut=(e={})=>_react.useMemo.call(void 0, ()=>_helpers.paginate.call(void 0, e),[e]);var Qt=(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)}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 Xt=(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 on=()=>k(_device.portraitMediaQuery);var Ie=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),cn= 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(Ie)),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 mn=(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 En=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 Hn=(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 Dn=(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 Wn(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 jn(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 = tt; exports.useDarkMode = Xt; exports.useDebounce = Hn; exports.useDeferCallback = gt; exports.useEffectOnce = ht; exports.useEventListener = P; exports.useFocusTrap = cn; exports.useInView = mn; exports.useInput = vt; exports.useInterval = _; exports.useIntervalWhenVisible = Wn; exports.useIsClient = G; exports.useIsFirstRender = h; exports.useIsPortrait = on; exports.useLightInterval = Dn; exports.useLightTimeout = Z; exports.useLocalStorage = $; exports.useMediaQuery = k; exports.usePagination = Ut; exports.useScrollBlock = En; exports.useSelection = Qt; exports.useSessionStorage = Xe; exports.useStorage = H; exports.useTimeout = jn; 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,r="local")=>{let t=_react.useCallback.call(void 0, ()=>_nullishCoalesce((r==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).get(e), () => (n)),[r,e,n]),[i,a]=_react.useState.call(void 0, n),s=_react.useCallback.call(void 0, o=>{a(c=>{let p=o instanceof Function?o(c):o;return(typeof window<"u"&&r==="local"?_LocalStorage.LocalStorage:_SessionStorage.SessionStorage).set(e,p),p})},[r,e]);return _react.useEffect.call(void 0, ()=>{a(t())},[t]),[i,s]};var $=(e,n)=>h(e,n,"local");var at=(e,n)=>h(e,n,"session");var j=e=>e?"online":"offline",dt= exports.useConnection =()=>{let[e,n]=_react.useState.call(void 0, j(!0)),r=e==="online",t=e==="offline",i=_react.useCallback.call(void 0, ()=>n(j(navigator.onLine)),[]);return B(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:r,isOffline:t}};var I=()=>{let e=_react.useRef.call(void 0, !0);return e.current?(e.current=!1,!0):e.current};var M=(e,n)=>{let r=I();_react.useEffect.call(void 0, ()=>{if(!r)return e()},n)};var _webutils = require('@alessiofrittoli/web-utils');var V=e=>typeof e=="string"?_webutils.isEmpty.call(void 0, 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:!V(_nullishCoalesce(e.value, () => ("")))};case"RESET":return w}};var It=(e={})=>{let{inputRef:n}=e,{initialValue:r}=e,{touchTimeout:t=600}=e,{validate:i,parse:a}=e,{onChange:s}=e,[o,c]=_react.useReducer.call(void 0, N,{...w,value:r}),p=_react.useMemo.call(void 0, ()=>a?a(o.value):o.value,[o.value,a]),u=_react.useMemo.call(void 0, ()=>typeof i=="function"?i(p):!0,[p,i]),{isTouched:l}=o,d=V(p),f=!u&&l||!!r&&!u;M(()=>{let T=setTimeout(()=>{V(p)||c({type:"TOUCHED"})},t);return()=>clearTimeout(T)},[p,t]);let y=_react.useCallback.call(void 0, T=>{let{target:x}=T,{type:v}=x,k=v==="checkbox"?x.checked:x.value;c({type:"CHANGE",value:k}),_optionalChain([s, 'optionalCall', _2 => _2(a?a(k):k)])},[s,a]),S=_react.useCallback.call(void 0, ()=>{c({type:"BLUR"})},[]),b=_react.useCallback.call(void 0, ()=>{c({type:"TOUCHED"})},[]),E=_react.useCallback.call(void 0, T=>{c({type:"CHANGE",value:T})},[]),O=_react.useCallback.call(void 0, ()=>{_optionalChain([n, 'optionalAccess', _3 => _3.current, 'optionalAccess', _4 => _4.focus, 'call', _5 => _5()])},[n]),D=_react.useCallback.call(void 0, ()=>{c({type:"RESET"})},[]);return{value:p,isTouched:l,isValid:u,isEmpty:d,hasError:f,changeHandler:y,blurHandler:S,setValue:E,submit:b,focus:O,reset:D}};var Dt=(e,n)=>_react.useCallback.call(void 0, _webutils.deferCallback.call(void 0, e),n);var H=e=>{let n=I();_react.useEffect.call(void 0, ()=>{if(n)return e()},[])};var _=()=>{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 Bt=(e={})=>_react.useMemo.call(void 0, ()=>_helpers.paginate.call(void 0, e),[e]);var zt=(e,n=[])=>{let[r,t]=_react.useState.call(void 0, n),i=r.length>0,a=u=>r.includes(u),s=_react.useCallback.call(void 0, u=>t(l=>{let d=new Set(l);return d.has(u)?d.delete(u):d.add(u),Array.from(d.values())}),[]),o=_react.useCallback.call(void 0, u=>{t(l=>{if(l.length===0)return[u];let d=[...e],f=d.indexOf(l.at(0)),y=d.indexOf(u);if(f>y){let S=[...d].reverse(),b=S.indexOf(l.at(0)),E=S.indexOf(u);return S.slice(b,E+1)}return d.slice(f,y+1)})},[e]),c=_react.useCallback.call(void 0, ()=>{t(e)},[e]),p=_react.useCallback.call(void 0, u=>t(u?n:[]),[n]);return{selection:r,hasSelection:i,isSelected:a,setSelection:t,select:s,groupSelect:o,selectAll:c,resetSelection:p}};var _browserapi = require('@alessiofrittoli/web-utils/browser-api');function C(e,n={}){let{updateState:r=!0,onChange:t}=n,[i,a]=_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);r&&a(o),_optionalChain([t, 'optionalCall', _6 => _6(o)])},[e,r,t]);if(_react.useEffect.call(void 0, ()=>{let o=window.matchMedia(e),{matches:c}=o;return r&&a(c),_optionalChain([t, 'optionalCall', _7 => _7(c)]),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,r,t,s]),!!r)return i}var an=(e={})=>{let n=_(),r=C("(prefers-color-scheme: dark)"),{initial:t=r,docClassNames:i=[]}=e,[a,s]=$("dark-mode",t),o=_nullishCoalesce(a, () => (r)),[c,p]=i,u=_react.useRef.call(void 0, {light:"",dark:""});return M(()=>{s(r)},[r,s]),_react.useEffect.call(void 0, ()=>{c&&document.documentElement.classList.toggle(c,o),p&&document.documentElement.classList.toggle(p,!o)},[o,c,p]),_react.useEffect.call(void 0, ()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(l=>{let d=l.getAttribute("media"),f=l.getAttribute("content");if(f){if(!d||d==="(prefers-color-scheme: light)"){u.current.light=f;return}u.current.dark=f}})},[]),M(()=>{let l=u.current.dark,d=u.current.light;a&&!l||!a&&!d||document.head.querySelectorAll('meta[name="theme-color"]').forEach(f=>{f.setAttribute("content",a?l:d)})},[a]),{isDarkMode:n?o:!1,isDarkOS:n?r:!1,toggleDarkMode:_react.useCallback.call(void 0, ()=>s(l=>!l),[s]),enableDarkMode:_react.useCallback.call(void 0, ()=>s(!0),[s]),disableDarkMode:_react.useCallback.call(void 0, ()=>s(!1),[s])}};function X(e={}){let{updateState:n=!0,onVisibilityChange:r}=e,[t,i]=_react.useState.call(void 0, !1),a=_react.useCallback.call(void 0, ()=>{let s=!document.hidden;n&&i(s),_optionalChain([r, 'optionalCall', _8 => _8(s)])},[n,r]);if(H(()=>{n&&i(!document.hidden)}),_react.useEffect.call(void 0, ()=>(document.addEventListener("visibilitychange",a),()=>document.removeEventListener("visibilitychange",a)),[a]),n)return t}function B(e,n){let{target:r,query:t,options:i,listener:a,onLoad:s,onCleanUp:o}=n;_react.useEffect.call(void 0, ()=>{let c=Array.isArray(e)?e:[e],p=_nullishCoalesce((t?window.matchMedia(t):r&&"current"in r?r.current:r), () => (window));if(p.addEventListener)return _optionalChain([s, 'optionalCall', _9 => _9()]),c.map(u=>{p.addEventListener(u,a,i)}),()=>{c.map(u=>{p.removeEventListener(u,a,i)}),_optionalChain([o, 'optionalCall', _10 => _10()])}},[e,r,t,i,a,s,o])}var _device = require('@alessiofrittoli/web-utils/device');var xn=()=>C(_device.portraitMediaQuery);var Mn=(e={})=>{let{onMount:n=!0,onError:r}=e,[t,i]=_react.useState.call(void 0, null),a=_react.useRef.call(void 0, n),s=t&&!t.released||!1,o=_react.useCallback.call(void 0, async l=>!t||t.released?navigator.wakeLock.request("screen").then(d=>{i(d),_optionalChain([l, 'optionalCall', _11 => _11()])}).catch(r||console.error):t,[t,r]),c=_react.useCallback.call(void 0, async()=>_optionalChain([t, 'optionalAccess', _12 => _12.release, 'call', _13 => _13()]),[t]),p=_react.useCallback.call(void 0, async()=>{o(()=>{a.current=!0})},[o]),u=_react.useCallback.call(void 0, async()=>{a.current=!1,c()},[c]);return X({updateState:!1,onVisibilityChange:_react.useCallback.call(void 0, l=>{if(l&&!s&&a.current)return o();if(!l)return c()},[s,o,c])}),H(()=>{s||!n||o()}),_react.useEffect.call(void 0, ()=>{if(!t)return;let l=()=>{t.released&&i(null)};return t.addEventListener("release",l),()=>{t.removeEventListener("release",l),!t.released&&t.release()}},[t]),{wakeLock:t,enabled:s,requestWakeLock:p,releaseWakeLock:u}};var Ue=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),Vn= exports.useFocusTrap =e=>{let n=_react.useMemo.call(void 0, ()=>_optionalChain([e, 'optionalAccess', _14 => _14.current]),[e]),[r,t]=_react.useState.call(void 0, !1),i=_react.useRef.call(void 0, null),a=_react.useCallback.call(void 0, o=>{i.current=document.activeElement;let c=o||n||!1;if(c)return t(c)},[n]),s=_react.useCallback.call(void 0, ()=>{_optionalChain([i, 'access', _15 => _15.current, 'optionalAccess', _16 => _16.focus, 'call', _17 => _17()]),t(!1)},[]);return _react.useEffect.call(void 0, ()=>{if(!r)return;let o=c=>{if(c.key!=="Tab")return;let p=Array.from(r.querySelectorAll(Ue)),u=p.at(0),l=p.at(-1);if(!c.shiftKey){document.activeElement===l&&(c.preventDefault(),_optionalChain([u, 'optionalAccess', _18 => _18.focus, 'call', _19 => _19()]));return}document.activeElement===u&&(c.preventDefault(),_optionalChain([l, 'optionalAccess', _20 => _20.focus, 'call', _21 => _21()]))};return document.addEventListener("keydown",o),()=>{document.removeEventListener("keydown",o)}},[r]),[a,s]};var Dn=(e,n={})=>{let{initial:r=!1,once:t,amount:i,margin:a,root:s,enable:o=!0}=n,{onEnter:c,onExit:p,onIntersect:u}=n,l=_react.useRef.call(void 0, !0),[d,f]=_react.useState.call(void 0, r),[y,S]=_react.useState.call(void 0, o),b=_react.useRef.call(void 0, null),E=_react.useRef.call(void 0, !1),O=_react.useMemo.call(void 0, ()=>{if(!y||typeof IntersectionObserver>"u")return;let D=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&&!!b.current,v&&c&&await c({entry:T,observer:x}),E.current&&p&&await p({entry:T,observer:x}),u&&(v||!v&&b.current!=null)){let k={isEntering:v,isExiting:E.current};await u({entry:T,observer:x,...k})}if(b.current=v,!l.current)return;f(v)}catch(k){console.error(k)}v&&t&&x.disconnect()},{root:s||void 0,rootMargin:a,threshold:D})}catch(T){console.error(T)}},[s,a,i,t,y,c,p,u]);return _react.useEffect.call(void 0, ()=>{if(l.current=!0,!(!y||!e.current||!O))return O.observe(e.current),()=>{l.current=!1,O.disconnect()}},[e,O,y]),{inView:d,enabled:y,observer:O,isExiting:E.current,setInView:f,setEnabled:S}};var _dom = require('@alessiofrittoli/web-utils/dom');var An=e=>{let n=_react.useMemo.call(void 0, ()=>_optionalChain([e, 'optionalAccess', _22 => _22.current]),[e]),r=_react.useCallback.call(void 0, ()=>_dom.blockScroll.call(void 0, n||void 0),[n]),t=_react.useCallback.call(void 0, ()=>_dom.restoreScroll.call(void 0, n||void 0),[n]);return[r,t]};var te=(e,n={})=>{let{delay:r=1,args:t}=n;_react.useEffect.call(void 0, ()=>{let i=setTimeout(e,r,...t||[]);return()=>clearTimeout(i)},[r,t,e])};var Nn=(e,n=500)=>{let[r,t]=_react.useState.call(void 0, e),i=_react.useMemo.call(void 0, ()=>[e],[e]);return te(t,{delay:n,args:i}),r};function ne(e,n={}){let{delay:r=1,args:t,autoplay:i=!0,runOnStart:a=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,p]=_react.useState.call(void 0, i),u=_react.useCallback.call(void 0, ()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),l=_react.useCallback.call(void 0, ()=>{let f=u();return a&&(t?e(...t):e()),o.current=setInterval(e,r,...t||[]),!f&&s&&p(!0),o.current},[r,t,s,a,e,u]),d=_react.useCallback.call(void 0, ()=>{u()&&s&&p(!1)},[s,u]);return _react.useEffect.call(void 0, ()=>{if(i)return l(),d},[i,l,d]),s?{isActive:c,start:l,stop:d}:{start:l,stop:d}}var Zn=(e,n={})=>{let{delay:r=1,args:t}=n;_react.useEffect.call(void 0, ()=>{let i=setInterval(e,r,...t||[]);return()=>clearInterval(i)},[r,t,e])};function or(e,n={}){let{autoplay:r=!0}=n,t=ne(e,{autoplay:!1,...n}),{start:i,stop:a}=t,s=_react.useCallback.call(void 0, ()=>document.hidden?a():i(),[i,a]),o=_react.useCallback.call(void 0, ()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=_react.useCallback.call(void 0, ()=>{a(),document.removeEventListener("visibilitychange",s)},[a,s]);return _react.useEffect.call(void 0, ()=>{if(r)return o(),c},[r,o,c]),{...t,start:o,stop:c}}function cr(e,n={}){let{delay:r=1,args:t,autoplay:i=!0,runOnStart:a=!1,updateState:s=!1}=n,o=_react.useRef.call(void 0, void 0),[c,p]=_react.useState.call(void 0, i),u=_react.useCallback.call(void 0, ()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),l=_react.useCallback.call(void 0, ()=>{let f=u();return a&&(t?e(...t):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&p(!1),t)return e(...t);e()},r),!f&&s&&p(!0),o.current},[r,t,s,a,e,u]),d=_react.useCallback.call(void 0, ()=>{u()&&s&&p(!1)},[s,u]);return _react.useEffect.call(void 0, ()=>{if(i)return l(),d},[i,l,d]),s?{isActive:c,start:l,stop:d}:{start:l,stop:d}}exports.getState = j; exports.useConnection = dt; exports.useDarkMode = an; exports.useDebounce = Nn; exports.useDeferCallback = Dt; exports.useDocumentVisibility = X; exports.useEffectOnce = H; exports.useEventListener = B; exports.useFocusTrap = Vn; exports.useInView = Dn; exports.useInput = It; exports.useInterval = ne; exports.useIntervalWhenVisible = or; exports.useIsClient = _; exports.useIsFirstRender = I; exports.useIsPortrait = xn; exports.useLightInterval = Zn; exports.useLightTimeout = te; exports.useLocalStorage = $; exports.useMediaQuery = C; exports.usePagination = Bt; exports.useScrollBlock = An; exports.useSelection = zt; exports.useSessionStorage = at; exports.useStorage = h; exports.useTimeout = cr; exports.useUpdateEffect = M; exports.useWakeLock = Mn;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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 Ye=(e,n)=>h(e,n,"session");import{useCallback as re,useState as oe}from"react";var P=e=>e?"online":"offline",nt=()=>{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 w,useEffect as J,useRef as Se}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,D={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 D}};var Et=(e={})=>{let{inputRef:n}=e,{initialValue:t}=e,{touchTimeout:r=600}=e,{validate:i,parse:u}=e,{onChange:s}=e,[o,c]=ue(N,{...D,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{useCallback as ce}from"react";import{deferCallback as le}from"@alessiofrittoli/web-utils";var Mt=(e,n)=>ce(le(e),n);import{useEffect as pe}from"react";var Rt=e=>{let n=R();pe(()=>{if(n)return e()},[])};import{useEffect as de,useState as me}from"react";var q=()=>{let[e,n]=me(!1);return de(()=>n(!0),[]),e};import{useMemo as fe}from"react";import{paginate as Te}from"@alessiofrittoli/math-utils/helpers";var Kt=(e={})=>fe(()=>Te(e),[e]);import{useCallback as k,useState as ye}from"react";var Ft=(e,n=[])=>{let[t,r]=ye(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)}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 ve,useEffect as Ee,useState as xe}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]=xe(z(e)),s=ve(()=>{let o=z(e);t&&u(o),r?.(o)},[e,t,r]);if(Ee(()=>{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 Yt=(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=Se({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:w(()=>s(p=>!p),[s]),enableDarkMode:w(()=>s(!0),[s]),disableDarkMode:w(()=>s(!1),[s])}};import{useEffect as Oe}from"react";function B(e,n){let{target:t,query:r,options:i,listener:u,onLoad:s,onCleanUp:o}=n;Oe(()=>{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 ge}from"@alessiofrittoli/web-utils/device";var sn=()=>V(ge);import{useCallback as X,useEffect as Me,useRef as be,useState as Ie}from"react";var He=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),ln=e=>{let[n,t]=Ie(!1),r=be(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 Me(()=>{if(!n)return;let s=o=>{if(o.key!=="Tab")return;let c=Array.from(n.querySelectorAll(He)),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 he,useMemo as Re,useRef as U,useState as Y}from"react";var fn=(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=Re(()=>{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 he(()=>{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 Le,restoreScroll as ke}from"@alessiofrittoli/web-utils/dom";var xn=e=>{let n=Z(()=>Le(e?.current||void 0),[e]),t=Z(()=>ke(e?.current||void 0),[e]);return[n,t]};import{useMemo as Ce,useState as De}from"react";import{useEffect as Ve}from"react";var _=(e,n={})=>{let{delay:t=1,args:r}=n;Ve(()=>{let i=setTimeout(e,t,...r||[]);return()=>clearTimeout(i)},[t,r,e])};var hn=(e,n=500)=>{let[t,r]=De(e),i=Ce(()=>[e],[e]);return _(r,{delay:n,args:i}),t};import{useCallback as K,useEffect as we,useRef as Ue,useState as Ke}from"react";function ee(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=Ue(void 0),[c,l]=Ke(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 we(()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}import{useEffect as Ae}from"react";var wn=(e,n={})=>{let{delay:t=1,args:r}=n;Ae(()=>{let i=setInterval(e,t,...r||[]);return()=>clearInterval(i)},[t,r,e])};import{useCallback as A,useEffect as We}from"react";function Qn(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 We(()=>{if(t)return o(),c},[t,o,c]),{...r,start:o,stop:c}}import{useCallback as W,useEffect as Qe,useRef as Fe,useState as $e}from"react";function Pn(e,n={}){let{delay:t=1,args:r,autoplay:i=!0,runOnStart:u=!1,updateState:s=!1}=n,o=Fe(void 0),[c,l]=$e(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 Qe(()=>{if(i)return p(),d},[i,p,d]),s?{isActive:c,start:p,stop:d}:{start:p,stop:d}}export{P as getState,nt as useConnection,Yt as useDarkMode,hn as useDebounce,Mt as useDeferCallback,Rt as useEffectOnce,B as useEventListener,ln as useFocusTrap,fn as useInView,Et as useInput,ee as useInterval,Qn as useIntervalWhenVisible,q as useIsClient,R as useIsFirstRender,sn as useIsPortrait,wn as useLightInterval,_ as useLightTimeout,j as useLocalStorage,V as useMediaQuery,Kt as usePagination,xn as useScrollBlock,Ft as useSelection,Ye as useSessionStorage,h as useStorage,Pn as useTimeout,H as useUpdateEffect};
1
+ import{useCallback as q,useEffect as oe,useState as se}from"react";import{LocalStorage as P}from"@alessiofrittoli/web-utils/storage/LocalStorage";import{SessionStorage as $}from"@alessiofrittoli/web-utils/storage/SessionStorage";var I=(e,n,r="local")=>{let t=q(()=>(r==="local"?P:$).get(e)??n,[r,e,n]),[i,a]=se(n),s=q(o=>{a(c=>{let p=o instanceof Function?o(c):o;return(typeof window<"u"&&r==="local"?P:$).set(e,p),p})},[r,e]);return oe(()=>{a(t())},[t]),[i,s]};var j=(e,n)=>I(e,n,"local");var ct=(e,n)=>I(e,n,"session");import{useCallback as ie,useState as ae}from"react";var B=e=>e?"online":"offline",mt=()=>{let[e,n]=ae(B(!0)),r=e==="online",t=e==="offline",i=ie(()=>n(B(navigator.onLine)),[]);return N(["online","offline"],{listener:i,onLoad:i}),{connection:e,isOnline:r,isOffline:t}};import{useCallback as W,useEffect as X,useRef as ke}from"react";import{useCallback as M,useMemo as _,useReducer as pe}from"react";import{useEffect as ue}from"react";import{useRef as ce}from"react";var V=()=>{let e=ce(!0);return e.current?(e.current=!1,!0):e.current};var L=(e,n)=>{let r=V();ue(()=>{if(!r)return e()},n)};import{isEmpty as le}from"@alessiofrittoli/web-utils";var H=e=>typeof e=="string"?le(e):!e,U={value:"",isTouched:!1,isValid:!0},G=(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:!H(e.value??"")};case"RESET":return U}};var Vt=(e={})=>{let{inputRef:n}=e,{initialValue:r}=e,{touchTimeout:t=600}=e,{validate:i,parse:a}=e,{onChange:s}=e,[o,c]=pe(G,{...U,value:r}),p=_(()=>a?a(o.value):o.value,[o.value,a]),u=_(()=>typeof i=="function"?i(p):!0,[p,i]),{isTouched:l}=o,d=H(p),T=!u&&l||!!r&&!u;L(()=>{let y=setTimeout(()=>{H(p)||c({type:"TOUCHED"})},t);return()=>clearTimeout(y)},[p,t]);let v=M(y=>{let{target:S}=y,{type:E}=S,g=E==="checkbox"?S.checked:S.value;c({type:"CHANGE",value:g}),s?.(a?a(g):g)},[s,a]),b=M(()=>{c({type:"BLUR"})},[]),O=M(()=>{c({type:"TOUCHED"})},[]),x=M(y=>{c({type:"CHANGE",value:y})},[]),k=M(()=>{n?.current?.focus()},[n]),w=M(()=>{c({type:"RESET"})},[]);return{value:p,isTouched:l,isValid:u,isEmpty:d,hasError:T,changeHandler:v,blurHandler:b,setValue:x,submit:O,focus:k,reset:w}};import{useCallback as de}from"react";import{deferCallback as me}from"@alessiofrittoli/web-utils";var wt=(e,n)=>de(me(e),n);import{useEffect as fe}from"react";var R=e=>{let n=V();fe(()=>{if(n)return e()},[])};import{useEffect as Te,useState as ye}from"react";var z=()=>{let[e,n]=ye(!1);return Te(()=>n(!0),[]),e};import{useMemo as ve}from"react";import{paginate as Ee}from"@alessiofrittoli/math-utils/helpers";var Nt=(e={})=>ve(()=>Ee(e),[e]);import{useCallback as C,useState as xe}from"react";var Jt=(e,n=[])=>{let[r,t]=xe(n),i=r.length>0,a=u=>r.includes(u),s=C(u=>t(l=>{let d=new Set(l);return d.has(u)?d.delete(u):d.add(u),Array.from(d.values())}),[]),o=C(u=>{t(l=>{if(l.length===0)return[u];let d=[...e],T=d.indexOf(l.at(0)),v=d.indexOf(u);if(T>v){let b=[...d].reverse(),O=b.indexOf(l.at(0)),x=b.indexOf(u);return b.slice(O,x+1)}return d.slice(T,v+1)})},[e]),c=C(()=>{t(e)},[e]),p=C(u=>t(u?n:[]),[n]);return{selection:r,hasSelection:i,isSelected:a,setSelection:t,select:s,groupSelect:o,selectAll:c,resetSelection:p}};import{useCallback as Se,useEffect as be,useState as Oe}from"react";import{getMediaMatches as J}from"@alessiofrittoli/web-utils/browser-api";function D(e,n={}){let{updateState:r=!0,onChange:t}=n,[i,a]=Oe(J(e)),s=Se(()=>{let o=J(e);r&&a(o),t?.(o)},[e,r,t]);if(be(()=>{let o=window.matchMedia(e),{matches:c}=o;return r&&a(c),t?.(c),o.addEventListener("change",s),()=>{o.removeEventListener("change",s)}},[e,r,t,s]),!!r)return i}var cn=(e={})=>{let n=z(),r=D("(prefers-color-scheme: dark)"),{initial:t=r,docClassNames:i=[]}=e,[a,s]=j("dark-mode",t),o=a??r,[c,p]=i,u=ke({light:"",dark:""});return L(()=>{s(r)},[r,s]),X(()=>{c&&document.documentElement.classList.toggle(c,o),p&&document.documentElement.classList.toggle(p,!o)},[o,c,p]),X(()=>{document.head.querySelectorAll('meta[name="theme-color"]').forEach(l=>{let d=l.getAttribute("media"),T=l.getAttribute("content");if(T){if(!d||d==="(prefers-color-scheme: light)"){u.current.light=T;return}u.current.dark=T}})},[]),L(()=>{let l=u.current.dark,d=u.current.light;a&&!l||!a&&!d||document.head.querySelectorAll('meta[name="theme-color"]').forEach(T=>{T.setAttribute("content",a?l:d)})},[a]),{isDarkMode:n?o:!1,isDarkOS:n?r:!1,toggleDarkMode:W(()=>s(l=>!l),[s]),enableDarkMode:W(()=>s(!0),[s]),disableDarkMode:W(()=>s(!1),[s])}};import{useCallback as ge,useEffect as Me,useState as Le}from"react";function Y(e={}){let{updateState:n=!0,onVisibilityChange:r}=e,[t,i]=Le(!1),a=ge(()=>{let s=!document.hidden;n&&i(s),r?.(s)},[n,r]);if(R(()=>{n&&i(!document.hidden)}),Me(()=>(document.addEventListener("visibilitychange",a),()=>document.removeEventListener("visibilitychange",a)),[a]),n)return t}import{useEffect as he}from"react";function N(e,n){let{target:r,query:t,options:i,listener:a,onLoad:s,onCleanUp:o}=n;he(()=>{let c=Array.isArray(e)?e:[e],p=(t?window.matchMedia(t):r&&"current"in r?r.current:r)??window;if(p.addEventListener)return s?.(),c.map(u=>{p.addEventListener(u,a,i)}),()=>{c.map(u=>{p.removeEventListener(u,a,i)}),o?.()}},[e,r,t,i,a,s,o])}import{portraitMediaQuery as Ie}from"@alessiofrittoli/web-utils/device";var Sn=()=>D(Ie);import{useCallback as h,useEffect as Ve,useRef as He,useState as Re}from"react";var Ln=(e={})=>{let{onMount:n=!0,onError:r}=e,[t,i]=Re(null),a=He(n),s=t&&!t.released||!1,o=h(async l=>!t||t.released?navigator.wakeLock.request("screen").then(d=>{i(d),l?.()}).catch(r||console.error):t,[t,r]),c=h(async()=>t?.release(),[t]),p=h(async()=>{o(()=>{a.current=!0})},[o]),u=h(async()=>{a.current=!1,c()},[c]);return Y({updateState:!1,onVisibilityChange:h(l=>{if(l&&!s&&a.current)return o();if(!l)return c()},[s,o,c])}),R(()=>{s||!n||o()}),Ve(()=>{if(!t)return;let l=()=>{t.released&&i(null)};return t.addEventListener("release",l),()=>{t.removeEventListener("release",l),!t.released&&t.release()}},[t]),{wakeLock:t,enabled:s,requestWakeLock:p,releaseWakeLock:u}};import{useCallback as Z,useEffect as Ce,useMemo as De,useRef as we,useState as Ue}from"react";var We=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),Hn=e=>{let n=De(()=>e?.current,[e]),[r,t]=Ue(!1),i=we(null),a=Z(o=>{i.current=document.activeElement;let c=o||n||!1;if(c)return t(c)},[n]),s=Z(()=>{i.current?.focus(),t(!1)},[]);return Ce(()=>{if(!r)return;let o=c=>{if(c.key!=="Tab")return;let p=Array.from(r.querySelectorAll(We)),u=p.at(0),l=p.at(-1);if(!c.shiftKey){document.activeElement===l&&(c.preventDefault(),u?.focus());return}document.activeElement===u&&(c.preventDefault(),l?.focus())};return document.addEventListener("keydown",o),()=>{document.removeEventListener("keydown",o)}},[r]),[a,s]};import{useEffect as Ke,useMemo as Ae,useRef as K,useState as ee}from"react";var wn=(e,n={})=>{let{initial:r=!1,once:t,amount:i,margin:a,root:s,enable:o=!0}=n,{onEnter:c,onExit:p,onIntersect:u}=n,l=K(!0),[d,T]=ee(r),[v,b]=ee(o),O=K(null),x=K(!1),k=Ae(()=>{if(!v||typeof IntersectionObserver>"u")return;let w=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&&!!O.current,E&&c&&await c({entry:y,observer:S}),x.current&&p&&await p({entry:y,observer:S}),u&&(E||!E&&O.current!=null)){let g={isEntering:E,isExiting:x.current};await u({entry:y,observer:S,...g})}if(O.current=E,!l.current)return;T(E)}catch(g){console.error(g)}E&&t&&S.disconnect()},{root:s||void 0,rootMargin:a,threshold:w})}catch(y){console.error(y)}},[s,a,i,t,v,c,p,u]);return Ke(()=>{if(l.current=!0,!(!v||!e.current||!k))return k.observe(e.current),()=>{l.current=!1,k.disconnect()}},[e,k,v]),{inView:d,enabled:v,observer:k,isExiting:x.current,setInView:T,setEnabled:b}};import{useCallback as te,useMemo as Qe}from"react";import{blockScroll as Fe,restoreScroll as qe}from"@alessiofrittoli/web-utils/dom";var Qn=e=>{let n=Qe(()=>e?.current,[e]),r=te(()=>Fe(n||void 0),[n]),t=te(()=>qe(n||void 0),[n]);return[r,t]};import{useMemo as $e,useState as je}from"react";import{useEffect as Pe}from"react";var ne=(e,n={})=>{let{delay:r=1,args:t}=n;Pe(()=>{let i=setTimeout(e,r,...t||[]);return()=>clearTimeout(i)},[r,t,e])};var Gn=(e,n=500)=>{let[r,t]=je(e),i=$e(()=>[e],[e]);return ne(t,{delay:n,args:i}),r};import{useCallback as A,useEffect as Be,useRef as Ne,useState as Ge}from"react";function re(e,n={}){let{delay:r=1,args:t,autoplay:i=!0,runOnStart:a=!1,updateState:s=!1}=n,o=Ne(void 0),[c,p]=Ge(i),u=A(()=>o.current?(clearInterval(o.current),o.current=void 0,!0):!1,[]),l=A(()=>{let T=u();return a&&(t?e(...t):e()),o.current=setInterval(e,r,...t||[]),!T&&s&&p(!0),o.current},[r,t,s,a,e,u]),d=A(()=>{u()&&s&&p(!1)},[s,u]);return Be(()=>{if(i)return l(),d},[i,l,d]),s?{isActive:c,start:l,stop:d}:{start:l,stop:d}}import{useEffect as _e}from"react";var er=(e,n={})=>{let{delay:r=1,args:t}=n;_e(()=>{let i=setInterval(e,r,...t||[]);return()=>clearInterval(i)},[r,t,e])};import{useCallback as Q,useEffect as ze}from"react";function sr(e,n={}){let{autoplay:r=!0}=n,t=re(e,{autoplay:!1,...n}),{start:i,stop:a}=t,s=Q(()=>document.hidden?a():i(),[i,a]),o=Q(()=>{if(document.addEventListener("visibilitychange",s),!document.hidden)return i()},[i,s]),c=Q(()=>{a(),document.removeEventListener("visibilitychange",s)},[a,s]);return ze(()=>{if(r)return o(),c},[r,o,c]),{...t,start:o,stop:c}}import{useCallback as F,useEffect as Je,useRef as Xe,useState as Ye}from"react";function ur(e,n={}){let{delay:r=1,args:t,autoplay:i=!0,runOnStart:a=!1,updateState:s=!1}=n,o=Xe(void 0),[c,p]=Ye(i),u=F(()=>o.current?(clearTimeout(o.current),o.current=void 0,!0):!1,[]),l=F(()=>{let T=u();return a&&(t?e(...t):e()),o.current=setTimeout(()=>{if(o.current=void 0,s&&p(!1),t)return e(...t);e()},r),!T&&s&&p(!0),o.current},[r,t,s,a,e,u]),d=F(()=>{u()&&s&&p(!1)},[s,u]);return Je(()=>{if(i)return l(),d},[i,l,d]),s?{isActive:c,start:l,stop:d}:{start:l,stop:d}}export{B as getState,mt as useConnection,cn as useDarkMode,Gn as useDebounce,wt as useDeferCallback,Y as useDocumentVisibility,R as useEffectOnce,N as useEventListener,Hn as useFocusTrap,wn as useInView,Vt as useInput,re as useInterval,sr as useIntervalWhenVisible,z as useIsClient,V as useIsFirstRender,Sn as useIsPortrait,er as useLightInterval,ne as useLightTimeout,j as useLocalStorage,D as useMediaQuery,Nt as usePagination,Qn as useScrollBlock,Jt as useSelection,ct as useSessionStorage,I as useStorage,ur as useTimeout,L as useUpdateEffect,Ln as useWakeLock};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alessiofrittoli/react-hooks",
3
- "version": "3.3.0",
3
+ "version": "3.5.0",
4
4
  "description": "TypeScript React utility Hooks",
5
5
  "author": {
6
6
  "name": "Alessio Frittoli",
@@ -87,48 +87,48 @@
87
87
  "test:misc": "pnpm test:watch misc/*",
88
88
  "test:timers": "pnpm test:watch timers/*"
89
89
  },
90
+ "peerDependencies": {
91
+ "@types/react": "^19",
92
+ "@types/react-dom": "^19",
93
+ "react": "^19",
94
+ "react-dom": "^19"
95
+ },
90
96
  "devDependencies": {
91
- "@alessiofrittoli/event-emitter": "^1.5.0",
92
- "@alessiofrittoli/node-scripts": "^3.0.0",
93
- "@eslint/compat": "^1.3.1",
94
- "@eslint/eslintrc": "^3.3.1",
95
- "@eslint/js": "^9.31.0",
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",
97
+ "@alessiofrittoli/event-emitter": "^1.6.0",
98
+ "@alessiofrittoli/node-scripts": "^3.3.0",
99
+ "@eslint/compat": "^2.0.0",
100
+ "@eslint/eslintrc": "^3.3.3",
101
+ "@eslint/js": "^9.39.2",
102
+ "@jest/globals": "^30.2.0",
103
+ "@testing-library/dom": "^10.4.1",
104
+ "@testing-library/jest-dom": "^6.9.1",
105
+ "@testing-library/react": "^16.3.1",
100
106
  "@testing-library/user-event": "^14.6.1",
101
107
  "@types/jest": "^30.0.0",
102
- "@types/node": "^24.0.15",
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.2.0",
108
- "eslint": "^9.31.0",
108
+ "@types/node": "^25.0.3",
109
+ "@types/react": "^19.2.7",
110
+ "@types/react-dom": "^19.2.3",
111
+ "concurrently": "^9.2.1",
112
+ "cross-env": "^10.1.0",
113
+ "dotenv": "^17.2.3",
114
+ "eslint": "^9.39.2",
109
115
  "eslint-plugin-react": "^7.37.5",
110
- "eslint-plugin-react-hooks": "^5.2.0",
111
- "globals": "^16.3.0",
116
+ "eslint-plugin-react-hooks": "^7.0.1",
117
+ "globals": "^16.5.0",
112
118
  "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",
119
+ "jest": "^30.2.0",
120
+ "jest-environment-jsdom": "^30.2.0",
121
+ "react": "^19.2.3",
122
+ "react-dom": "^19.2.3",
123
+ "ts-jest": "^29.4.6",
118
124
  "ts-node": "^10.9.2",
119
- "tsup": "^8.5.0",
120
- "typescript": "^5.8.3",
121
- "typescript-eslint": "^8.37.0"
125
+ "tsup": "^8.5.1",
126
+ "typescript": "^5.9.3",
127
+ "typescript-eslint": "^8.50.0"
122
128
  },
123
129
  "dependencies": {
124
- "@alessiofrittoli/math-utils": "^1.14.0",
125
- "@alessiofrittoli/type-utils": "^1.8.0",
126
- "@alessiofrittoli/web-utils": "^1.16.1"
127
- },
128
- "peerDependencies": {
129
- "@types/react": "^19",
130
- "@types/react-dom": "^19",
131
- "react": "^19",
132
- "react-dom": "^19"
130
+ "@alessiofrittoli/math-utils": "^1.17.0",
131
+ "@alessiofrittoli/type-utils": "^1.9.0",
132
+ "@alessiofrittoli/web-utils": "^1.18.0"
133
133
  }
134
134
  }