react-simplikit 0.1.0 → 0.3.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.
Files changed (161) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +6 -73
  3. package/dist/hooks/useAsyncEffect/useAsyncEffect.cjs +2 -2
  4. package/dist/hooks/useAsyncEffect/useAsyncEffect.d.cts +2 -2
  5. package/dist/hooks/useAsyncEffect/useAsyncEffect.d.mts +2 -2
  6. package/dist/hooks/useAsyncEffect/useAsyncEffect.mjs +2 -2
  7. package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/useAvoidKeyboard.cjs +1 -1
  8. package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/useAvoidKeyboard.d.cts +1 -1
  9. package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/useAvoidKeyboard.d.mts +1 -1
  10. package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/useAvoidKeyboard.mjs +1 -1
  11. package/dist/{mobile/hooks → hooks}/useBodyScrollLock/useBodyScrollLock.cjs +1 -1
  12. package/dist/{mobile/hooks → hooks}/useBodyScrollLock/useBodyScrollLock.d.cts +1 -1
  13. package/dist/{mobile/hooks → hooks}/useBodyScrollLock/useBodyScrollLock.d.mts +1 -1
  14. package/dist/{mobile/hooks → hooks}/useBodyScrollLock/useBodyScrollLock.mjs +1 -1
  15. package/dist/hooks/useBooleanState/useBooleanState.cjs +3 -3
  16. package/dist/hooks/useBooleanState/useBooleanState.d.cts +3 -2
  17. package/dist/hooks/useBooleanState/useBooleanState.d.mts +3 -2
  18. package/dist/hooks/useBooleanState/useBooleanState.mjs +3 -3
  19. package/dist/hooks/useDebounce/debounce.cjs +1 -0
  20. package/dist/hooks/useDebounce/debounce.mjs +1 -0
  21. package/dist/hooks/useDebounce/useDebounce.cjs +1 -1
  22. package/dist/hooks/useDebounce/useDebounce.mjs +1 -1
  23. package/dist/hooks/useDebouncedCallback/useDebouncedCallback.cjs +31 -36
  24. package/dist/hooks/useDebouncedCallback/useDebouncedCallback.d.cts +17 -7
  25. package/dist/hooks/useDebouncedCallback/useDebouncedCallback.d.mts +17 -7
  26. package/dist/hooks/useDebouncedCallback/useDebouncedCallback.mjs +32 -37
  27. package/dist/hooks/useDebouncedValue/useDebouncedValue.cjs +64 -0
  28. package/dist/hooks/useDebouncedValue/useDebouncedValue.d.cts +49 -0
  29. package/dist/hooks/useDebouncedValue/useDebouncedValue.d.mts +49 -0
  30. package/dist/hooks/useDebouncedValue/useDebouncedValue.mjs +64 -0
  31. package/dist/hooks/useDoubleClick/useDoubleClick.cjs +0 -4
  32. package/dist/hooks/useDoubleClick/useDoubleClick.d.cts +0 -4
  33. package/dist/hooks/useDoubleClick/useDoubleClick.d.mts +0 -4
  34. package/dist/hooks/useDoubleClick/useDoubleClick.mjs +0 -4
  35. package/dist/hooks/useGeolocation/useGeolocation.cjs +0 -4
  36. package/dist/hooks/useGeolocation/useGeolocation.d.cts +0 -4
  37. package/dist/hooks/useGeolocation/useGeolocation.d.mts +0 -4
  38. package/dist/hooks/useGeolocation/useGeolocation.mjs +0 -4
  39. package/dist/hooks/useImpressionRef/useImpressionRef.cjs +25 -7
  40. package/dist/hooks/useImpressionRef/useImpressionRef.d.cts +0 -4
  41. package/dist/hooks/useImpressionRef/useImpressionRef.d.mts +0 -4
  42. package/dist/hooks/useImpressionRef/useImpressionRef.mjs +26 -8
  43. package/dist/hooks/useInputState/useInputState.cjs +1 -1
  44. package/dist/hooks/useInputState/useInputState.d.cts +3 -2
  45. package/dist/hooks/useInputState/useInputState.d.mts +3 -2
  46. package/dist/hooks/useInputState/useInputState.mjs +1 -1
  47. package/dist/hooks/useIntersectionObserver/useIntersectionObserver.cjs +9 -8
  48. package/dist/hooks/useIntersectionObserver/useIntersectionObserver.d.cts +1 -5
  49. package/dist/hooks/useIntersectionObserver/useIntersectionObserver.d.mts +1 -5
  50. package/dist/hooks/useIntersectionObserver/useIntersectionObserver.mjs +9 -8
  51. package/dist/{mobile/hooks → hooks}/useKeyboardHeight/useKeyboardHeight.cjs +1 -1
  52. package/dist/{mobile/hooks → hooks}/useKeyboardHeight/useKeyboardHeight.d.cts +1 -1
  53. package/dist/{mobile/hooks → hooks}/useKeyboardHeight/useKeyboardHeight.d.mts +1 -1
  54. package/dist/{mobile/hooks → hooks}/useKeyboardHeight/useKeyboardHeight.mjs +1 -1
  55. package/dist/hooks/useList/useList.cjs +11 -8
  56. package/dist/hooks/useList/useList.d.cts +12 -9
  57. package/dist/hooks/useList/useList.d.mts +12 -9
  58. package/dist/hooks/useList/useList.mjs +11 -8
  59. package/dist/hooks/useLongPress/useLongPress.cjs +3 -4
  60. package/dist/hooks/useLongPress/useLongPress.d.cts +0 -4
  61. package/dist/hooks/useLongPress/useLongPress.d.mts +0 -4
  62. package/dist/hooks/useLongPress/useLongPress.mjs +4 -5
  63. package/dist/{mobile/hooks → hooks}/useNetworkStatus/useNetworkStatus.cjs +1 -1
  64. package/dist/{mobile/hooks → hooks}/useNetworkStatus/useNetworkStatus.d.cts +1 -1
  65. package/dist/{mobile/hooks → hooks}/useNetworkStatus/useNetworkStatus.d.mts +1 -1
  66. package/dist/{mobile/hooks → hooks}/useNetworkStatus/useNetworkStatus.mjs +1 -1
  67. package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.cjs +0 -4
  68. package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.d.cts +0 -4
  69. package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.d.mts +0 -4
  70. package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.mjs +0 -4
  71. package/dist/{mobile/hooks → hooks}/usePageVisibility/usePageVisibility.cjs +3 -1
  72. package/dist/{mobile/hooks → hooks}/usePageVisibility/usePageVisibility.d.cts +3 -1
  73. package/dist/{mobile/hooks → hooks}/usePageVisibility/usePageVisibility.d.mts +3 -1
  74. package/dist/{mobile/hooks → hooks}/usePageVisibility/usePageVisibility.mjs +3 -1
  75. package/dist/hooks/useRefEffect/useRefEffect.cjs +6 -4
  76. package/dist/hooks/useRefEffect/useRefEffect.mjs +7 -5
  77. package/dist/{mobile/hooks → hooks}/useSafeAreaInset/useSafeAreaInset.cjs +1 -1
  78. package/dist/{mobile/hooks → hooks}/useSafeAreaInset/useSafeAreaInset.d.cts +1 -1
  79. package/dist/{mobile/hooks → hooks}/useSafeAreaInset/useSafeAreaInset.d.mts +1 -1
  80. package/dist/{mobile/hooks → hooks}/useSafeAreaInset/useSafeAreaInset.mjs +1 -1
  81. package/dist/{mobile/hooks → hooks}/useScrollDirection/useScrollDirection.cjs +1 -1
  82. package/dist/{mobile/hooks → hooks}/useScrollDirection/useScrollDirection.d.cts +1 -1
  83. package/dist/{mobile/hooks → hooks}/useScrollDirection/useScrollDirection.d.mts +1 -1
  84. package/dist/{mobile/hooks → hooks}/useScrollDirection/useScrollDirection.mjs +1 -1
  85. package/dist/hooks/useSet/useSet.cjs +11 -2
  86. package/dist/hooks/useSet/useSet.d.cts +11 -2
  87. package/dist/hooks/useSet/useSet.d.mts +11 -2
  88. package/dist/hooks/useSet/useSet.mjs +11 -2
  89. package/dist/hooks/useStorageState/useStorageState.cjs +1 -1
  90. package/dist/hooks/useStorageState/useStorageState.d.cts +0 -4
  91. package/dist/hooks/useStorageState/useStorageState.d.mts +0 -4
  92. package/dist/hooks/useStorageState/useStorageState.mjs +1 -1
  93. package/dist/hooks/useThrottle/throttle.cjs +22 -5
  94. package/dist/hooks/useThrottle/throttle.mjs +22 -5
  95. package/dist/hooks/useThrottledCallback/useThrottledCallback.cjs +22 -37
  96. package/dist/hooks/useThrottledCallback/useThrottledCallback.d.cts +18 -11
  97. package/dist/hooks/useThrottledCallback/useThrottledCallback.d.mts +18 -11
  98. package/dist/hooks/useThrottledCallback/useThrottledCallback.mjs +23 -38
  99. package/dist/hooks/useThrottledValue/useThrottledValue.cjs +66 -0
  100. package/dist/hooks/useThrottledValue/useThrottledValue.d.cts +48 -0
  101. package/dist/hooks/useThrottledValue/useThrottledValue.d.mts +48 -0
  102. package/dist/hooks/useThrottledValue/useThrottledValue.mjs +66 -0
  103. package/dist/hooks/useVisibilityEvent/useVisibilityEvent.cjs +2 -4
  104. package/dist/hooks/useVisibilityEvent/useVisibilityEvent.d.cts +2 -4
  105. package/dist/hooks/useVisibilityEvent/useVisibilityEvent.d.mts +2 -4
  106. package/dist/hooks/useVisibilityEvent/useVisibilityEvent.mjs +2 -4
  107. package/dist/{mobile/hooks → hooks}/useVisualViewport/useVisualViewport.cjs +1 -1
  108. package/dist/{mobile/hooks → hooks}/useVisualViewport/useVisualViewport.d.cts +1 -1
  109. package/dist/{mobile/hooks → hooks}/useVisualViewport/useVisualViewport.d.mts +1 -1
  110. package/dist/{mobile/hooks → hooks}/useVisualViewport/useVisualViewport.mjs +1 -1
  111. package/dist/index.cjs +23 -19
  112. package/dist/index.d.cts +21 -19
  113. package/dist/index.d.mts +21 -19
  114. package/dist/index.mjs +22 -20
  115. package/dist/{mobile/utils → utils}/disableBodyScrollLock/disableBodyScrollLock.cjs +1 -1
  116. package/dist/{mobile/utils → utils}/disableBodyScrollLock/disableBodyScrollLock.d.cts +1 -1
  117. package/dist/{mobile/utils → utils}/disableBodyScrollLock/disableBodyScrollLock.d.mts +1 -1
  118. package/dist/{mobile/utils → utils}/disableBodyScrollLock/disableBodyScrollLock.mjs +1 -1
  119. package/dist/{mobile/utils → utils}/enableBodyScrollLock/enableBodyScrollLock.cjs +1 -1
  120. package/dist/{mobile/utils → utils}/enableBodyScrollLock/enableBodyScrollLock.d.cts +1 -1
  121. package/dist/{mobile/utils → utils}/enableBodyScrollLock/enableBodyScrollLock.d.mts +1 -1
  122. package/dist/{mobile/utils → utils}/enableBodyScrollLock/enableBodyScrollLock.mjs +1 -1
  123. package/dist/{mobile/utils → utils}/getKeyboardHeight/getKeyboardHeight.cjs +1 -1
  124. package/dist/{mobile/utils → utils}/getKeyboardHeight/getKeyboardHeight.d.cts +1 -1
  125. package/dist/{mobile/utils → utils}/getKeyboardHeight/getKeyboardHeight.d.mts +1 -1
  126. package/dist/{mobile/utils → utils}/getKeyboardHeight/getKeyboardHeight.mjs +1 -1
  127. package/dist/{mobile/utils → utils}/getSafeAreaInset/getSafeAreaInset.cjs +1 -1
  128. package/dist/{mobile/utils → utils}/getSafeAreaInset/getSafeAreaInset.d.cts +1 -1
  129. package/dist/{mobile/utils → utils}/getSafeAreaInset/getSafeAreaInset.d.mts +1 -1
  130. package/dist/{mobile/utils → utils}/getSafeAreaInset/getSafeAreaInset.mjs +1 -1
  131. package/dist/{mobile/utils → utils}/isAndroid/isAndroid.cjs +1 -1
  132. package/dist/{mobile/utils → utils}/isAndroid/isAndroid.d.cts +1 -1
  133. package/dist/{mobile/utils → utils}/isAndroid/isAndroid.d.mts +1 -1
  134. package/dist/{mobile/utils → utils}/isAndroid/isAndroid.mjs +1 -1
  135. package/dist/{mobile/utils → utils}/isIOS/isIOS.cjs +1 -1
  136. package/dist/{mobile/utils → utils}/isIOS/isIOS.d.cts +1 -1
  137. package/dist/{mobile/utils → utils}/isIOS/isIOS.d.mts +1 -1
  138. package/dist/{mobile/utils → utils}/isIOS/isIOS.mjs +1 -1
  139. package/dist/{mobile/utils → utils}/isKeyboardVisible/isKeyboardVisible.cjs +1 -1
  140. package/dist/{mobile/utils → utils}/isKeyboardVisible/isKeyboardVisible.d.cts +1 -1
  141. package/dist/{mobile/utils → utils}/isKeyboardVisible/isKeyboardVisible.d.mts +1 -1
  142. package/dist/{mobile/utils → utils}/isKeyboardVisible/isKeyboardVisible.mjs +1 -1
  143. package/dist/{mobile/utils → utils}/isServer/isServer.cjs +1 -1
  144. package/dist/{mobile/utils → utils}/isServer/isServer.d.cts +1 -1
  145. package/dist/{mobile/utils → utils}/isServer/isServer.d.mts +1 -1
  146. package/dist/{mobile/utils → utils}/isServer/isServer.mjs +1 -1
  147. package/dist/utils/mergeProps/mergeProps.cjs +1 -1
  148. package/dist/utils/mergeProps/mergeProps.d.cts +1 -1
  149. package/dist/utils/mergeProps/mergeProps.d.mts +1 -1
  150. package/dist/utils/mergeProps/mergeProps.mjs +1 -1
  151. package/dist/utils/mergeRefs/mergeRefs.cjs +1 -1
  152. package/dist/utils/mergeRefs/mergeRefs.d.cts +1 -1
  153. package/dist/utils/mergeRefs/mergeRefs.d.mts +1 -1
  154. package/dist/utils/mergeRefs/mergeRefs.mjs +1 -1
  155. package/dist/{mobile/utils → utils}/subscribeKeyboardHeight/subscribeKeyboardHeight.cjs +1 -1
  156. package/dist/{mobile/utils → utils}/subscribeKeyboardHeight/subscribeKeyboardHeight.d.cts +1 -1
  157. package/dist/{mobile/utils → utils}/subscribeKeyboardHeight/subscribeKeyboardHeight.d.mts +1 -1
  158. package/dist/{mobile/utils → utils}/subscribeKeyboardHeight/subscribeKeyboardHeight.mjs +1 -1
  159. package/package.json +2 -4
  160. /package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/index.d.cts +0 -0
  161. /package/dist/{mobile/hooks → hooks}/useAvoidKeyboard/index.d.mts +0 -0
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  const require_isServer = require("../../utils/isServer/isServer.cjs");
3
3
  let react = require("react");
