@react-aria/utils 3.16.0 → 3.18.0
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/import.mjs +102 -41
- package/dist/main.js +102 -39
- package/dist/main.js.map +1 -1
- package/dist/module.js +102 -41
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +2 -0
- package/src/useDeepMemo.ts +27 -0
- package/src/useDrag1D.ts +2 -0
- package/src/useEffectEvent.ts +25 -0
- package/src/useEvent.ts +6 -9
- package/src/useLabels.ts +1 -1
- package/src/useObjectRef.ts +12 -27
- package/src/useUpdateEffect.ts +10 -1
- package/src/useValueEffect.ts +22 -19
package/src/useObjectRef.ts
CHANGED
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {MutableRefObject, useRef} from 'react';
|
|
14
|
-
import {useLayoutEffect} from './';
|
|
13
|
+
import {MutableRefObject, useMemo, useRef} from 'react';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Offers an object ref for a given callback ref or an object ref. Especially
|
|
@@ -24,31 +23,17 @@ import {useLayoutEffect} from './';
|
|
|
24
23
|
*/
|
|
25
24
|
export function useObjectRef<T>(forwardedRef?: ((instance: T | null) => void) | MutableRefObject<T | null> | null): MutableRefObject<T> {
|
|
26
25
|
const objRef = useRef<T>();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
useLayoutEffect(() => {
|
|
34
|
-
if (!forwardedRef) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (typeof forwardedRef === 'function') {
|
|
39
|
-
forwardedRef(objRef.current);
|
|
40
|
-
} else {
|
|
41
|
-
forwardedRef.current = objRef.current;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return () => {
|
|
26
|
+
return useMemo(() => ({
|
|
27
|
+
get current() {
|
|
28
|
+
return objRef.current;
|
|
29
|
+
},
|
|
30
|
+
set current(value) {
|
|
31
|
+
objRef.current = value;
|
|
45
32
|
if (typeof forwardedRef === 'function') {
|
|
46
|
-
forwardedRef(
|
|
47
|
-
} else {
|
|
48
|
-
forwardedRef.current =
|
|
33
|
+
forwardedRef(value);
|
|
34
|
+
} else if (forwardedRef) {
|
|
35
|
+
forwardedRef.current = value;
|
|
49
36
|
}
|
|
50
|
-
}
|
|
51
|
-
}, [forwardedRef]);
|
|
52
|
-
|
|
53
|
-
return objRef;
|
|
37
|
+
}
|
|
38
|
+
}), [forwardedRef]);
|
|
54
39
|
}
|
package/src/useUpdateEffect.ts
CHANGED
|
@@ -15,13 +15,22 @@ import {EffectCallback, useEffect, useRef} from 'react';
|
|
|
15
15
|
// Like useEffect, but only called for updates after the initial render.
|
|
16
16
|
export function useUpdateEffect(effect: EffectCallback, dependencies: any[]) {
|
|
17
17
|
const isInitialMount = useRef(true);
|
|
18
|
+
const lastDeps = useRef<any[] | null>(null);
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
isInitialMount.current = true;
|
|
22
|
+
return () => {
|
|
23
|
+
isInitialMount.current = false;
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
18
26
|
|
|
19
27
|
useEffect(() => {
|
|
20
28
|
if (isInitialMount.current) {
|
|
21
29
|
isInitialMount.current = false;
|
|
22
|
-
} else {
|
|
30
|
+
} else if (!lastDeps.current || dependencies.some((dep, i) => !Object.is(dep, lastDeps[i]))) {
|
|
23
31
|
effect();
|
|
24
32
|
}
|
|
33
|
+
lastDeps.current = dependencies;
|
|
25
34
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
26
35
|
}, dependencies);
|
|
27
36
|
}
|
package/src/useValueEffect.ts
CHANGED
|
@@ -24,42 +24,45 @@ export function useValueEffect<S>(defaultValue: S | (() => S)): [S, Dispatch<Set
|
|
|
24
24
|
let valueRef = useRef(value);
|
|
25
25
|
let effect = useRef(null);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// Store the function in a ref so we can always access the current version
|
|
30
|
-
// which has the proper `value` in scope.
|
|
31
|
-
let nextRef = useRef(null);
|
|
32
|
-
nextRef.current = () => {
|
|
27
|
+
// Must be stable so that `queue` is stable.
|
|
28
|
+
let nextIter = useCallback(() => {
|
|
33
29
|
// Run the generator to the next yield.
|
|
34
30
|
let newValue = effect.current.next();
|
|
35
|
-
|
|
31
|
+
while (!newValue.done && valueRef.current === newValue.value) {
|
|
32
|
+
// If the value is the same as the current value,
|
|
33
|
+
// then continue to the next yield. Otherwise,
|
|
34
|
+
// set the value in state and wait for the next layout effect.
|
|
35
|
+
newValue = effect.current.next();
|
|
36
|
+
}
|
|
36
37
|
// If the generator is done, reset the effect.
|
|
37
38
|
if (newValue.done) {
|
|
38
39
|
effect.current = null;
|
|
39
40
|
return;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
};
|
|
43
|
+
// Always update valueRef when setting the state.
|
|
44
|
+
// This is needed because the function is not regenerated with the new state value since
|
|
45
|
+
// they must be stable across renders. Instead, it gets carried in the ref, but the setState
|
|
46
|
+
// is also needed in order to cause a rerender.
|
|
47
|
+
setValue(newValue.value);
|
|
48
|
+
valueRef.current = newValue.value;
|
|
49
|
+
// this list of dependencies is stable, setState and refs never change after first render.
|
|
50
|
+
}, [setValue, valueRef, effect]);
|
|
51
51
|
|
|
52
52
|
useLayoutEffect(() => {
|
|
53
53
|
// If there is an effect currently running, continue to the next yield.
|
|
54
54
|
if (effect.current) {
|
|
55
|
-
|
|
55
|
+
nextIter();
|
|
56
56
|
}
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
// queue must be a stable function, much like setState.
|
|
59
60
|
let queue = useCallback(fn => {
|
|
60
61
|
effect.current = fn(valueRef.current);
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
nextIter();
|
|
63
|
+
// this list of dependencies is stable, setState and refs never change after first render.
|
|
64
|
+
// in addition, nextIter is stable as outlined above
|
|
65
|
+
}, [nextIter, effect, valueRef]);
|
|
63
66
|
|
|
64
67
|
return [value, queue];
|
|
65
68
|
}
|