react-hook-toolkit 3.0.2 → 3.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.
- package/dist/chunk1213/chunk158261.js +22 -22
- package/dist/chunk1415/chunk143.js +21 -25
- package/dist/chunk1516/chunk0021.d.ts +23 -3
- package/dist/chunk1516/chunk0021.js +386 -352
- package/dist/chunk1516/chunk0022.js +159 -277
- package/dist/chunk1516/chunk3312.d.ts +63 -0
- package/dist/chunk1516/chunk3312.js +462 -0
- package/dist/chunk1516/chunk726433.js +172 -206
- package/dist/chunk1516/chunk940514.js +284 -415
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/utils.js +42 -68
- package/package.json +1 -1
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
-
if (ar || !(i in from)) {
|
|
15
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
-
ar[i] = from[i];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
-
};
|
|
21
1
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState } from "react";
|
|
22
2
|
import { isBrowser, noop, off, on, throttle } from '../utils';
|
|
23
3
|
import { VoiceStatus } from "../type";
|
|
@@ -26,18 +6,18 @@ function historyReducer(state, action) {
|
|
|
26
6
|
case 'UNDO':
|
|
27
7
|
if (state.past.length === 0)
|
|
28
8
|
return state;
|
|
29
|
-
|
|
9
|
+
const [newPresent, ...newPast] = state.past;
|
|
30
10
|
return {
|
|
31
11
|
past: newPast,
|
|
32
12
|
present: newPresent,
|
|
33
|
-
future:
|
|
13
|
+
future: [state.present, ...state.future],
|
|
34
14
|
};
|
|
35
15
|
case 'REDO':
|
|
36
16
|
if (state.future.length === 0)
|
|
37
17
|
return state;
|
|
38
|
-
|
|
18
|
+
const [nextPresent, ...newFuture] = state.future;
|
|
39
19
|
return {
|
|
40
|
-
past:
|
|
20
|
+
past: [state.present, ...state.past],
|
|
41
21
|
present: nextPresent,
|
|
42
22
|
future: newFuture,
|
|
43
23
|
};
|
|
@@ -45,7 +25,7 @@ function historyReducer(state, action) {
|
|
|
45
25
|
if (action.newPresent === state.present)
|
|
46
26
|
return state;
|
|
47
27
|
return {
|
|
48
|
-
past:
|
|
28
|
+
past: [state.present, ...state.past],
|
|
49
29
|
present: action.newPresent,
|
|
50
30
|
future: [],
|
|
51
31
|
};
|
|
@@ -66,61 +46,60 @@ function historyReducer(state, action) {
|
|
|
66
46
|
}
|
|
67
47
|
}
|
|
68
48
|
export function useHistoryState(initialPresent) {
|
|
69
|
-
|
|
49
|
+
const [state, dispatch] = useReducer((historyReducer), {
|
|
70
50
|
past: [],
|
|
71
51
|
present: initialPresent,
|
|
72
52
|
future: [],
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
53
|
+
});
|
|
54
|
+
const canUndo = state.past.length > 0;
|
|
55
|
+
const canRedo = state.future.length > 0;
|
|
56
|
+
const undo = useCallback(() => {
|
|
77
57
|
if (canUndo) {
|
|
78
58
|
dispatch({ type: 'UNDO' });
|
|
79
59
|
}
|
|
80
60
|
}, [canUndo]);
|
|
81
|
-
|
|
61
|
+
const redo = useCallback(() => {
|
|
82
62
|
if (canRedo) {
|
|
83
63
|
dispatch({ type: 'REDO' });
|
|
84
64
|
}
|
|
85
65
|
}, [canRedo]);
|
|
86
|
-
|
|
87
|
-
dispatch({ type: 'SET', newPresent
|
|
66
|
+
const set = useCallback((newPresent) => {
|
|
67
|
+
dispatch({ type: 'SET', newPresent });
|
|
88
68
|
}, []);
|
|
89
|
-
|
|
90
|
-
dispatch({ type: 'RESET', newPresent
|
|
69
|
+
const reset = useCallback((newPresent) => {
|
|
70
|
+
dispatch({ type: 'RESET', newPresent });
|
|
91
71
|
}, []);
|
|
92
|
-
|
|
72
|
+
const clear = useCallback(() => {
|
|
93
73
|
dispatch({ type: 'CLEAR' });
|
|
94
74
|
}, []);
|
|
95
75
|
return {
|
|
96
76
|
state: state.present,
|
|
97
|
-
set
|
|
98
|
-
undo
|
|
99
|
-
redo
|
|
100
|
-
reset
|
|
101
|
-
clear
|
|
102
|
-
canUndo
|
|
103
|
-
canRedo
|
|
77
|
+
set,
|
|
78
|
+
undo,
|
|
79
|
+
redo,
|
|
80
|
+
reset,
|
|
81
|
+
clear,
|
|
82
|
+
canUndo,
|
|
83
|
+
canRedo,
|
|
104
84
|
past: state.past,
|
|
105
85
|
future: state.future,
|
|
106
86
|
};
|
|
107
87
|
}
|
|
108
|
-
export function useIdle(ms) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
var handleTimeout = function () {
|
|
88
|
+
export function useIdle(ms = 1000 * 60) {
|
|
89
|
+
const [idle, setIdle] = useState(false);
|
|
90
|
+
const timeoutIdRef = useRef(null);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
const handleTimeout = () => {
|
|
114
93
|
setIdle(true);
|
|
115
94
|
};
|
|
116
|
-
|
|
95
|
+
const handleEvent = throttle(() => {
|
|
117
96
|
setIdle(false);
|
|
118
97
|
if (timeoutIdRef.current) {
|
|
119
98
|
clearTimeout(timeoutIdRef.current);
|
|
120
99
|
}
|
|
121
100
|
timeoutIdRef.current = setTimeout(handleTimeout, ms);
|
|
122
101
|
}, 500);
|
|
123
|
-
|
|
102
|
+
const handleVisibilityChange = () => {
|
|
124
103
|
if (!document.hidden) {
|
|
125
104
|
handleEvent();
|
|
126
105
|
}
|
|
@@ -128,7 +107,7 @@ export function useIdle(ms) {
|
|
|
128
107
|
// Initialize
|
|
129
108
|
timeoutIdRef.current = setTimeout(handleTimeout, ms);
|
|
130
109
|
// Events to listen for activity
|
|
131
|
-
|
|
110
|
+
const events = [
|
|
132
111
|
'mousemove',
|
|
133
112
|
'mousedown',
|
|
134
113
|
'resize',
|
|
@@ -137,10 +116,10 @@ export function useIdle(ms) {
|
|
|
137
116
|
'wheel',
|
|
138
117
|
'scroll',
|
|
139
118
|
];
|
|
140
|
-
events.forEach(
|
|
119
|
+
events.forEach((event) => window.addEventListener(event, handleEvent));
|
|
141
120
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
142
|
-
return
|
|
143
|
-
events.forEach(
|
|
121
|
+
return () => {
|
|
122
|
+
events.forEach((event) => window.removeEventListener(event, handleEvent));
|
|
144
123
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
145
124
|
if (timeoutIdRef.current) {
|
|
146
125
|
clearTimeout(timeoutIdRef.current);
|
|
@@ -150,66 +129,64 @@ export function useIdle(ms) {
|
|
|
150
129
|
return idle;
|
|
151
130
|
}
|
|
152
131
|
export function useIsFirstRender() {
|
|
153
|
-
|
|
154
|
-
useEffect(
|
|
132
|
+
const isFirstRender = useRef(true);
|
|
133
|
+
useEffect(() => {
|
|
155
134
|
isFirstRender.current = false;
|
|
156
135
|
}, []);
|
|
157
136
|
return isFirstRender.current;
|
|
158
137
|
}
|
|
159
|
-
export function useList(initialList) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
var set = useCallback(function (newList) {
|
|
138
|
+
export function useList(initialList = []) {
|
|
139
|
+
const [list, setList] = useState(initialList);
|
|
140
|
+
const set = useCallback((newList) => {
|
|
163
141
|
setList(newList);
|
|
164
142
|
}, []);
|
|
165
|
-
|
|
166
|
-
setList(
|
|
143
|
+
const push = useCallback((element) => {
|
|
144
|
+
setList(current => [...current, element]);
|
|
167
145
|
}, []);
|
|
168
|
-
|
|
169
|
-
setList(
|
|
146
|
+
const removeAt = useCallback((index) => {
|
|
147
|
+
setList(current => current.filter((_, i) => i !== index));
|
|
170
148
|
}, []);
|
|
171
|
-
|
|
172
|
-
setList(
|
|
173
|
-
|
|
174
|
-
|
|
149
|
+
const insertAt = useCallback((index, element) => {
|
|
150
|
+
setList(current => [
|
|
151
|
+
...current.slice(0, index),
|
|
152
|
+
element,
|
|
153
|
+
...current.slice(index)
|
|
154
|
+
]);
|
|
175
155
|
}, []);
|
|
176
|
-
|
|
177
|
-
setList(
|
|
178
|
-
return current.map(function (item, i) { return (i === index ? element : item); });
|
|
179
|
-
});
|
|
156
|
+
const updateAt = useCallback((index, element) => {
|
|
157
|
+
setList(current => current.map((item, i) => (i === index ? element : item)));
|
|
180
158
|
}, []);
|
|
181
|
-
|
|
159
|
+
const clear = useCallback(() => {
|
|
182
160
|
setList([]);
|
|
183
161
|
}, []);
|
|
184
162
|
return {
|
|
185
|
-
list
|
|
163
|
+
list,
|
|
186
164
|
actions: {
|
|
187
|
-
set
|
|
188
|
-
push
|
|
189
|
-
removeAt
|
|
190
|
-
insertAt
|
|
191
|
-
updateAt
|
|
192
|
-
clear
|
|
165
|
+
set,
|
|
166
|
+
push,
|
|
167
|
+
removeAt,
|
|
168
|
+
insertAt,
|
|
169
|
+
updateAt,
|
|
170
|
+
clear,
|
|
193
171
|
},
|
|
194
172
|
};
|
|
195
173
|
}
|
|
196
|
-
export function useLockBodyScroll(lock) {
|
|
197
|
-
|
|
198
|
-
useLayoutEffect(function () {
|
|
174
|
+
export function useLockBodyScroll(lock = true) {
|
|
175
|
+
useLayoutEffect(() => {
|
|
199
176
|
if (!lock)
|
|
200
177
|
return;
|
|
201
178
|
// Get current body overflow value
|
|
202
|
-
|
|
203
|
-
|
|
179
|
+
const originalOverflow = document.body.style.overflow;
|
|
180
|
+
const originalPaddingRight = document.body.style.paddingRight;
|
|
204
181
|
// Calculate scrollbar width to prevent layout shift
|
|
205
|
-
|
|
182
|
+
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
206
183
|
// Lock body scroll
|
|
207
184
|
document.body.style.overflow = 'hidden';
|
|
208
185
|
// Compensate for scrollbar width if needed
|
|
209
186
|
if (scrollbarWidth > 0) {
|
|
210
|
-
document.body.style.paddingRight =
|
|
187
|
+
document.body.style.paddingRight = `${scrollbarWidth}px`;
|
|
211
188
|
}
|
|
212
|
-
return
|
|
189
|
+
return () => {
|
|
213
190
|
// Restore original styles
|
|
214
191
|
document.body.style.overflow = originalOverflow;
|
|
215
192
|
document.body.style.paddingRight = originalPaddingRight;
|
|
@@ -222,27 +199,26 @@ function isMouseEvent(event) {
|
|
|
222
199
|
function isTouchEvent(event) {
|
|
223
200
|
return 'touches' in event;
|
|
224
201
|
}
|
|
225
|
-
export function useLongPress(callback, options) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
var start = useCallback(function (event) {
|
|
202
|
+
export function useLongPress(callback, options = {}) {
|
|
203
|
+
const { threshold = 400, onStart, onFinish, onCancel } = options;
|
|
204
|
+
const isLongPressActive = useRef(false);
|
|
205
|
+
const isPressed = useRef(false);
|
|
206
|
+
const timerId = useRef();
|
|
207
|
+
const start = useCallback((event) => {
|
|
232
208
|
if (!isMouseEvent(event) && !isTouchEvent(event))
|
|
233
209
|
return;
|
|
234
210
|
if (onStart) {
|
|
235
211
|
onStart(event);
|
|
236
212
|
}
|
|
237
213
|
isPressed.current = true;
|
|
238
|
-
timerId.current = setTimeout(
|
|
214
|
+
timerId.current = setTimeout(() => {
|
|
239
215
|
if (isPressed.current) {
|
|
240
216
|
callback(event);
|
|
241
217
|
isLongPressActive.current = true;
|
|
242
218
|
}
|
|
243
219
|
}, threshold);
|
|
244
220
|
}, [callback, threshold, onStart]);
|
|
245
|
-
|
|
221
|
+
const cancel = useCallback((event) => {
|
|
246
222
|
if (!isMouseEvent(event) && !isTouchEvent(event))
|
|
247
223
|
return;
|
|
248
224
|
if (isLongPressActive.current) {
|
|
@@ -262,30 +238,29 @@ export function useLongPress(callback, options) {
|
|
|
262
238
|
}
|
|
263
239
|
}, [onFinish, onCancel]);
|
|
264
240
|
// Clean up on unmount
|
|
265
|
-
useEffect(
|
|
266
|
-
return
|
|
241
|
+
useEffect(() => {
|
|
242
|
+
return () => {
|
|
267
243
|
if (timerId.current) {
|
|
268
244
|
clearTimeout(timerId.current);
|
|
269
245
|
}
|
|
270
246
|
};
|
|
271
247
|
}, []);
|
|
272
|
-
return useMemo(
|
|
248
|
+
return useMemo(() => ({
|
|
273
249
|
onMouseDown: start,
|
|
274
250
|
onMouseUp: cancel,
|
|
275
251
|
onMouseLeave: cancel,
|
|
276
252
|
onTouchStart: start,
|
|
277
253
|
onTouchEnd: cancel,
|
|
278
|
-
})
|
|
254
|
+
}), [start, cancel]);
|
|
279
255
|
}
|
|
280
|
-
export
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
var saved = localStorage.getItem(key);
|
|
256
|
+
export const useRecentSearch = (options = {}) => {
|
|
257
|
+
const { key = 'recentSearches', limit = 3, uniqueKey = 'index', excludeEmpty = true } = options;
|
|
258
|
+
const [recentSearches, setRecentSearches] = useState([]);
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
const saved = localStorage.getItem(key);
|
|
286
261
|
if (saved) {
|
|
287
262
|
try {
|
|
288
|
-
|
|
263
|
+
const parsed = JSON.parse(saved);
|
|
289
264
|
if (Array.isArray(parsed)) {
|
|
290
265
|
setRecentSearches(parsed.slice(0, limit));
|
|
291
266
|
}
|
|
@@ -296,12 +271,12 @@ export var useRecentSearch = function (options) {
|
|
|
296
271
|
}
|
|
297
272
|
}
|
|
298
273
|
}, [key, limit]);
|
|
299
|
-
|
|
274
|
+
const addSearch = useCallback((item) => {
|
|
300
275
|
if (excludeEmpty && !item[uniqueKey])
|
|
301
276
|
return;
|
|
302
277
|
try {
|
|
303
|
-
setRecentSearches(
|
|
304
|
-
|
|
278
|
+
setRecentSearches((prev) => {
|
|
279
|
+
const updated = [item, ...prev.filter((p) => p[uniqueKey] !== item[uniqueKey])].slice(0, limit);
|
|
305
280
|
localStorage.setItem(key, JSON.stringify(updated));
|
|
306
281
|
return updated;
|
|
307
282
|
});
|
|
@@ -310,51 +285,49 @@ export var useRecentSearch = function (options) {
|
|
|
310
285
|
console.error('Failed to update recent searches:', error);
|
|
311
286
|
}
|
|
312
287
|
}, [excludeEmpty, uniqueKey, key, limit]);
|
|
313
|
-
|
|
314
|
-
return recentSearches.some(
|
|
288
|
+
const hasSearch = (value) => {
|
|
289
|
+
return recentSearches.some((item) => { var _a; return ((_a = item[uniqueKey]) === null || _a === void 0 ? void 0 : _a.toString().trim()) === value.trim(); });
|
|
315
290
|
};
|
|
316
|
-
|
|
291
|
+
const clearSearch = () => {
|
|
317
292
|
localStorage.removeItem(key);
|
|
318
293
|
setRecentSearches([]);
|
|
319
294
|
};
|
|
320
|
-
return { recentSearches
|
|
295
|
+
return { recentSearches, addSearch, clearSearch, hasSearch };
|
|
321
296
|
};
|
|
322
|
-
export
|
|
323
|
-
|
|
324
|
-
useEffect(
|
|
297
|
+
export const useUnmountedRef = () => {
|
|
298
|
+
const unmountedRef = useRef(false);
|
|
299
|
+
useEffect(() => {
|
|
325
300
|
unmountedRef.current = false;
|
|
326
|
-
return
|
|
301
|
+
return () => {
|
|
327
302
|
unmountedRef.current = true;
|
|
328
303
|
};
|
|
329
304
|
}, []);
|
|
330
305
|
return unmountedRef;
|
|
331
306
|
};
|
|
332
|
-
|
|
333
|
-
|
|
307
|
+
let info;
|
|
308
|
+
let responsiveConfig = {
|
|
334
309
|
xs: 0,
|
|
335
310
|
sm: 576,
|
|
336
311
|
md: 768,
|
|
337
312
|
lg: 992,
|
|
338
313
|
xl: 1200,
|
|
339
314
|
};
|
|
340
|
-
|
|
315
|
+
const subscribers = new Set();
|
|
341
316
|
function handleResize() {
|
|
342
|
-
|
|
317
|
+
const oldInfo = info;
|
|
343
318
|
calculate();
|
|
344
319
|
if (oldInfo === info)
|
|
345
320
|
return;
|
|
346
|
-
for (
|
|
347
|
-
var subscriber = _a[_i];
|
|
321
|
+
for (const subscriber of subscribers) {
|
|
348
322
|
subscriber();
|
|
349
323
|
}
|
|
350
324
|
}
|
|
351
|
-
|
|
325
|
+
let listening = false;
|
|
352
326
|
function calculate() {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
for (
|
|
357
|
-
var key = _a[_i];
|
|
327
|
+
const width = window.innerWidth;
|
|
328
|
+
const newInfo = {};
|
|
329
|
+
let shouldUpdate = false;
|
|
330
|
+
for (const key of Object.keys(responsiveConfig)) {
|
|
358
331
|
newInfo[key] = width >= responsiveConfig[key];
|
|
359
332
|
if (newInfo[key] !== info[key]) {
|
|
360
333
|
shouldUpdate = true;
|
|
@@ -369,25 +342,25 @@ export function configResponsive(config) {
|
|
|
369
342
|
if (info)
|
|
370
343
|
calculate();
|
|
371
344
|
}
|
|
372
|
-
export
|
|
345
|
+
export const useResponsive = () => {
|
|
373
346
|
if (isBrowser && !listening) {
|
|
374
347
|
info = {};
|
|
375
348
|
calculate();
|
|
376
349
|
window.addEventListener('resize', handleResize);
|
|
377
350
|
listening = true;
|
|
378
351
|
}
|
|
379
|
-
|
|
380
|
-
useEffect(
|
|
352
|
+
const [state, setState] = useState(info);
|
|
353
|
+
useEffect(() => {
|
|
381
354
|
if (!isBrowser)
|
|
382
355
|
return;
|
|
383
356
|
if (!listening) {
|
|
384
357
|
window.addEventListener('resize', handleResize);
|
|
385
358
|
}
|
|
386
|
-
|
|
359
|
+
const subscriber = () => {
|
|
387
360
|
setState(info);
|
|
388
361
|
};
|
|
389
362
|
subscribers.add(subscriber);
|
|
390
|
-
return
|
|
363
|
+
return () => {
|
|
391
364
|
subscribers.delete(subscriber);
|
|
392
365
|
if (subscribers.size === 0) {
|
|
393
366
|
window.removeEventListener('resize', handleResize);
|
|
@@ -397,34 +370,30 @@ export var useResponsive = function () {
|
|
|
397
370
|
}, []);
|
|
398
371
|
return state;
|
|
399
372
|
};
|
|
400
|
-
|
|
401
|
-
export
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
useEffect(function () {
|
|
373
|
+
const defaultEvents = ['mousedown', 'touchstart'];
|
|
374
|
+
export const useClickAway = (ref, onClickAway, events = defaultEvents) => {
|
|
375
|
+
const savedCallback = useRef(onClickAway);
|
|
376
|
+
useEffect(() => {
|
|
405
377
|
savedCallback.current = onClickAway;
|
|
406
378
|
}, [onClickAway]);
|
|
407
|
-
useEffect(
|
|
408
|
-
|
|
409
|
-
|
|
379
|
+
useEffect(() => {
|
|
380
|
+
const handler = (event) => {
|
|
381
|
+
const { current: el } = ref;
|
|
410
382
|
el && !el.contains(event.target) && savedCallback.current(event);
|
|
411
383
|
};
|
|
412
|
-
for (
|
|
413
|
-
var eventName = events_1[_i];
|
|
384
|
+
for (const eventName of events) {
|
|
414
385
|
on(document, eventName, handler);
|
|
415
386
|
}
|
|
416
|
-
return
|
|
417
|
-
for (
|
|
418
|
-
var eventName = events_2[_i];
|
|
387
|
+
return () => {
|
|
388
|
+
for (const eventName of events) {
|
|
419
389
|
off(document, eventName, handler);
|
|
420
390
|
}
|
|
421
391
|
};
|
|
422
392
|
}, [events, ref]);
|
|
423
393
|
};
|
|
424
|
-
export
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
var finalEnabled = typeof enabled === 'function' ? enabled() : true;
|
|
394
|
+
export const useBeforeUnload = (enabled = true, message) => {
|
|
395
|
+
const handler = useCallback((event) => {
|
|
396
|
+
const finalEnabled = typeof enabled === 'function' ? enabled() : true;
|
|
428
397
|
if (!finalEnabled) {
|
|
429
398
|
return;
|
|
430
399
|
}
|
|
@@ -434,31 +403,30 @@ export var useBeforeUnload = function (enabled, message) {
|
|
|
434
403
|
}
|
|
435
404
|
return message;
|
|
436
405
|
}, [enabled, message]);
|
|
437
|
-
useEffect(
|
|
406
|
+
useEffect(() => {
|
|
438
407
|
if (!enabled) {
|
|
439
408
|
return;
|
|
440
409
|
}
|
|
441
410
|
on(window, 'beforeunload', handler);
|
|
442
|
-
return
|
|
411
|
+
return () => off(window, 'beforeunload', handler);
|
|
443
412
|
}, [enabled, handler]);
|
|
444
413
|
};
|
|
445
|
-
export
|
|
446
|
-
if (enabled === void 0) { enabled = true; }
|
|
414
|
+
export const useHoverDirty = (ref, enabled = true) => {
|
|
447
415
|
if (process.env.NODE_ENV === 'development') {
|
|
448
416
|
if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
|
449
417
|
console.error('useHoverDirty expects a single ref argument.');
|
|
450
418
|
}
|
|
451
419
|
}
|
|
452
|
-
|
|
453
|
-
useEffect(
|
|
454
|
-
|
|
455
|
-
|
|
420
|
+
const [value, setValue] = useState(false);
|
|
421
|
+
useEffect(() => {
|
|
422
|
+
const onMouseOver = () => setValue(true);
|
|
423
|
+
const onMouseOut = () => setValue(false);
|
|
456
424
|
if (enabled && ref && ref.current) {
|
|
457
425
|
on(ref.current, 'mouseover', onMouseOver);
|
|
458
426
|
on(ref.current, 'mouseout', onMouseOut);
|
|
459
427
|
}
|
|
460
|
-
|
|
461
|
-
return
|
|
428
|
+
const { current } = ref;
|
|
429
|
+
return () => {
|
|
462
430
|
if (enabled && current) {
|
|
463
431
|
off(current, 'mouseover', onMouseOver);
|
|
464
432
|
off(current, 'mouseout', onMouseOut);
|
|
@@ -467,7 +435,7 @@ export var useHoverDirty = function (ref, enabled) {
|
|
|
467
435
|
}, [enabled, ref]);
|
|
468
436
|
return value;
|
|
469
437
|
};
|
|
470
|
-
|
|
438
|
+
const defaultState = {
|
|
471
439
|
acceleration: {
|
|
472
440
|
x: null,
|
|
473
441
|
y: null,
|
|
@@ -485,12 +453,11 @@ var defaultState = {
|
|
|
485
453
|
},
|
|
486
454
|
interval: 16,
|
|
487
455
|
};
|
|
488
|
-
export
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
var acceleration = event.acceleration, accelerationIncludingGravity = event.accelerationIncludingGravity, rotationRate = event.rotationRate, interval = event.interval;
|
|
456
|
+
export const useMotion = (initialState = defaultState) => {
|
|
457
|
+
const [state, setState] = useState(initialState);
|
|
458
|
+
useEffect(() => {
|
|
459
|
+
const handler = (event) => {
|
|
460
|
+
const { acceleration, accelerationIncludingGravity, rotationRate, interval } = event;
|
|
494
461
|
setState({
|
|
495
462
|
acceleration: {
|
|
496
463
|
x: acceleration.x,
|
|
@@ -507,54 +474,53 @@ export var useMotion = function (initialState) {
|
|
|
507
474
|
beta: rotationRate.beta,
|
|
508
475
|
gamma: rotationRate.gamma,
|
|
509
476
|
},
|
|
510
|
-
interval
|
|
477
|
+
interval,
|
|
511
478
|
});
|
|
512
479
|
};
|
|
513
480
|
on(window, 'devicemotion', handler);
|
|
514
|
-
return
|
|
481
|
+
return () => {
|
|
515
482
|
off(window, 'devicemotion', handler);
|
|
516
483
|
};
|
|
517
484
|
}, []);
|
|
518
485
|
return state;
|
|
519
486
|
};
|
|
520
|
-
export
|
|
521
|
-
|
|
522
|
-
useEffect(function () {
|
|
487
|
+
export const usePageLeave = (onPageLeave, args = []) => {
|
|
488
|
+
useEffect(() => {
|
|
523
489
|
if (!onPageLeave) {
|
|
524
490
|
return;
|
|
525
491
|
}
|
|
526
|
-
|
|
492
|
+
const handler = (event) => {
|
|
527
493
|
event = event ? event : window.event;
|
|
528
|
-
|
|
494
|
+
const from = event.relatedTarget || event.toElement;
|
|
529
495
|
if (!from || from.nodeName === 'HTML') {
|
|
530
496
|
onPageLeave();
|
|
531
497
|
}
|
|
532
498
|
};
|
|
533
499
|
on(document, 'mouseout', handler);
|
|
534
|
-
return
|
|
500
|
+
return () => {
|
|
535
501
|
off(document, 'mouseout', handler);
|
|
536
502
|
};
|
|
537
503
|
}, args);
|
|
538
504
|
};
|
|
539
|
-
export
|
|
540
|
-
|
|
541
|
-
useEffect(
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
505
|
+
export const usePermission = (permissionDesc) => {
|
|
506
|
+
const [state, setState] = useState('');
|
|
507
|
+
useEffect(() => {
|
|
508
|
+
let mounted = true;
|
|
509
|
+
let permissionStatus = null;
|
|
510
|
+
const onChange = () => {
|
|
545
511
|
if (!mounted)
|
|
546
512
|
return;
|
|
547
|
-
setState(
|
|
513
|
+
setState(() => { var _a; return (_a = permissionStatus === null || permissionStatus === void 0 ? void 0 : permissionStatus.state) !== null && _a !== void 0 ? _a : ''; });
|
|
548
514
|
};
|
|
549
515
|
navigator.permissions
|
|
550
516
|
.query(permissionDesc) // 👈 cast to fix TS error
|
|
551
|
-
.then(
|
|
517
|
+
.then((status) => {
|
|
552
518
|
permissionStatus = status;
|
|
553
519
|
on(permissionStatus, 'change', onChange);
|
|
554
520
|
onChange();
|
|
555
521
|
})
|
|
556
522
|
.catch(noop);
|
|
557
|
-
return
|
|
523
|
+
return () => {
|
|
558
524
|
permissionStatus && off(permissionStatus, 'change', onChange);
|
|
559
525
|
mounted = false;
|
|
560
526
|
permissionStatus = null;
|
|
@@ -562,47 +528,47 @@ export var usePermission = function (permissionDesc) {
|
|
|
562
528
|
}, [permissionDesc]);
|
|
563
529
|
return state;
|
|
564
530
|
};
|
|
565
|
-
export
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
531
|
+
export const useSpeech = (text, options) => {
|
|
532
|
+
let mounted = useRef(false);
|
|
533
|
+
const [state, setState] = useState(() => {
|
|
534
|
+
const { lang = 'default', name = '' } = options.voice || {};
|
|
569
535
|
return {
|
|
570
536
|
isPlaying: false,
|
|
571
537
|
status: VoiceStatus[VoiceStatus.init],
|
|
572
538
|
lang: options.lang || 'default',
|
|
573
|
-
voiceInfo: { lang
|
|
539
|
+
voiceInfo: { lang, name },
|
|
574
540
|
rate: options.rate || 1,
|
|
575
541
|
pitch: options.pitch || 1,
|
|
576
542
|
volume: options.volume || 1,
|
|
577
543
|
};
|
|
578
|
-
})
|
|
579
|
-
|
|
544
|
+
});
|
|
545
|
+
const handlePlay = useCallback(() => {
|
|
580
546
|
if (!mounted.current) {
|
|
581
547
|
return;
|
|
582
548
|
}
|
|
583
|
-
setState(
|
|
584
|
-
return
|
|
549
|
+
setState((preState) => {
|
|
550
|
+
return Object.assign(Object.assign({}, preState), { isPlaying: true, status: VoiceStatus[VoiceStatus.play] });
|
|
585
551
|
});
|
|
586
552
|
}, []);
|
|
587
|
-
|
|
553
|
+
const handlePause = useCallback(() => {
|
|
588
554
|
if (!mounted.current) {
|
|
589
555
|
return;
|
|
590
556
|
}
|
|
591
|
-
setState(
|
|
592
|
-
return
|
|
557
|
+
setState((preState) => {
|
|
558
|
+
return Object.assign(Object.assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.pause] });
|
|
593
559
|
});
|
|
594
560
|
}, []);
|
|
595
|
-
|
|
561
|
+
const handleEnd = useCallback(() => {
|
|
596
562
|
if (!mounted.current) {
|
|
597
563
|
return;
|
|
598
564
|
}
|
|
599
|
-
setState(
|
|
600
|
-
return
|
|
565
|
+
setState((preState) => {
|
|
566
|
+
return Object.assign(Object.assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.end] });
|
|
601
567
|
});
|
|
602
568
|
}, []);
|
|
603
|
-
useEffect(
|
|
569
|
+
useEffect(() => {
|
|
604
570
|
mounted.current = true;
|
|
605
|
-
|
|
571
|
+
const utterance = new SpeechSynthesisUtterance(text);
|
|
606
572
|
options.lang && (utterance.lang = options.lang);
|
|
607
573
|
options.voice && (utterance.voice = options.voice);
|
|
608
574
|
utterance.rate = options.rate || 1;
|
|
@@ -613,7 +579,7 @@ export var useSpeech = function (text, options) {
|
|
|
613
579
|
utterance.onresume = handlePlay;
|
|
614
580
|
utterance.onend = handleEnd;
|
|
615
581
|
window.speechSynthesis.speak(utterance);
|
|
616
|
-
return
|
|
582
|
+
return () => {
|
|
617
583
|
mounted.current = false;
|
|
618
584
|
};
|
|
619
585
|
}, []);
|