react-simplikit 0.0.6 → 0.0.7

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.
@@ -27,87 +27,8 @@ module.exports = __toCommonJS(ImpressionArea_exports);
27
27
  // src/hooks/useImpressionRef/useImpressionRef.ts
28
28
  var import_react6 = require("react");
29
29
 
30
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
31
- var import_react3 = require("react");
32
-
33
- // src/hooks/usePreservedCallback/usePreservedCallback.ts
34
- var import_react = require("react");
35
- function usePreservedCallback(callback) {
36
- const callbackRef = (0, import_react.useRef)(callback);
37
- (0, import_react.useEffect)(() => {
38
- callbackRef.current = callback;
39
- }, [callback]);
40
- return (0, import_react.useCallback)((...args) => {
41
- return callbackRef.current(...args);
42
- }, []);
43
- }
44
-
45
- // src/hooks/useRefEffect/useRefEffect.ts
30
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
46
31
  var import_react2 = require("react");
47
- function useRefEffect(callback, deps) {
48
- const preservedCallback = usePreservedCallback(callback);
49
- const cleanupCallbackRef = (0, import_react2.useRef)(() => {
50
- });
51
- const effect = (0, import_react2.useCallback)(
52
- (element) => {
53
- cleanupCallbackRef.current();
54
- cleanupCallbackRef.current = () => {
55
- };
56
- if (element == null) {
57
- return;
58
- }
59
- const cleanup = preservedCallback(element);
60
- if (typeof cleanup === "function") {
61
- cleanupCallbackRef.current = cleanup;
62
- }
63
- },
64
- // eslint-disable-next-line react-hooks/exhaustive-deps
65
- [preservedCallback, ...deps]
66
- );
67
- return effect;
68
- }
69
-
70
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
71
- function useIntersectionObserver(callback, options) {
72
- const preservedCallback = usePreservedCallback(callback);
73
- const observer = (0, import_react3.useMemo)(() => {
74
- if (typeof IntersectionObserver === "undefined") {
75
- return;
76
- }
77
- return new IntersectionObserver(([entry]) => {
78
- preservedCallback(entry);
79
- }, options);
80
- }, [preservedCallback, options]);
81
- return useRefEffect(
82
- (element) => {
83
- observer?.observe(element);
84
- return () => {
85
- observer?.unobserve(element);
86
- };
87
- },
88
- [preservedCallback, options]
89
- );
90
- }
91
-
92
- // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
93
- var import_react4 = require("react");
94
- function useVisibilityEvent(callback, options = {}) {
95
- const handleVisibilityChange = (0, import_react4.useCallback)(() => {
96
- callback(document.visibilityState);
97
- }, [callback]);
98
- (0, import_react4.useEffect)(() => {
99
- if (options?.immediate ?? false) {
100
- handleVisibilityChange();
101
- }
102
- document.addEventListener("visibilitychange", handleVisibilityChange);
103
- return () => {
104
- document.removeEventListener("visibilitychange", handleVisibilityChange);
105
- };
106
- }, [handleVisibilityChange, options?.immediate]);
107
- }
108
-
109
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
110
- var import_react5 = require("react");
111
32
 
112
33
  // src/hooks/useDebounce/debounce.ts
113
34
  function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
@@ -162,37 +83,132 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
162
83
  return debounced;
163
84
  }
164
85
 
