react-hook-toolkit 1.0.22 β†’ 1.1.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/LICENSE ADDED
@@ -0,0 +1,35 @@
1
+ MIT License
2
+
3
+ Copyright Β© 2022 Shivaji Surwase. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+
7
+ of this software and associated documentation files (the "Software"), to deal
8
+
9
+ in the Software without restriction, including without limitation the rights
10
+
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+
13
+ copies of the Software, and to permit persons to whom the Software is
14
+
15
+ furnished to do so, subject to the following conditions:
16
+
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+
20
+ copies or substantial portions of the Software.
21
+
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+
31
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+
33
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
+
35
+ SOFTWARE.
package/README.md CHANGED
@@ -40,18 +40,18 @@ useEffect(() => {
40
40
  }, []);
41
41
  ```
42
42
  ---
43
- πŸ“Œ **useFetch**
43
+ πŸ“Œ **useRequest**
44
44
  Provides an easy way to fetch data with hooks, managing loading, data, and error states.
45
45
 
46
46
  ```ts
47
- const { data, loading, error } = useFetch('/api/data');
47
+ const { data, loading, error } = useRequest('/api/data');
48
48
  ```
49
49
  ---
50
- πŸ“Œ **useFetchRetry**
50
+ πŸ“Œ **useRequestRetry**
51
51
  Performs an HTTP request with retry logic, retrying a specified number of times upon failure before returning an error.
52
52
 
53
53
  ```ts
54
- const { data, loading, error } = useFetchRetry('/api/user', { method: 'GET' }, 3);
54
+ const { data, loading, error } = useRequestRetry('/api/user', { method: 'GET' }, 3);
55
55
  ```
56
56
  ---
57
57
  πŸ“Œ **useAsync**
@@ -350,11 +350,11 @@ setState({ items: [...] });
350
350
  undo(); // Reverts to previous state
351
351
  ```
352
352
  ---
353
- πŸ“Œ **useAdvReducer**
353
+ πŸ“Œ **useGenericReducer**
354
354
  A custom reducer function that takes an `initialState` and an object of actions, returning a new state based on the action type.
355
355
 
356
356
  ```ts
357
- const reducer = useAdvReducer(initialState, {
357
+ const reducer = useGenericReducer(initialState, {
358
358
  increment: (state) => ({ count: state.count + 1 }),
359
359
  });
360
360
  const [state, dispatch] = useReducer(reducer, initialState);
@@ -648,6 +648,85 @@ Converts text to speech using the Web Speech API, allowing the text to be spoken
648
648
  const { speak, error } = useSpeak('Hello world!');
649
649
  speak();
650
650
  ```
651
+ ---
652
+
653
+ ### πŸ“Œ **usePermission**
654
+ Monitors the status of a specific browser permission (like geolocation, camera, microphone, etc.) and updates reactively when the permission state changes.
655
+
656
+ ```ts
657
+ const permission = usePermission({ name: 'geolocation' });
658
+ ```
659
+
660
+ ---
661
+
662
+ ### πŸ“Œ **usePageLeave**
663
+ Detects when the mouse leaves the viewport (commonly used to detect exit intent on websites).
664
+
665
+ ```ts
666
+ usePageLeave(() => {
667
+ console.log('Mouse left the page');
668
+ });
669
+ ```
670
+
671
+ ---
672
+
673
+ ### πŸ“Œ **useMotion**
674
+ Subscribes to device motion events (like `acceleration` or `rotation`) and returns the latest motion values from sensors on mobile devices.
675
+
676
+ ```ts
677
+ const motion = useMotion();
678
+ console.log(motion.acceleration);
679
+ ```
680
+
681
+ ---
682
+
683
+ ### πŸ“Œ **useHoverDirty**
684
+ Returns `true` when the mouse is hovering over a target element. Unlike `useHover`, this doesn't handle delays or complex state logicβ€”just raw hover state.
685
+
686
+ ```ts
687
+ const ref = useRef(null);
688
+ const isHovered = useHoverDirty(ref);
689
+ ```
690
+
691
+ ---
692
+
693
+ ### πŸ“Œ **useBeforeUnload**
694
+ Prompts the user with a confirmation dialog before leaving the page (refresh, close tab, etc.).
695
+
696
+ ```ts
697
+ useBeforeUnload(true); // Triggers default browser dialog
698
+ ```
699
+
700
+ ---
701
+
702
+ ### πŸ“Œ **useClickAway**
703
+ Triggers a handler function when a user clicks outside of a specified element.
704
+
705
+ ```ts
706
+ const ref = useRef(null);
707
+ useClickAway(ref, () => {
708
+ console.log('Clicked outside');
709
+ });
710
+ ```
711
+
712
+ ---
713
+
714
+ ### πŸ“Œ **useResponsive**
715
+ Tracks screen size and returns breakpoint-based flags (`xs`, `sm`, `md`, etc.) to help in building responsive UI layouts.
716
+
717
+ ```ts
718
+ const screens = useResponsive();
719
+ console.log(screens.md); // true or false
720
+ ```
721
+
722
+ ---
723
+
724
+ ### πŸ“Œ **useUnmountedRef**
725
+ Returns a `ref` that indicates whether the component is currently unmountedβ€”useful to prevent state updates on unmounted components.
726
+
727
+ ```ts
728
+ const unmountedRef = useUnmountedRef();
729
+ ```
651
730
  ---
