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