4
- //#region src/mobile/hooks/useScrollDirection/useScrollDirection.ts
4
+ //#region src/hooks/useScrollDirection/useScrollDirection.ts
5
5
  /**
6
6
  * @description
7
7
  * `useScrollDirection` is a React hook that detects scroll direction.
@@ -1,5 +1,5 @@
1
1
 
2
- //#region src/mobile/hooks/useScrollDirection/useScrollDirection.d.ts
2
+ //#region src/hooks/useScrollDirection/useScrollDirection.d.ts
3
3
  type ScrollDirection = 'up' | 'down' | null;
4
4
  type ScrollDirectionState = {
5
5
  /** Current scroll direction */
@@ -1,5 +1,5 @@
1
1
 
2
- //#region src/mobile/hooks/useScrollDirection/useScrollDirection.d.ts
2
+ //#region src/hooks/useScrollDirection/useScrollDirection.d.ts
3
3
  type ScrollDirection = 'up' | 'down' | null;
4
4
  type ScrollDirectionState = {
5
5
  /** Current scroll direction */
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { isServer } from "../../utils/isServer/isServer.mjs";
3
3
  import { useCallback, useEffect, useRef, useState } from "react";
4
- //#region src/mobile/hooks/useScrollDirection/useScrollDirection.ts
4
+ //#region src/hooks/useScrollDirection/useScrollDirection.ts
5
5
  /**
6
6
  * @description
7
7
  * `useScrollDirection` is a React hook that detects scroll direction.
@@ -8,8 +8,17 @@ let react = require("react");
8
8
  * A React hook that manages a Set as state.
9
9
  * Provides efficient state management and stable action functions.
10
10
  *
11
- * @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
12
- * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
11
+ * @template T - The type of the values held in the set.
12
+ *
13
+ * @param {SetOrValues<T>} [initialState=new Set()] - Initial Set state (Set object or array of values).
14
+ *
15
+ * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it.
16
+ * - [0] `Omit<Set<T>, 'add' | 'clear' | 'delete'>` - The current Set state with mutation methods hidden;
17
+ * - [1].add `(value: T) => void` - Adds a value to the set;
18
+ * - [1].remove `(value: T) => void` - Removes a value from the set;
19
+ * - [1].toggle `(value: T) => void` - Adds the value if absent, removes it if present;
20
+ * - [1].setAll `(values: Set<T> | T[]) => void` - Replaces all values in the set;
21
+ * - [1].reset `() => void` - Resets the set to its initial state;
13
22
  *
14
23
  * @example
15
24
  * ```tsx
@@ -14,8 +14,17 @@ type UseSetReturn<T> = [Omit<Set<T>, 'add' | 'clear' | 'delete'>, SetActions<T>]
14
14
  * A React hook that manages a Set as state.
15
15
  * Provides efficient state management and stable action functions.
16
16
  *
17
- * @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
18
- * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
17
+ * @template T - The type of the values held in the set.
18
+ *
19
+ * @param {SetOrValues<T>} [initialState=new Set()] - Initial Set state (Set object or array of values).
20
+ *
21
+ * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it.
22
+ * - [0] `Omit<Set<T>, 'add' | 'clear' | 'delete'>` - The current Set state with mutation methods hidden;
23
+ * - [1].add `(value: T) => void` - Adds a value to the set;
24
+ * - [1].remove `(value: T) => void` - Removes a value from the set;
25
+ * - [1].toggle `(value: T) => void` - Adds the value if absent, removes it if present;
26
+ * - [1].setAll `(values: Set<T> | T[]) => void` - Replaces all values in the set;
27
+ * - [1].reset `() => void` - Resets the set to its initial state;
19
28
  *
20
29
  * @example
21
30
  * ```tsx
@@ -14,8 +14,17 @@ type UseSetReturn<T> = [Omit<Set<T>, 'add' | 'clear' | 'delete'>, SetActions<T>]
14
14
  * A React hook that manages a Set as state.
15
15
  * Provides efficient state management and stable action functions.
16
16
  *
17
- * @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
18
- * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
17
+ * @template T - The type of the values held in the set.
18
+ *
19
+ * @param {SetOrValues<T>} [initialState=new Set()] - Initial Set state (Set object or array of values).
20
+ *
21
+ * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it.
22
+ * - [0] `Omit<Set<T>, 'add' | 'clear' | 'delete'>` - The current Set state with mutation methods hidden;
23
+ * - [1].add `(value: T) => void` - Adds a value to the set;
24
+ * - [1].remove `(value: T) => void` - Removes a value from the set;
25
+ * - [1].toggle `(value: T) => void` - Adds the value if absent, removes it if present;
26
+ * - [1].setAll `(values: Set<T> | T[]) => void` - Replaces all values in the set;
27
+ * - [1].reset `() => void` - Resets the set to its initial state;
19
28
  *
20
29
  * @example
21
30
  * ```tsx
@@ -8,8 +8,17 @@ import { useMemo, useState } from "react";
8
8
  * A React hook that manages a Set as state.
9
9
  * Provides efficient state management and stable action functions.
10
10
  *
11
- * @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
12
- * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
11
+ * @template T - The type of the values held in the set.
12
+ *
13
+ * @param {SetOrValues<T>} [initialState=new Set()] - Initial Set state (Set object or array of values).
14
+ *
15
+ * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it.
16
+ * - [0] `Omit<Set<T>, 'add' | 'clear' | 'delete'>` - The current Set state with mutation methods hidden;
17
+ * - [1].add `(value: T) => void` - Adds a value to the set;
18
+ * - [1].remove `(value: T) => void` - Removes a value from the set;
19
+ * - [1].toggle `(value: T) => void` - Adds the value if absent, removes it if present;
20
+ * - [1].setAll `(values: Set<T> | T[]) => void` - Replaces all values in the set;
21
+ * - [1].reset `() => void` - Resets the set to its initial state;
13
22
  *
14
23
  * @example
15
24
  * ```tsx
@@ -46,7 +46,7 @@ function useStorageState(key, { storage = require_storage.safeLocalStorage, defa
46
46
  const storageState = (0, react.useSyncExternalStore)((onStoreChange) => {
47
47
  listeners.add(onStoreChange);
48
48
  const handler = (event) => {
49
- if (event.key === key) onStoreChange();
49
+ if (event.key == null || event.key === key) onStoreChange();
50
50
  };
51
51
  window.addEventListener("storage", handler);
52
52
  return () => {
@@ -17,10 +17,6 @@ type StorageStateOptionsWithSerializer<T> = StorageStateOptions<T> & {
17
17
  };
18
18
  type SerializableGuard<T extends readonly any[]> = T[0] extends any ? T : T[0] extends never ? 'Received a non-serializable value' : T;
19
19
  /**
20
- * @deprecated This hook is deprecated as it depends on browser-specific APIs (localStorage/sessionStorage).
21
- * react-simplikit is now focused on platform-independent, pure state/logic hooks.
22
- * This hook will be removed in a future major version.
23
- *
24
20
  * @description
25
21
  * `useStorageState` is a React that functions like `useState` but persists the state value in browser storage.
26
22
  * The value is retained across page reloads and can be shared between tabs when using `localStorage`.
@@ -17,10 +17,6 @@ type StorageStateOptionsWithSerializer<T> = StorageStateOptions<T> & {
17
17
  };
18
18
  type SerializableGuard<T extends readonly any[]> = T[0] extends any ? T : T[0] extends never ? 'Received a non-serializable value' : T;
19
19
  /**
20
- * @deprecated This hook is deprecated as it depends on browser-specific APIs (localStorage/sessionStorage).
21
- * react-simplikit is now focused on platform-independent, pure state/logic hooks.
22
- * This hook will be removed in a future major version.
23
- *
24
20
  * @description
25
21
  * `useStorageState` is a React that functions like `useState` but persists the state value in browser storage.
26
22
  * The value is retained across page reloads and can be shared between tabs when using `localStorage`.
@@ -46,7 +46,7 @@ function useStorageState(key, { storage = safeLocalStorage, defaultValue, ...opt
46
46
  const storageState = useSyncExternalStore((onStoreChange) => {
47
47
  listeners.add(onStoreChange);
48
48
  const handler = (event) => {
49
- if (event.key === key) onStoreChange();
49
+ if (event.key == null || event.key === key) onStoreChange();
50
50
  };
51
51
  window.addEventListener("storage", handler);
52
52
  return () => {
@@ -2,17 +2,34 @@
2
2
  const require_debounce = require("../useDebounce/debounce.cjs");
3
3
  //#region src/hooks/useThrottle/throttle.ts
4
4
  function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
5
+ const leading = edges.includes("leading");
6
+ const trailing = edges.includes("trailing");
5
7
  let pendingAt = null;
6
- const debounced = require_debounce.debounce(func, throttleMs, { edges });
8
+ let isPending = false;
9
+ const debounced = require_debounce.debounce(function(...args) {
10
+ pendingAt = Date.now();
11
+ isPending = false;
12
+ func.apply(this, args);
13
+ }, throttleMs, { edges });
7
14
  const throttled = function(...args) {
8
- if (pendingAt == null) pendingAt = Date.now();
9
- else if (Date.now() - pendingAt >= throttleMs) {
15
+ if (pendingAt === null) pendingAt = Date.now();
16
+ const windowElapsed = Date.now() - pendingAt >= throttleMs;
17
+ if (windowElapsed && (leading || trailing && isPending)) {
10
18
  pendingAt = Date.now();
19
+ isPending = false;
20
+ func.apply(this, args);
11
21
  debounced.cancel();
22
+ debounced.schedule();
23
+ return;
12
24
  }
13
- debounced(...args);
25
+ if (windowElapsed) pendingAt = Date.now();
26
+ isPending = true;
27
+ debounced.apply(this, args);
28
+ };
29
+ throttled.cancel = () => {
30
+ isPending = false;
31
+ debounced.cancel();
14
32
  };
15
- throttled.cancel = debounced.cancel;
16
33
  return throttled;
17
34
  }
18
35
  //#endregion
@@ -2,17 +2,34 @@
2
2
  import { debounce } from "../useDebounce/debounce.mjs";
3
3
  //#region src/hooks/useThrottle/throttle.ts
4
4
  function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
5
+ const leading = edges.includes("leading");
6
+ const trailing = edges.includes("trailing");
5
7
  let pendingAt = null;
6
- const debounced = debounce(func, throttleMs, { edges });
8
+ let isPending = false;
9
+ const debounced = debounce(function(...args) {
10
+ pendingAt = Date.now();
11
+ isPending = false;
12
+ func.apply(this, args);
13
+ }, throttleMs, { edges });
7
14
  const throttled = function(...args) {
8
- if (pendingAt == null) pendingAt = Date.now();
9
- else if (Date.now() - pendingAt >= throttleMs) {
15
+ if (pendingAt === null) pendingAt = Date.now();
16
+ const windowElapsed = Date.now() - pendingAt >= throttleMs;
17
+ if (windowElapsed && (leading || trailing && isPending)) {
10
18
  pendingAt = Date.now();
19
+ isPending = false;
20
+ func.apply(this, args);
11
21
  debounced.cancel();
22
+ debounced.schedule();
23
+ return;
12
24
  }
13
- debounced(...args);
25
+ if (windowElapsed) pendingAt = Date.now();
26
+ isPending = true;
27
+ debounced.apply(this, args);
28
+ };
29
+ throttled.cancel = () => {
30
+ isPending = false;
31
+ debounced.cancel();
14
32
  };
15
- throttled.cancel = debounced.cancel;
16
33
  return throttled;
17
34
  }
18
35
  //#endregion
@@ -1,7 +1,5 @@
1
1
  "use client";
2
- const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
3
- const require_usePreservedReference = require("../usePreservedReference/usePreservedReference.cjs");
4
- const require_throttle = require("../useThrottle/throttle.cjs");
2
+ const require_useThrottle = require("../useThrottle/useThrottle.cjs");
5
3
  let react = require("react");
6
4
  //#region src/hooks/useThrottledCallback/useThrottledCallback.ts
7
5
  /**
@@ -17,49 +15,36 @@ const NOT_INVOKED = Symbol("NOT_INVOKED");
17
15
  * `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
18
16
  * The throttled callback will only be invoked at most once per specified interval.
19
17
  *
18
+ * @template T - The type of the value passed to `onChange`.
20
19
  * @param {Object} options - The options object.
21
- * @param {Function} options.onChange - The callback function to throttle.
20
+ * @param {(newValue: T) => void} options.onChange - The callback to throttle. A call with the same value as the last forwarded one is skipped.
22
21
  * @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
23
22
  * @param {Array<'leading' | 'trailing'>} [options.edges=['leading', 'trailing']] - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
24
23
  *
25
- * @returns {Function} A throttled function that limits invoking the callback.
24
+ * @returns {(nextValue: T) => void} A throttled function that forwards the value to `onChange` at most once per interval.
26
25
  *
27
26
  * @example
28
- * function ScrollTracker() {
29
- * const throttledScroll = useThrottledCallback({
30
- * onChange: (scrollY: number) => console.log(scrollY),
31
- * timeThreshold: 200,
32
- * });
33
- * return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
27
+ * import { useThrottledCallback } from 'react-simplikit';
28
+ * import { useState } from 'react';
29
+ *
30
+ * function ScrollPosition() {
31
+ * const [scrollTop, setScrollTop] = useState(0);
32
+ * const setScrollTopThrottled = useThrottledCallback({ onChange: setScrollTop, timeThreshold: 200 });
33
+ *
34
+ * return (
35
+ * <div onScroll={e => setScrollTopThrottled(e.currentTarget.scrollTop)}>
36
+ * <p>Scrolled {scrollTop}px</p>
37
+ * </div>
38
+ * );
34
39
  * }
35
40
  */
36
41
  function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
37
- const handleChange = require_usePreservedCallback.usePreservedCallback(onChange);
38
- const ref = (0, react.useRef)({
39
- value: NOT_INVOKED,
40
- clearPreviousThrottle: () => {}
41
- });
42
- (0, react.useEffect)(function cleanupThrottleOnUnmount() {
43
- const current = ref.current;
44
- return () => {
45
- current.clearPreviousThrottle();
46
- };
47
- }, []);
48
- const preservedEdges = require_usePreservedReference.usePreservedReference(edges);
49
- return (0, react.useCallback)((nextValue) => {
50
- if (nextValue === ref.current.value) return;
51
- const throttled = require_throttle.throttle(() => {
52
- handleChange(nextValue);
53
- ref.current.value = nextValue;
54
- }, timeThreshold, { edges: preservedEdges });
55
- ref.current.clearPreviousThrottle();
56
- throttled();
57
- ref.current.clearPreviousThrottle = throttled.cancel;
58
- }, [
59
- handleChange,
60
- timeThreshold,
61
- preservedEdges
62
- ]);
42
+ const lastForwardedRef = (0, react.useRef)(NOT_INVOKED);
43
+ return require_useThrottle.useThrottle((nextValue) => {
44
+ if (nextValue === lastForwardedRef.current) return;
45
+ onChange(nextValue);
46
+ lastForwardedRef.current = nextValue;
47
+ }, timeThreshold, { edges });
63
48
  }
