react-simplikit 0.0.48 → 0.0.50

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 (63) hide show
  1. package/dist/components/ImpressionArea/index.cjs +7 -4
  2. package/dist/components/ImpressionArea/index.d.cts +1 -1
  3. package/dist/components/Separated/index.cjs +1 -1
  4. package/dist/hooks/useCallbackOncePerRender/index.cjs +6 -3
  5. package/dist/hooks/useCounter/index.cjs +12 -14
  6. package/dist/hooks/useDebounce/index.cjs +14 -8
  7. package/dist/hooks/useDebouncedCallback/index.cjs +7 -4
  8. package/dist/hooks/useDoubleClick/index.cjs +6 -3
  9. package/dist/hooks/useImpressionRef/index.cjs +7 -4
  10. package/dist/hooks/useIntersectionObserver/index.cjs +6 -3
  11. package/dist/hooks/useInterval/index.cjs +24 -15
  12. package/dist/hooks/useIsClient/index.cjs +40 -0
  13. package/dist/hooks/useIsClient/index.d.cts +40 -0
  14. package/dist/hooks/useList/index.cjs +104 -0
  15. package/dist/hooks/useList/index.d.cts +50 -0
  16. package/dist/hooks/useLoading/index.cjs +9 -6
  17. package/dist/hooks/useLongPress/index.cjs +6 -3
  18. package/dist/hooks/useOutsideClickEffect/index.cjs +6 -3
  19. package/dist/hooks/usePreservedCallback/index.cjs +6 -3
  20. package/dist/hooks/usePrevious/index.d.cts +3 -3
  21. package/dist/hooks/useRefEffect/index.cjs +6 -3
  22. package/dist/hooks/useRefEffect/index.d.cts +3 -3
  23. package/dist/hooks/useSet/index.cjs +105 -0
  24. package/dist/hooks/useSet/index.d.cts +39 -0
  25. package/dist/hooks/useThrottle/index.cjs +6 -3
  26. package/dist/hooks/useThrottledCallback/index.cjs +172 -0
  27. package/dist/hooks/useThrottledCallback/index.d.cts +30 -0
  28. package/dist/hooks/useTimeout/index.cjs +8 -5
  29. package/dist/hooks/useTimeout/index.d.cts +1 -1
  30. package/dist/index.cjs +264 -110
  31. package/dist/index.d.cts +4 -0
  32. package/esm/components/ImpressionArea/index.d.ts +1 -1
  33. package/esm/components/ImpressionArea/index.js +7 -4
  34. package/esm/components/Separated/index.js +1 -1
  35. package/esm/hooks/useCallbackOncePerRender/index.js +6 -3
  36. package/esm/hooks/useCounter/index.js +12 -14
  37. package/esm/hooks/useDebounce/index.js +14 -8
  38. package/esm/hooks/useDebouncedCallback/index.js +7 -4
  39. package/esm/hooks/useDoubleClick/index.js +6 -3
  40. package/esm/hooks/useImpressionRef/index.js +7 -4
  41. package/esm/hooks/useIntersectionObserver/index.js +6 -3
  42. package/esm/hooks/useInterval/index.js +24 -15
  43. package/esm/hooks/useIsClient/index.d.ts +40 -0
  44. package/esm/hooks/useIsClient/index.js +14 -0
  45. package/esm/hooks/useList/index.d.ts +50 -0
  46. package/esm/hooks/useList/index.js +78 -0
  47. package/esm/hooks/useLoading/index.js +9 -6
  48. package/esm/hooks/useLongPress/index.js +6 -3
  49. package/esm/hooks/useOutsideClickEffect/index.js +6 -3
  50. package/esm/hooks/usePreservedCallback/index.js +6 -3
  51. package/esm/hooks/usePrevious/index.d.ts +3 -3
  52. package/esm/hooks/useRefEffect/index.d.ts +3 -3
  53. package/esm/hooks/useRefEffect/index.js +6 -3
  54. package/esm/hooks/useSet/index.d.ts +39 -0
  55. package/esm/hooks/useSet/index.js +79 -0
  56. package/esm/hooks/useThrottle/index.js +6 -3
  57. package/esm/hooks/useThrottledCallback/index.d.ts +30 -0
  58. package/esm/hooks/useThrottledCallback/index.js +146 -0
  59. package/esm/hooks/useTimeout/index.d.ts +1 -1
  60. package/esm/hooks/useTimeout/index.js +8 -5
  61. package/esm/index.d.ts +4 -0
  62. package/esm/index.js +234 -84
  63. package/package.json +1 -1