652
731
  πŸ“Œ **useCountUp**
653
732
  Animates a counter that counts up from 0 to a target value within a specified duration, returning the current count and any errors.
@@ -1,16 +1,5 @@
1
- import { FC, ReactNode } from 'react';
2
- export interface DrawerContextValue {
3
- drawerOpen: boolean;
4
- openDrawer: () => void;
5
- closeDrawer: () => void;
6
- openDrawerInButton: () => void;
7
- closeDrawerInButton: () => void;
8
- currentMainMenu?: string;
9
- setCurrentMainMenu?: (value: string) => void;
10
- }
11
- interface DrawerProviderProps {
12
- children: ReactNode;
13
- }
1
+ import { FC } from 'react';
2
+ import { DrawerContextValue, DrawerProviderProps } from '../type';
14
3
  declare const DrawerContext: import("react").Context<DrawerContextValue>;
15
4
  export declare const DrawerProvider: FC<DrawerProviderProps>;
16
5
  export default DrawerContext;
@@ -8,7 +8,7 @@ var DrawerContext = createContext({
8
8
  openDrawerInButton: function () { },
9
9
  closeDrawerInButton: function () { },
10
10
  currentMainMenu: '',
11
- setCurrentMainMenu: function () { }
11
+ setCurrentMainMenu: function () { },
12
12
  });
13
13
  export var DrawerProvider = function (_a) {
14
14
  var children = _a.children;
@@ -26,10 +26,10 @@ export var DrawerProvider = function (_a) {
26
26
  openDrawerInButton: openDrawerInButton,
27
27
  closeDrawerInButton: closeDrawerInButton,
28
28
  currentMainMenu: currentMainMenu,
29
- setCurrentMainMenu: setCurrentMainMenu
29
+ setCurrentMainMenu: setCurrentMainMenu,
30
30
  }, children: children }));
31
31
  };
32
32
  DrawerProvider.propTypes = {
33
- children: PropTypes.node.isRequired
33
+ children: PropTypes.node.isRequired,
34
34
  };
35
35
  export default DrawerContext;