165
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
86
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
87
+ var import_react = require("react");
88
+ function usePreservedCallback(callback) {
89
+ const callbackRef = (0, import_react.useRef)(callback);
90
+ (0, import_react.useEffect)(() => {
91
+ callbackRef.current = callback;
92
+ }, [callback]);
93
+ return (0, import_react.useCallback)((...args) => {
94
+ return callbackRef.current(...args);
95
+ }, []);
96
+ }
97
+
98
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
166
99
  function useDebouncedCallback({
167
100
  onChange,
168
- timeThreshold
101
+ timeThreshold,
102
+ leading = false,
103
+ trailing = true
169
104
  }) {
170
105
  const handleChange = usePreservedCallback(onChange);
171
- const ref = (0, import_react5.useRef)({ value: false, clearPreviousDebounce: () => {
106
+ const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
172
107
  } });
173
- (0, import_react5.useEffect)(() => {
108
+ (0, import_react2.useEffect)(() => {
174
109
  const current = ref.current;
175
110
  return () => {
176
111
  current.clearPreviousDebounce();
177
112
  };
178
113
  }, []);
179
- return (0, import_react5.useCallback)(
114
+ const edges = (0, import_react2.useMemo)(() => {
115
+ const _edges = [];
116
+ if (leading) {
117
+ _edges.push("leading");
118
+ }
119
+ if (trailing) {
120
+ _edges.push("trailing");
121
+ }
122
+ return _edges;
123
+ }, [leading, trailing]);
124
+ return (0, import_react2.useCallback)(
180
125
  (nextValue) => {
181
126
  if (nextValue === ref.current.value) {
182
127
  return;
183
128
  }
184
- const debounced = debounce(() => {
185
- handleChange(nextValue);
186
- ref.current.value = nextValue;
187
- }, timeThreshold);
129
+ const debounced = debounce(
130
+ () => {
131
+ handleChange(nextValue);
132
+ ref.current.value = nextValue;
133
+ },
134
+ timeThreshold,
135
+ { edges }
136
+ );
188
137
  ref.current.clearPreviousDebounce();
189
138
  debounced();
190
139
  ref.current.clearPreviousDebounce = debounced.cancel;
191
140
  },
192
- [handleChange, timeThreshold]
141
+ [handleChange, timeThreshold, edges]
142
+ );
143
+ }
144
+
145
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
146
+ var import_react4 = require("react");
147
+
148
+ // src/hooks/useRefEffect/useRefEffect.ts
149
+ var import_react3 = require("react");
150
+ function useRefEffect(callback, deps) {
151
+ const preservedCallback = usePreservedCallback(callback);
152
+ const cleanupCallbackRef = (0, import_react3.useRef)(() => {
153
+ });
154
+ const effect = (0, import_react3.useCallback)(
155
+ (element) => {
156
+ cleanupCallbackRef.current();
157
+ cleanupCallbackRef.current = () => {
158
+ };
159
+ if (element == null) {
160
+ return;
161
+ }
162
+ const cleanup = preservedCallback(element);
163
+ if (typeof cleanup === "function") {
164
+ cleanupCallbackRef.current = cleanup;
165
+ }
166
+ },
167
+ // eslint-disable-next-line react-hooks/exhaustive-deps
168
+ [preservedCallback, ...deps]
169
+ );
170
+ return effect;
171
+ }
172
+
173
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
174
+ function useIntersectionObserver(callback, options) {
175
+ const preservedCallback = usePreservedCallback(callback);
176
+ const observer = (0, import_react4.useMemo)(() => {
177
+ if (typeof IntersectionObserver === "undefined") {
178
+ return;
179
+ }
180
+ return new IntersectionObserver(([entry]) => {
181
+ preservedCallback(entry);
182
+ }, options);
183
+ }, [preservedCallback, options]);
184
+ return useRefEffect(
185
+ (element) => {
186
+ observer?.observe(element);
187
+ return () => {
188
+ observer?.unobserve(element);
189
+ };
190
+ },
191
+ [preservedCallback, options]
193
192
  );
194
193
  }
195
194
 
195
+ // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
196
+ var import_react5 = require("react");
197
+ function useVisibilityEvent(callback, options = {}) {
198
+ const handleVisibilityChange = (0, import_react5.useCallback)(() => {
199
+ callback(document.visibilityState);
200
+ }, [callback]);
201
+ (0, import_react5.useEffect)(() => {
202
+ if (options?.immediate ?? false) {
203
+ handleVisibilityChange();
204
+ }
205
+ document.addEventListener("visibilitychange", handleVisibilityChange);
206
+ return () => {
207
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
208
+ };
209
+ }, [handleVisibilityChange, options?.immediate]);
210
+ }
211
+
196
212
  // src/hooks/useImpressionRef/useImpressionRef.ts
197
213
  function useImpressionRef({
198
214
  onImpressionStart = () => {
@@ -210,7 +226,8 @@ function useImpressionRef({
210
226
  timeThreshold,
211
227
  onChange: (impressed) => {
212
228
  (impressed ? impressionStartHandler : impressionEndHandler)();
213
- }
229
+ },
230
+ leading: true
214
231
  });
215
232
  useVisibilityEvent((documentVisible) => {
216
233
  if (!isIntersectingRef.current) {
@@ -5,7 +5,7 @@ import { DependencyList } from 'react';
5
5
  * `useAsyncEffect` is a custom hook for handling asynchronous side effects in React components.
6
6
  * It follows the same cleanup pattern as `useEffect` while ensuring async operations are handled safely.
7
7
  *
8
- * @param {() => Promise<void | (() => void)>} effect - An asynchronous function executed in the `useEffect` pattern.
8
+ * @param {() => Promise<void | (() => void)>} [effect] - An asynchronous function executed in the `useEffect` pattern.
9
9
  * This function can optionally return a cleanup function.
10
10
  * @param {DependencyList} [deps] - A dependency array.
11
11
  * The effect will re-run whenever any value in this array changes. If omitted, it runs only once when the component mounts.
@@ -27,87 +27,8 @@ module.exports = __toCommonJS(useImpressionRef_exports);
27
27
  // src/hooks/useImpressionRef/useImpressionRef.ts
28
28
  var import_react6 = require("react");
29
29
 
30
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
31
- var import_react3 = require("react");
32
-
33
- // src/hooks/usePreservedCallback/usePreservedCallback.ts
34
- var import_react = require("react");
35
- function usePreservedCallback(callback) {
36
- const callbackRef = (0, import_react.useRef)(callback);
37
- (0, import_react.useEffect)(() => {
38
- callbackRef.current = callback;
39
- }, [callback]);
40
- return (0, import_react.useCallback)((...args) => {
41
- return callbackRef.current(...args);
42
- }, []);
43
- }
44
-
45
- // src/hooks/useRefEffect/useRefEffect.ts
30
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
46
31
  var import_react2 = require("react");
47
- function useRefEffect(callback, deps) {
48
- const preservedCallback = usePreservedCallback(callback);
49
- const cleanupCallbackRef = (0, import_react2.useRef)(() => {
50
- });
51
- const effect = (0, import_react2.useCallback)(
52
- (element) => {
53
- cleanupCallbackRef.current();
54
- cleanupCallbackRef.current = () => {
55
- };
56
- if (element == null) {
57
- return;
58
- }
59
- const cleanup = preservedCallback(element);
60
- if (typeof cleanup === "function") {
61
- cleanupCallbackRef.current = cleanup;
62
- }
63
- },
64
- // eslint-disable-next-line react-hooks/exhaustive-deps
65
- [preservedCallback, ...deps]
66
- );
67
- return effect;
68
- }
69
-
70
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
71
- function useIntersectionObserver(callback, options) {
72
- const preservedCallback = usePreservedCallback(callback);
73
- const observer = (0, import_react3.useMemo)(() => {
74
- if (typeof IntersectionObserver === "undefined") {
75
- return;
76
- }
77
- return new IntersectionObserver(([entry]) => {
78
- preservedCallback(entry);
79
- }, options);
80
- }, [preservedCallback, options]);
81
- return useRefEffect(
82
- (element) => {
83
- observer?.observe(element);
84
- return () => {
85
- observer?.unobserve(element);
86
- };
87
- },
88
- [preservedCallback, options]
89
- );
90
- }
91
-
92
- // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
93
- var import_react4 = require("react");
94
- function useVisibilityEvent(callback, options = {}) {
95
- const handleVisibilityChange = (0, import_react4.useCallback)(() => {
96
- callback(document.visibilityState);
97
- }, [callback]);
98
- (0, import_react4.useEffect)(() => {
99
- if (options?.immediate ?? false) {
100
- handleVisibilityChange();
101
- }
102
- document.addEventListener("visibilitychange", handleVisibilityChange);
103
- return () => {
104
- document.removeEventListener("visibilitychange", handleVisibilityChange);
105
- };
106
- }, [handleVisibilityChange, options?.immediate]);
107
- }
108
-
109
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
110
- var import_react5 = require("react");
111
32
 
112
33
  // src/hooks/useDebounce/debounce.ts
113
34
  function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
@@ -162,37 +83,132 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
162
83
  return debounced;
163
84
  }
164
85
 
165
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
86
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
87
+ var import_react = require("react");
88
+ function usePreservedCallback(callback) {
89
+ const callbackRef = (0, import_react.useRef)(callback);
90
+ (0, import_react.useEffect)(() => {
91
+ callbackRef.current = callback;
92
+ }, [callback]);
93
+ return (0, import_react.useCallback)((...args) => {
94
+ return callbackRef.current(...args);
95
+ }, []);
96
+ }
97
+
98
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
166
99
  function useDebouncedCallback({
167
100
  onChange,
168
- timeThreshold
101
+ timeThreshold,
102
+ leading = false,
103
+ trailing = true
169
104
  }) {
170
105
  const handleChange = usePreservedCallback(onChange);
171
- const ref = (0, import_react5.useRef)({ value: false, clearPreviousDebounce: () => {
106
+ const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
172
107
  } });
173
- (0, import_react5.useEffect)(() => {
108
+ (0, import_react2.useEffect)(() => {
174
109
  const current = ref.current;
175
110
  return () => {
176
111
  current.clearPreviousDebounce();
177
112
  };
178
113
  }, []);
179
- return (0, import_react5.useCallback)(
114
+ const edges = (0, import_react2.useMemo)(() => {
115
+ const _edges = [];
116
+ if (leading) {
117
+ _edges.push("leading");
118
+ }
119
+ if (trailing) {
120
+ _edges.push("trailing");
121
+ }
122
+ return _edges;
123
+ }, [leading, trailing]);
124
+ return (0, import_react2.useCallback)(
180
125
  (nextValue) => {
181
126
  if (nextValue === ref.current.value) {
182
127
  return;
183
128
  }
184
- const debounced = debounce(() => {
185
- handleChange(nextValue);
186
- ref.current.value = nextValue;
187
- }, timeThreshold);
129
+ const debounced = debounce(
130
+ () => {
131
+ handleChange(nextValue);
132
+ ref.current.value = nextValue;
133
+ },
134
+ timeThreshold,
135
+ { edges }
136
+ );
188
137
  ref.current.clearPreviousDebounce();
189
138
  debounced();
190
139
  ref.current.clearPreviousDebounce = debounced.cancel;
191
140
  },
192
- [handleChange, timeThreshold]
141
+ [handleChange, timeThreshold, edges]
193
142
  );
194
143
  }
195
144
 
145
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
146
+ var import_react4 = require("react");
147
+
148
+ // src/hooks/useRefEffect/useRefEffect.ts
149
+ var import_react3 = require("react");
150
+ function useRefEffect(callback, deps) {
151
+ const preservedCallback = usePreservedCallback(callback);
152
+ const cleanupCallbackRef = (0, import_react3.useRef)(() => {
153
+ });
154
+ const effect = (0, import_react3.useCallback)(
155
+ (element) => {
156
+ cleanupCallbackRef.current();
157
+ cleanupCallbackRef.current = () => {
158
+ };
159
+ if (element == null) {
160
+ return;
161
+ }
162
+ const cleanup = preservedCallback(element);
163
+ if (typeof cleanup === "function") {
164
+ cleanupCallbackRef.current = cleanup;
165
+ }
166
+ },
167
+ // eslint-disable-next-line react-hooks/exhaustive-deps
168
+ [preservedCallback, ...deps]
169
+ );
170
+ return effect;
171
+ }
172
+
173
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
174
+ function useIntersectionObserver(callback, options) {
175
+ const preservedCallback = usePreservedCallback(callback);
176
+ const observer = (0, import_react4.useMemo)(() => {
177
+ if (typeof IntersectionObserver === "undefined") {
178
+ return;
179
+ }
180
+ return new IntersectionObserver(([entry]) => {
181
+ preservedCallback(entry);
182
+ }, options);
183
+ }, [preservedCallback, options]);
184
+ return useRefEffect(
185
+ (element) => {
186
+ observer?.observe(element);
187
+ return () => {
188
+ observer?.unobserve(element);
189
+ };
190
+ },
191
+ [preservedCallback, options]
192
+ );
193
+ }
194
+
195
+ // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
196
+ var import_react5 = require("react");
197
+ function useVisibilityEvent(callback, options = {}) {
198
+ const handleVisibilityChange = (0, import_react5.useCallback)(() => {
199
+ callback(document.visibilityState);
200
+ }, [callback]);
201
+ (0, import_react5.useEffect)(() => {
202
+ if (options?.immediate ?? false) {
203
+ handleVisibilityChange();
204
+ }
205
+ document.addEventListener("visibilitychange", handleVisibilityChange);
206
+ return () => {
207
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
208
+ };
209
+ }, [handleVisibilityChange, options?.immediate]);
210
+ }
211
+
196
212
  // src/hooks/useImpressionRef/useImpressionRef.ts
197
213
  function useImpressionRef({
198
214
  onImpressionStart = () => {
@@ -210,7 +226,8 @@ function useImpressionRef({
210
226
  timeThreshold,
211
227
  onChange: (impressed) => {
212
228
  (impressed ? impressionStartHandler : impressionEndHandler)();
213
- }
229
+ },
230
+ leading: true
214
231
  });
215
232
  useVisibilityEvent((documentVisible) => {
216
233
  if (!isIntersectingRef.current) {
@@ -23,7 +23,7 @@ type StorageStateOptionsWithDefaultValue<T> = StorageStateOptions<T> & {
23
23
  * The value is retained across page reloads and can be shared between tabs when using `localStorage`.
24
24
  *
25
25
  * @param {string} key - The key used to store the value in storage.
26
- * @param {object} [options] - Configuration options for storage behavior.
26
+ * @param {Object} [options] - Configuration options for storage behavior.
27
27
  * @param {Storage} [options.storage=localStorage] - The storage type (`localStorage` or `sessionStorage`). Defaults to `localStorage`.
28
28
  * @param {T} [options.defaultValue] - The initial value if no existing value is found.
29
29
  *