@laser-ui/hooks 1.6.1 → 2.0.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/CHANGELOG.md +13 -0
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/package.json +2 -2
- package/useAsync.d.ts +1 -0
- package/useAsync.js +23 -0
- package/useForceUpdate.d.ts +1 -1
- package/useRefExtra.d.ts +3 -3
- package/useRefExtra.js +35 -6
- package/useResize.js +11 -13
- package/useForkRef.d.ts +0 -1
- package/useForkRef.js +0 -19
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.0.1](https://github.com/laser-ui/laser-ui/compare/v2.0.0...v2.0.1) (2024-12-17)
|
|
6
|
+
|
|
7
|
+
### Bug Fixes
|
|
8
|
+
|
|
9
|
+
- **hooks:** fix cant get ref after render by `useRefExtra` ([18c6aaf](https://github.com/laser-ui/laser-ui/commit/18c6aaffadf1f1e06dbb5c0a0a8642a7df8c2ed2))
|
|
10
|
+
|
|
11
|
+
# [2.0.0](https://github.com/laser-ui/laser-ui/compare/v1.6.1...v2.0.0) (2024-12-17)
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
- **hooks:** add `setAfterPainted` method to `useAsync` ([5b53b6a](https://github.com/laser-ui/laser-ui/commit/5b53b6a4d5fd75b01105debadc32f87860854f59))
|
|
16
|
+
- **hooks:** support callback after painted ([ed54a01](https://github.com/laser-ui/laser-ui/commit/ed54a01d8b099f2206ad2f64483a60af2df6a456))
|
|
17
|
+
|
|
5
18
|
## [1.6.1](https://github.com/laser-ui/laser-ui/compare/v1.6.0...v1.6.1) (2024-11-17)
|
|
6
19
|
|
|
7
20
|
**Note:** Version bump only for package @laser-ui/hooks
|
package/index.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ export { useAsync } from './useAsync';
|
|
|
2
2
|
export { useEvent } from './useEvent';
|
|
3
3
|
export { useEventCallback } from './useEventCallback';
|
|
4
4
|
export { useForceUpdate } from './useForceUpdate';
|
|
5
|
-
export { useForkRef } from './useForkRef';
|
|
6
5
|
export { useImmer } from './useImmer';
|
|
7
6
|
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
8
7
|
export { useMount } from './useMount';
|
package/index.js
CHANGED
|
@@ -2,7 +2,6 @@ export { useAsync } from './useAsync';
|
|
|
2
2
|
export { useEvent } from './useEvent';
|
|
3
3
|
export { useEventCallback } from './useEventCallback';
|
|
4
4
|
export { useForceUpdate } from './useForceUpdate';
|
|
5
|
-
export { useForkRef } from './useForkRef';
|
|
6
5
|
export { useImmer } from './useImmer';
|
|
7
6
|
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
8
7
|
export { useMount } from './useMount';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laser-ui/hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Shared hooks used by Laser UI packages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ui",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"access": "public",
|
|
36
36
|
"directory": "../../dist/libs/hooks"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "5198c1835aa2a718c200cf61181fb372e3f54ed1"
|
|
39
39
|
}
|
package/useAsync.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ declare class AsyncInstance {
|
|
|
3
3
|
clearAll(): void;
|
|
4
4
|
setTimeout(handler: TimerHandler, timeout?: number, clearFn?: () => void): () => void;
|
|
5
5
|
requestAnimationFrame(cb: FrameRequestCallback, clearFn?: () => void): () => void;
|
|
6
|
+
setAfterPainted(cb: () => void, clearFn?: () => void): () => void;
|
|
6
7
|
}
|
|
7
8
|
export declare class Async extends AsyncInstance {
|
|
8
9
|
private instances;
|
package/useAsync.js
CHANGED
|
@@ -32,6 +32,29 @@ class AsyncInstance {
|
|
|
32
32
|
this.clearFns.add(clear);
|
|
33
33
|
return clear;
|
|
34
34
|
}
|
|
35
|
+
setAfterPainted(cb, clearFn) {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
37
|
+
let close = () => { };
|
|
38
|
+
const tid = requestAnimationFrame(() => {
|
|
39
|
+
const messageChannel = new MessageChannel();
|
|
40
|
+
close = () => {
|
|
41
|
+
messageChannel.port1.close();
|
|
42
|
+
messageChannel.port2.close();
|
|
43
|
+
};
|
|
44
|
+
messageChannel.port1.onmessage = () => {
|
|
45
|
+
close();
|
|
46
|
+
cb();
|
|
47
|
+
};
|
|
48
|
+
messageChannel.port2.postMessage(undefined);
|
|
49
|
+
});
|
|
50
|
+
const clear = () => {
|
|
51
|
+
clearFn === null || clearFn === void 0 ? void 0 : clearFn();
|
|
52
|
+
cancelAnimationFrame(tid);
|
|
53
|
+
close();
|
|
54
|
+
};
|
|
55
|
+
this.clearFns.add(clear);
|
|
56
|
+
return clear;
|
|
57
|
+
}
|
|
35
58
|
}
|
|
36
59
|
export class Async extends AsyncInstance {
|
|
37
60
|
constructor() {
|
package/useForceUpdate.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function useForceUpdate(): import("react").
|
|
1
|
+
export declare function useForceUpdate(): import("react").ActionDispatch<[]>;
|
package/useRefExtra.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type RefExtra<T = HTMLElement> = (() => T | null) | string;
|
|
2
|
-
export declare function useRefExtra(refExtra?: string): React.RefObject<HTMLElement>;
|
|
3
|
-
export declare function useRefExtra<T = HTMLElement>(refExtra?: () => T | null): React.RefObject<T>;
|
|
4
|
-
export declare function useRefExtra<T = HTMLElement>(refExtra?: RefExtra<T>): React.RefObject<T |
|
|
2
|
+
export declare function useRefExtra(refExtra?: string): React.RefObject<HTMLElement | null>;
|
|
3
|
+
export declare function useRefExtra<T = HTMLElement>(refExtra?: () => T | null): React.RefObject<T | null>;
|
|
4
|
+
export declare function useRefExtra<T = HTMLElement>(refExtra?: RefExtra<T>): React.RefObject<T | null>;
|
package/useRefExtra.js
CHANGED
|
@@ -1,13 +1,42 @@
|
|
|
1
|
-
import { isString, isUndefined } from 'lodash';
|
|
2
|
-
import {
|
|
1
|
+
import { isNull, isString, isUndefined } from 'lodash';
|
|
2
|
+
import { useMemo } from 'react';
|
|
3
3
|
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
4
|
+
class Ref {
|
|
5
|
+
constructor() {
|
|
6
|
+
Object.defineProperty(this, "_current", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: null
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "refExtra", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
get current() {
|
|
20
|
+
if (isNull(this._current)) {
|
|
21
|
+
this.getCurrent();
|
|
22
|
+
}
|
|
23
|
+
return this._current;
|
|
24
|
+
}
|
|
25
|
+
getCurrent() {
|
|
26
|
+
if (!isUndefined(this.refExtra)) {
|
|
27
|
+
this._current = isString(this.refExtra) ? document.querySelector(this.refExtra) : this.refExtra();
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this._current = null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
4
34
|
export function useRefExtra(refExtra) {
|
|
5
|
-
const ref =
|
|
35
|
+
const ref = useMemo(() => new Ref(), []);
|
|
36
|
+
ref.refExtra = refExtra;
|
|
6
37
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7
38
|
useIsomorphicLayoutEffect(() => {
|
|
8
|
-
|
|
9
|
-
ref.current = isString(refExtra) ? document.querySelector(refExtra) : refExtra();
|
|
10
|
-
}
|
|
39
|
+
ref.getCurrent();
|
|
11
40
|
});
|
|
12
41
|
return ref;
|
|
13
42
|
}
|
package/useResize.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isUndefined } from 'lodash';
|
|
2
2
|
import { useEffect, useRef } from 'react';
|
|
3
|
-
import { flushSync } from 'react-dom';
|
|
4
3
|
let checkResize = (cb1, cb2, entry) => {
|
|
5
4
|
if ('borderBoxSize' in entry) {
|
|
6
5
|
checkResize = (cb1) => {
|
|
@@ -15,30 +14,29 @@ let checkResize = (cb1, cb2, entry) => {
|
|
|
15
14
|
};
|
|
16
15
|
export function useResize(target, cb, options, disabled = false) {
|
|
17
16
|
const { skipEmpty = true } = options !== null && options !== void 0 ? options : {};
|
|
18
|
-
const
|
|
17
|
+
const prevContentRect = useRef(undefined);
|
|
19
18
|
if (disabled) {
|
|
20
|
-
|
|
19
|
+
prevContentRect.current = undefined;
|
|
21
20
|
}
|
|
22
21
|
useEffect(() => {
|
|
23
22
|
if (target.current && !disabled) {
|
|
24
23
|
const observer = new ResizeObserver((entries, observer) => {
|
|
25
24
|
const entry = entries[0];
|
|
26
25
|
checkResize(() => {
|
|
27
|
-
if (!isUndefined(
|
|
26
|
+
if (!isUndefined(prevContentRect.current) &&
|
|
28
27
|
!(skipEmpty && entry.borderBoxSize[0].blockSize === 0 && entry.borderBoxSize[0].inlineSize === 0) &&
|
|
29
|
-
(
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
(prevContentRect.current.width !== entry.borderBoxSize[0].inlineSize ||
|
|
29
|
+
prevContentRect.current.height !== entry.borderBoxSize[0].blockSize)) {
|
|
30
|
+
cb === null || cb === void 0 ? void 0 : cb(entries, observer);
|
|
32
31
|
}
|
|
33
|
-
|
|
32
|
+
prevContentRect.current = { width: entry.borderBoxSize[0].inlineSize, height: entry.borderBoxSize[0].blockSize };
|
|
34
33
|
}, () => {
|
|
35
|
-
if (!isUndefined(
|
|
34
|
+
if (!isUndefined(prevContentRect.current) &&
|
|
36
35
|
!(skipEmpty && entry.contentRect.width === 0 && entry.contentRect.height === 0) &&
|
|
37
|
-
(
|
|
38
|
-
|
|
39
|
-
flushSync(() => cb === null || cb === void 0 ? void 0 : cb(entries, observer));
|
|
36
|
+
(prevContentRect.current.width !== entry.contentRect.width || prevContentRect.current.height !== entry.contentRect.height)) {
|
|
37
|
+
cb === null || cb === void 0 ? void 0 : cb(entries, observer);
|
|
40
38
|
}
|
|
41
|
-
|
|
39
|
+
prevContentRect.current = { width: entry.contentRect.width, height: entry.contentRect.height };
|
|
42
40
|
}, entry);
|
|
43
41
|
});
|
|
44
42
|
observer.observe(target.current);
|
package/useForkRef.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function useForkRef<T>(...refs: (React.ForwardedRef<T> | undefined)[]): React.RefCallback<T>;
|
package/useForkRef.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { isFunction, isObject } from 'lodash';
|
|
2
|
-
import { useCallback } from 'react';
|
|
3
|
-
function setRef(ref, value) {
|
|
4
|
-
if (isFunction(ref)) {
|
|
5
|
-
ref(value);
|
|
6
|
-
}
|
|
7
|
-
else if (isObject(ref) && 'current' in ref) {
|
|
8
|
-
ref['current'] = value;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export function useForkRef(...refs) {
|
|
12
|
-
return useCallback((refValue) => {
|
|
13
|
-
refs.forEach((ref) => {
|
|
14
|
-
setRef(ref, refValue);
|
|
15
|
-
});
|
|
16
|
-
},
|
|
17
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
-
[...refs]);
|
|
19
|
-
}
|