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/components/ImpressionArea/index.js +108 -91
- package/dist/components/SwitchCase/index.d.ts +1 -1
- package/dist/hooks/useImpressionRef/index.d.ts +4 -4
- package/dist/hooks/useImpressionRef/index.js +108 -91
- package/dist/hooks/useIntersectionObserver/index.d.ts +3 -1
- package/dist/hooks/useStorageState/index.d.ts +1 -1
- package/dist/hooks/useThrottle/index.d.ts +1 -0
- package/dist/index.js +108 -91
- package/esm/components/ImpressionArea/index.mjs +109 -92
- package/esm/components/SwitchCase/index.d.mts +1 -1
- package/esm/hooks/useImpressionRef/index.d.mts +4 -4
- package/esm/hooks/useImpressionRef/index.mjs +109 -92
- package/esm/hooks/useIntersectionObserver/index.d.mts +3 -1
- package/esm/hooks/useStorageState/index.d.mts +1 -1
- package/esm/hooks/useThrottle/index.d.mts +1 -0
- package/esm/index.mjs +120 -103
- package/package.json +8 -3
|
@@ -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/
|
|
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/
|
|
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,
|
|
106
|
+
const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
172
107
|
} });
|
|
173
|
-
(0,
|
|
108
|
+
(0, import_react2.useEffect)(() => {
|
|
174
109
|
const current = ref.current;
|
|
175
110
|
return () => {
|
|
176
111
|
current.clearPreviousDebounce();
|
|
177
112
|
};
|
|
178
113
|
}, []);
|
|
179
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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) {
|
|
@@ -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.
|
|
@@ -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/
|
|
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/
|
|
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,
|
|
106
|
+
const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
172
107
|
} });
|
|
173
|
-
(0,
|
|
108
|
+
(0, import_react2.useEffect)(() => {
|
|
174
109
|
const current = ref.current;
|
|
175
110
|
return () => {
|
|
176
111
|
current.clearPreviousDebounce();
|
|
177
112
|
};
|
|
178
113
|
}, []);
|
|
179
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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) {
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* @param {(entry: IntersectionObserverEntry) => void} callback - A callback function that is executed when the visibility of the element changes.
|
|
7
7
|
* You can check `entry.isIntersecting` to determine if the element is in view.
|
|
8
8
|
* @param {IntersectionObserverInit} options - Options for the `IntersectionObserver`.
|
|
9
|
-
*
|
|
9
|
+
* @param {boolean} [options.root] - The element that is used as the viewport for checking visibility of the target.
|
|
10
|
+
* @param {string} [options.rootMargin] - Margin around the root.
|
|
11
|
+
* @param {number | number[]} [options.threshold] - Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed.
|
|
10
12
|
*
|
|
11
13
|
* @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the `callback` will be executed whenever the element's visibility changes.
|
|
12
14
|
*
|
|
@@ -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 {
|
|
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
|
*
|
|
@@ -24,6 +24,7 @@ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleM
|
|
|
24
24
|
* @param {F} callback - The function to be throttled.
|
|
25
25
|
* @param {number} wait - The number of milliseconds to throttle invocations to.
|
|
26
26
|
* @param {{ edges?: Array<'leading' | 'trailing'> }} [options] - Options to control the behavior of the throttle.
|
|
27
|
+
* @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.
|
|
27
28
|
* @returns {F & { cancel: () => void }} - Returns the throttled function with a `cancel` method to cancel pending executions.
|
|
28
29
|
*
|
|
29
30
|
* @example
|