@react-aria/utils 3.19.0 → 3.21.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.
@@ -47,7 +47,7 @@ export function isVirtualPointerEvent(event: PointerEvent) {
47
47
  // instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216. event.pointerType === 'mouse' is to distingush
48
48
  // Talkback double tap from Windows Firefox touch screen press
49
49
  return (
50
- (event.width === 0 && event.height === 0) ||
50
+ (!isAndroid() && event.width === 0 && event.height === 0) ||
51
51
  (event.width === 1 &&
52
52
  event.height === 1 &&
53
53
  event.pressure === 0 &&
@@ -0,0 +1,154 @@
1
+ /*
2
+ * Copyright 2023 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {focusWithoutScrolling, isMac, isWebKit} from './index';
14
+ import {isFirefox, isIPad} from './platform';
15
+ import {LinkDOMProps} from '@react-types/shared';
16
+ import React, {createContext, ReactNode, useContext, useMemo} from 'react';
17
+
18
+ interface Router {
19
+ isNative: boolean,
20
+ open: (target: Element, modifiers: Modifiers) => void
21
+ }
22
+
23
+ const RouterContext = createContext<Router>({
24
+ isNative: true,
25
+ open: openSyntheticLink
26
+ });
27
+
28
+ interface RouterProviderProps {
29
+ navigate: (path: string) => void,
30
+ children: ReactNode
31
+ }
32
+
33
+ /**
34
+ * A RouterProvider accepts a `navigate` function from a framework or client side router,
35
+ * and provides it to all nested React Aria links to enable client side navigation.
36
+ */
37
+ export function RouterProvider(props: RouterProviderProps) {
38
+ let {children, navigate} = props;
39
+
40
+ let ctx = useMemo(() => ({
41
+ isNative: false,
42
+ open: (target: Element, modifiers: Modifiers) => {
43
+ getSyntheticLink(target, link => {
44
+ if (shouldClientNavigate(link, modifiers)) {
45
+ navigate(link.pathname + link.search + link.hash);
46
+ } else {
47
+ openLink(link, modifiers);
48
+ }
49
+ });
50
+ }
51
+ }), [navigate]);
52
+
53
+ return (
54
+ <RouterContext.Provider value={ctx}>
55
+ {children}
56
+ </RouterContext.Provider>
57
+ );
58
+ }
59
+
60
+ export function useRouter(): Router {
61
+ return useContext(RouterContext);
62
+ }
63
+
64
+ interface Modifiers {
65
+ metaKey?: boolean,
66
+ ctrlKey?: boolean,
67
+ altKey?: boolean,
68
+ shiftKey?: boolean
69
+ }
70
+
71
+ export function shouldClientNavigate(link: HTMLAnchorElement, modifiers: Modifiers) {
72
+ // Use getAttribute here instead of link.target. Firefox will default link.target to "_parent" when inside an iframe.
73
+ let target = link.getAttribute('target');
74
+ return (
75
+ (!target || target === '_self') &&
76
+ link.origin === location.origin &&
77
+ !link.hasAttribute('download') &&
78
+ !modifiers.metaKey && // open in new tab (mac)
79
+ !modifiers.ctrlKey && // open in new tab (windows)
80
+ !modifiers.altKey && // download
81
+ !modifiers.shiftKey
82
+ );
83
+ }
84
+
85
+ export function openLink(target: HTMLAnchorElement, modifiers: Modifiers, setOpening = true) {
86
+ let {metaKey, ctrlKey, altKey, shiftKey} = modifiers;
87
+
88
+ // Firefox does not recognize keyboard events as a user action by default, and the popup blocker
89
+ // will prevent links with target="_blank" from opening. However, it does allow the event if the
90
+ // Command/Control key is held, which opens the link in a background tab. This seems like the best we can do.
91
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=257870 and https://bugzilla.mozilla.org/show_bug.cgi?id=746640.
92
+ if (isFirefox() && window.event?.type?.startsWith('key') && target.target === '_blank') {
93
+ if (isMac()) {
94
+ metaKey = true;
95
+ } else {
96
+ ctrlKey = true;
97
+ }
98
+ }
99
+
100
+ // WebKit does not support firing click events with modifier keys, but does support keyboard events.
101
+ // https://github.com/WebKit/WebKit/blob/c03d0ac6e6db178f90923a0a63080b5ca210d25f/Source/WebCore/html/HTMLAnchorElement.cpp#L184
102
+ let event = isWebKit() && isMac() && !isIPad() && process.env.NODE_ENV !== 'test'
103
+ // @ts-ignore - keyIdentifier is a non-standard property, but it's what webkit expects
104
+ ? new KeyboardEvent('keydown', {keyIdentifier: 'Enter', metaKey, ctrlKey, altKey, shiftKey})
105
+ : new MouseEvent('click', {metaKey, ctrlKey, altKey, shiftKey, bubbles: true, cancelable: true});
106
+ openLink.isOpening = setOpening;
107
+ focusWithoutScrolling(target);
108
+ target.dispatchEvent(event);
109
+ openLink.isOpening = false;
110
+ }
111
+
112
+ openLink.isOpening = false;
113
+
114
+ function getSyntheticLink(target: Element, open: (link: HTMLAnchorElement) => void) {
115
+ if (target instanceof HTMLAnchorElement) {
116
+ open(target);
117
+ } else if (target.hasAttribute('data-href')) {
118
+ let link = document.createElement('a');
119
+ link.href = target.getAttribute('data-href');
120
+ if (target.hasAttribute('data-target')) {
121
+ link.target = target.getAttribute('data-target');
122
+ }
123
+ if (target.hasAttribute('data-rel')) {
124
+ link.rel = target.getAttribute('data-rel');
125
+ }
126
+ if (target.hasAttribute('data-download')) {
127
+ link.download = target.getAttribute('data-download');
128
+ }
129
+ if (target.hasAttribute('data-ping')) {
130
+ link.ping = target.getAttribute('data-ping');
131
+ }
132
+ if (target.hasAttribute('data-referrer-policy')) {
133
+ link.referrerPolicy = target.getAttribute('data-referrer-policy');
134
+ }
135
+ target.appendChild(link);
136
+ open(link);
137
+ target.removeChild(link);
138
+ }
139
+ }
140
+
141
+ function openSyntheticLink(target: Element, modifiers: Modifiers) {
142
+ getSyntheticLink(target, link => openLink(link, modifiers));
143
+ }
144
+
145
+ export function getSyntheticLinkProps(props: LinkDOMProps) {
146
+ return {
147
+ 'data-href': props.href,
148
+ 'data-target': props.target,
149
+ 'data-rel': props.rel,
150
+ 'data-download': props.download,
151
+ 'data-ping': props.ping,
152
+ 'data-referrer-policy': props.referrerPolicy
153
+ };
154
+ }
package/src/platform.ts CHANGED
@@ -59,3 +59,7 @@ export function isChrome() {
59
59
  export function isAndroid() {
60
60
  return testUserAgent(/Android/i);
61
61
  }
62
+
63
+ export function isFirefox() {
64
+ return testUserAgent(/Firefox/i);
65
+ }
package/src/useSyncRef.ts CHANGED
@@ -26,5 +26,5 @@ export function useSyncRef<T>(context: ContextValue<T>, ref: RefObject<T>) {
26
26
  context.ref.current = null;
27
27
  };
28
28
  }
29
- }, [context, ref]);
29
+ });
30
30
  }
