@aiszlab/relax 1.4.4 → 1.4.6
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/hooks/use-debounce-callback.cjs +2 -17
- package/dist/hooks/use-debounce-callback.d.ts +4 -2
- package/dist/hooks/use-debounce-callback.mjs +2 -17
- package/dist/hooks/use-drag.d.ts +49 -0
- package/dist/hooks/use-event.cjs +0 -1
- package/dist/hooks/use-event.d.ts +2 -2
- package/dist/hooks/use-event.mjs +0 -1
- package/dist/hooks/use-infinite-scroll.cjs +40 -36
- package/dist/hooks/use-infinite-scroll.d.ts +3 -3
- package/dist/hooks/use-infinite-scroll.mjs +40 -36
- package/dist/hooks/use-is-mounted.d.ts +2 -2
- package/dist/hooks/use-media-query.cjs +9 -13
- package/dist/hooks/use-media-query.mjs +10 -14
- package/dist/hooks/use-raf.d.ts +2 -1
- package/dist/hooks/use-throttle-callback.cjs +2 -17
- package/dist/hooks/use-throttle-callback.d.ts +4 -2
- package/dist/hooks/use-throttle-callback.mjs +2 -17
- package/dist/hooks/use-timeout.d.ts +2 -1
- package/dist/hooks/use-update-state.d.ts +12 -0
- package/dist/is/is-html-element.cjs +0 -1
- package/dist/is/is-html-element.mjs +0 -1
- package/dist/is/is-mobile.cjs +2 -1
- package/dist/is/is-mobile.mjs +2 -1
- package/dist/is/is-overflow.cjs +0 -1
- package/dist/is/is-overflow.mjs +0 -1
- package/dist/types/any-function.d.ts +1 -0
- package/dist/types/first.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/last.d.ts +1 -1
- package/dist/types/partial-in.d.ts +5 -0
- package/dist/utils/clone.cjs +0 -1
- package/dist/utils/clone.mjs +0 -1
- package/dist/utils/debounce.cjs +0 -1
- package/dist/utils/debounce.d.ts +4 -4
- package/dist/utils/debounce.mjs +0 -1
- package/dist/utils/exclude.cjs +0 -1
- package/dist/utils/exclude.mjs +0 -1
- package/dist/utils/first.cjs +0 -1
- package/dist/utils/first.mjs +0 -1
- package/dist/utils/last.cjs +0 -1
- package/dist/utils/last.mjs +0 -1
- package/dist/utils/max.cjs +4 -10
- package/dist/utils/max.d.ts +3 -1
- package/dist/utils/max.mjs +4 -10
- package/dist/utils/merge.cjs +0 -2
- package/dist/utils/merge.mjs +0 -2
- package/dist/utils/min.cjs +6 -12
- package/dist/utils/min.d.ts +3 -1
- package/dist/utils/min.mjs +6 -12
- package/dist/utils/replace.cjs +16 -15
- package/dist/utils/replace.d.ts +14 -13
- package/dist/utils/replace.mjs +16 -15
- package/dist/utils/set-style.cjs +3 -5
- package/dist/utils/set-style.d.ts +1 -1
- package/dist/utils/set-style.mjs +3 -5
- package/dist/utils/throttle.cjs +0 -1
- package/dist/utils/throttle.d.ts +4 -4
- package/dist/utils/throttle.mjs +0 -1
- package/dist/utils/to-function.cjs +1 -2
- package/dist/utils/to-function.d.ts +4 -1
- package/dist/utils/to-function.mjs +1 -2
- package/dist/utils/waitable.cjs +7 -6
- package/dist/utils/waitable.d.ts +4 -4
- package/dist/utils/waitable.mjs +7 -6
- package/package.json +4 -4
|
@@ -32,22 +32,7 @@ var useDebouncer = function useDebouncer(debouncer) {
|
|
|
32
32
|
pipe: pipe
|
|
33
33
|
};
|
|
34
34
|
};
|
|
35
|
-
|
|
36
|
-
* @author murukal
|
|
37
|
-
*
|
|
38
|
-
* @description
|
|
39
|
-
* debounce callback
|
|
40
|
-
*
|
|
41
|
-
* @param callback
|
|
42
|
-
* @param wait number
|
|
43
|
-
* @description
|
|
44
|
-
* The delay time (in milliseconds) until the debounce function is called.
|
|
45
|
-
* default 1000
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* 1000
|
|
49
|
-
*/
|
|
50
|
-
var useDebounceCallback = function useDebounceCallback(debouncer) {
|
|
35
|
+
function useDebounceCallback(debouncer) {
|
|
51
36
|
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
52
37
|
var debounced = react.useRef(null);
|
|
53
38
|
var _useDebouncer = useDebouncer(debouncer),
|
|
@@ -84,6 +69,6 @@ var useDebounceCallback = function useDebounceCallback(debouncer) {
|
|
|
84
69
|
}
|
|
85
70
|
};
|
|
86
71
|
});
|
|
87
|
-
}
|
|
72
|
+
}
|
|
88
73
|
|
|
89
74
|
exports.useDebounceCallback = useDebounceCallback;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Debounced, type Debouncer } from "../utils/debounce";
|
|
2
|
-
import {
|
|
2
|
+
import type { AnyFunction } from "@aiszlab/relax/types";
|
|
3
3
|
/**
|
|
4
4
|
* @author murukal
|
|
5
5
|
*
|
|
@@ -15,4 +15,6 @@ import { type Callable } from "./use-event";
|
|
|
15
15
|
* @example
|
|
16
16
|
* 1000
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
declare function useDebounceCallback<T extends AnyFunction>(callback: T, wait: number): Debounced<T>;
|
|
19
|
+
declare function useDebounceCallback<T extends AnyFunction, R = unknown>(debouncer: Debouncer<T, R>, wait: number): Debounced<T>;
|
|
20
|
+
export { useDebounceCallback };
|
|
@@ -30,22 +30,7 @@ var useDebouncer = function useDebouncer(debouncer) {
|
|
|
30
30
|
pipe: pipe
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
|
-
|
|
34
|
-
* @author murukal
|
|
35
|
-
*
|
|
36
|
-
* @description
|
|
37
|
-
* debounce callback
|
|
38
|
-
*
|
|
39
|
-
* @param callback
|
|
40
|
-
* @param wait number
|
|
41
|
-
* @description
|
|
42
|
-
* The delay time (in milliseconds) until the debounce function is called.
|
|
43
|
-
* default 1000
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* 1000
|
|
47
|
-
*/
|
|
48
|
-
var useDebounceCallback = function useDebounceCallback(debouncer) {
|
|
33
|
+
function useDebounceCallback(debouncer) {
|
|
49
34
|
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
50
35
|
var debounced = useRef(null);
|
|
51
36
|
var _useDebouncer = useDebouncer(debouncer),
|
|
@@ -82,6 +67,6 @@ var useDebounceCallback = function useDebounceCallback(debouncer) {
|
|
|
82
67
|
}
|
|
83
68
|
};
|
|
84
69
|
});
|
|
85
|
-
}
|
|
70
|
+
}
|
|
86
71
|
|
|
87
72
|
export { useDebounceCallback };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type DragEvent } from "react";
|
|
2
|
+
export type UsingDrag = {
|
|
3
|
+
/** Optional callback invoked upon drag end. */
|
|
4
|
+
onDragEnd?: (event: DragEvent<HTMLDivElement>, state: DragState) => void;
|
|
5
|
+
/** Optional callback invoked upon drag movement. */
|
|
6
|
+
onDragMove?: (event: DragEvent<HTMLDivElement>, state: DragState) => void;
|
|
7
|
+
/** Optional callback invoked upon drag start. */
|
|
8
|
+
onDragStart?: (event: DragEvent<HTMLDivElement>, state: DragState) => void;
|
|
9
|
+
};
|
|
10
|
+
export type DragState = {
|
|
11
|
+
/**
|
|
12
|
+
* @description
|
|
13
|
+
* x position
|
|
14
|
+
*/
|
|
15
|
+
x: number;
|
|
16
|
+
/**
|
|
17
|
+
* @description
|
|
18
|
+
* y position
|
|
19
|
+
*/
|
|
20
|
+
y: number;
|
|
21
|
+
/**
|
|
22
|
+
* @description
|
|
23
|
+
* offset X
|
|
24
|
+
*/
|
|
25
|
+
offsetX?: number;
|
|
26
|
+
/**
|
|
27
|
+
* @description
|
|
28
|
+
* offset Y
|
|
29
|
+
*/
|
|
30
|
+
offsetY?: number;
|
|
31
|
+
/**
|
|
32
|
+
* @description
|
|
33
|
+
* Whether a drag is currently in progress.
|
|
34
|
+
*/
|
|
35
|
+
isDragging: boolean;
|
|
36
|
+
};
|
|
37
|
+
export type UsedDrag = [
|
|
38
|
+
DragState,
|
|
39
|
+
{
|
|
40
|
+
onDragEnd: (event: DragEvent<HTMLDivElement>) => void;
|
|
41
|
+
onDragMove: (event: DragEvent<HTMLDivElement>) => void;
|
|
42
|
+
onDragStart: (event: DragEvent<HTMLDivElement>) => void;
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* @description
|
|
47
|
+
* Hook for dragging
|
|
48
|
+
*/
|
|
49
|
+
export default function useDrag({ onDragEnd: _onDragEnd, onDragMove: _onDragMove, onDragStart: _onDragStart, }?: UsingDrag | undefined): UsedDrag;
|
package/dist/hooks/use-event.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const useEvent: <T extends
|
|
1
|
+
import type { AnyFunction } from "@aiszlab/relax/types";
|
|
2
|
+
export declare const useEvent: <T extends AnyFunction>(callback: T) => T;
|
package/dist/hooks/use-event.mjs
CHANGED
|
@@ -1,64 +1,68 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
4
3
|
var react = require('react');
|
|
5
4
|
var useEvent = require('./use-event.cjs');
|
|
6
5
|
var debounce = require('../utils/debounce.cjs');
|
|
6
|
+
var first = require('../utils/first.cjs');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @description
|
|
10
10
|
* use infinite scroll
|
|
11
11
|
*/
|
|
12
|
-
var useInfiniteScroll = function useInfiniteScroll(
|
|
13
|
-
var _ref
|
|
12
|
+
var useInfiniteScroll = function useInfiniteScroll() {
|
|
13
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
14
|
+
_ref$hasMore = _ref.hasMore,
|
|
14
15
|
hasMore = _ref$hasMore === void 0 ? true : _ref$hasMore,
|
|
15
16
|
_ref$distance = _ref.distance,
|
|
16
17
|
distance = _ref$distance === void 0 ? 0 : _ref$distance,
|
|
17
18
|
onLoadMore = _ref.onLoadMore;
|
|
18
|
-
var
|
|
19
|
-
var
|
|
19
|
+
var sentinelRef = react.useRef(null);
|
|
20
|
+
var viewportRef = react.useRef(null);
|
|
21
|
+
// `useEvent` keep loadMore always the same
|
|
20
22
|
var loadMore = useEvent.useEvent(function () {
|
|
21
23
|
onLoadMore === null || onLoadMore === void 0 || onLoadMore();
|
|
22
24
|
});
|
|
23
25
|
react.useEffect(function () {
|
|
24
|
-
var
|
|
25
|
-
var
|
|
26
|
+
var _viewportRef$current;
|
|
27
|
+
var _sentinel = sentinelRef.current;
|
|
28
|
+
var _viewport = (_viewportRef$current = viewportRef.current) !== null && _viewportRef$current !== void 0 ? _viewportRef$current : globalThis.window.document.body;
|
|
29
|
+
// no more data, never listen
|
|
26
30
|
if (!hasMore) return;
|
|
27
|
-
|
|
28
|
-
// use
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}, options);
|
|
41
|
-
observer.observe(loader);
|
|
31
|
+
// when _sentinel is not provided
|
|
32
|
+
// use `scroll` event to listen `viewport`
|
|
33
|
+
// it is not recommended, has performance issue!!!
|
|
34
|
+
if (!_sentinel) {
|
|
35
|
+
var _debounce = debounce.debounce(function () {
|
|
36
|
+
if (_viewport.scrollHeight - _viewport.scrollTop > _viewport.clientHeight + distance) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
loadMore();
|
|
40
|
+
}, 200),
|
|
41
|
+
next = _debounce.next,
|
|
42
|
+
abort = _debounce.abort;
|
|
43
|
+
_viewport.addEventListener("scroll", next);
|
|
42
44
|
return function () {
|
|
43
|
-
|
|
45
|
+
abort();
|
|
46
|
+
_viewport.removeEventListener("scroll", next);
|
|
44
47
|
};
|
|
45
48
|
}
|
|
46
|
-
//
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
// use `IntersectionObserver` to check current node is in viewport
|
|
50
|
+
var _listener = new IntersectionObserver(function (entries) {
|
|
51
|
+
var _element = first.first(entries);
|
|
52
|
+
if (!_element) return;
|
|
53
|
+
if (!_element.isIntersecting) return;
|
|
54
|
+
loadMore();
|
|
55
|
+
}, {
|
|
56
|
+
root: _viewport,
|
|
57
|
+
rootMargin: "0px 0px ".concat(distance, "px 0px")
|
|
58
|
+
});
|
|
59
|
+
_listener.observe(_sentinel);
|
|
56
60
|
return function () {
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
_listener.unobserve(_sentinel);
|
|
62
|
+
_listener.disconnect();
|
|
59
63
|
};
|
|
60
64
|
}, [hasMore]);
|
|
61
|
-
return [
|
|
65
|
+
return [sentinelRef, viewportRef];
|
|
62
66
|
};
|
|
63
67
|
|
|
64
68
|
exports.useInfiniteScroll = useInfiniteScroll;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type RefObject } from "react";
|
|
2
|
-
type
|
|
2
|
+
type UsingInfiniteScroll = {
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
5
5
|
* hasMore
|
|
@@ -16,10 +16,10 @@ type UseInfiniteScrollProps = {
|
|
|
16
16
|
*/
|
|
17
17
|
onLoadMore?: () => void;
|
|
18
18
|
};
|
|
19
|
-
type UsedInfiniteScroll = [
|
|
19
|
+
type UsedInfiniteScroll<S, V> = [sentinelRef: RefObject<S>, viewportRef: RefObject<V>];
|
|
20
20
|
/**
|
|
21
21
|
* @description
|
|
22
22
|
* use infinite scroll
|
|
23
23
|
*/
|
|
24
|
-
export declare const useInfiniteScroll: ({ hasMore, distance, onLoadMore
|
|
24
|
+
export declare const useInfiniteScroll: <S extends HTMLElement = HTMLElement, V extends HTMLElement = HTMLElement>({ hasMore, distance, onLoadMore }?: UsingInfiniteScroll) => UsedInfiniteScroll<S, V>;
|
|
25
25
|
export {};
|
|
@@ -1,62 +1,66 @@
|
|
|
1
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
1
|
import { useRef, useEffect } from 'react';
|
|
3
2
|
import { useEvent } from './use-event.mjs';
|
|
4
3
|
import { debounce } from '../utils/debounce.mjs';
|
|
4
|
+
import { first } from '../utils/first.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @description
|
|
8
8
|
* use infinite scroll
|
|
9
9
|
*/
|
|
10
|
-
var useInfiniteScroll = function useInfiniteScroll(
|
|
11
|
-
var _ref
|
|
10
|
+
var useInfiniteScroll = function useInfiniteScroll() {
|
|
11
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
12
|
+
_ref$hasMore = _ref.hasMore,
|
|
12
13
|
hasMore = _ref$hasMore === void 0 ? true : _ref$hasMore,
|
|
13
14
|
_ref$distance = _ref.distance,
|
|
14
15
|
distance = _ref$distance === void 0 ? 0 : _ref$distance,
|
|
15
16
|
onLoadMore = _ref.onLoadMore;
|
|
16
|
-
var
|
|
17
|
-
var
|
|
17
|
+
var sentinelRef = useRef(null);
|
|
18
|
+
var viewportRef = useRef(null);
|
|
19
|
+
// `useEvent` keep loadMore always the same
|
|
18
20
|
var loadMore = useEvent(function () {
|
|
19
21
|
onLoadMore === null || onLoadMore === void 0 || onLoadMore();
|
|
20
22
|
});
|
|
21
23
|
useEffect(function () {
|
|
22
|
-
var
|
|
23
|
-
var
|
|
24
|
+
var _viewportRef$current;
|
|
25
|
+
var _sentinel = sentinelRef.current;
|
|
26
|
+
var _viewport = (_viewportRef$current = viewportRef.current) !== null && _viewportRef$current !== void 0 ? _viewportRef$current : globalThis.window.document.body;
|
|
27
|
+
// no more data, never listen
|
|
24
28
|
if (!hasMore) return;
|
|
25
|
-
|
|
26
|
-
// use
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}, options);
|
|
39
|
-
observer.observe(loader);
|
|
29
|
+
// when _sentinel is not provided
|
|
30
|
+
// use `scroll` event to listen `viewport`
|
|
31
|
+
// it is not recommended, has performance issue!!!
|
|
32
|
+
if (!_sentinel) {
|
|
33
|
+
var _debounce = debounce(function () {
|
|
34
|
+
if (_viewport.scrollHeight - _viewport.scrollTop > _viewport.clientHeight + distance) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
loadMore();
|
|
38
|
+
}, 200),
|
|
39
|
+
next = _debounce.next,
|
|
40
|
+
abort = _debounce.abort;
|
|
41
|
+
_viewport.addEventListener("scroll", next);
|
|
40
42
|
return function () {
|
|
41
|
-
|
|
43
|
+
abort();
|
|
44
|
+
_viewport.removeEventListener("scroll", next);
|
|
42
45
|
};
|
|
43
46
|
}
|
|
44
|
-
//
|
|
45
|
-
var
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
// use `IntersectionObserver` to check current node is in viewport
|
|
48
|
+
var _listener = new IntersectionObserver(function (entries) {
|
|
49
|
+
var _element = first(entries);
|
|
50
|
+
if (!_element) return;
|
|
51
|
+
if (!_element.isIntersecting) return;
|
|
52
|
+
loadMore();
|
|
53
|
+
}, {
|
|
54
|
+
root: _viewport,
|
|
55
|
+
rootMargin: "0px 0px ".concat(distance, "px 0px")
|
|
56
|
+
});
|
|
57
|
+
_listener.observe(_sentinel);
|
|
54
58
|
return function () {
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
_listener.unobserve(_sentinel);
|
|
60
|
+
_listener.disconnect();
|
|
57
61
|
};
|
|
58
62
|
}, [hasMore]);
|
|
59
|
-
return [
|
|
63
|
+
return [sentinelRef, viewportRef];
|
|
60
64
|
};
|
|
61
65
|
|
|
62
66
|
export { useInfiniteScroll };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type
|
|
1
|
+
type UsingIsMounted = {
|
|
2
2
|
/**
|
|
3
3
|
* @description
|
|
4
4
|
* after mount, react will rerender current component
|
|
@@ -9,5 +9,5 @@ type UseIsMountedBy = {
|
|
|
9
9
|
* @description
|
|
10
10
|
* `useIsMounted`
|
|
11
11
|
*/
|
|
12
|
-
export declare const useIsMounted: ({ rerender }?:
|
|
12
|
+
export declare const useIsMounted: ({ rerender }?: UsingIsMounted) => () => boolean;
|
|
13
13
|
export {};
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
4
4
|
var react = require('react');
|
|
5
5
|
var toArray = require('../utils/to-array.cjs');
|
|
6
|
-
var useEvent = require('./use-event.cjs');
|
|
7
6
|
var replace = require('../utils/replace.cjs');
|
|
8
7
|
|
|
9
8
|
var useMediaQuery = function useMediaQuery(query) {
|
|
@@ -13,24 +12,21 @@ var useMediaQuery = function useMediaQuery(query) {
|
|
|
13
12
|
var queries = toArray.toArray(query);
|
|
14
13
|
var _useState = react.useState(function () {
|
|
15
14
|
return queries.map(function (query) {
|
|
16
|
-
var
|
|
17
|
-
return !!((
|
|
15
|
+
var _globalThis$window$ma, _globalThis$window;
|
|
16
|
+
return !!((_globalThis$window$ma = (_globalThis$window = globalThis.window).matchMedia) !== null && _globalThis$window$ma !== void 0 && (_globalThis$window$ma = _globalThis$window$ma.call(_globalThis$window, query)) !== null && _globalThis$window$ma !== void 0 && _globalThis$window$ma.matches);
|
|
18
17
|
});
|
|
19
18
|
}),
|
|
20
19
|
_useState2 = _slicedToArray(_useState, 2),
|
|
21
20
|
value = _useState2[0],
|
|
22
21
|
setValue = _useState2[1];
|
|
23
|
-
var onMediaQueryChange =
|
|
22
|
+
var onMediaQueryChange = react.useCallback(function (event, index) {
|
|
24
23
|
setValue(function (prev) {
|
|
25
|
-
return replace.replace(prev,
|
|
26
|
-
index: index,
|
|
27
|
-
replaceValue: event.matches
|
|
28
|
-
});
|
|
24
|
+
return replace.replace(prev, event.matches, index);
|
|
29
25
|
});
|
|
30
|
-
});
|
|
26
|
+
}, []);
|
|
31
27
|
react.useEffect(function () {
|
|
32
28
|
var mediaQueries = queries.map(function (query) {
|
|
33
|
-
return window.matchMedia(query);
|
|
29
|
+
return globalThis.window.matchMedia(query);
|
|
34
30
|
});
|
|
35
31
|
// listen media query change
|
|
36
32
|
var cleaners = mediaQueries.map(function (mediaQuery, index) {
|
|
@@ -39,13 +35,13 @@ var useMediaQuery = function useMediaQuery(query) {
|
|
|
39
35
|
};
|
|
40
36
|
mediaQuery.addEventListener("change", listener);
|
|
41
37
|
return function () {
|
|
42
|
-
|
|
38
|
+
mediaQuery.removeEventListener("change", listener);
|
|
43
39
|
};
|
|
44
40
|
});
|
|
45
41
|
// clean up all listeners
|
|
46
42
|
return function () {
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
cleaners.forEach(function (cleaner) {
|
|
44
|
+
cleaner();
|
|
49
45
|
});
|
|
50
46
|
};
|
|
51
47
|
}, [_query]);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
|
-
import { useMemo, useState, useEffect } from 'react';
|
|
2
|
+
import { useMemo, useState, useCallback, useEffect } from 'react';
|
|
3
3
|
import { toArray } from '../utils/to-array.mjs';
|
|
4
|
-
import { useEvent } from './use-event.mjs';
|
|
5
4
|
import { replace } from '../utils/replace.mjs';
|
|
6
5
|
|
|
7
6
|
var useMediaQuery = function useMediaQuery(query) {
|
|
@@ -11,24 +10,21 @@ var useMediaQuery = function useMediaQuery(query) {
|
|
|
11
10
|
var queries = toArray(query);
|
|
12
11
|
var _useState = useState(function () {
|
|
13
12
|
return queries.map(function (query) {
|
|
14
|
-
var
|
|
15
|
-
return !!((
|
|
13
|
+
var _globalThis$window$ma, _globalThis$window;
|
|
14
|
+
return !!((_globalThis$window$ma = (_globalThis$window = globalThis.window).matchMedia) !== null && _globalThis$window$ma !== void 0 && (_globalThis$window$ma = _globalThis$window$ma.call(_globalThis$window, query)) !== null && _globalThis$window$ma !== void 0 && _globalThis$window$ma.matches);
|
|
16
15
|
});
|
|
17
16
|
}),
|
|
18
17
|
_useState2 = _slicedToArray(_useState, 2),
|
|
19
18
|
value = _useState2[0],
|
|
20
19
|
setValue = _useState2[1];
|
|
21
|
-
var onMediaQueryChange =
|
|
20
|
+
var onMediaQueryChange = useCallback(function (event, index) {
|
|
22
21
|
setValue(function (prev) {
|
|
23
|
-
return replace(prev,
|
|
24
|
-
index: index,
|
|
25
|
-
replaceValue: event.matches
|
|
26
|
-
});
|
|
22
|
+
return replace(prev, event.matches, index);
|
|
27
23
|
});
|
|
28
|
-
});
|
|
24
|
+
}, []);
|
|
29
25
|
useEffect(function () {
|
|
30
26
|
var mediaQueries = queries.map(function (query) {
|
|
31
|
-
return window.matchMedia(query);
|
|
27
|
+
return globalThis.window.matchMedia(query);
|
|
32
28
|
});
|
|
33
29
|
// listen media query change
|
|
34
30
|
var cleaners = mediaQueries.map(function (mediaQuery, index) {
|
|
@@ -37,13 +33,13 @@ var useMediaQuery = function useMediaQuery(query) {
|
|
|
37
33
|
};
|
|
38
34
|
mediaQuery.addEventListener("change", listener);
|
|
39
35
|
return function () {
|
|
40
|
-
|
|
36
|
+
mediaQuery.removeEventListener("change", listener);
|
|
41
37
|
};
|
|
42
38
|
});
|
|
43
39
|
// clean up all listeners
|
|
44
40
|
return function () {
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
cleaners.forEach(function (cleaner) {
|
|
42
|
+
cleaner();
|
|
47
43
|
});
|
|
48
44
|
};
|
|
49
45
|
}, [_query]);
|
package/dist/hooks/use-raf.d.ts
CHANGED
|
@@ -32,22 +32,7 @@ var useThrottler = function useThrottler(throttler) {
|
|
|
32
32
|
pipe: pipe
|
|
33
33
|
};
|
|
34
34
|
};
|
|
35
|
-
|
|
36
|
-
* @author murukal
|
|
37
|
-
*
|
|
38
|
-
* @description
|
|
39
|
-
* throttle callback
|
|
40
|
-
*
|
|
41
|
-
* @param callback
|
|
42
|
-
* @param wait number
|
|
43
|
-
* @description
|
|
44
|
-
* The wait time (in milliseconds) until the throttle function is called.
|
|
45
|
-
* default 1000
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* 1000
|
|
49
|
-
*/
|
|
50
|
-
var useThrottleCallback = function useThrottleCallback(throttler) {
|
|
35
|
+
function useThrottleCallback(throttler) {
|
|
51
36
|
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
52
37
|
var throttled = react.useRef(null);
|
|
53
38
|
var _useThrottler = useThrottler(throttler),
|
|
@@ -84,6 +69,6 @@ var useThrottleCallback = function useThrottleCallback(throttler) {
|
|
|
84
69
|
}
|
|
85
70
|
};
|
|
86
71
|
});
|
|
87
|
-
}
|
|
72
|
+
}
|
|
88
73
|
|
|
89
74
|
exports.useThrottleCallback = useThrottleCallback;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Throttler, type Throttled } from "../utils/throttle";
|
|
2
|
-
import {
|
|
2
|
+
import type { AnyFunction } from "@aiszlab/relax/types";
|
|
3
3
|
/**
|
|
4
4
|
* @author murukal
|
|
5
5
|
*
|
|
@@ -15,4 +15,6 @@ import { type Callable } from "./use-event";
|
|
|
15
15
|
* @example
|
|
16
16
|
* 1000
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
declare function useThrottleCallback<T extends AnyFunction>(callback: T, wait: number): Throttled<T>;
|
|
19
|
+
declare function useThrottleCallback<T extends AnyFunction, R>(throttler: Throttler<T, R>, wait: number): Throttled<T>;
|
|
20
|
+
export { useThrottleCallback };
|
|
@@ -30,22 +30,7 @@ var useThrottler = function useThrottler(throttler) {
|
|
|
30
30
|
pipe: pipe
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
|
-
|
|
34
|
-
* @author murukal
|
|
35
|
-
*
|
|
36
|
-
* @description
|
|
37
|
-
* throttle callback
|
|
38
|
-
*
|
|
39
|
-
* @param callback
|
|
40
|
-
* @param wait number
|
|
41
|
-
* @description
|
|
42
|
-
* The wait time (in milliseconds) until the throttle function is called.
|
|
43
|
-
* default 1000
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* 1000
|
|
47
|
-
*/
|
|
48
|
-
var useThrottleCallback = function useThrottleCallback(throttler) {
|
|
33
|
+
function useThrottleCallback(throttler) {
|
|
49
34
|
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
50
35
|
var throttled = useRef(null);
|
|
51
36
|
var _useThrottler = useThrottler(throttler),
|
|
@@ -82,6 +67,6 @@ var useThrottleCallback = function useThrottleCallback(throttler) {
|
|
|
82
67
|
}
|
|
83
68
|
};
|
|
84
69
|
});
|
|
85
|
-
}
|
|
70
|
+
}
|
|
86
71
|
|
|
87
72
|
export { useThrottleCallback };
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import type { AnyFunction } from "@aiszlab/relax/types";
|
|
1
2
|
/**
|
|
2
3
|
* @author murukal
|
|
3
4
|
*
|
|
4
5
|
* @description
|
|
5
6
|
* timeout effect
|
|
6
7
|
*/
|
|
7
|
-
export declare const useTimeout: (callback:
|
|
8
|
+
export declare const useTimeout: (callback: AnyFunction, wait: number) => {
|
|
8
9
|
flush: () => void;
|
|
9
10
|
cancel: () => void;
|
|
10
11
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SetStateAction } from "react";
|
|
2
|
+
type Callback<S> = (state: S) => void;
|
|
3
|
+
type StateSetter<S> = (state: SetStateAction<S>, callback?: Callback<S>) => void;
|
|
4
|
+
type UsedUpdateState<S> = [S, StateSetter<S>];
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* in class component
|
|
8
|
+
* use setState second parameter callback to get the latest state
|
|
9
|
+
*/
|
|
10
|
+
declare function useUpdateState<S = undefined>(): UsedUpdateState<S | undefined>;
|
|
11
|
+
declare function useUpdateState<S>(initialState: S): UsedUpdateState<S>;
|
|
12
|
+
export { useUpdateState };
|