64
49
  //#endregion
65
50
  exports.useThrottledCallback = useThrottledCallback;
@@ -8,25 +8,32 @@ type ThrottleOptions = {
8
8
  * `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
9
9
  * The throttled callback will only be invoked at most once per specified interval.
10
10
  *
11
+ * @template T - The type of the value passed to `onChange`.
11
12
  * @param {Object} options - The options object.
12
- * @param {Function} options.onChange - The callback function to throttle.
13
+ * @param {(newValue: T) => void} options.onChange - The callback to throttle. A call with the same value as the last forwarded one is skipped.
13
14
  * @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
14
15
  * @param {Array<'leading' | 'trailing'>} [options.edges=['leading', 'trailing']] - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
15
16
  *
16
- * @returns {Function} A throttled function that limits invoking the callback.
17
+ * @returns {(nextValue: T) => void} A throttled function that forwards the value to `onChange` at most once per interval.
17
18
  *
18
19
  * @example
19
- * function ScrollTracker() {
20
- * const throttledScroll = useThrottledCallback({
21
- * onChange: (scrollY: number) => console.log(scrollY),
22
- * timeThreshold: 200,
23
- * });
24
- * return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
20
+ * import { useThrottledCallback } from 'react-simplikit';
21
+ * import { useState } from 'react';
22
+ *
23
+ * function ScrollPosition() {
24
+ * const [scrollTop, setScrollTop] = useState(0);
25
+ * const setScrollTopThrottled = useThrottledCallback({ onChange: setScrollTop, timeThreshold: 200 });
26
+ *
27
+ * return (
28
+ * <div onScroll={e => setScrollTopThrottled(e.currentTarget.scrollTop)}>
29
+ * <p>Scrolled {scrollTop}px</p>
30
+ * </div>
31
+ * );
25
32
  * }
26
33
  */
27
- declare function useThrottledCallback({ onChange, timeThreshold, edges }: ThrottleOptions & {
28
- onChange: (newValue: boolean) => void;
34
+ declare function useThrottledCallback<T>({ onChange, timeThreshold, edges }: ThrottleOptions & {
35
+ onChange: (newValue: T) => void;
29
36
  timeThreshold: number;
30
- }): (nextValue: boolean) => void;
37
+ }): (nextValue: T) => void;
31
38
  //#endregion
32
39
  export { useThrottledCallback };
@@ -8,25 +8,32 @@ type ThrottleOptions = {
8
8
  * `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
9
9
  * The throttled callback will only be invoked at most once per specified interval.
10
10
  *
11
+ * @template T - The type of the value passed to `onChange`.
11
12
  * @param {Object} options - The options object.
12
- * @param {Function} options.onChange - The callback function to throttle.
13
+ * @param {(newValue: T) => void} options.onChange - The callback to throttle. A call with the same value as the last forwarded one is skipped.
13
14
  * @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
14
15
  * @param {Array<'leading' | 'trailing'>} [options.edges=['leading', 'trailing']] - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
15
16
  *
16
- * @returns {Function} A throttled function that limits invoking the callback.
17
+ * @returns {(nextValue: T) => void} A throttled function that forwards the value to `onChange` at most once per interval.
17
18
  *
18
19
  * @example
19
- * function ScrollTracker() {
20
- * const throttledScroll = useThrottledCallback({
21
- * onChange: (scrollY: number) => console.log(scrollY),
22
- * timeThreshold: 200,
23
- * });
24
- * return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
20
+ * import { useThrottledCallback } from 'react-simplikit';
21
+ * import { useState } from 'react';
22
+ *
23
+ * function ScrollPosition() {
24
+ * const [scrollTop, setScrollTop] = useState(0);
25
+ * const setScrollTopThrottled = useThrottledCallback({ onChange: setScrollTop, timeThreshold: 200 });
26
+ *
27
+ * return (
28
+ * <div onScroll={e => setScrollTopThrottled(e.currentTarget.scrollTop)}>
29
+ * <p>Scrolled {scrollTop}px</p>
30
+ * </div>
31
+ * );
25
32
  * }
26
33
  */
27
- declare function useThrottledCallback({ onChange, timeThreshold, edges }: ThrottleOptions & {
28
- onChange: (newValue: boolean) => void;
34
+ declare function useThrottledCallback<T>({ onChange, timeThreshold, edges }: ThrottleOptions & {
35
+ onChange: (newValue: T) => void;
29
36
  timeThreshold: number;
30
- }): (nextValue: boolean) => void;
37
+ }): (nextValue: T) => void;
31
38
  //#endregion
32
39
  export { useThrottledCallback };
@@ -1,8 +1,6 @@
1
1
  "use client";
2
- import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
3
- import { usePreservedReference } from "../usePreservedReference/usePreservedReference.mjs";
4
- import { throttle } from "../useThrottle/throttle.mjs";
5
- import { useCallback, useEffect, useRef } from "react";
2
+ import { useThrottle } from "../useThrottle/useThrottle.mjs";
3
+ import { useRef } from "react";
6
4
  //#region src/hooks/useThrottledCallback/useThrottledCallback.ts
7
5
  /**
8
6
  * Marks that no value has been forwarded to `onChange` yet.
@@ -17,49 +15,36 @@ const NOT_INVOKED = Symbol("NOT_INVOKED");
17
15
  * `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
18
16
  * The throttled callback will only be invoked at most once per specified interval.
19
17
  *
18
+ * @template T - The type of the value passed to `onChange`.
20
19
  * @param {Object} options - The options object.
21
- * @param {Function} options.onChange - The callback function to throttle.
20
+ * @param {(newValue: T) => void} options.onChange - The callback to throttle. A call with the same value as the last forwarded one is skipped.
22
21
  * @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
23
22
  * @param {Array<'leading' | 'trailing'>} [options.edges=['leading', 'trailing']] - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
24
23
  *
25
- * @returns {Function} A throttled function that limits invoking the callback.
24
+ * @returns {(nextValue: T) => void} A throttled function that forwards the value to `onChange` at most once per interval.
26
25
  *
27
26
  * @example
28
- * function ScrollTracker() {
29
- * const throttledScroll = useThrottledCallback({
30
- * onChange: (scrollY: number) => console.log(scrollY),
31
- * timeThreshold: 200,
32
- * });
33
- * return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
27
+ * import { useThrottledCallback } from 'react-simplikit';
28
+ * import { useState } from 'react';
29
+ *
30
+ * function ScrollPosition() {
31
+ * const [scrollTop, setScrollTop] = useState(0);
32
+ * const setScrollTopThrottled = useThrottledCallback({ onChange: setScrollTop, timeThreshold: 200 });
33
+ *
34
+ * return (
35
+ * <div onScroll={e => setScrollTopThrottled(e.currentTarget.scrollTop)}>
36
+ * <p>Scrolled {scrollTop}px</p>
37
+ * </div>
38
+ * );
34
39
  * }
35
40
  */
36
41
  function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
37
- const handleChange = usePreservedCallback(onChange);
38
- const ref = useRef({
39
- value: NOT_INVOKED,
40
- clearPreviousThrottle: () => {}
41
- });
42
- useEffect(function cleanupThrottleOnUnmount() {
43
- const current = ref.current;
44
- return () => {
45
- current.clearPreviousThrottle();
46
- };
47
- }, []);
48
- const preservedEdges = usePreservedReference(edges);
49
- return useCallback((nextValue) => {
50
- if (nextValue === ref.current.value) return;
51
- const throttled = throttle(() => {
52
- handleChange(nextValue);
53
- ref.current.value = nextValue;
54
- }, timeThreshold, { edges: preservedEdges });
55
- ref.current.clearPreviousThrottle();
56
- throttled();
57
- ref.current.clearPreviousThrottle = throttled.cancel;
58
- }, [
59
- handleChange,
60
- timeThreshold,
61
- preservedEdges
62
- ]);
42
+ const lastForwardedRef = useRef(NOT_INVOKED);
43
+ return useThrottle((nextValue) => {
44
+ if (nextValue === lastForwardedRef.current) return;
45
+ onChange(nextValue);
46
+ lastForwardedRef.current = nextValue;
47
+ }, timeThreshold, { edges });
63
48
  }
64
49
  //#endregion
65
50
  export { useThrottledCallback };
@@ -0,0 +1,66 @@
1
+ "use client";
2
+ const require_useThrottle = require("../useThrottle/useThrottle.cjs");
3
+ let react = require("react");
4
+ //#region src/hooks/useThrottledValue/useThrottledValue.ts
5
+ /**
6
+ * @description
7
+ * `useThrottledValue` is a React hook that returns a throttled copy of the given value.
8
+ * The caller keeps owning the state; the returned value follows it at most once per `wait` milliseconds,
9
+ * which is useful for driving expensive renders from scroll position, pointer position, or an element's
10
+ * size on resize.
11
+ *
12
+ * On the first render and on the server the value is returned as is. A change is never scheduled
13
+ * on mount, so the first change after mount is applied immediately when `leading` is `true`.
14
+ * If both `leading` and `trailing` are `false`, the returned value never updates.
15
+ *
16
+ * The value is compared by reference. Passing a new object or array on every render keeps
17
+ * the returned value updating every `wait` milliseconds; stabilize the reference first, for
18
+ * example with `usePreservedReference`.
19
+ *
20
+ * @template T - The type of the value.
21
+ * @param {T} value - The value to throttle.
22
+ * @param {number} wait - The length of the throttle window in milliseconds.
23
+ * @param {ThrottleOptions} [options] - Configuration options for throttle behavior.
24
+ * @param {boolean} [options.leading=true] - If `true`, the first change in a window is applied immediately.
25
+ * @param {boolean} [options.trailing=true] - If `true`, the last change in a window is applied `wait` milliseconds after that change.
26
+ *
27
+ * @returns {T} The throttled value.
28
+ *
29
+ * @example
30
+ * import { useThrottledValue } from 'react-simplikit';
31
+ * import { useState } from 'react';
32
+ *
33
+ * function ScrollProgress() {
34
+ * const [scrollY, setScrollY] = useState(0);
35
+ * const throttledScrollY = useThrottledValue(scrollY, 100);
36
+ *
37
+ * return (
38
+ * <div onScroll={e => setScrollY(e.currentTarget.scrollTop)}>
39
+ * <ProgressBar position={throttledScrollY} />
40
+ * </div>
41
+ * );
42
+ * }
43
+ */
44
+ function useThrottledValue(value, wait, { leading = true, trailing = true } = {}) {
45
+ const [throttledValue, setThrottledValue] = (0, react.useState)(() => value);
46
+ const throttled = require_useThrottle.useThrottle((next) => setThrottledValue(() => next), wait, { edges: toEdges(leading, trailing) });
47
+ const lastForwardedRef = (0, react.useRef)(value);
48
+ (0, react.useEffect)(function forwardChangedValue() {
49
+ if (Object.is(lastForwardedRef.current, value) && Object.is(value, throttledValue)) return;
50
+ lastForwardedRef.current = value;
51
+ throttled(value);
52
+ }, [
53
+ value,
54
+ throttledValue,
55
+ throttled
56
+ ]);
57
+ return throttledValue;
58
+ }
59
+ function toEdges(leading, trailing) {
60
+ const edges = [];
61
+ if (leading) edges.push("leading");
62
+ if (trailing) edges.push("trailing");
63
+ return edges;
64
+ }
65
+ //#endregion
66
+ exports.useThrottledValue = useThrottledValue;
@@ -0,0 +1,48 @@
1
+
2
+ //#region src/hooks/useThrottledValue/useThrottledValue.d.ts
3
+ type ThrottleOptions = {
4
+ leading?: boolean;
5
+ trailing?: boolean;
6
+ };
7
+ /**
8
+ * @description
9
+ * `useThrottledValue` is a React hook that returns a throttled copy of the given value.
10
+ * The caller keeps owning the state; the returned value follows it at most once per `wait` milliseconds,
11
+ * which is useful for driving expensive renders from scroll position, pointer position, or an element's
12
+ * size on resize.
13
+ *
14
+ * On the first render and on the server the value is returned as is. A change is never scheduled
15
+ * on mount, so the first change after mount is applied immediately when `leading` is `true`.
16
+ * If both `leading` and `trailing` are `false`, the returned value never updates.
17
+ *
18
+ * The value is compared by reference. Passing a new object or array on every render keeps
19
+ * the returned value updating every `wait` milliseconds; stabilize the reference first, for
20
+ * example with `usePreservedReference`.
21
+ *
22
+ * @template T - The type of the value.
23
+ * @param {T} value - The value to throttle.
24
+ * @param {number} wait - The length of the throttle window in milliseconds.
25
+ * @param {ThrottleOptions} [options] - Configuration options for throttle behavior.
26
+ * @param {boolean} [options.leading=true] - If `true`, the first change in a window is applied immediately.
27
+ * @param {boolean} [options.trailing=true] - If `true`, the last change in a window is applied `wait` milliseconds after that change.
28
+ *
29
+ * @returns {T} The throttled value.
30
+ *
31
+ * @example
32
+ * import { useThrottledValue } from 'react-simplikit';
33
+ * import { useState } from 'react';
34
+ *
35
+ * function ScrollProgress() {
36
+ * const [scrollY, setScrollY] = useState(0);
37
+ * const throttledScrollY = useThrottledValue(scrollY, 100);
38
+ *
39
+ * return (
40
+ * <div onScroll={e => setScrollY(e.currentTarget.scrollTop)}>
41
+ * <ProgressBar position={throttledScrollY} />
42
+ * </div>
43
+ * );
44
+ * }
45
+ */
46
+ declare function useThrottledValue<T>(value: T, wait: number, { leading, trailing }?: ThrottleOptions): T;
47
+ //#endregion
48
+ export { useThrottledValue };