@react-aria/utils 3.18.0 → 3.20.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 +74 -60
- package/dist/main.js +74 -59
- package/dist/main.js.map +1 -1
- package/dist/module.js +74 -60
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +1 -0
- package/src/isVirtualEvent.ts +1 -1
- package/src/useFormReset.ts +34 -0
- package/src/useLayoutEffect.ts +1 -1
- package/src/useValueEffect.ts +20 -27
- package/src/useViewportSize.ts +1 -1
package/src/useValueEffect.ts
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {Dispatch,
|
|
14
|
-
import {useLayoutEffect} from './';
|
|
13
|
+
import {Dispatch, useRef, useState} from 'react';
|
|
14
|
+
import {useEffectEvent, useLayoutEffect} from './';
|
|
15
15
|
|
|
16
16
|
type SetValueAction<S> = (prev: S) => Generator<any, void, unknown>;
|
|
17
17
|
|
|
@@ -21,48 +21,41 @@ type SetValueAction<S> = (prev: S) => Generator<any, void, unknown>;
|
|
|
21
21
|
// written linearly.
|
|
22
22
|
export function useValueEffect<S>(defaultValue: S | (() => S)): [S, Dispatch<SetValueAction<S>>] {
|
|
23
23
|
let [value, setValue] = useState(defaultValue);
|
|
24
|
-
let valueRef = useRef(value);
|
|
25
24
|
let effect = useRef(null);
|
|
26
25
|
|
|
27
|
-
//
|
|
28
|
-
|
|
26
|
+
// Store the function in a ref so we can always access the current version
|
|
27
|
+
// which has the proper `value` in scope.
|
|
28
|
+
let nextRef = useEffectEvent(() => {
|
|
29
29
|
// Run the generator to the next yield.
|
|
30
30
|
let newValue = effect.current.next();
|
|
31
|
-
|
|
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
|
-
}
|
|
31
|
+
|
|
37
32
|
// If the generator is done, reset the effect.
|
|
38
33
|
if (newValue.done) {
|
|
39
34
|
effect.current = null;
|
|
40
35
|
return;
|
|
41
36
|
}
|
|
42
37
|
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
// If the value is the same as the current value,
|
|
39
|
+
// then continue to the next yield. Otherwise,
|
|
40
|
+
// set the value in state and wait for the next layout effect.
|
|
41
|
+
if (value === newValue.value) {
|
|
42
|
+
nextRef();
|
|
43
|
+
} else {
|
|
44
|
+
setValue(newValue.value);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
51
47
|
|
|
52
48
|
useLayoutEffect(() => {
|
|
53
49
|
// If there is an effect currently running, continue to the next yield.
|
|
54
50
|
if (effect.current) {
|
|
55
|
-
|
|
51
|
+
nextRef();
|
|
56
52
|
}
|
|
57
53
|
});
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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]);
|
|
55
|
+
let queue = useEffectEvent(fn => {
|
|
56
|
+
effect.current = fn(value);
|
|
57
|
+
nextRef();
|
|
58
|
+
});
|
|
66
59
|
|
|
67
60
|
return [value, queue];
|
|
68
61
|
}
|
package/src/useViewportSize.ts
CHANGED
|
@@ -18,7 +18,7 @@ interface ViewportSize {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
// @ts-ignore
|
|
21
|
-
let visualViewport = typeof
|
|
21
|
+
let visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
22
22
|
|
|
23
23
|
export function useViewportSize(): ViewportSize {
|
|
24
24
|
let [size, setSize] = useState(() => getViewportSize());
|