react-simplikit 0.0.2 → 0.0.4

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 (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/dist/components/ImpressionArea/index.d.ts +46 -0
  4. package/dist/components/ImpressionArea/index.js +276 -0
  5. package/dist/components/Separated/index.d.ts +38 -0
  6. package/dist/components/Separated/index.js +40 -0
  7. package/dist/components/SwitchCase/index.d.ts +46 -0
  8. package/dist/components/SwitchCase/index.js +35 -0
  9. package/dist/hooks/useAsyncEffect/index.d.ts +25 -0
  10. package/dist/hooks/useAsyncEffect/index.js +43 -0
  11. package/dist/hooks/useBooleanState/index.d.ts +19 -0
  12. package/dist/hooks/useBooleanState/index.js +45 -0
  13. package/dist/hooks/useCallbackOncePerRender/index.d.ts +41 -0
  14. package/dist/hooks/useCallbackOncePerRender/index.js +59 -0
  15. package/dist/hooks/useDebounce/index.d.ts +48 -0
  16. package/dist/hooks/useDebounce/index.js +123 -0
  17. package/dist/hooks/useImpressionRef/index.d.ts +38 -0
  18. package/dist/hooks/useImpressionRef/index.js +237 -0
  19. package/dist/hooks/useInputState/index.d.ts +27 -0
  20. package/dist/hooks/useInputState/index.js +45 -0
  21. package/dist/hooks/useIntersectionObserver/index.d.ts +33 -0
  22. package/dist/hooks/useIntersectionObserver/index.js +91 -0
  23. package/dist/hooks/useInterval/index.d.ts +37 -0
  24. package/dist/hooks/useInterval/index.js +64 -0
  25. package/dist/hooks/useLoading/index.d.ts +37 -0
  26. package/dist/hooks/useLoading/index.js +61 -0
  27. package/dist/hooks/useOutsideClickEffect/index.d.ts +27 -0
  28. package/dist/hooks/useOutsideClickEffect/index.js +70 -0
  29. package/dist/hooks/usePreservedCallback/index.d.ts +30 -0
  30. package/dist/hooks/usePreservedCallback/index.js +41 -0
  31. package/dist/hooks/usePreservedReference/index.d.ts +42 -0
  32. package/dist/hooks/usePreservedReference/index.js +44 -0
  33. package/dist/hooks/usePrevious/index.d.ts +21 -0
  34. package/dist/hooks/usePrevious/index.js +47 -0
  35. package/dist/hooks/useRefEffect/index.d.ts +31 -0
  36. package/dist/hooks/useRefEffect/index.js +68 -0
  37. package/dist/hooks/useStorageState/index.d.ts +50 -0
  38. package/dist/hooks/useStorageState/index.js +168 -0
  39. package/dist/hooks/useThrottle/index.d.ts +47 -0
  40. package/dist/hooks/useThrottle/index.js +147 -0
  41. package/dist/hooks/useTimeout/index.d.ts +30 -0
  42. package/dist/hooks/useTimeout/index.js +53 -0
  43. package/dist/hooks/useToggle/index.d.ts +30 -0
  44. package/dist/hooks/useToggle/index.js +36 -0
  45. package/dist/hooks/useVisibilityEvent/index.d.ts +27 -0
  46. package/dist/hooks/useVisibilityEvent/index.js +46 -0
  47. package/dist/index.d.ts +26 -0
  48. package/dist/index.js +761 -0
  49. package/dist/utils/buildContext/index.d.ts +38 -0
  50. package/dist/utils/buildContext/index.js +55 -0
  51. package/dist/utils/mergeRefs/index.d.ts +37 -0
  52. package/dist/utils/mergeRefs/index.js +45 -0
  53. package/esm/components/ImpressionArea/index.d.mts +46 -0
  54. package/esm/components/ImpressionArea/index.mjs +249 -0
  55. package/esm/components/Separated/index.d.mts +38 -0
  56. package/esm/components/Separated/index.mjs +13 -0
  57. package/esm/components/SwitchCase/index.d.mts +46 -0
  58. package/esm/components/SwitchCase/index.mjs +8 -0
  59. package/esm/hooks/useAsyncEffect/index.d.mts +25 -0
  60. package/esm/hooks/useAsyncEffect/index.mjs +16 -0
  61. package/esm/hooks/useBooleanState/index.d.mts +19 -0
  62. package/esm/hooks/useBooleanState/index.mjs +18 -0
  63. package/esm/hooks/useCallbackOncePerRender/index.d.mts +41 -0
  64. package/esm/hooks/useCallbackOncePerRender/index.mjs +32 -0
  65. package/esm/hooks/useDebounce/index.d.mts +48 -0
  66. package/esm/hooks/useDebounce/index.mjs +96 -0
  67. package/esm/hooks/useImpressionRef/index.d.mts +38 -0
  68. package/esm/hooks/useImpressionRef/index.mjs +210 -0
  69. package/esm/hooks/useInputState/index.d.mts +27 -0
  70. package/esm/hooks/useInputState/index.mjs +18 -0
  71. package/esm/hooks/useIntersectionObserver/index.d.mts +33 -0
  72. package/esm/hooks/useIntersectionObserver/index.mjs +64 -0
  73. package/esm/hooks/useInterval/index.d.mts +37 -0
  74. package/esm/hooks/useInterval/index.mjs +37 -0
  75. package/esm/hooks/useLoading/index.d.mts +37 -0
  76. package/esm/hooks/useLoading/index.mjs +34 -0
  77. package/esm/hooks/useOutsideClickEffect/index.d.mts +27 -0
  78. package/esm/hooks/useOutsideClickEffect/index.mjs +43 -0
  79. package/esm/hooks/usePreservedCallback/index.d.mts +30 -0
  80. package/esm/hooks/usePreservedCallback/index.mjs +14 -0
  81. package/esm/hooks/usePreservedReference/index.d.mts +42 -0
  82. package/esm/hooks/usePreservedReference/index.mjs +17 -0
  83. package/esm/hooks/usePrevious/index.d.mts +21 -0
  84. package/esm/hooks/usePrevious/index.mjs +20 -0
  85. package/esm/hooks/useRefEffect/index.d.mts +31 -0
  86. package/esm/hooks/useRefEffect/index.mjs +41 -0
  87. package/esm/hooks/useStorageState/index.d.mts +50 -0
  88. package/esm/hooks/useStorageState/index.mjs +141 -0
  89. package/esm/hooks/useThrottle/index.d.mts +47 -0
  90. package/esm/hooks/useThrottle/index.mjs +120 -0
  91. package/esm/hooks/useTimeout/index.d.mts +30 -0
  92. package/esm/hooks/useTimeout/index.mjs +26 -0
  93. package/esm/hooks/useToggle/index.d.mts +30 -0
  94. package/esm/hooks/useToggle/index.mjs +9 -0
  95. package/esm/hooks/useVisibilityEvent/index.d.mts +27 -0
  96. package/esm/hooks/useVisibilityEvent/index.mjs +19 -0
  97. package/esm/index.d.mts +26 -0
  98. package/esm/index.mjs +711 -0
  99. package/esm/utils/buildContext/index.d.mts +38 -0
  100. package/esm/utils/buildContext/index.mjs +28 -0
  101. package/esm/utils/mergeRefs/index.d.mts +37 -0
  102. package/esm/utils/mergeRefs/index.mjs +18 -0
  103. package/package.json +87 -7
@@ -0,0 +1,120 @@
1
+ // src/hooks/useThrottle/useThrottle.ts
2
+ import { useEffect as useEffect2, useMemo as useMemo2 } from "react";
3
+
4
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
5
+ import { useCallback, useEffect, useRef } from "react";
6
+ function usePreservedCallback(callback) {
7
+ const callbackRef = useRef(callback);
8
+ useEffect(() => {
9
+ callbackRef.current = callback;
10
+ }, [callback]);
11
+ return useCallback((...args) => {
12
+ return callbackRef.current(...args);
13
+ }, []);
14
+ }
15
+
16
+ // src/hooks/usePreservedReference/usePreservedReference.ts
17
+ import { useMemo, useRef as useRef2 } from "react";
18
+ function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
19
+ const ref = useRef2(value);
20
+ return useMemo(() => {
21
+ if (!areValuesEqual(ref.current, value)) {
22
+ ref.current = value;
23
+ }
24
+ return ref.current;
25
+ }, [areValuesEqual, value]);
26
+ }
27
+ function areDeeplyEqual(x, y) {
28
+ return JSON.stringify(x) === JSON.stringify(y);
29
+ }
30
+
31
+ // src/hooks/useDebounce/debounce.ts
32
+ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
33
+ let pendingThis = void 0;
34
+ let pendingArgs = null;
35
+ const leading = edges != null && edges.includes("leading");
36
+ const trailing = edges == null || edges.includes("trailing");
37
+ const invoke = () => {
38
+ if (pendingArgs !== null) {
39
+ func.apply(pendingThis, pendingArgs);
40
+ pendingThis = void 0;
41
+ pendingArgs = null;
42
+ }
43
+ };
44
+ const onTimerEnd = () => {
45
+ if (trailing) {
46
+ invoke();
47
+ }
48
+ cancel();
49
+ };
50
+ let timeoutId = null;
51
+ const schedule = () => {
52
+ if (timeoutId != null) {
53
+ clearTimeout(timeoutId);
54
+ }
55
+ timeoutId = setTimeout(() => {
56
+ timeoutId = null;
57
+ onTimerEnd();
58
+ }, debounceMs);
59
+ };
60
+ const cancelTimer = () => {
61
+ if (timeoutId !== null) {
62
+ clearTimeout(timeoutId);
63
+ timeoutId = null;
64
+ }
65
+ };
66
+ const cancel = () => {
67
+ cancelTimer();
68
+ pendingThis = void 0;
69
+ pendingArgs = null;
70
+ };
71
+ const debounced = function(...args) {
72
+ pendingThis = this;
73
+ pendingArgs = args;
74
+ const isFirstCall = timeoutId == null;
75
+ schedule();
76
+ if (leading && isFirstCall) {
77
+ invoke();
78
+ }
79
+ };
80
+ debounced.cancel = cancel;
81
+ return debounced;
82
+ }
83
+
84
+ // src/hooks/useThrottle/throttle.ts
85
+ function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
86
+ let pendingAt = null;
87
+ const debounced = debounce(func, throttleMs, { edges });
88
+ const throttled = function(...args) {
89
+ if (pendingAt == null) {
90
+ pendingAt = Date.now();
91
+ } else {
92
+ if (Date.now() - pendingAt >= throttleMs) {
93
+ pendingAt = Date.now();
94
+ debounced.cancel();
95
+ debounced(...args);
96
+ }
97
+ }
98
+ debounced(...args);
99
+ };
100
+ throttled.cancel = debounced.cancel;
101
+ return throttled;
102
+ }
103
+
104
+ // src/hooks/useThrottle/useThrottle.ts
105
+ function useThrottle(callback, wait, options) {
106
+ const preservedCallback = usePreservedCallback(callback);
107
+ const preservedOptions = usePreservedReference(options ?? {});
108
+ const throttledCallback = useMemo2(() => {
109
+ return throttle(preservedCallback, wait, preservedOptions);
110
+ }, [preservedOptions, preservedCallback, wait]);
111
+ useEffect2(() => {
112
+ return () => {
113
+ throttledCallback.cancel();
114
+ };
115
+ }, [throttledCallback]);
116
+ return throttledCallback;
117
+ }
118
+ export {
119
+ useThrottle
120
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @description
3
+ * `useTimeout` is a React hook that executes a callback function after a specified delay.
4
+ * It manages `window.setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
5
+ *
6
+ * @param {() => void} callback - The function to be executed after the delay.
7
+ * @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.
8
+ *
9
+ * @example
10
+ * // Updating a title after a delay
11
+ * import { useTimeout } from 'react-simplikit';
12
+ * import { useState } from 'react';
13
+ *
14
+ * function Example() {
15
+ * const [title, setTitle] = useState('');
16
+ *
17
+ * useTimeout(() => {
18
+ * setTitle('Searching for products...');
19
+ * }, 2000);
20
+ *
21
+ * useTimeout(() => {
22
+ * setTitle('Almost done...');
23
+ * }, 4000);
24
+ *
25
+ * return <div>{title}</div>;
26
+ * }
27
+ */
28
+ declare function useTimeout(callback: () => void, delay?: number): void;
29
+
30
+ export { useTimeout };
@@ -0,0 +1,26 @@
1
+ // src/hooks/useTimeout/useTimeout.ts
2
+ import { useEffect as useEffect2 } from "react";
3
+
4
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
5
+ import { useCallback, useEffect, useRef } from "react";
6
+ function usePreservedCallback(callback) {
7
+ const callbackRef = useRef(callback);
8
+ useEffect(() => {
9
+ callbackRef.current = callback;
10
+ }, [callback]);
11
+ return useCallback((...args) => {
12
+ return callbackRef.current(...args);
13
+ }, []);
14
+ }
15
+
16
+ // src/hooks/useTimeout/useTimeout.ts
17
+ function useTimeout(callback, delay = 0) {
18
+ const preservedCallback = usePreservedCallback(callback);
19
+ useEffect2(() => {
20
+ const timeoutId = window.setTimeout(preservedCallback, delay);
21
+ return () => window.clearTimeout(timeoutId);
22
+ }, [delay, preservedCallback]);
23
+ }
24
+ export {
25
+ useTimeout
26
+ };
@@ -0,0 +1,30 @@
1
+ import * as React from 'react';
2
+
3
+ /**
4
+ * @description
5
+ * `useToggle` is a React hook that simplifies managing a boolean state.
6
+ * It provides a function to toggle the state between `true` and `false`.
7
+ *
8
+ * @param {boolean} [initialValue=false] - The initial state value. Defaults to `false`.
9
+ *
10
+ * @returns {[boolean, () => void]} A tuple:
11
+ * - `state`: The current state value.
12
+ * - `toggle`: A function to toggle the state.
13
+ *
14
+ * @example
15
+ * import { useToggle } from 'react-simplikit';
16
+ *
17
+ * function Component() {
18
+ * const [open, toggle] = useToggle(false);
19
+ *
20
+ * return (
21
+ * <div>
22
+ * <p>Bottom Sheet state: {open ? 'opened' : 'closed'}</p>
23
+ * <button onClick={toggle}>Toggle</button>
24
+ * </div>
25
+ * );
26
+ * }
27
+ */
28
+ declare function useToggle(initialValue?: boolean): [boolean, React.ActionDispatch<[]>];
29
+
30
+ export { useToggle };
@@ -0,0 +1,9 @@
1
+ // src/hooks/useToggle/useToggle.ts
2
+ import { useReducer } from "react";
3
+ function useToggle(initialValue = false) {
4
+ return useReducer(toggle, initialValue);
5
+ }
6
+ var toggle = (state) => !state;
7
+ export {
8
+ useToggle
9
+ };
@@ -0,0 +1,27 @@
1
+ type Options = {
2
+ immediate?: boolean;
3
+ };
4
+ /**
5
+ * @description
6
+ * A React hook that listens to changes in the document's visibility state and triggers a callback.
7
+ *
8
+ * @param {(visibilityState: 'visible' | 'hidden') => void} callback - A function to be called
9
+ * when the visibility state changes. It receives the current visibility state ('visible' or 'hidden') as an argument.
10
+ * @param {object} [options] - Optional configuration for the hook.
11
+ * @param {boolean} [options.immediate=false] - If true, the callback is invoked immediately upon mounting
12
+ * with the current visibility state.
13
+ *
14
+ * @example
15
+ * import { useVisibilityEvent } from 'react-simplikit';
16
+ *
17
+ * function Component() {
18
+ * useVisibilityEvent(visibilityState => {
19
+ * console.log(`Document is now ${visibilityState}`);
20
+ * });
21
+ *
22
+ * return <p>Check the console for visibility changes.</p>;
23
+ * }
24
+ */
25
+ declare function useVisibilityEvent(callback: (visibilityState: 'visible' | 'hidden') => void, options?: Options): void;
26
+
27
+ export { useVisibilityEvent };
@@ -0,0 +1,19 @@
1
+ // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
2
+ import { useCallback, useEffect } from "react";
3
+ function useVisibilityEvent(callback, options = {}) {
4
+ const handleVisibilityChange = useCallback(() => {
5
+ callback(document.visibilityState);
6
+ }, [callback]);
7
+ useEffect(() => {
8
+ if (options?.immediate ?? false) {
9
+ handleVisibilityChange();
10
+ }
11
+ document.addEventListener("visibilitychange", handleVisibilityChange);
12
+ return () => {
13
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
14
+ };
15
+ }, [handleVisibilityChange, options?.immediate]);
16
+ }
17
+ export {
18
+ useVisibilityEvent
19
+ };
@@ -0,0 +1,26 @@
1
+ export { ImpressionArea } from './components/ImpressionArea/index.mjs';
2
+ export { Separated } from './components/Separated/index.mjs';
3
+ export { SwitchCase } from './components/SwitchCase/index.mjs';
4
+ export { useAsyncEffect } from './hooks/useAsyncEffect/index.mjs';
5
+ export { useBooleanState } from './hooks/useBooleanState/index.mjs';
6
+ export { useCallbackOncePerRender } from './hooks/useCallbackOncePerRender/index.mjs';
7
+ export { useDebounce } from './hooks/useDebounce/index.mjs';
8
+ export { useImpressionRef } from './hooks/useImpressionRef/index.mjs';
9
+ export { useInputState } from './hooks/useInputState/index.mjs';
10
+ export { useIntersectionObserver } from './hooks/useIntersectionObserver/index.mjs';
11
+ export { useInterval } from './hooks/useInterval/index.mjs';
12
+ export { useLoading } from './hooks/useLoading/index.mjs';
13
+ export { useOutsideClickEffect } from './hooks/useOutsideClickEffect/index.mjs';
14
+ export { usePreservedCallback } from './hooks/usePreservedCallback/index.mjs';
15
+ export { usePreservedReference } from './hooks/usePreservedReference/index.mjs';
16
+ export { usePrevious } from './hooks/usePrevious/index.mjs';
17
+ export { useRefEffect } from './hooks/useRefEffect/index.mjs';
18
+ export { useStorageState } from './hooks/useStorageState/index.mjs';
19
+ export { useThrottle } from './hooks/useThrottle/index.mjs';
20
+ export { useTimeout } from './hooks/useTimeout/index.mjs';
21
+ export { useToggle } from './hooks/useToggle/index.mjs';
22
+ export { useVisibilityEvent } from './hooks/useVisibilityEvent/index.mjs';
23
+ export { buildContext } from './utils/buildContext/index.mjs';
24
+ export { mergeRefs } from './utils/mergeRefs/index.mjs';
25
+ import 'react/jsx-runtime';
26
+ import 'react';