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.
@@ -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
- var _a = state.past, newPresent = _a[0], newPast = _a.slice(1);
9
+ const [newPresent, ...newPast] = state.past;
30
10
  return {
31
11
  past: newPast,
32
12
  present: newPresent,
33
- future: __spreadArray([state.present], state.future, true),
13
+ future: [state.present, ...state.future],
34
14
  };
35
15
  case 'REDO':
36
16
  if (state.future.length === 0)
37
17
  return state;
38
- var _b = state.future, nextPresent = _b[0], newFuture = _b.slice(1);
18
+ const [nextPresent, ...newFuture] = state.future;
39
19
  return {
40
- past: __spreadArray([state.present], state.past, true),
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: __spreadArray([state.present], state.past, true),
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
- var _a = useReducer((historyReducer), {
49
+ const [state, dispatch] = useReducer((historyReducer), {
70
50
  past: [],
71
51
  present: initialPresent,
72
52
  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 () {
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
- var redo = useCallback(function () {
61
+ const redo = useCallback(() => {
82
62
  if (canRedo) {
83
63
  dispatch({ type: 'REDO' });
84
64
  }
85
65
  }, [canRedo]);
86
- var set = useCallback(function (newPresent) {
87
- dispatch({ type: 'SET', newPresent: newPresent });
66
+ const set = useCallback((newPresent) => {
67
+ dispatch({ type: 'SET', newPresent });
88
68
  }, []);
89
- var reset = useCallback(function (newPresent) {
90
- dispatch({ type: 'RESET', newPresent: newPresent });
69
+ const reset = useCallback((newPresent) => {
70
+ dispatch({ type: 'RESET', newPresent });
91
71
  }, []);
92
- var clear = useCallback(function () {
72
+ const clear = useCallback(() => {
93
73
  dispatch({ type: 'CLEAR' });
94
74
  }, []);
95
75
  return {
96
76
  state: state.present,
97
- set: set,
98
- undo: undo,
99
- redo: redo,
100
- reset: reset,
101
- clear: clear,
102
- canUndo: canUndo,
103
- canRedo: 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
- 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 () {
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
- var handleEvent = throttle(function () {
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
- var handleVisibilityChange = function () {
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
- var events = [
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(function (event) { return window.addEventListener(event, handleEvent); });
119
+ events.forEach((event) => window.addEventListener(event, handleEvent));
141
120
  document.addEventListener('visibilitychange', handleVisibilityChange);
142
- return function () {
143
- events.forEach(function (event) { return window.removeEventListener(event, handleEvent); });
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
- var isFirstRender = useRef(true);
154
- useEffect(function () {
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
- if (initialList === void 0) { initialList = []; }
161
- var _a = useState(initialList), list = _a[0], setList = _a[1];
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
- var push = useCallback(function (element) {
166
- setList(function (current) { return __spreadArray(__spreadArray([], current, true), [element], false); });
143
+ const push = useCallback((element) => {
144
+ setList(current => [...current, element]);
167
145
  }, []);
168
- var removeAt = useCallback(function (index) {
169
- setList(function (current) { return current.filter(function (_, i) { return i !== index; }); });
146
+ const removeAt = useCallback((index) => {
147
+ setList(current => current.filter((_, i) => i !== index));
170
148
  }, []);
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); });
149
+ const insertAt = useCallback((index, element) => {
150
+ setList(current => [
151
+ ...current.slice(0, index),
152
+ element,
153
+ ...current.slice(index)
154
+ ]);
175
155
  }, []);
176
- var updateAt = useCallback(function (index, element) {
177
- setList(function (current) {
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
- var clear = useCallback(function () {
159
+ const clear = useCallback(() => {
182
160
  setList([]);
183
161
  }, []);
184
162
  return {
185
- list: list,
163
+ list,
186
164
  actions: {
187
- set: set,
188
- push: push,
189
- removeAt: removeAt,
190
- insertAt: insertAt,
191
- updateAt: updateAt,
192
- clear: 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
- if (lock === void 0) { lock = true; }
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
- var originalOverflow = document.body.style.overflow;
203
- var originalPaddingRight = document.body.style.paddingRight;
179
+ const originalOverflow = document.body.style.overflow;
180
+ const originalPaddingRight = document.body.style.paddingRight;
204
181
  // Calculate scrollbar width to prevent layout shift
205
- var scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
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 = "".concat(scrollbarWidth, "px");
187
+ document.body.style.paddingRight = `${scrollbarWidth}px`;
211
188
  }
212
- return function () {
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
- 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) {
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(function () {
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
- var cancel = useCallback(function (event) {
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(function () {
266
- return function () {
241
+ useEffect(() => {
242
+ return () => {
267
243
  if (timerId.current) {
268
244
  clearTimeout(timerId.current);
269
245
  }
270
246
  };
271
247
  }, []);
272
- return useMemo(function () { return ({
248
+ return useMemo(() => ({
273
249
  onMouseDown: start,
274
250
  onMouseUp: cancel,
275
251
  onMouseLeave: cancel,
276
252
  onTouchStart: start,
277
253
  onTouchEnd: cancel,
278
- }); }, [start, cancel]);
254
+ }), [start, cancel]);
279
255
  }
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);
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
- var parsed = JSON.parse(saved);
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
- var addSearch = useCallback(function (item) {
274
+ const addSearch = useCallback((item) => {
300
275
  if (excludeEmpty && !item[uniqueKey])
301
276
  return;
302
277
  try {
303
- setRecentSearches(function (prev) {
304
- var updated = __spreadArray([item], prev.filter(function (p) { return p[uniqueKey] !== item[uniqueKey]; }), true).slice(0, limit);
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
- 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(); });
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
- var clearSearch = function () {
291
+ const clearSearch = () => {
317
292
  localStorage.removeItem(key);
318
293
  setRecentSearches([]);
319
294
  };
320
- return { recentSearches: recentSearches, addSearch: addSearch, clearSearch: clearSearch, hasSearch: hasSearch };
295
+ return { recentSearches, addSearch, clearSearch, hasSearch };
321
296
  };
322
- export var useUnmountedRef = function () {
323
- var unmountedRef = useRef(false);
324
- useEffect(function () {
297
+ export const useUnmountedRef = () => {
298
+ const unmountedRef = useRef(false);
299
+ useEffect(() => {
325
300
  unmountedRef.current = false;
326
- return function () {
301
+ return () => {
327
302
  unmountedRef.current = true;
328
303
  };
329
304
  }, []);
330
305
  return unmountedRef;
331
306
  };
332
- var info;
333
- var responsiveConfig = {
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
- var subscribers = new Set();
315
+ const subscribers = new Set();
341
316
  function handleResize() {
342
- var oldInfo = info;
317
+ const oldInfo = info;
343
318
  calculate();
344
319
  if (oldInfo === info)
345
320
  return;
346
- for (var _i = 0, _a = subscribers; _i < _a.length; _i++) {
347
- var subscriber = _a[_i];
321
+ for (const subscriber of subscribers) {
348
322
  subscriber();
349
323
  }
350
324
  }
351
- var listening = false;
325
+ let listening = false;
352
326
  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];
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 var useResponsive = function () {
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
- var _a = useState(info), state = _a[0], setState = _a[1];
380
- useEffect(function () {
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
- var subscriber = function () {
359
+ const subscriber = () => {
387
360
  setState(info);
388
361
  };
389
362
  subscribers.add(subscriber);
390
- return function () {
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
- 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 () {
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(function () {
408
- var handler = function (event) {
409
- var el = ref.current;
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 (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
413
- var eventName = events_1[_i];
384
+ for (const eventName of events) {
414
385
  on(document, eventName, handler);
415
386
  }
416
- return function () {
417
- for (var _i = 0, events_2 = events; _i < events_2.length; _i++) {
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 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;
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(function () {
406
+ useEffect(() => {
438
407
  if (!enabled) {
439
408
  return;
440
409
  }
441
410
  on(window, 'beforeunload', handler);
442
- return function () { return off(window, 'beforeunload', handler); };
411
+ return () => off(window, 'beforeunload', handler);
443
412
  }, [enabled, handler]);
444
413
  };
445
- export var useHoverDirty = function (ref, enabled) {
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
- 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); };
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
- var current = ref.current;
461
- return function () {
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
- var defaultState = {
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 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;
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: interval,
477
+ interval,
511
478
  });
512
479
  };
513
480
  on(window, 'devicemotion', handler);
514
- return function () {
481
+ return () => {
515
482
  off(window, 'devicemotion', handler);
516
483
  };
517
484
  }, []);
518
485
  return state;
519
486
  };
520
- export var usePageLeave = function (onPageLeave, args) {
521
- if (args === void 0) { args = []; }
522
- useEffect(function () {
487
+ export const usePageLeave = (onPageLeave, args = []) => {
488
+ useEffect(() => {
523
489
  if (!onPageLeave) {
524
490
  return;
525
491
  }
526
- var handler = function (event) {
492
+ const handler = (event) => {
527
493
  event = event ? event : window.event;
528
- var from = event.relatedTarget || event.toElement;
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 function () {
500
+ return () => {
535
501
  off(document, 'mouseout', handler);
536
502
  };
537
503
  }, args);
538
504
  };
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 () {
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(function () { var _a; return (_a = permissionStatus === null || permissionStatus === void 0 ? void 0 : permissionStatus.state) !== null && _a !== void 0 ? _a : ''; });
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(function (status) {
517
+ .then((status) => {
552
518
  permissionStatus = status;
553
519
  on(permissionStatus, 'change', onChange);
554
520
  onChange();
555
521
  })
556
522
  .catch(noop);
557
- return function () {
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 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;
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: lang, name: name },
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
- }), state = _a[0], setState = _a[1];
579
- var handlePlay = useCallback(function () {
544
+ });
545
+ const handlePlay = useCallback(() => {
580
546
  if (!mounted.current) {
581
547
  return;
582
548
  }
583
- setState(function (preState) {
584
- return __assign(__assign({}, preState), { isPlaying: true, status: VoiceStatus[VoiceStatus.play] });
549
+ setState((preState) => {
550
+ return Object.assign(Object.assign({}, preState), { isPlaying: true, status: VoiceStatus[VoiceStatus.play] });
585
551
  });
586
552
  }, []);
587
- var handlePause = useCallback(function () {
553
+ const handlePause = useCallback(() => {
588
554
  if (!mounted.current) {
589
555
  return;
590
556
  }
591
- setState(function (preState) {
592
- return __assign(__assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.pause] });
557
+ setState((preState) => {
558
+ return Object.assign(Object.assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.pause] });
593
559
  });
594
560
  }, []);
595
- var handleEnd = useCallback(function () {
561
+ const handleEnd = useCallback(() => {
596
562
  if (!mounted.current) {
597
563
  return;
598
564
  }
599
- setState(function (preState) {
600
- return __assign(__assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.end] });
565
+ setState((preState) => {
566
+ return Object.assign(Object.assign({}, preState), { isPlaying: false, status: VoiceStatus[VoiceStatus.end] });
601
567
  });
602
568
  }, []);
603
- useEffect(function () {
569
+ useEffect(() => {
604
570
  mounted.current = true;
605
- var utterance = new SpeechSynthesisUtterance(text);
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 function () {
582
+ return () => {
617
583
  mounted.current = false;
618
584
  };
619
585
  }, []);