@react-aria/interactions 3.8.0 → 3.8.3
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/main.js +401 -304
- package/dist/main.js.map +1 -1
- package/dist/module.js +390 -293
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/useFocus.ts +21 -15
- package/src/useFocusWithin.ts +20 -21
- package/src/usePress.ts +6 -6
- package/src/utils.ts +117 -0
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AGwBA,2BAA4B,SAAQ,WAAW;IAC7C,+FAA+F;IAC/F,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,mDAAmD;IACnD,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,4DAA4D;IAC5D,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;OAKG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;IACpC,yEAAyE;IACzE,yBAAyB,CAAC,EAAE,OAAO,CAAA;CACpC;AAED,+BAAgC,SAAQ,UAAU;IAChD,mCAAmC;IACnC,GAAG,CAAC,EAAE,UAAU,WAAW,CAAC,CAAA;CAC7B;AAsBD;IACE,+CAA+C;IAC/C,SAAS,EAAE,OAAO,CAAC;IACnB,6CAA6C;IAC7C,UAAU,EAAE,eAAe,WAAW,CAAC,CAAA;CACxC;AAeD;;;;GAIG;AACH,yBAAyB,KAAK,EAAE,cAAc,GAAG,WAAW,CA0jB3D;ACtoBD,wBAAyB,SAAQ,UAAU;IACzC,QAAQ,EAAE,aAAa,eAAe,WAAW,CAAC,EAAE,MAAM,CAAC,CAAA;CAC5D;AAED,OAAO,MAAM,6FAUX,CAAC;ACbH,6BAA8B,SAAQ,UAAU;IAC9C,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED,OAAO,MAAM,uGA8BX,CAAC;
|
|
1
|
+
{"mappings":";;AGwBA,2BAA4B,S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6BAA6B,KAAK,EAAE,cAAc,GAAG,eAAe,CAwEnE","sources":["packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/textSelection.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/utils.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/context.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/usePress.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/Pressable.tsx","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/PressResponder.tsx","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useFocus.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useFocusVisible.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useFocusWithin.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useHover.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useInteractOutside.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/createEventHandler.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useKeyboard.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useMove.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useScrollWheel.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/useLongPress.ts","packages/@react-aria/interactions/src/packages/@react-aria/interactions/src/index.ts","packages/@react-aria/interactions/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport * from './Pressable';\nexport * from './PressResponder';\nexport * from './useFocus';\nexport * from './useFocusVisible';\nexport * from './useFocusWithin';\nexport * from './useHover';\nexport * from './useInteractOutside';\nexport * from './useKeyboard';\nexport * from './useMove';\nexport * from './usePress';\nexport * from './useScrollWheel';\nexport * from './useLongPress';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/interactions",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.3",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
|
-
"@react-aria/utils": "^3.11.
|
|
22
|
-
"@react-types/shared": "^3.11.
|
|
21
|
+
"@react-aria/utils": "^3.11.3",
|
|
22
|
+
"@react-types/shared": "^3.11.2"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"react": "^16.8.0 || ^17.0.0-rc.1"
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "e7708349a637642a30d33a11ca4a75ad5829eaa3"
|
|
31
31
|
}
|
package/src/useFocus.ts
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
import {FocusEvent, HTMLAttributes} from 'react';
|
|
19
19
|
import {FocusEvents} from '@react-types/shared';
|
|
20
|
+
import {useSyntheticBlurEvent} from './utils';
|
|
20
21
|
|
|
21
22
|
interface FocusProps extends FocusEvents {
|
|
22
23
|
/** Whether the focus events should be disabled. */
|
|
@@ -33,35 +34,40 @@ interface FocusResult {
|
|
|
33
34
|
* Focus events on child elements will be ignored.
|
|
34
35
|
*/
|
|
35
36
|
export function useFocus(props: FocusProps): FocusResult {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
let onFocus, onBlur;
|
|
41
|
-
if (props.onFocus || props.onFocusChange) {
|
|
42
|
-
onFocus = (e: FocusEvent) => {
|
|
37
|
+
let onBlur: FocusProps['onBlur'];
|
|
38
|
+
if (!props.isDisabled && (props.onBlur || props.onFocusChange)) {
|
|
39
|
+
onBlur = (e: FocusEvent) => {
|
|
43
40
|
if (e.target === e.currentTarget) {
|
|
44
|
-
if (props.
|
|
45
|
-
props.
|
|
41
|
+
if (props.onBlur) {
|
|
42
|
+
props.onBlur(e);
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
if (props.onFocusChange) {
|
|
49
|
-
props.onFocusChange(
|
|
46
|
+
props.onFocusChange(false);
|
|
50
47
|
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
51
50
|
}
|
|
52
51
|
};
|
|
52
|
+
} else {
|
|
53
|
+
onBlur = null;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
let onSyntheticFocus = useSyntheticBlurEvent(onBlur);
|
|
57
|
+
|
|
58
|
+
let onFocus: FocusProps['onFocus'];
|
|
59
|
+
if (!props.isDisabled && (props.onFocus || props.onFocusChange || props.onBlur)) {
|
|
60
|
+
onFocus = (e: FocusEvent) => {
|
|
57
61
|
if (e.target === e.currentTarget) {
|
|
58
|
-
if (props.
|
|
59
|
-
props.
|
|
62
|
+
if (props.onFocus) {
|
|
63
|
+
props.onFocus(e);
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
if (props.onFocusChange) {
|
|
63
|
-
props.onFocusChange(
|
|
67
|
+
props.onFocusChange(true);
|
|
64
68
|
}
|
|
69
|
+
|
|
70
|
+
onSyntheticFocus(e);
|
|
65
71
|
}
|
|
66
72
|
};
|
|
67
73
|
}
|
package/src/useFocusWithin.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
17
17
|
|
|
18
18
|
import {FocusEvent, HTMLAttributes, useRef} from 'react';
|
|
19
|
+
import {useSyntheticBlurEvent} from './utils';
|
|
19
20
|
|
|
20
21
|
interface FocusWithinProps {
|
|
21
22
|
/** Whether the focus within events should be disabled. */
|
|
@@ -41,45 +42,43 @@ export function useFocusWithin(props: FocusWithinProps): FocusWithinResult {
|
|
|
41
42
|
isFocusWithin: false
|
|
42
43
|
}).current;
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
let onBlur = props.isDisabled ? null : (e: FocusEvent) => {
|
|
46
|
+
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
47
|
+
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
48
|
+
// include the relatedTarget (where focus is moving).
|
|
49
|
+
if (state.isFocusWithin && !(e.currentTarget as Element).contains(e.relatedTarget as Element)) {
|
|
50
|
+
state.isFocusWithin = false;
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (props.onFocusWithin) {
|
|
51
|
-
props.onFocusWithin(e);
|
|
52
|
+
if (props.onBlurWithin) {
|
|
53
|
+
props.onBlurWithin(e);
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
if (props.onFocusWithinChange) {
|
|
55
|
-
props.onFocusWithinChange(
|
|
57
|
+
props.onFocusWithinChange(false);
|
|
56
58
|
}
|
|
57
|
-
|
|
58
|
-
state.isFocusWithin = true;
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
let
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (props.onBlurWithin) {
|
|
68
|
-
props.onBlurWithin(e);
|
|
62
|
+
let onSyntheticFocus = useSyntheticBlurEvent(onBlur);
|
|
63
|
+
let onFocus = props.isDisabled ? null : (e: FocusEvent) => {
|
|
64
|
+
if (!state.isFocusWithin) {
|
|
65
|
+
if (props.onFocusWithin) {
|
|
66
|
+
props.onFocusWithin(e);
|
|
69
67
|
}
|
|
70
68
|
|
|
71
69
|
if (props.onFocusWithinChange) {
|
|
72
|
-
props.onFocusWithinChange(
|
|
70
|
+
props.onFocusWithinChange(true);
|
|
73
71
|
}
|
|
74
72
|
|
|
75
|
-
state.isFocusWithin =
|
|
73
|
+
state.isFocusWithin = true;
|
|
74
|
+
onSyntheticFocus(e);
|
|
76
75
|
}
|
|
77
76
|
};
|
|
78
77
|
|
|
79
78
|
return {
|
|
80
79
|
focusWithinProps: {
|
|
81
|
-
onFocus
|
|
82
|
-
onBlur
|
|
80
|
+
onFocus,
|
|
81
|
+
onBlur
|
|
83
82
|
}
|
|
84
83
|
};
|
|
85
84
|
}
|
package/src/usePress.ts
CHANGED
|
@@ -321,7 +321,7 @@ export function usePress(props: PressHookProps): PressResult {
|
|
|
321
321
|
|
|
322
322
|
// Due to browser inconsistencies, especially on mobile browsers, we prevent
|
|
323
323
|
// default on pointer down and handle focusing the pressable element ourselves.
|
|
324
|
-
if (shouldPreventDefault(e.
|
|
324
|
+
if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
|
|
325
325
|
e.preventDefault();
|
|
326
326
|
}
|
|
327
327
|
|
|
@@ -359,7 +359,7 @@ export function usePress(props: PressHookProps): PressResult {
|
|
|
359
359
|
// Chrome and Firefox on touch Windows devices require mouse down events
|
|
360
360
|
// to be canceled in addition to pointer events, or an extra asynchronous
|
|
361
361
|
// focus event will be fired.
|
|
362
|
-
if (shouldPreventDefault(e.
|
|
362
|
+
if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
|
|
363
363
|
e.preventDefault();
|
|
364
364
|
}
|
|
365
365
|
|
|
@@ -443,7 +443,7 @@ export function usePress(props: PressHookProps): PressResult {
|
|
|
443
443
|
|
|
444
444
|
// Due to browser inconsistencies, especially on mobile browsers, we prevent
|
|
445
445
|
// default on mouse down and handle focusing the pressable element ourselves.
|
|
446
|
-
if (shouldPreventDefault(e.
|
|
446
|
+
if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
|
|
447
447
|
e.preventDefault();
|
|
448
448
|
}
|
|
449
449
|
|
|
@@ -764,9 +764,9 @@ function isOverTarget(point: EventPoint, target: HTMLElement) {
|
|
|
764
764
|
return areRectanglesOverlapping(rect, pointRect);
|
|
765
765
|
}
|
|
766
766
|
|
|
767
|
-
function shouldPreventDefault(target:
|
|
768
|
-
// We cannot prevent default if the target is
|
|
769
|
-
return !target.
|
|
767
|
+
function shouldPreventDefault(target: HTMLElement) {
|
|
768
|
+
// We cannot prevent default if the target is a draggable element.
|
|
769
|
+
return !target.draggable;
|
|
770
770
|
}
|
|
771
771
|
|
|
772
772
|
function shouldPreventDefaultKeyboard(target: Element) {
|
package/src/utils.ts
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {FocusEvent as ReactFocusEvent, useRef} from 'react';
|
|
14
|
+
import {useLayoutEffect} from '@react-aria/utils';
|
|
15
|
+
|
|
13
16
|
// Original licensing for the following method can be found in the
|
|
14
17
|
// NOTICE file in the root directory of this source tree.
|
|
15
18
|
// See https://github.com/facebook/react/blob/3c713d513195a53788b3f8bb4b70279d68b15bcc/packages/react-interactions/events/src/dom/shared/index.js#L74-L87
|
|
@@ -29,3 +32,117 @@ export function isVirtualClick(event: MouseEvent | PointerEvent): boolean {
|
|
|
29
32
|
|
|
30
33
|
return event.detail === 0 && !(event as PointerEvent).pointerType;
|
|
31
34
|
}
|
|
35
|
+
|
|
36
|
+
export class SyntheticFocusEvent implements ReactFocusEvent {
|
|
37
|
+
nativeEvent: FocusEvent;
|
|
38
|
+
target: Element;
|
|
39
|
+
currentTarget: Element;
|
|
40
|
+
relatedTarget: Element;
|
|
41
|
+
bubbles: boolean;
|
|
42
|
+
cancelable: boolean;
|
|
43
|
+
defaultPrevented: boolean;
|
|
44
|
+
eventPhase: number;
|
|
45
|
+
isTrusted: boolean;
|
|
46
|
+
timeStamp: number;
|
|
47
|
+
type: string;
|
|
48
|
+
|
|
49
|
+
constructor(type: string, nativeEvent: FocusEvent) {
|
|
50
|
+
this.nativeEvent = nativeEvent;
|
|
51
|
+
this.target = nativeEvent.target as Element;
|
|
52
|
+
this.currentTarget = nativeEvent.currentTarget as Element;
|
|
53
|
+
this.relatedTarget = nativeEvent.relatedTarget as Element;
|
|
54
|
+
this.bubbles = nativeEvent.bubbles;
|
|
55
|
+
this.cancelable = nativeEvent.cancelable;
|
|
56
|
+
this.defaultPrevented = nativeEvent.defaultPrevented;
|
|
57
|
+
this.eventPhase = nativeEvent.eventPhase;
|
|
58
|
+
this.isTrusted = nativeEvent.isTrusted;
|
|
59
|
+
this.timeStamp = nativeEvent.timeStamp;
|
|
60
|
+
this.type = type;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
isDefaultPrevented(): boolean {
|
|
64
|
+
return this.nativeEvent.defaultPrevented;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
preventDefault(): void {
|
|
68
|
+
this.defaultPrevented = true;
|
|
69
|
+
this.nativeEvent.preventDefault();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
stopPropagation(): void {
|
|
73
|
+
this.nativeEvent.stopPropagation();
|
|
74
|
+
this.isPropagationStopped = () => true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
isPropagationStopped(): boolean {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
persist() {}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function useSyntheticBlurEvent(onBlur: (e: ReactFocusEvent) => void) {
|
|
85
|
+
let stateRef = useRef({
|
|
86
|
+
isFocused: false,
|
|
87
|
+
onBlur,
|
|
88
|
+
observer: null as MutationObserver
|
|
89
|
+
});
|
|
90
|
+
let state = stateRef.current;
|
|
91
|
+
state.onBlur = onBlur;
|
|
92
|
+
|
|
93
|
+
// Clean up MutationObserver on unmount. See below.
|
|
94
|
+
// eslint-disable-next-line arrow-body-style
|
|
95
|
+
useLayoutEffect(() => {
|
|
96
|
+
return () => {
|
|
97
|
+
if (state.observer) {
|
|
98
|
+
state.observer.disconnect();
|
|
99
|
+
state.observer = null;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}, [state]);
|
|
103
|
+
|
|
104
|
+
// This function is called during a React onFocus event.
|
|
105
|
+
return (e: ReactFocusEvent) => {
|
|
106
|
+
// React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142
|
|
107
|
+
// Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a
|
|
108
|
+
// MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.
|
|
109
|
+
// For browsers that do, focusout fires before the MutationObserver, so onBlur should not fire twice.
|
|
110
|
+
if (
|
|
111
|
+
e.target instanceof HTMLButtonElement ||
|
|
112
|
+
e.target instanceof HTMLInputElement ||
|
|
113
|
+
e.target instanceof HTMLTextAreaElement ||
|
|
114
|
+
e.target instanceof HTMLSelectElement
|
|
115
|
+
) {
|
|
116
|
+
state.isFocused = true;
|
|
117
|
+
|
|
118
|
+
let target = e.target;
|
|
119
|
+
let onBlurHandler = (e: FocusEvent) => {
|
|
120
|
+
let state = stateRef.current;
|
|
121
|
+
state.isFocused = false;
|
|
122
|
+
|
|
123
|
+
if (target.disabled) {
|
|
124
|
+
// For backward compatibility, dispatch a (fake) React synthetic event.
|
|
125
|
+
state.onBlur?.(new SyntheticFocusEvent('blur', e));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// We no longer need the MutationObserver once the target is blurred.
|
|
129
|
+
if (state.observer) {
|
|
130
|
+
state.observer.disconnect();
|
|
131
|
+
state.observer = null;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
target.addEventListener('focusout', onBlurHandler, {once: true});
|
|
136
|
+
|
|
137
|
+
state.observer = new MutationObserver(() => {
|
|
138
|
+
if (state.isFocused && target.disabled) {
|
|
139
|
+
state.observer.disconnect();
|
|
140
|
+
target.dispatchEvent(new FocusEvent('blur'));
|
|
141
|
+
target.dispatchEvent(new FocusEvent('focusout', {bubbles: true}));
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
state.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|