@react-aria/interactions 3.8.2 → 3.9.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/main.js +150 -34
- package/dist/main.js.map +1 -1
- package/dist/module.js +151 -35
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/useFocus.ts +35 -29
- package/src/useFocusWithin.ts +41 -27
- package/src/usePress.ts +4 -2
- package/src/utils.ts +116 -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.
|
|
3
|
+
"version": "3.9.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
|
-
"@react-aria/utils": "^3.
|
|
22
|
-
"@react-types/shared": "^3.
|
|
21
|
+
"@react-aria/utils": "^3.13.0",
|
|
22
|
+
"@react-types/shared": "^3.13.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"react": "^16.8.0 || ^17.0.0-rc.1"
|
|
25
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "8f921ec5094e7c2b3c301bcb6133372e35a2052b"
|
|
31
31
|
}
|
package/src/useFocus.ts
CHANGED
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
// NOTICE file in the root directory of this source tree.
|
|
16
16
|
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
17
17
|
|
|
18
|
-
import {FocusEvent, HTMLAttributes} from 'react';
|
|
18
|
+
import {FocusEvent, HTMLAttributes, useCallback} 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,43 +34,48 @@ 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
|
-
|
|
37
|
+
let {
|
|
38
|
+
isDisabled,
|
|
39
|
+
onFocus: onFocusProp,
|
|
40
|
+
onBlur: onBlurProp,
|
|
41
|
+
onFocusChange
|
|
42
|
+
} = props;
|
|
39
43
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
props.onFocus(e);
|
|
46
|
-
}
|
|
44
|
+
const onBlur: FocusProps['onBlur'] = useCallback((e: FocusEvent) => {
|
|
45
|
+
if (e.target === e.currentTarget) {
|
|
46
|
+
if (onBlurProp) {
|
|
47
|
+
onBlurProp(e);
|
|
48
|
+
}
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
50
|
+
if (onFocusChange) {
|
|
51
|
+
onFocusChange(false);
|
|
51
52
|
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}, [onBlurProp, onFocusChange]);
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const onSyntheticFocus = useSyntheticBlurEvent(onBlur);
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
const onFocus: FocusProps['onFocus'] = useCallback((e: FocusEvent) => {
|
|
62
|
+
if (e.target === e.currentTarget) {
|
|
63
|
+
if (onFocusProp) {
|
|
64
|
+
onFocusProp(e);
|
|
65
65
|
}
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
|
|
67
|
+
if (onFocusChange) {
|
|
68
|
+
onFocusChange(true);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onSyntheticFocus(e);
|
|
72
|
+
}
|
|
73
|
+
}, [onFocusChange, onFocusProp, onSyntheticFocus]);
|
|
68
74
|
|
|
69
75
|
return {
|
|
70
76
|
focusProps: {
|
|
71
|
-
onFocus,
|
|
72
|
-
onBlur
|
|
77
|
+
onFocus: (!isDisabled && (onFocusProp || onFocusChange || onBlurProp)) ? onFocus : undefined,
|
|
78
|
+
onBlur: (!isDisabled && (onBlurProp || onFocusChange)) ? onBlur : null
|
|
73
79
|
}
|
|
74
80
|
};
|
|
75
81
|
}
|
package/src/useFocusWithin.ts
CHANGED
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
// NOTICE file in the root directory of this source tree.
|
|
16
16
|
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
17
17
|
|
|
18
|
-
import {FocusEvent, HTMLAttributes, useRef} from 'react';
|
|
18
|
+
import {FocusEvent, HTMLAttributes, useCallback, useRef} from 'react';
|
|
19
|
+
import {useSyntheticBlurEvent} from './utils';
|
|
19
20
|
|
|
20
21
|
interface FocusWithinProps {
|
|
21
22
|
/** Whether the focus within events should be disabled. */
|
|
@@ -37,49 +38,62 @@ interface FocusWithinResult {
|
|
|
37
38
|
* Handles focus events for the target and its descendants.
|
|
38
39
|
*/
|
|
39
40
|
export function useFocusWithin(props: FocusWithinProps): FocusWithinResult {
|
|
41
|
+
let {
|
|
42
|
+
isDisabled,
|
|
43
|
+
onBlurWithin,
|
|
44
|
+
onFocusWithin,
|
|
45
|
+
onFocusWithinChange
|
|
46
|
+
} = props;
|
|
40
47
|
let state = useRef({
|
|
41
48
|
isFocusWithin: false
|
|
42
|
-
})
|
|
49
|
+
});
|
|
43
50
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
let onBlur = useCallback((e: FocusEvent) => {
|
|
52
|
+
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
53
|
+
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
54
|
+
// include the relatedTarget (where focus is moving).
|
|
55
|
+
if (state.current.isFocusWithin && !(e.currentTarget as Element).contains(e.relatedTarget as Element)) {
|
|
56
|
+
state.current.isFocusWithin = false;
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (props.onFocusWithin) {
|
|
51
|
-
props.onFocusWithin(e);
|
|
58
|
+
if (onBlurWithin) {
|
|
59
|
+
onBlurWithin(e);
|
|
52
60
|
}
|
|
53
61
|
|
|
54
|
-
if (
|
|
55
|
-
|
|
62
|
+
if (onFocusWithinChange) {
|
|
63
|
+
onFocusWithinChange(false);
|
|
56
64
|
}
|
|
57
|
-
|
|
58
|
-
state.isFocusWithin = true;
|
|
59
65
|
}
|
|
60
|
-
};
|
|
66
|
+
}, [onBlurWithin, onFocusWithinChange, state]);
|
|
61
67
|
|
|
62
|
-
let
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (props.onBlurWithin) {
|
|
68
|
-
props.onBlurWithin(e);
|
|
68
|
+
let onSyntheticFocus = useSyntheticBlurEvent(onBlur);
|
|
69
|
+
let onFocus = useCallback((e: FocusEvent) => {
|
|
70
|
+
if (!state.current.isFocusWithin) {
|
|
71
|
+
if (onFocusWithin) {
|
|
72
|
+
onFocusWithin(e);
|
|
69
73
|
}
|
|
70
74
|
|
|
71
|
-
if (
|
|
72
|
-
|
|
75
|
+
if (onFocusWithinChange) {
|
|
76
|
+
onFocusWithinChange(true);
|
|
73
77
|
}
|
|
74
78
|
|
|
75
|
-
state.isFocusWithin =
|
|
79
|
+
state.current.isFocusWithin = true;
|
|
80
|
+
onSyntheticFocus(e);
|
|
76
81
|
}
|
|
77
|
-
};
|
|
82
|
+
}, [onFocusWithin, onFocusWithinChange, onSyntheticFocus]);
|
|
83
|
+
|
|
84
|
+
if (isDisabled) {
|
|
85
|
+
return {
|
|
86
|
+
focusWithinProps: {
|
|
87
|
+
onFocus: null,
|
|
88
|
+
onBlur: null
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
78
92
|
|
|
79
93
|
return {
|
|
80
94
|
focusWithinProps: {
|
|
81
|
-
onFocus
|
|
82
|
-
onBlur
|
|
95
|
+
onFocus,
|
|
96
|
+
onBlur
|
|
83
97
|
}
|
|
84
98
|
};
|
|
85
99
|
}
|
package/src/usePress.ts
CHANGED
|
@@ -778,13 +778,15 @@ function isVirtualPointerEvent(event: PointerEvent) {
|
|
|
778
778
|
// Android TalkBack double tap will sometimes return a event with width and height of 1
|
|
779
779
|
// and pointerType === 'mouse' so we need to check for a specific combination of event attributes.
|
|
780
780
|
// Cannot use "event.pressure === 0" as the sole check due to Safari pointer events always returning pressure === 0
|
|
781
|
-
// instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216
|
|
781
|
+
// instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216. event.pointerType === 'mouse' is to distingush
|
|
782
|
+
// Talkback double tap from Windows Firefox touch screen press
|
|
782
783
|
return (
|
|
783
784
|
(event.width === 0 && event.height === 0) ||
|
|
784
785
|
(event.width === 1 &&
|
|
785
786
|
event.height === 1 &&
|
|
786
787
|
event.pressure === 0 &&
|
|
787
|
-
event.detail === 0
|
|
788
|
+
event.detail === 0 &&
|
|
789
|
+
event.pointerType === 'mouse'
|
|
788
790
|
)
|
|
789
791
|
);
|
|
790
792
|
}
|
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, useCallback, 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,116 @@ 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
|
+
stateRef.current.onBlur = onBlur;
|
|
91
|
+
|
|
92
|
+
// Clean up MutationObserver on unmount. See below.
|
|
93
|
+
// eslint-disable-next-line arrow-body-style
|
|
94
|
+
useLayoutEffect(() => {
|
|
95
|
+
const state = stateRef.current;
|
|
96
|
+
return () => {
|
|
97
|
+
if (state.observer) {
|
|
98
|
+
state.observer.disconnect();
|
|
99
|
+
state.observer = null;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}, []);
|
|
103
|
+
|
|
104
|
+
// This function is called during a React onFocus event.
|
|
105
|
+
return useCallback((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
|
+
stateRef.current.isFocused = true;
|
|
117
|
+
|
|
118
|
+
let target = e.target;
|
|
119
|
+
let onBlurHandler = (e: FocusEvent) => {
|
|
120
|
+
stateRef.current.isFocused = false;
|
|
121
|
+
|
|
122
|
+
if (target.disabled) {
|
|
123
|
+
// For backward compatibility, dispatch a (fake) React synthetic event.
|
|
124
|
+
stateRef.current.onBlur?.(new SyntheticFocusEvent('blur', e));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// We no longer need the MutationObserver once the target is blurred.
|
|
128
|
+
if (stateRef.current.observer) {
|
|
129
|
+
stateRef.current.observer.disconnect();
|
|
130
|
+
stateRef.current.observer = null;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
target.addEventListener('focusout', onBlurHandler, {once: true});
|
|
135
|
+
|
|
136
|
+
stateRef.current.observer = new MutationObserver(() => {
|
|
137
|
+
if (stateRef.current.isFocused && target.disabled) {
|
|
138
|
+
stateRef.current.observer.disconnect();
|
|
139
|
+
target.dispatchEvent(new FocusEvent('blur'));
|
|
140
|
+
target.dispatchEvent(new FocusEvent('focusout', {bubbles: true}));
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
stateRef.current.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
|
|
145
|
+
}
|
|
146
|
+
}, []);
|
|
147
|
+
}
|