@@ -0,0 +1,105 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/hooks/useSet/index.ts
22
+ var useSet_exports = {};
23
+ __export(useSet_exports, {
24
+ useSet: () => useSet
25
+ });
26
+ module.exports = __toCommonJS(useSet_exports);
27
+
28
+ // src/hooks/useSet/useSet.ts
29
+ var import_react3 = require("react");
30
+
31
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
32
+ var import_react = require("react");
33
+ function usePreservedCallback(callback) {
34
+ const callbackRef = (0, import_react.useRef)(callback);
35
+ (0, import_react.useEffect)(
36
+ function syncCallbackRef() {
37
+ callbackRef.current = callback;
38
+ },
39
+ [callback]
40
+ );
41
+ return (0, import_react.useCallback)((...args) => {
42
+ return callbackRef.current(...args);
43
+ }, []);
44
+ }
45
+
46
+ // src/hooks/usePreservedReference/usePreservedReference.ts
47
+ var import_react2 = require("react");
48
+ function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
49
+ const ref = (0, import_react2.useRef)(value);
50
+ return (0, import_react2.useMemo)(() => {
51
+ if (!areValuesEqual(ref.current, value)) {
52
+ ref.current = value;
53
+ }
54
+ return ref.current;
55
+ }, [areValuesEqual, value]);
56
+ }
57
+ function areDeeplyEqual(x, y) {
58
+ return JSON.stringify(x) === JSON.stringify(y);
59
+ }
60
+
61
+ // src/hooks/useSet/useSet.ts
62
+ function useSet(initialState = /* @__PURE__ */ new Set()) {
63
+ const [set, setSet] = (0, import_react3.useState)(() => new Set(initialState));
64
+ const preservedInitialState = usePreservedReference(initialState);
65
+ const add = usePreservedCallback((value) => {
66
+ setSet((prev) => {
67
+ const nextSet = new Set(prev);
68
+ nextSet.add(value);
69
+ return nextSet;
70
+ });
71
+ });
72
+ const remove = usePreservedCallback((value) => {
73
+ setSet((prev) => {
74
+ const nextSet = new Set(prev);
75
+ nextSet.delete(value);
76
+ return nextSet;
77
+ });
78
+ });
79
+ const toggle = usePreservedCallback((value) => {
80
+ setSet((prev) => {
81
+ const nextSet = new Set(prev);
82
+ if (nextSet.has(value)) {
83
+ nextSet.delete(value);
84
+ } else {
85
+ nextSet.add(value);
86
+ }
87
+ return nextSet;
88
+ });
89
+ });
90
+ const setAll = usePreservedCallback((values) => {
91
+ setSet(() => new Set(values));
92
+ });
93
+ const reset = usePreservedCallback(() => {
94
+ setSet(() => new Set(preservedInitialState));
95
+ });
96
+ const actions = (0, import_react3.useMemo)(
97
+ () => ({ add, remove, toggle, setAll, reset }),
98
+ [add, remove, toggle, setAll, reset]
99
+ );
100
+ return [set, actions];
101
+ }
102
+ // Annotate the CommonJS export names for ESM import in node:
103
+ 0 && (module.exports = {
104
+ useSet
105
+ });
@@ -0,0 +1,39 @@
1
+ type SetOrValues<T> = Set<T> | T[];
2
+ type SetActions<T> = {
3
+ add: (value: T) => void;
4
+ remove: (value: T) => void;
5
+ toggle: (value: T) => void;
6
+ setAll: (values: SetOrValues<T>) => void;
7
+ reset: () => void;
8
+ };
9
+ type UseSetReturn<T> = [Omit<Set<T>, 'add' | 'clear' | 'delete'>, SetActions<T>];
10
+ /**
11
+ * @description
12
+ * A React hook that manages a Set as state.
13
+ * Provides efficient state management and stable action functions.
14
+ *
15
+ * @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
16
+ * @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * import { useSet } from 'react-simplikit';
21
+ *
22
+ * function TagSelector() {
23
+ * const [selectedTags, { add, remove, toggle }] = useSet<string>(['react']);
24
+ *
25
+ * return (
26
+ * <div>
27
+ * {['react', 'vue', 'svelte'].map(tag => (
28
+ * <button key={tag} onClick={() => toggle(tag)}>
29
+ * {selectedTags.has(tag) ? '✓' : ''} {tag}
30
+ * </button>
31
+ * ))}
32
+ * </div>
33
+ * );
34
+ * }
35
+ * ```
36
+ */
37
+ declare function useSet<T>(initialState?: SetOrValues<T>): UseSetReturn<T>;
38
+
39
+ export { useSet };
@@ -32,9 +32,12 @@ var import_react3 = require("react");
32
32
  var import_react = require("react");