@@ -0,0 +1,109 @@
1
+ import { BatteryState, UseHistory, UseSessionStorage, UseSound, UseTouch } from '../type';
2
+ export declare function useGeoLocation(): {
3
+ position: GeolocationPosition | null;
4
+ error: Error | null;
5
+ };
6
+ export declare function useTimer(initialTime: number): {
7
+ time: number;
8
+ error: Error | null;
9
+ };
10
+ export declare function useIsMounted(): boolean;
11
+ export declare function useCss(css: string): {
12
+ error: Error | null;
13
+ };
14
+ export declare function useSpeak(text: string): {
15
+ speak: () => void;
16
+ error: Error | null;
17
+ };
18
+ export declare function useCountUp(target: number, duration: number): {
19
+ count: number;
20
+ error: Error | null;
21
+ };
22
+ export declare function useCountDown(start: number): {
23
+ count: number;
24
+ error: Error | null;
25
+ };
26
+ export declare const useBattery: () => BatteryState;
27
+ export declare const useEventListener: (eventName: string, handler: (event: Event) => void, elementRef?: React.RefObject<HTMLElement>, options?: boolean | AddEventListenerOptions) => void;
28
+ export declare const useHistory: () => UseHistory;
29
+ interface UsePreferredLanguage {
30
+ language: string;
31
+ languages: Array<string>;
32
+ isSupported: boolean;
33
+ }
34
+ export declare const usePreferredLanguage: () => UsePreferredLanguage;
35
+ export declare const useSessionStorage: UseSessionStorage<any>;
36
+ export declare const useSound: (url: string) => UseSound;
37
+ export declare const useTouch: UseTouch;
38
+ export declare const useUpdateEffect: (effect: React.EffectCallback, deps: React.DependencyList) => void;
39
+ export declare const usePersistedForm: <T>(key: string, initialValue: T) => [T, (value: T) => void];
40
+ export declare const useCrossFieldValidation: <T extends Record<string, any>>(validate: (values: T) => Record<keyof T, string | null>) => {
41
+ errors: Record<keyof T, string | null>;
42
+ validateFields: (values: T) => boolean;
43
+ };
44
+ export declare const useFieldArray: <T>(initialValue: T[]) => {
45
+ fields: T[];
46
+ append: (item: T) => void;
47
+ remove: (index: number) => void;
48
+ update: (index: number, item: T) => void;
49
+ };
50
+ type SubmitHandler<T> = (data: T) => Promise<void>;
51
+ export declare const useFormSubmit: <T>(handler: SubmitHandler<T>) => {
52
+ handleSubmit: (data: T) => Promise<void>;
53
+ isSubmitting: boolean;
54
+ submitError: string | null;
55
+ };
56
+ export declare const useSmartForm: <T extends Record<string, any>>(initialValues: T, storageKey?: string) => {
57
+ values: T;
58
+ handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
59
+ setValues: import("react").Dispatch<import("react").SetStateAction<T>>;
60
+ dirty: boolean;
61
+ };
62
+ export declare const useUndo: <T>(initialState: T) => {
63
+ state: T;
64
+ setState: (newState: T) => void;
65
+ undo: () => false | void;
66
+ redo: () => false | void;
67
+ canUndo: boolean;
68
+ canRedo: boolean;
69
+ };
70
+ export declare const useFormWizard: <T extends Record<string, any>>(steps: {
71
+ validate?: (values: T) => Record<string, string>;
72
+ component: React.FC<{
73
+ values: T;
74
+ setValues: (v: T) => void;
75
+ }>;
76
+ }[], initialValues: T) => {
77
+ currentStep: number;
78
+ CurrentStep: import("react").FC<{
79
+ values: T;
80
+ setValues: (v: T) => void;
81
+ }>;
82
+ values: T;
83
+ setValues: import("react").Dispatch<import("react").SetStateAction<T>>;
84
+ next: () => void;
85
+ prev: () => void;
86
+ errors: Record<string, string>;
87
+ isFirstStep: boolean;
88
+ isLastStep: boolean;
89
+ };
90
+ export declare const createOptimizedContext: <T>() => readonly [import("react").Provider<T | undefined>, <U>(selector: (value: T) => U) => U];
91
+ export declare const useWebSocket: <T>(url: string, onMessage?: (data: T) => void) => {
92
+ data: T | null;
93
+ send: (message: any) => void;
94
+ isConnected: boolean;
95
+ };
96
+ export declare const useDragReorder: <T>(initialItems: T[]) => {
97
+ items: T[];
98
+ handleDragStart: (index: number) => void;
99
+ handleDragEnter: (index: number) => void;
100
+ handleDrop: () => void;
101
+ };
102
+ export declare const useInfiniteScroll: <T>(fetchData: (page: number) => Promise<T[]>, initialData?: T[]) => {
103
+ data: T[];
104
+ loading: boolean;
105
+ hasMore: boolean;
106
+ lastElementRef: (node: HTMLElement | null) => void;
107
+ };
108
+ export declare const useEventListeners: (eventType: string, handler: (event: Event) => void, element?: HTMLElement | Window, options?: AddEventListenerOptions) => void;
109
+ export {};