@@ -10,8 +10,8 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {Dispatch, useCallback, useRef, useState} from 'react';
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
- // Must be stable so that `queue` is stable.
28
- let nextIter = useCallback(() => {
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
- 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
- }
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
- // 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]);
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
- nextIter();
51
+ nextRef();
56
52
  }
57
53
  });
58
54
 
59
- // queue must be a stable function, much like setState.
60
- let queue = useCallback(fn => {
61
- effect.current = fn(valueRef.current);
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]);
55
+ let queue = useEffectEvent(fn => {
56
+ effect.current = fn(value);
57
+ nextRef();
58
+ });
66
59
 
67
60
  return [value, queue];
68
61
  }
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import {useEffect, useState} from 'react';
14
+ import {useIsSSR} from '@react-aria/ssr';
14
15
 
15
16
  interface ViewportSize {
16
17
  width: number,
@@ -21,7 +22,8 @@ interface ViewportSize {
21
22
  let visualViewport = typeof document !== 'undefined' && window.visualViewport;
22
23
 
23
24
  export function useViewportSize(): ViewportSize {
24
- let [size, setSize] = useState(() => getViewportSize());
25
+ let isSSR = useIsSSR();
26
+ let [size, setSize] = useState(() => isSSR ? {width: 0, height: 0} : getViewportSize());
25
27
 
26
28
  useEffect(() => {
27
29
  // Use visualViewport api to track available height even on iOS virtual keyboard opening