react-simplikit 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -50,87 +50,8 @@ module.exports = __toCommonJS(index_exports);
50
50
  // src/hooks/useImpressionRef/useImpressionRef.ts
51
51
  var import_react6 = require("react");
52
52
 
53
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
54
- var import_react3 = require("react");
55
-
56
- // src/hooks/usePreservedCallback/usePreservedCallback.ts
57
- var import_react = require("react");
58
- function usePreservedCallback(callback) {
59
- const callbackRef = (0, import_react.useRef)(callback);
60
- (0, import_react.useEffect)(() => {
61
- callbackRef.current = callback;
62
- }, [callback]);
63
- return (0, import_react.useCallback)((...args) => {
64
- return callbackRef.current(...args);
65
- }, []);
66
- }
67
-
68
- // src/hooks/useRefEffect/useRefEffect.ts
53
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
69
54
  var import_react2 = require("react");
70
- function useRefEffect(callback, deps) {
71
- const preservedCallback = usePreservedCallback(callback);
72
- const cleanupCallbackRef = (0, import_react2.useRef)(() => {
73
- });
74
- const effect = (0, import_react2.useCallback)(
75
- (element) => {
76
- cleanupCallbackRef.current();
77
- cleanupCallbackRef.current = () => {
78
- };
79
- if (element == null) {
80
- return;
81
- }
82
- const cleanup = preservedCallback(element);
83
- if (typeof cleanup === "function") {
84
- cleanupCallbackRef.current = cleanup;
85
- }
86
- },
87
- // eslint-disable-next-line react-hooks/exhaustive-deps
88
- [preservedCallback, ...deps]
89
- );
90
- return effect;
91
- }
92
-
93
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
94
- function useIntersectionObserver(callback, options) {
95
- const preservedCallback = usePreservedCallback(callback);
96
- const observer = (0, import_react3.useMemo)(() => {
97
- if (typeof IntersectionObserver === "undefined") {
98
- return;
99
- }
100
- return new IntersectionObserver(([entry]) => {
101
- preservedCallback(entry);
102
- }, options);
103
- }, [preservedCallback, options]);
104
- return useRefEffect(
105
- (element) => {
106
- observer?.observe(element);
107
- return () => {
108
- observer?.unobserve(element);
109
- };
110
- },
111
- [preservedCallback, options]
112
- );
113
- }
114
-
115
- // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
116
- var import_react4 = require("react");
117
- function useVisibilityEvent(callback, options = {}) {
118
- const handleVisibilityChange = (0, import_react4.useCallback)(() => {
119
- callback(document.visibilityState);
120
- }, [callback]);
121
- (0, import_react4.useEffect)(() => {
122
- if (options?.immediate ?? false) {
123
- handleVisibilityChange();
124
- }
125
- document.addEventListener("visibilitychange", handleVisibilityChange);
126
- return () => {
127
- document.removeEventListener("visibilitychange", handleVisibilityChange);
128
- };
129
- }, [handleVisibilityChange, options?.immediate]);
130
- }
131
-
132
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
133
- var import_react5 = require("react");
134
55
 
135
56
  // src/hooks/useDebounce/debounce.ts
136
57
  function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
@@ -185,37 +106,132 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
185
106
  return debounced;
186
107
  }
187
108
 