33
33
  function usePreservedCallback(callback) {
34
34
  const callbackRef = (0, import_react.useRef)(callback);
35
- (0, import_react.useEffect)(() => {
36
- callbackRef.current = callback;
37
- }, [callback]);
35
+ (0, import_react.useEffect)(
36
+ function syncCallbackRef() {
37
+ callbackRef.current = callback;
38
+ },
39
+ [callback]
40
+ );
38
41
  return (0, import_react.useCallback)((...args) => {
39
42
  return callbackRef.current(...args);
40
43
  }, []);
@@ -0,0 +1,172 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/hooks/useThrottledCallback/index.ts
22
+ var useThrottledCallback_exports = {};
23
+ __export(useThrottledCallback_exports, {
24
+ useThrottledCallback: () => useThrottledCallback
25
+ });
26
+ module.exports = __toCommonJS(useThrottledCallback_exports);
27
+
28
+ // src/hooks/useThrottledCallback/useThrottledCallback.ts
29
+ var import_react3 = require("react");
30
+
31
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
32
+ var import_react = require("react");
33
+ function usePreservedCallback(callback) {
34
+ const callbackRef = (0, import_react.useRef)(callback);
35
+ (0, import_react.useEffect)(
36
+ function syncCallbackRef() {
37
+ callbackRef.current = callback;
38
+ },
39
+ [callback]
40
+ );
41
+ return (0, import_react.useCallback)((...args) => {
42
+ return callbackRef.current(...args);
43
+ }, []);
44
+ }
45
+
46
+ // src/hooks/usePreservedReference/usePreservedReference.ts
47
+ var import_react2 = require("react");
48
+ function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
49
+ const ref = (0, import_react2.useRef)(value);
50
+ return (0, import_react2.useMemo)(() => {
51
+ if (!areValuesEqual(ref.current, value)) {
52
+ ref.current = value;
53
+ }
54
+ return ref.current;
55
+ }, [areValuesEqual, value]);
56
+ }
57
+ function areDeeplyEqual(x, y) {
58
+ return JSON.stringify(x) === JSON.stringify(y);
59
+ }
60
+
61
+ // src/hooks/useDebounce/debounce.ts
62
+ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
63
+ let pendingThis = void 0;
64
+ let pendingArgs = null;
65
+ const leading = edges != null && edges.includes("leading");
66
+ const trailing = edges == null || edges.includes("trailing");
67
+ const invoke = () => {
68
+ if (pendingArgs !== null) {
69
+ func.apply(pendingThis, pendingArgs);
70
+ pendingThis = void 0;
71
+ pendingArgs = null;
72
+ }
73
+ };
74
+ const onTimerEnd = () => {
75
+ if (trailing) {
76
+ invoke();
77
+ }
78
+ cancel();
79
+ };
80
+ let timeoutId = null;
81
+ const schedule = () => {
82
+ if (timeoutId != null) {
83
+ clearTimeout(timeoutId);
84
+ }
85
+ timeoutId = setTimeout(() => {
86
+ timeoutId = null;
87
+ onTimerEnd();
88
+ }, debounceMs);
89
+ };
90
+ const cancelTimer = () => {
91
+ if (timeoutId !== null) {
92
+ clearTimeout(timeoutId);
93
+ timeoutId = null;
94
+ }
95
+ };
96
+ const cancel = () => {
97
+ cancelTimer();
98
+ pendingThis = void 0;
99
+ pendingArgs = null;
100
+ };
101
+ const debounced = function(...args) {
102
+ pendingThis = this;
103
+ pendingArgs = args;
104
+ const isFirstCall = timeoutId == null;
105
+ schedule();
106
+ if (leading && isFirstCall) {
107
+ invoke();
108
+ }
109
+ };
110
+ debounced.cancel = cancel;
111
+ return debounced;
112
+ }
113
+
114
+ // src/hooks/useThrottle/throttle.ts
115
+ function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
116
+ let pendingAt = null;
117
+ const debounced = debounce(func, throttleMs, { edges });
118
+ const throttled = function(...args) {
119
+ if (pendingAt == null) {
120
+ pendingAt = Date.now();
121
+ } else {
122
+ if (Date.now() - pendingAt >= throttleMs) {
123
+ pendingAt = Date.now();
124
+ debounced.cancel();
125
+ }
126
+ }
127
+ debounced(...args);
128
+ };
129
+ throttled.cancel = debounced.cancel;
130
+ return throttled;
131
+ }
132
+
133
+ // src/hooks/useThrottledCallback/useThrottledCallback.ts
134
+ function useThrottledCallback({
135
+ onChange,
136
+ timeThreshold,
137
+ edges = ["leading", "trailing"]
138
+ }) {
139
+ const handleChange = usePreservedCallback(onChange);
140
+ const ref = (0, import_react3.useRef)({ value: false, clearPreviousThrottle: () => {
141
+ } });
142
+ (0, import_react3.useEffect)(function cleanupThrottleOnUnmount() {
143
+ const current = ref.current;
144
+ return () => {
145
+ current.clearPreviousThrottle();
146
+ };
147
+ }, []);
148
+ const preservedEdges = usePreservedReference(edges);
149
+ return (0, import_react3.useCallback)(
150
+ (nextValue) => {
151
+ if (nextValue === ref.current.value) {
152
+ return;
153
+ }
154
+ const throttled = throttle(
155
+ () => {
156
+ handleChange(nextValue);
157
+ ref.current.value = nextValue;
158
+ },
159
+ timeThreshold,
160
+ { edges: preservedEdges }
161
+ );
162
+ ref.current.clearPreviousThrottle();
163
+ throttled();
164
+ ref.current.clearPreviousThrottle = throttled.cancel;
165
+ },
166
+ [handleChange, timeThreshold, preservedEdges]
167
+ );
168
+ }
169
+ // Annotate the CommonJS export names for ESM import in node:
170
+ 0 && (module.exports = {
171
+ useThrottledCallback
172
+ });
@@ -0,0 +1,30 @@
1
+ type ThrottleOptions = {
2
+ edges?: Array<'leading' | 'trailing'>;
3
+ };
4
+ /**
5
+ * @description
6
+ * `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
7
+ * The throttled callback will only be invoked at most once per specified interval.
8
+ *
9
+ * @param {Object} options - The options object.
10
+ * @param {Function} options.onChange - The callback function to throttle.
11
+ * @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
12
+ * @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.
13
+ *
14
+ * @returns {Function} A throttled function that limits invoking the callback.
15
+ *
16
+ * @example
17
+ * function ScrollTracker() {
18
+ * const throttledScroll = useThrottledCallback({
19
+ * onChange: (scrollY: number) => console.log(scrollY),
20
+ * timeThreshold: 200,
21
+ * });
22
+ * return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
23
+ * }
24
+ */
25
+ declare function useThrottledCallback({ onChange, timeThreshold, edges, }: ThrottleOptions & {
26
+ onChange: (newValue: boolean) => void;
27
+ timeThreshold: number;
28
+ }): (nextValue: boolean) => void;
29
+
30
+ export { useThrottledCallback };
@@ -32,9 +32,12 @@ var import_react2 = require("react");
32
32
  var import_react = require("react");
