@react-aria/utils 3.17.0 → 3.19.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/src/useLabels.ts CHANGED
@@ -29,7 +29,7 @@ export function useLabels(props: DOMProps & AriaLabelingProps, defaultLabel?: st
29
29
  // combine them by pointing to the element itself.
30
30
  id = useId(id);
31
31
  if (labelledBy && label) {
32
- let ids = new Set([...labelledBy.trim().split(/\s+/), id]);
32
+ let ids = new Set([id, ...labelledBy.trim().split(/\s+/)]);
33
33
  labelledBy = [...ids].join(' ');
34
34
  } else if (labelledBy) {
35
35
  labelledBy = labelledBy.trim().split(/\s+/).join(' ');
@@ -15,6 +15,6 @@ import React from 'react';
15
15
  // During SSR, React emits a warning when calling useLayoutEffect.
16
16
  // Since neither useLayoutEffect nor useEffect run on the server,
17
17
  // we can suppress this by replace it with a noop on the server.
18
- export const useLayoutEffect = typeof window !== 'undefined'
18
+ export const useLayoutEffect = typeof document !== 'undefined'
19
19
  ? React.useLayoutEffect
20
20
  : () => {};
@@ -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
- * We're using `useLayoutEffect` here instead of `useEffect` because we want
30
- * to make sure that the `ref` value is up to date before other places in the
31
- * the execution cycle try to read it.
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(null);
47
- } else {
48
- forwardedRef.current = null;
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
  }
@@ -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
  }
@@ -18,7 +18,7 @@ interface ViewportSize {
18
18
  }
19
19
 
20
20
  // @ts-ignore
21
- let visualViewport = typeof window !== 'undefined' && window.visualViewport;
21
+ let visualViewport = typeof document !== 'undefined' && window.visualViewport;
22
22
 
23
23
  export function useViewportSize(): ViewportSize {
24
24
  let [size, setSize] = useState(() => getViewportSize());