188
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
109
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
110
+ var import_react = require("react");
111
+ function usePreservedCallback(callback) {
112
+ const callbackRef = (0, import_react.useRef)(callback);
113
+ (0, import_react.useEffect)(() => {
114
+ callbackRef.current = callback;
115
+ }, [callback]);
116
+ return (0, import_react.useCallback)((...args) => {
117
+ return callbackRef.current(...args);
118
+ }, []);
119
+ }
120
+
121
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
189
122
  function useDebouncedCallback({
190
123
  onChange,
191
- timeThreshold
124
+ timeThreshold,
125
+ leading = false,
126
+ trailing = true
192
127
  }) {
193
128
  const handleChange = usePreservedCallback(onChange);
194
- const ref = (0, import_react5.useRef)({ value: false, clearPreviousDebounce: () => {
129
+ const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
195
130
  } });
196
- (0, import_react5.useEffect)(() => {
131
+ (0, import_react2.useEffect)(() => {
197
132
  const current = ref.current;
198
133
  return () => {
199
134
  current.clearPreviousDebounce();
200
135
  };
201
136
  }, []);
202
- return (0, import_react5.useCallback)(
137
+ const edges = (0, import_react2.useMemo)(() => {
138
+ const _edges = [];
139
+ if (leading) {
140
+ _edges.push("leading");
141
+ }
142
+ if (trailing) {
143
+ _edges.push("trailing");
144
+ }
145
+ return _edges;
146
+ }, [leading, trailing]);
147
+ return (0, import_react2.useCallback)(
203
148
  (nextValue) => {
204
149
  if (nextValue === ref.current.value) {
205
150
  return;
206
151
  }
207
- const debounced = debounce(() => {
208
- handleChange(nextValue);
209
- ref.current.value = nextValue;
210
- }, timeThreshold);
152
+ const debounced = debounce(
153
+ () => {
154
+ handleChange(nextValue);
155
+ ref.current.value = nextValue;
156
+ },
157
+ timeThreshold,
158
+ { edges }
159
+ );
211
160
  ref.current.clearPreviousDebounce();
212
161
  debounced();
213
162
  ref.current.clearPreviousDebounce = debounced.cancel;
214
163
  },
215
- [handleChange, timeThreshold]
164
+ [handleChange, timeThreshold, edges]
216
165
  );
217
166
  }
218
167
 
168
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
169
+ var import_react4 = require("react");
170
+
171
+ // src/hooks/useRefEffect/useRefEffect.ts
172
+ var import_react3 = require("react");
173
+ function useRefEffect(callback, deps) {
174
+ const preservedCallback = usePreservedCallback(callback);
175
+ const cleanupCallbackRef = (0, import_react3.useRef)(() => {
176
+ });
177
+ const effect = (0, import_react3.useCallback)(
178
+ (element) => {
179
+ cleanupCallbackRef.current();
180
+ cleanupCallbackRef.current = () => {
181
+ };
182
+ if (element == null) {
183
+ return;
184
+ }
185
+ const cleanup = preservedCallback(element);
186
+ if (typeof cleanup === "function") {
187
+ cleanupCallbackRef.current = cleanup;
188
+ }
189
+ },
190
+ // eslint-disable-next-line react-hooks/exhaustive-deps
191
+ [preservedCallback, ...deps]
192
+ );
193
+ return effect;
194
+ }
195
+
196
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
197
+ function useIntersectionObserver(callback, options) {
198
+ const preservedCallback = usePreservedCallback(callback);
199
+ const observer = (0, import_react4.useMemo)(() => {
200
+ if (typeof IntersectionObserver === "undefined") {
201
+ return;
202
+ }
203
+ return new IntersectionObserver(([entry]) => {
204
+ preservedCallback(entry);
205
+ }, options);
206
+ }, [preservedCallback, options]);
207
+ return useRefEffect(
208
+ (element) => {
209
+ observer?.observe(element);
210
+ return () => {
211
+ observer?.unobserve(element);
212
+ };
213
+ },
214
+ [preservedCallback, options]
215
+ );
216
+ }
217
+
218
+ // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
219
+ var import_react5 = require("react");
220
+ function useVisibilityEvent(callback, options = {}) {
221
+ const handleVisibilityChange = (0, import_react5.useCallback)(() => {
222
+ callback(document.visibilityState);
223
+ }, [callback]);
224
+ (0, import_react5.useEffect)(() => {
225
+ if (options?.immediate ?? false) {
226
+ handleVisibilityChange();
227
+ }
228
+ document.addEventListener("visibilitychange", handleVisibilityChange);
229
+ return () => {
230
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
231
+ };
232
+ }, [handleVisibilityChange, options?.immediate]);
233
+ }
234
+
219
235
  // src/hooks/useImpressionRef/useImpressionRef.ts
220
236
  function useImpressionRef({
221
237
  onImpressionStart = () => {
@@ -233,7 +249,8 @@ function useImpressionRef({
233
249
  timeThreshold,
234
250
  onChange: (impressed) => {
235
251
  (impressed ? impressionStartHandler : impressionEndHandler)();
236
- }
252
+ },
253
+ leading: true
237
254
  });
238
255
  useVisibilityEvent((documentVisible) => {
239
256
  if (!isIntersectingRef.current) {
@@ -1,87 +1,8 @@
1
1
  // src/hooks/useImpressionRef/useImpressionRef.ts
2
2
  import { useRef as useRef4 } from "react";
3
3
 
4
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
5
- import { useMemo } from "react";
6
-
7
- // src/hooks/usePreservedCallback/usePreservedCallback.ts
8
- import { useCallback, useEffect, useRef } from "react";
9
- function usePreservedCallback(callback) {
10
- const callbackRef = useRef(callback);
11
- useEffect(() => {
12
- callbackRef.current = callback;
13
- }, [callback]);
14
- return useCallback((...args) => {
15
- return callbackRef.current(...args);
16
- }, []);
17
- }
18
-
19
- // src/hooks/useRefEffect/useRefEffect.ts
20
- import { useCallback as useCallback2, useRef as useRef2 } from "react";
21
- function useRefEffect(callback, deps) {
22
- const preservedCallback = usePreservedCallback(callback);
23
- const cleanupCallbackRef = useRef2(() => {
24
- });
25
- const effect = useCallback2(
26
- (element) => {
27
- cleanupCallbackRef.current();
28
- cleanupCallbackRef.current = () => {
29
- };
30
- if (element == null) {
31
- return;
32
- }
33
- const cleanup = preservedCallback(element);
34
- if (typeof cleanup === "function") {
35
- cleanupCallbackRef.current = cleanup;
36
- }
37
- },
38
- // eslint-disable-next-line react-hooks/exhaustive-deps
39
- [preservedCallback, ...deps]
40
- );
41
- return effect;
42
- }
43
-
44
- // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
45
- function useIntersectionObserver(callback, options) {
46
- const preservedCallback = usePreservedCallback(callback);
47
- const observer = useMemo(() => {
48
- if (typeof IntersectionObserver === "undefined") {
49
- return;
50
- }
51
- return new IntersectionObserver(([entry]) => {
52
- preservedCallback(entry);
53
- }, options);
54
- }, [preservedCallback, options]);
55
- return useRefEffect(
56
- (element) => {
57
- observer?.observe(element);
58
- return () => {
59
- observer?.unobserve(element);
60
- };
61
- },
62
- [preservedCallback, options]
63
- );
64
- }
65
-
66
- // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
67
- import { useCallback as useCallback3, useEffect as useEffect2 } from "react";
68
- function useVisibilityEvent(callback, options = {}) {
69
- const handleVisibilityChange = useCallback3(() => {
70
- callback(document.visibilityState);
71
- }, [callback]);
72
- useEffect2(() => {
73
- if (options?.immediate ?? false) {
74
- handleVisibilityChange();
75
- }
76
- document.addEventListener("visibilitychange", handleVisibilityChange);
77
- return () => {
78
- document.removeEventListener("visibilitychange", handleVisibilityChange);
79
- };
80
- }, [handleVisibilityChange, options?.immediate]);
81
- }
82
-
83
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
84
- import { useCallback as useCallback4, useEffect as useEffect3, useRef as useRef3 } from "react";
4
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
5
+ import { useCallback as useCallback2, useEffect as useEffect2, useMemo, useRef as useRef2 } from "react";
85
6
 
86
7
  // src/hooks/useDebounce/debounce.ts
87
8
  function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
@@ -136,37 +57,132 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
136
57
  return debounced;
137
58
  }
138
59
 
139
- // src/hooks/useImpressionRef/useDebouncedCallback.ts
60
+ // src/hooks/usePreservedCallback/usePreservedCallback.ts
61
+ import { useCallback, useEffect, useRef } from "react";
62
+ function usePreservedCallback(callback) {
63
+ const callbackRef = useRef(callback);
64
+ useEffect(() => {
65
+ callbackRef.current = callback;
66
+ }, [callback]);
67
+ return useCallback((...args) => {
68
+ return callbackRef.current(...args);
69
+ }, []);
70
+ }
71
+
72
+ // src/hooks/useDebouncedCallback/useDebouncedCallback.ts
140
73
  function useDebouncedCallback({
141
74
  onChange,
142
- timeThreshold
75
+ timeThreshold,
76
+ leading = false,
77
+ trailing = true
143
78
  }) {
144
79
  const handleChange = usePreservedCallback(onChange);
145
- const ref = useRef3({ value: false, clearPreviousDebounce: () => {
80
+ const ref = useRef2({ value: false, clearPreviousDebounce: () => {
146
81
  } });
147
- useEffect3(() => {
82
+ useEffect2(() => {
148
83
  const current = ref.current;
149
84
  return () => {
150
85
  current.clearPreviousDebounce();
151
86
  };
152
87
  }, []);
153
- return useCallback4(
88
+ const edges = useMemo(() => {
89
+ const _edges = [];
90
+ if (leading) {
91
+ _edges.push("leading");
92
+ }
93
+ if (trailing) {
94
+ _edges.push("trailing");
95
+ }
96
+ return _edges;
97
+ }, [leading, trailing]);
98
+ return useCallback2(
154
99
  (nextValue) => {
155
100
  if (nextValue === ref.current.value) {
156
101
  return;
157
102
  }
158
- const debounced = debounce(() => {
159
- handleChange(nextValue);
160
- ref.current.value = nextValue;
161
- }, timeThreshold);
103
+ const debounced = debounce(
104
+ () => {
105
+ handleChange(nextValue);
106
+ ref.current.value = nextValue;
107
+ },
108
+ timeThreshold,
109
+ { edges }
110
+ );
162
111
  ref.current.clearPreviousDebounce();
163
112
  debounced();
164
113
  ref.current.clearPreviousDebounce = debounced.cancel;
165
114
  },
166
- [handleChange, timeThreshold]
115
+ [handleChange, timeThreshold, edges]
116
+ );
117
+ }
118
+
119
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
120
+ import { useMemo as useMemo2 } from "react";
121
+
122
+ // src/hooks/useRefEffect/useRefEffect.ts
123
+ import { useCallback as useCallback3, useRef as useRef3 } from "react";
124
+ function useRefEffect(callback, deps) {
125
+ const preservedCallback = usePreservedCallback(callback);
126
+ const cleanupCallbackRef = useRef3(() => {
127
+ });
128
+ const effect = useCallback3(
129
+ (element) => {
130
+ cleanupCallbackRef.current();
131
+ cleanupCallbackRef.current = () => {
132
+ };
133
+ if (element == null) {
134
+ return;
135
+ }
136
+ const cleanup = preservedCallback(element);
137
+ if (typeof cleanup === "function") {
138
+ cleanupCallbackRef.current = cleanup;
139
+ }
140
+ },
141
+ // eslint-disable-next-line react-hooks/exhaustive-deps
142
+ [preservedCallback, ...deps]
143
+ );
144
+ return effect;
145
+ }
146
+
147
+ // src/hooks/useIntersectionObserver/useIntersectionObserver.ts
148
+ function useIntersectionObserver(callback, options) {
149
+ const preservedCallback = usePreservedCallback(callback);
150
+ const observer = useMemo2(() => {
151
+ if (typeof IntersectionObserver === "undefined") {
152
+ return;
153
+ }
154
+ return new IntersectionObserver(([entry]) => {
155
+ preservedCallback(entry);
156
+ }, options);
157
+ }, [preservedCallback, options]);
158
+ return useRefEffect(
159
+ (element) => {
160
+ observer?.observe(element);
161
+ return () => {
162
+ observer?.unobserve(element);
163
+ };
164
+ },
165
+ [preservedCallback, options]
167
166
  );
168
167
  }
169
168
 
169
+ // src/hooks/useVisibilityEvent/useVisibilityEvent.ts
170
+ import { useCallback as useCallback4, useEffect as useEffect3 } from "react";
171
+ function useVisibilityEvent(callback, options = {}) {
172
+ const handleVisibilityChange = useCallback4(() => {
173
+ callback(document.visibilityState);
174
+ }, [callback]);
175
+ useEffect3(() => {
176
+ if (options?.immediate ?? false) {
177
+ handleVisibilityChange();
178
+ }
179
+ document.addEventListener("visibilitychange", handleVisibilityChange);
180
+ return () => {
181
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
182
+ };
183
+ }, [handleVisibilityChange, options?.immediate]);
184
+ }
185
+
170
186
  // src/hooks/useImpressionRef/useImpressionRef.ts
171
187
  function useImpressionRef({
172
188
  onImpressionStart = () => {
@@ -184,7 +200,8 @@ function useImpressionRef({
184
200
  timeThreshold,
185
201
  onChange: (impressed) => {
186
202
  (impressed ? impressionStartHandler : impressionEndHandler)();
187
- }
203
+ },
204
+ leading: true
188
205
  });
189
206
  useVisibilityEvent((documentVisible) => {
190
207
  if (!isIntersectingRef.current) {
@@ -19,7 +19,7 @@ type Props<Case> = {
19
19
  * @param {Record<string | number, () => JSX.Element>} caseBy - An object that maps values to
20
20
  * components to render. The keys represent possible values, and the values are functions returning
21
21
  * the corresponding components.
22
- * @param {() => JSX.Element} defaultComponent - The component to render if `value` does not match
22
+ * @param {() => JSX.Element} [defaultComponent] - The component to render if `value` does not match
23
23
  * any key in `caseBy`.
24
24
  *
25
25
  * @returns {JSX.Element} A React component that conditionally renders based on cases.
@@ -11,10 +11,10 @@ type UseImpressionRefOptions = Partial<{
11
11
  * This hook uses `IntersectionObserver` and the `Visibility API` to track the element's visibility.
12
12
  *
13
13
  * @param {UseImpressionRefOptions} options - Options for tracking the element's visibility.
14
- * @param {() => void} options.onImpressionStart - Callback function executed when the element enters the view
15
- * @param {() => void} options.onImpressionEnd - Callback function executed when the element exits the view
16
- * @param {number} options.timeThreshold - Minimum time the element must be visible (in milliseconds)
17
- * @param {number} options.areaThreshold - Minimum ratio of the element that must be visible (0 to 1)
14
+ * @param {() => void} [options.onImpressionStart] - Callback function executed when the element enters the view
15
+ * @param {() => void} [options.onImpressionEnd] - Callback function executed when the element exits the view
16
+ * @param {number} [options.timeThreshold=0] - Minimum time the element must be visible (in milliseconds)
17
+ * @param {number} [options.areaThreshold=0] - Minimum ratio of the element that must be visible (0 to 1)
18
18
  * @param {string} options.rootMargin - Margin to adjust the detection area
19
19
  *
20
20
  * @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the callbacks will be executed whenever the element's visibility changes.