33
33
  function usePreservedCallback(callback) {
34
34
  const callbackRef = (0, import_react.useRef)(callback);
35
- (0, import_react.useEffect)(() => {
36
- callbackRef.current = callback;
37
- }, [callback]);
35
+ (0, import_react.useEffect)(
36
+ function syncCallbackRef() {
37
+ callbackRef.current = callback;
38
+ },
39
+ [callback]
40
+ );
38
41
  return (0, import_react.useCallback)((...args) => {
39
42
  return callbackRef.current(...args);
40
43
  }, []);
@@ -44,8 +47,8 @@ function usePreservedCallback(callback) {
44
47
  function useTimeout(callback, delay = 0) {
45
48
  const preservedCallback = usePreservedCallback(callback);
46
49
  (0, import_react2.useEffect)(() => {
47
- const timeoutId = window.setTimeout(preservedCallback, delay);
48
- return () => window.clearTimeout(timeoutId);
50
+ const timeoutId = setTimeout(preservedCallback, delay);
51
+ return () => clearTimeout(timeoutId);
49
52
  }, [delay, preservedCallback]);
50
53
  }
51
54
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @description
3
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.
4
+ * It manages `setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
5
5
  *
6
6
  * @param {() => void} callback - The function to be executed after the delay.
7
7
  * @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.