@react-aria/interactions 3.27.1 → 3.28.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 +21 -15
- package/dist/main.js +45 -39
- package/dist/main.js.map +1 -1
- package/dist/module.js +21 -15
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +31 -0
- package/package.json +16 -15
- package/src/index.ts +30 -34
- package/dist/PressResponder.main.js +0 -61
- package/dist/PressResponder.main.js.map +0 -1
- package/dist/PressResponder.mjs +0 -51
- package/dist/PressResponder.module.js +0 -51
- package/dist/PressResponder.module.js.map +0 -1
- package/dist/Pressable.main.js +0 -69
- package/dist/Pressable.main.js.map +0 -1
- package/dist/Pressable.mjs +0 -60
- package/dist/Pressable.module.js +0 -60
- package/dist/Pressable.module.js.map +0 -1
- package/dist/context.main.js +0 -30
- package/dist/context.main.js.map +0 -1
- package/dist/context.mjs +0 -21
- package/dist/context.module.js +0 -21
- package/dist/context.module.js.map +0 -1
- package/dist/createEventHandler.main.js +0 -46
- package/dist/createEventHandler.main.js.map +0 -1
- package/dist/createEventHandler.mjs +0 -41
- package/dist/createEventHandler.module.js +0 -41
- package/dist/createEventHandler.module.js.map +0 -1
- package/dist/focusSafely.main.js +0 -41
- package/dist/focusSafely.main.js.map +0 -1
- package/dist/focusSafely.mjs +0 -36
- package/dist/focusSafely.module.js +0 -36
- package/dist/focusSafely.module.js.map +0 -1
- package/dist/textSelection.main.js +0 -77
- package/dist/textSelection.main.js.map +0 -1
- package/dist/textSelection.mjs +0 -71
- package/dist/textSelection.module.js +0 -71
- package/dist/textSelection.module.js.map +0 -1
- package/dist/types.d.ts +0 -255
- package/dist/types.d.ts.map +0 -1
- package/dist/useFocus.main.js +0 -66
- package/dist/useFocus.main.js.map +0 -1
- package/dist/useFocus.mjs +0 -61
- package/dist/useFocus.module.js +0 -61
- package/dist/useFocus.module.js.map +0 -1
- package/dist/useFocusVisible.main.js +0 -267
- package/dist/useFocusVisible.main.js.map +0 -1
- package/dist/useFocusVisible.mjs +0 -255
- package/dist/useFocusVisible.module.js +0 -255
- package/dist/useFocusVisible.module.js.map +0 -1
- package/dist/useFocusWithin.main.js +0 -107
- package/dist/useFocusWithin.main.js.map +0 -1
- package/dist/useFocusWithin.mjs +0 -102
- package/dist/useFocusWithin.module.js +0 -102
- package/dist/useFocusWithin.module.js.map +0 -1
- package/dist/useFocusable.main.js +0 -113
- package/dist/useFocusable.main.js.map +0 -1
- package/dist/useFocusable.mjs +0 -101
- package/dist/useFocusable.module.js +0 -101
- package/dist/useFocusable.module.js.map +0 -1
- package/dist/useHover.main.js +0 -159
- package/dist/useHover.main.js.map +0 -1
- package/dist/useHover.mjs +0 -154
- package/dist/useHover.module.js +0 -154
- package/dist/useHover.module.js.map +0 -1
- package/dist/useInteractOutside.main.js +0 -107
- package/dist/useInteractOutside.main.js.map +0 -1
- package/dist/useInteractOutside.mjs +0 -102
- package/dist/useInteractOutside.module.js +0 -102
- package/dist/useInteractOutside.module.js.map +0 -1
- package/dist/useKeyboard.main.js +0 -30
- package/dist/useKeyboard.main.js.map +0 -1
- package/dist/useKeyboard.mjs +0 -25
- package/dist/useKeyboard.module.js +0 -25
- package/dist/useKeyboard.module.js.map +0 -1
- package/dist/useLongPress.main.js +0 -86
- package/dist/useLongPress.main.js.map +0 -1
- package/dist/useLongPress.mjs +0 -81
- package/dist/useLongPress.module.js +0 -81
- package/dist/useLongPress.module.js.map +0 -1
- package/dist/useMove.main.js +0 -272
- package/dist/useMove.main.js.map +0 -1
- package/dist/useMove.mjs +0 -267
- package/dist/useMove.module.js +0 -267
- package/dist/useMove.module.js.map +0 -1
- package/dist/usePress.main.js +0 -763
- package/dist/usePress.main.js.map +0 -1
- package/dist/usePress.mjs +0 -758
- package/dist/usePress.module.js +0 -758
- package/dist/usePress.module.js.map +0 -1
- package/dist/useScrollWheel.main.js +0 -41
- package/dist/useScrollWheel.main.js.map +0 -1
- package/dist/useScrollWheel.mjs +0 -36
- package/dist/useScrollWheel.module.js +0 -36
- package/dist/useScrollWheel.module.js.map +0 -1
- package/dist/utils.main.js +0 -164
- package/dist/utils.main.js.map +0 -1
- package/dist/utils.mjs +0 -155
- package/dist/utils.module.js +0 -155
- package/dist/utils.module.js.map +0 -1
- package/src/PressResponder.tsx +0 -67
- package/src/Pressable.tsx +0 -97
- package/src/context.ts +0 -23
- package/src/createEventHandler.ts +0 -55
- package/src/focusSafely.ts +0 -49
- package/src/textSelection.ts +0 -101
- package/src/useFocus.ts +0 -88
- package/src/useFocusVisible.ts +0 -359
- package/src/useFocusWithin.ts +0 -134
- package/src/useFocusable.tsx +0 -191
- package/src/useHover.ts +0 -222
- package/src/useInteractOutside.ts +0 -143
- package/src/useKeyboard.ts +0 -36
- package/src/useLongPress.ts +0 -135
- package/src/useMove.ts +0 -259
- package/src/usePress.ts +0 -1095
- package/src/useScrollWheel.ts +0 -41
- package/src/utils.ts +0 -175
package/dist/utils.module.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import {useLayoutEffect as $6dfIe$useLayoutEffect, getEventTarget as $6dfIe$getEventTarget, getActiveElement as $6dfIe$getActiveElement, isFocusable as $6dfIe$isFocusable, getOwnerWindow as $6dfIe$getOwnerWindow, focusWithoutScrolling as $6dfIe$focusWithoutScrolling} from "@react-aria/utils";
|
|
2
|
-
import {useRef as $6dfIe$useRef, useCallback as $6dfIe$useCallback} from "react";
|
|
3
|
-
|
|
4
|
-
/*
|
|
5
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
6
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
8
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
11
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
12
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
13
|
-
* governing permissions and limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
function $8a9cb279dc87e130$export$525bc4921d56d4a(nativeEvent) {
|
|
17
|
-
let event = nativeEvent;
|
|
18
|
-
event.nativeEvent = nativeEvent;
|
|
19
|
-
event.isDefaultPrevented = ()=>event.defaultPrevented;
|
|
20
|
-
// cancelBubble is technically deprecated in the spec, but still supported in all browsers.
|
|
21
|
-
event.isPropagationStopped = ()=>event.cancelBubble;
|
|
22
|
-
event.persist = ()=>{};
|
|
23
|
-
return event;
|
|
24
|
-
}
|
|
25
|
-
function $8a9cb279dc87e130$export$c2b7abe5d61ec696(event, target) {
|
|
26
|
-
Object.defineProperty(event, 'target', {
|
|
27
|
-
value: target
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(event, 'currentTarget', {
|
|
30
|
-
value: target
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
function $8a9cb279dc87e130$export$715c682d09d639cc(onBlur) {
|
|
34
|
-
let stateRef = (0, $6dfIe$useRef)({
|
|
35
|
-
isFocused: false,
|
|
36
|
-
observer: null
|
|
37
|
-
});
|
|
38
|
-
// Clean up MutationObserver on unmount. See below.
|
|
39
|
-
(0, $6dfIe$useLayoutEffect)(()=>{
|
|
40
|
-
const state = stateRef.current;
|
|
41
|
-
return ()=>{
|
|
42
|
-
if (state.observer) {
|
|
43
|
-
state.observer.disconnect();
|
|
44
|
-
state.observer = null;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}, []);
|
|
48
|
-
// This function is called during a React onFocus event.
|
|
49
|
-
return (0, $6dfIe$useCallback)((e)=>{
|
|
50
|
-
// React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142
|
|
51
|
-
// Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a
|
|
52
|
-
// MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.
|
|
53
|
-
// For browsers that do, focusout fires before the MutationObserver, so onBlur should not fire twice.
|
|
54
|
-
let eventTarget = (0, $6dfIe$getEventTarget)(e);
|
|
55
|
-
if (eventTarget instanceof HTMLButtonElement || eventTarget instanceof HTMLInputElement || eventTarget instanceof HTMLTextAreaElement || eventTarget instanceof HTMLSelectElement) {
|
|
56
|
-
stateRef.current.isFocused = true;
|
|
57
|
-
let target = eventTarget;
|
|
58
|
-
let onBlurHandler = (e)=>{
|
|
59
|
-
stateRef.current.isFocused = false;
|
|
60
|
-
if (target.disabled) {
|
|
61
|
-
// For backward compatibility, dispatch a (fake) React synthetic event.
|
|
62
|
-
let event = $8a9cb279dc87e130$export$525bc4921d56d4a(e);
|
|
63
|
-
onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
|
|
64
|
-
}
|
|
65
|
-
// We no longer need the MutationObserver once the target is blurred.
|
|
66
|
-
if (stateRef.current.observer) {
|
|
67
|
-
stateRef.current.observer.disconnect();
|
|
68
|
-
stateRef.current.observer = null;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
target.addEventListener('focusout', onBlurHandler, {
|
|
72
|
-
once: true
|
|
73
|
-
});
|
|
74
|
-
stateRef.current.observer = new MutationObserver(()=>{
|
|
75
|
-
if (stateRef.current.isFocused && target.disabled) {
|
|
76
|
-
var _stateRef_current_observer;
|
|
77
|
-
(_stateRef_current_observer = stateRef.current.observer) === null || _stateRef_current_observer === void 0 ? void 0 : _stateRef_current_observer.disconnect();
|
|
78
|
-
let relatedTargetEl = target === (0, $6dfIe$getActiveElement)() ? null : (0, $6dfIe$getActiveElement)();
|
|
79
|
-
target.dispatchEvent(new FocusEvent('blur', {
|
|
80
|
-
relatedTarget: relatedTargetEl
|
|
81
|
-
}));
|
|
82
|
-
target.dispatchEvent(new FocusEvent('focusout', {
|
|
83
|
-
bubbles: true,
|
|
84
|
-
relatedTarget: relatedTargetEl
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
stateRef.current.observer.observe(target, {
|
|
89
|
-
attributes: true,
|
|
90
|
-
attributeFilter: [
|
|
91
|
-
'disabled'
|
|
92
|
-
]
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}, [
|
|
96
|
-
onBlur
|
|
97
|
-
]);
|
|
98
|
-
}
|
|
99
|
-
let $8a9cb279dc87e130$export$fda7da73ab5d4c48 = false;
|
|
100
|
-
function $8a9cb279dc87e130$export$cabe61c495ee3649(target) {
|
|
101
|
-
// The browser will focus the nearest focusable ancestor of our target.
|
|
102
|
-
while(target && !(0, $6dfIe$isFocusable)(target))target = target.parentElement;
|
|
103
|
-
let window = (0, $6dfIe$getOwnerWindow)(target);
|
|
104
|
-
let activeElement = window.document.activeElement;
|
|
105
|
-
if (!activeElement || activeElement === target) return;
|
|
106
|
-
$8a9cb279dc87e130$export$fda7da73ab5d4c48 = true;
|
|
107
|
-
let isRefocusing = false;
|
|
108
|
-
let onBlur = (e)=>{
|
|
109
|
-
if ((0, $6dfIe$getEventTarget)(e) === activeElement || isRefocusing) e.stopImmediatePropagation();
|
|
110
|
-
};
|
|
111
|
-
let onFocusOut = (e)=>{
|
|
112
|
-
if ((0, $6dfIe$getEventTarget)(e) === activeElement || isRefocusing) {
|
|
113
|
-
e.stopImmediatePropagation();
|
|
114
|
-
// If there was no focusable ancestor, we don't expect a focus event.
|
|
115
|
-
// Re-focus the original active element here.
|
|
116
|
-
if (!target && !isRefocusing) {
|
|
117
|
-
isRefocusing = true;
|
|
118
|
-
(0, $6dfIe$focusWithoutScrolling)(activeElement);
|
|
119
|
-
cleanup();
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
let onFocus = (e)=>{
|
|
124
|
-
if ((0, $6dfIe$getEventTarget)(e) === target || isRefocusing) e.stopImmediatePropagation();
|
|
125
|
-
};
|
|
126
|
-
let onFocusIn = (e)=>{
|
|
127
|
-
if ((0, $6dfIe$getEventTarget)(e) === target || isRefocusing) {
|
|
128
|
-
e.stopImmediatePropagation();
|
|
129
|
-
if (!isRefocusing) {
|
|
130
|
-
isRefocusing = true;
|
|
131
|
-
(0, $6dfIe$focusWithoutScrolling)(activeElement);
|
|
132
|
-
cleanup();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
window.addEventListener('blur', onBlur, true);
|
|
137
|
-
window.addEventListener('focusout', onFocusOut, true);
|
|
138
|
-
window.addEventListener('focusin', onFocusIn, true);
|
|
139
|
-
window.addEventListener('focus', onFocus, true);
|
|
140
|
-
let cleanup = ()=>{
|
|
141
|
-
cancelAnimationFrame(raf);
|
|
142
|
-
window.removeEventListener('blur', onBlur, true);
|
|
143
|
-
window.removeEventListener('focusout', onFocusOut, true);
|
|
144
|
-
window.removeEventListener('focusin', onFocusIn, true);
|
|
145
|
-
window.removeEventListener('focus', onFocus, true);
|
|
146
|
-
$8a9cb279dc87e130$export$fda7da73ab5d4c48 = false;
|
|
147
|
-
isRefocusing = false;
|
|
148
|
-
};
|
|
149
|
-
let raf = requestAnimationFrame(cleanup);
|
|
150
|
-
return cleanup;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
export {$8a9cb279dc87e130$export$525bc4921d56d4a as createSyntheticEvent, $8a9cb279dc87e130$export$c2b7abe5d61ec696 as setEventTarget, $8a9cb279dc87e130$export$715c682d09d639cc as useSyntheticBlurEvent, $8a9cb279dc87e130$export$fda7da73ab5d4c48 as ignoreFocusEvent, $8a9cb279dc87e130$export$cabe61c495ee3649 as preventFocus};
|
|
155
|
-
//# sourceMappingURL=utils.module.js.map
|
package/dist/utils.module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"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","sources":["packages/@react-aria/interactions/src/utils.ts"],"sourcesContent":["/*\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\nimport {FocusableElement} from '@react-types/shared';\nimport {focusWithoutScrolling, getActiveElement, getEventTarget, getOwnerWindow, isFocusable, useLayoutEffect} from '@react-aria/utils';\nimport {FocusEvent as ReactFocusEvent, SyntheticEvent, useCallback, useRef} from 'react';\n\n// Turn a native event into a React synthetic event.\nexport function createSyntheticEvent<E extends SyntheticEvent>(nativeEvent: Event): E {\n let event = nativeEvent as any as E;\n event.nativeEvent = nativeEvent;\n event.isDefaultPrevented = () => event.defaultPrevented;\n // cancelBubble is technically deprecated in the spec, but still supported in all browsers.\n event.isPropagationStopped = () => (event as any).cancelBubble;\n event.persist = () => {};\n return event;\n}\n\nexport function setEventTarget(event: Event, target: Element): void {\n Object.defineProperty(event, 'target', {value: target});\n Object.defineProperty(event, 'currentTarget', {value: target});\n}\n\nexport function useSyntheticBlurEvent<Target extends Element = Element>(onBlur: (e: ReactFocusEvent<Target>) => void): (e: ReactFocusEvent<Target>) => void {\n let stateRef = useRef({\n isFocused: false,\n observer: null as MutationObserver | null\n });\n\n // Clean up MutationObserver on unmount. See below.\n\n useLayoutEffect(() => {\n const state = stateRef.current;\n return () => {\n if (state.observer) {\n state.observer.disconnect();\n state.observer = null;\n }\n };\n }, []);\n\n // This function is called during a React onFocus event.\n return useCallback((e: ReactFocusEvent<Target>) => {\n // React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142\n // Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a\n // MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.\n // For browsers that do, focusout fires before the MutationObserver, so onBlur should not fire twice.\n let eventTarget = getEventTarget(e);\n if (\n eventTarget instanceof HTMLButtonElement ||\n eventTarget instanceof HTMLInputElement ||\n eventTarget instanceof HTMLTextAreaElement ||\n eventTarget instanceof HTMLSelectElement\n ) {\n stateRef.current.isFocused = true;\n\n let target = eventTarget;\n let onBlurHandler: EventListenerOrEventListenerObject | null = (e) => {\n stateRef.current.isFocused = false;\n\n if (target.disabled) {\n // For backward compatibility, dispatch a (fake) React synthetic event.\n let event = createSyntheticEvent<ReactFocusEvent<Target>>(e);\n onBlur?.(event);\n }\n\n // We no longer need the MutationObserver once the target is blurred.\n if (stateRef.current.observer) {\n stateRef.current.observer.disconnect();\n stateRef.current.observer = null;\n }\n };\n\n target.addEventListener('focusout', onBlurHandler, {once: true});\n\n stateRef.current.observer = new MutationObserver(() => {\n if (stateRef.current.isFocused && target.disabled) {\n stateRef.current.observer?.disconnect();\n let relatedTargetEl = target === getActiveElement() ? null : getActiveElement();\n target.dispatchEvent(new FocusEvent('blur', {relatedTarget: relatedTargetEl}));\n target.dispatchEvent(new FocusEvent('focusout', {bubbles: true, relatedTarget: relatedTargetEl}));\n }\n });\n\n stateRef.current.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});\n }\n }, [onBlur]);\n}\n\nexport let ignoreFocusEvent = false;\n\n/**\n * This function prevents the next focus event fired on `target`, without using `event.preventDefault()`.\n * It works by waiting for the series of focus events to occur, and reverts focus back to where it was before.\n * It also makes these events mostly non-observable by using a capturing listener on the window and stopping propagation.\n */\nexport function preventFocus(target: FocusableElement | null): (() => void) | undefined {\n // The browser will focus the nearest focusable ancestor of our target.\n while (target && !isFocusable(target)) {\n target = target.parentElement;\n }\n\n let window = getOwnerWindow(target);\n let activeElement = window.document.activeElement as FocusableElement | null;\n if (!activeElement || activeElement === target) {\n return;\n }\n\n ignoreFocusEvent = true;\n let isRefocusing = false;\n let onBlur = (e: FocusEvent) => {\n if (getEventTarget(e) === activeElement || isRefocusing) {\n e.stopImmediatePropagation();\n }\n };\n\n let onFocusOut = (e: FocusEvent) => {\n if (getEventTarget(e) === activeElement || isRefocusing) {\n e.stopImmediatePropagation();\n\n // If there was no focusable ancestor, we don't expect a focus event.\n // Re-focus the original active element here.\n if (!target && !isRefocusing) {\n isRefocusing = true;\n focusWithoutScrolling(activeElement);\n cleanup();\n }\n }\n };\n\n let onFocus = (e: FocusEvent) => {\n if (getEventTarget(e) === target || isRefocusing) {\n e.stopImmediatePropagation();\n }\n };\n\n let onFocusIn = (e: FocusEvent) => {\n if (getEventTarget(e) === target || isRefocusing) {\n e.stopImmediatePropagation();\n\n if (!isRefocusing) {\n isRefocusing = true;\n focusWithoutScrolling(activeElement);\n cleanup();\n }\n }\n };\n\n window.addEventListener('blur', onBlur, true);\n window.addEventListener('focusout', onFocusOut, true);\n window.addEventListener('focusin', onFocusIn, true);\n window.addEventListener('focus', onFocus, true);\n\n let cleanup = () => {\n cancelAnimationFrame(raf);\n window.removeEventListener('blur', onBlur, true);\n window.removeEventListener('focusout', onFocusOut, true);\n window.removeEventListener('focusin', onFocusIn, true);\n window.removeEventListener('focus', onFocus, true);\n ignoreFocusEvent = false;\n isRefocusing = false;\n };\n\n let raf = requestAnimationFrame(cleanup);\n return cleanup;\n}\n"],"names":[],"version":3,"file":"utils.module.js.map"}
|
package/src/PressResponder.tsx
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {FocusableElement} from '@react-types/shared';
|
|
14
|
-
import {mergeProps, useObjectRef, useSyncRef} from '@react-aria/utils';
|
|
15
|
-
import {PressProps} from './usePress';
|
|
16
|
-
import {PressResponderContext} from './context';
|
|
17
|
-
import React, {ForwardedRef, JSX, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
|
|
18
|
-
|
|
19
|
-
interface PressResponderProps extends PressProps {
|
|
20
|
-
children: ReactNode
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const PressResponder:
|
|
24
|
-
React.ForwardRefExoticComponent<PressResponderProps & React.RefAttributes<FocusableElement>> =
|
|
25
|
-
React.forwardRef(({children, ...props}: PressResponderProps, ref: ForwardedRef<FocusableElement>) => {
|
|
26
|
-
let isRegistered = useRef(false);
|
|
27
|
-
let prevContext = useContext(PressResponderContext);
|
|
28
|
-
let context: any = mergeProps(prevContext || {}, {
|
|
29
|
-
...props,
|
|
30
|
-
register() {
|
|
31
|
-
isRegistered.current = true;
|
|
32
|
-
if (prevContext) {
|
|
33
|
-
prevContext.register();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
context.ref = useObjectRef(ref || prevContext?.ref);
|
|
39
|
-
useSyncRef(prevContext, context.ref);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
if (!isRegistered.current) {
|
|
43
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
44
|
-
console.warn(
|
|
45
|
-
'A PressResponder was rendered without a pressable child. ' +
|
|
46
|
-
'Either call the usePress hook, or wrap your DOM node with <Pressable> component.'
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
isRegistered.current = true; // only warn once in strict mode.
|
|
50
|
-
}
|
|
51
|
-
}, []);
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
<PressResponderContext.Provider value={context}>
|
|
55
|
-
{children}
|
|
56
|
-
</PressResponderContext.Provider>
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
export function ClearPressResponder({children}: {children: ReactNode}): JSX.Element {
|
|
61
|
-
let context = useMemo(() => ({register: () => {}}), []);
|
|
62
|
-
return (
|
|
63
|
-
<PressResponderContext.Provider value={context}>
|
|
64
|
-
{children}
|
|
65
|
-
</PressResponderContext.Provider>
|
|
66
|
-
);
|
|
67
|
-
}
|
package/src/Pressable.tsx
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {DOMAttributes, FocusableElement} from '@react-types/shared';
|
|
14
|
-
import {getOwnerWindow, isFocusable, mergeProps, mergeRefs, useObjectRef} from '@react-aria/utils';
|
|
15
|
-
import {PressProps, usePress} from './usePress';
|
|
16
|
-
import React, {ForwardedRef, ReactElement, useEffect} from 'react';
|
|
17
|
-
import {useFocusable} from './useFocusable';
|
|
18
|
-
|
|
19
|
-
interface PressableProps extends PressProps {
|
|
20
|
-
children: ReactElement<DOMAttributes, string>
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const Pressable:
|
|
24
|
-
React.ForwardRefExoticComponent<PressableProps & React.RefAttributes<FocusableElement>> =
|
|
25
|
-
React.forwardRef(({children, ...props}: PressableProps, ref: ForwardedRef<FocusableElement>) => {
|
|
26
|
-
ref = useObjectRef(ref);
|
|
27
|
-
let {pressProps} = usePress({...props, ref});
|
|
28
|
-
let {focusableProps} = useFocusable(props, ref);
|
|
29
|
-
let child = React.Children.only(children);
|
|
30
|
-
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
if (process.env.NODE_ENV === 'production') {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let el = ref.current;
|
|
37
|
-
if (!el || !(el instanceof getOwnerWindow(el).Element)) {
|
|
38
|
-
console.error('<Pressable> child must forward its ref to a DOM element.');
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!props.isDisabled && !isFocusable(el)) {
|
|
43
|
-
console.warn('<Pressable> child must be focusable. Please ensure the tabIndex prop is passed through.');
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
el.localName !== 'button' &&
|
|
49
|
-
el.localName !== 'input' &&
|
|
50
|
-
el.localName !== 'select' &&
|
|
51
|
-
el.localName !== 'textarea' &&
|
|
52
|
-
el.localName !== 'a' &&
|
|
53
|
-
el.localName !== 'area' &&
|
|
54
|
-
el.localName !== 'summary'
|
|
55
|
-
) {
|
|
56
|
-
let role = el.getAttribute('role');
|
|
57
|
-
if (!role) {
|
|
58
|
-
console.warn('<Pressable> child must have an interactive ARIA role.');
|
|
59
|
-
} else if (
|
|
60
|
-
// https://w3c.github.io/aria/#widget_roles
|
|
61
|
-
role !== 'application' &&
|
|
62
|
-
role !== 'button' &&
|
|
63
|
-
role !== 'checkbox' &&
|
|
64
|
-
role !== 'combobox' &&
|
|
65
|
-
role !== 'gridcell' &&
|
|
66
|
-
role !== 'link' &&
|
|
67
|
-
role !== 'menuitem' &&
|
|
68
|
-
role !== 'menuitemcheckbox' &&
|
|
69
|
-
role !== 'menuitemradio' &&
|
|
70
|
-
role !== 'option' &&
|
|
71
|
-
role !== 'radio' &&
|
|
72
|
-
role !== 'searchbox' &&
|
|
73
|
-
role !== 'separator' &&
|
|
74
|
-
role !== 'slider' &&
|
|
75
|
-
role !== 'spinbutton' &&
|
|
76
|
-
role !== 'switch' &&
|
|
77
|
-
role !== 'tab' &&
|
|
78
|
-
role !== 'textbox' &&
|
|
79
|
-
role !== 'treeitem'
|
|
80
|
-
) {
|
|
81
|
-
console.warn(`<Pressable> child must have an interactive ARIA role. Got "${role}".`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}, [ref, props.isDisabled]);
|
|
85
|
-
|
|
86
|
-
// @ts-ignore
|
|
87
|
-
let childRef = parseInt(React.version, 10) < 19 ? child.ref : child.props.ref;
|
|
88
|
-
|
|
89
|
-
return React.cloneElement(
|
|
90
|
-
child,
|
|
91
|
-
{
|
|
92
|
-
...mergeProps(pressProps, focusableProps, child.props),
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
ref: mergeRefs(childRef, ref)
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
});
|
package/src/context.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {FocusableElement} from '@react-types/shared';
|
|
14
|
-
import {PressProps} from './usePress';
|
|
15
|
-
import React, {MutableRefObject} from 'react';
|
|
16
|
-
|
|
17
|
-
interface IPressResponderContext extends PressProps {
|
|
18
|
-
register(): void,
|
|
19
|
-
ref?: MutableRefObject<FocusableElement>
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const PressResponderContext: React.Context<IPressResponderContext> = React.createContext<IPressResponderContext>({register: () => {}});
|
|
23
|
-
PressResponderContext.displayName = 'PressResponderContext';
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {BaseEvent} from '@react-types/shared';
|
|
14
|
-
import {SyntheticEvent} from 'react';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* This function wraps a React event handler to make stopPropagation the default, and support continuePropagation instead.
|
|
18
|
-
*/
|
|
19
|
-
export function createEventHandler<T extends SyntheticEvent>(handler?: (e: BaseEvent<T>) => void): ((e: T) => void) | undefined {
|
|
20
|
-
if (!handler) {
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
let shouldStopPropagation = true;
|
|
25
|
-
return (e: T) => {
|
|
26
|
-
let event: BaseEvent<T> = {
|
|
27
|
-
...e,
|
|
28
|
-
preventDefault() {
|
|
29
|
-
e.preventDefault();
|
|
30
|
-
},
|
|
31
|
-
isDefaultPrevented() {
|
|
32
|
-
return e.isDefaultPrevented();
|
|
33
|
-
},
|
|
34
|
-
stopPropagation() {
|
|
35
|
-
if (shouldStopPropagation && process.env.NODE_ENV !== 'production') {
|
|
36
|
-
console.error('stopPropagation is now the default behavior for events in React Spectrum. You can use continuePropagation() to revert this behavior.');
|
|
37
|
-
} else {
|
|
38
|
-
shouldStopPropagation = true;
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
continuePropagation() {
|
|
42
|
-
shouldStopPropagation = false;
|
|
43
|
-
},
|
|
44
|
-
isPropagationStopped() {
|
|
45
|
-
return shouldStopPropagation;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
handler(event);
|
|
50
|
-
|
|
51
|
-
if (shouldStopPropagation) {
|
|
52
|
-
e.stopPropagation();
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
}
|
package/src/focusSafely.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {FocusableElement} from '@react-types/shared';
|
|
14
|
-
import {
|
|
15
|
-
focusWithoutScrolling,
|
|
16
|
-
getActiveElement,
|
|
17
|
-
getOwnerDocument,
|
|
18
|
-
runAfterTransition
|
|
19
|
-
} from '@react-aria/utils';
|
|
20
|
-
import {getInteractionModality} from './useFocusVisible';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* A utility function that focuses an element while avoiding undesired side effects such
|
|
24
|
-
* as page scrolling and screen reader issues with CSS transitions.
|
|
25
|
-
*/
|
|
26
|
-
export function focusSafely(element: FocusableElement): void {
|
|
27
|
-
if (!element.isConnected) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// If the user is interacting with a virtual cursor, e.g. screen reader, then
|
|
32
|
-
// wait until after any animated transitions that are currently occurring on
|
|
33
|
-
// the page before shifting focus. This avoids issues with VoiceOver on iOS
|
|
34
|
-
// causing the page to scroll when moving focus if the element is transitioning
|
|
35
|
-
// from off the screen.
|
|
36
|
-
const ownerDocument = getOwnerDocument(element);
|
|
37
|
-
if (getInteractionModality() === 'virtual') {
|
|
38
|
-
let lastFocusedElement = getActiveElement(ownerDocument);
|
|
39
|
-
runAfterTransition(() => {
|
|
40
|
-
const activeElement = getActiveElement(ownerDocument);
|
|
41
|
-
// If focus did not move or focus was lost to the body, and the element is still in the document, focus it.
|
|
42
|
-
if ((activeElement === lastFocusedElement || activeElement === ownerDocument.body) && element.isConnected) {
|
|
43
|
-
focusWithoutScrolling(element);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
} else {
|
|
47
|
-
focusWithoutScrolling(element);
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/textSelection.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {getOwnerDocument, isIOS, runAfterTransition} from '@react-aria/utils';
|
|
14
|
-
|
|
15
|
-
// Safari on iOS starts selecting text on long press. The only way to avoid this, it seems,
|
|
16
|
-
// is to add user-select: none to the entire page. Adding it to the pressable element prevents
|
|
17
|
-
// that element from being selected, but nearby elements may still receive selection. We add
|
|
18
|
-
// user-select: none on touch start, and remove it again on touch end to prevent this.
|
|
19
|
-
// This must be implemented using global state to avoid race conditions between multiple elements.
|
|
20
|
-
|
|
21
|
-
// There are three possible states due to the delay before removing user-select: none after
|
|
22
|
-
// pointer up. The 'default' state always transitions to the 'disabled' state, which transitions
|
|
23
|
-
// to 'restoring'. The 'restoring' state can either transition back to 'disabled' or 'default'.
|
|
24
|
-
|
|
25
|
-
// For non-iOS devices, we apply user-select: none to the pressed element instead to avoid possible
|
|
26
|
-
// performance issues that arise from applying and removing user-select: none to the entire page
|
|
27
|
-
// (see https://github.com/adobe/react-spectrum/issues/1609).
|
|
28
|
-
type State = 'default' | 'disabled' | 'restoring';
|
|
29
|
-
|
|
30
|
-
// Note that state only matters here for iOS. Non-iOS gets user-select: none applied to the target element
|
|
31
|
-
// rather than at the document level so we just need to apply/remove user-select: none for each pressed element individually
|
|
32
|
-
let state: State = 'default';
|
|
33
|
-
let savedUserSelect = '';
|
|
34
|
-
let modifiedElementMap = new WeakMap<Element, string>();
|
|
35
|
-
|
|
36
|
-
export function disableTextSelection(target?: Element): void {
|
|
37
|
-
if (isIOS()) {
|
|
38
|
-
if (state === 'default') {
|
|
39
|
-
|
|
40
|
-
const documentObject = getOwnerDocument(target);
|
|
41
|
-
savedUserSelect = documentObject.documentElement.style.webkitUserSelect;
|
|
42
|
-
documentObject.documentElement.style.webkitUserSelect = 'none';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
state = 'disabled';
|
|
46
|
-
} else if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
47
|
-
// If not iOS, store the target's original user-select and change to user-select: none
|
|
48
|
-
// Ignore state since it doesn't apply for non iOS
|
|
49
|
-
let property = 'userSelect' in target.style ? 'userSelect' : 'webkitUserSelect';
|
|
50
|
-
modifiedElementMap.set(target, target.style[property]);
|
|
51
|
-
target.style[property] = 'none';
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function restoreTextSelection(target?: Element): void {
|
|
56
|
-
if (isIOS()) {
|
|
57
|
-
// If the state is already default, there's nothing to do.
|
|
58
|
-
// If it is restoring, then there's no need to queue a second restore.
|
|
59
|
-
if (state !== 'disabled') {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
state = 'restoring';
|
|
64
|
-
|
|
65
|
-
// There appears to be a delay on iOS where selection still might occur
|
|
66
|
-
// after pointer up, so wait a bit before removing user-select.
|
|
67
|
-
setTimeout(() => {
|
|
68
|
-
// Wait for any CSS transitions to complete so we don't recompute style
|
|
69
|
-
// for the whole page in the middle of the animation and cause jank.
|
|
70
|
-
runAfterTransition(() => {
|
|
71
|
-
// Avoid race conditions
|
|
72
|
-
if (state === 'restoring') {
|
|
73
|
-
|
|
74
|
-
const documentObject = getOwnerDocument(target);
|
|
75
|
-
if (documentObject.documentElement.style.webkitUserSelect === 'none') {
|
|
76
|
-
documentObject.documentElement.style.webkitUserSelect = savedUserSelect || '';
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
savedUserSelect = '';
|
|
80
|
-
state = 'default';
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}, 300);
|
|
84
|
-
} else if (target instanceof HTMLElement || target instanceof SVGElement) {
|
|
85
|
-
// If not iOS, restore the target's original user-select if any
|
|
86
|
-
// Ignore state since it doesn't apply for non iOS
|
|
87
|
-
if (target && modifiedElementMap.has(target)) {
|
|
88
|
-
let targetOldUserSelect = modifiedElementMap.get(target) as string;
|
|
89
|
-
let property = 'userSelect' in target.style ? 'userSelect' : 'webkitUserSelect';
|
|
90
|
-
|
|
91
|
-
if (target.style[property] === 'none') {
|
|
92
|
-
target.style[property] = targetOldUserSelect;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (target.getAttribute('style') === '') {
|
|
96
|
-
target.removeAttribute('style');
|
|
97
|
-
}
|
|
98
|
-
modifiedElementMap.delete(target);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
package/src/useFocus.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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
|
-
// Portions of the code in this file are based on code from react.
|
|
14
|
-
// Original licensing for the following can be found in the
|
|
15
|
-
// NOTICE file in the root directory of this source tree.
|
|
16
|
-
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
17
|
-
|
|
18
|
-
import {DOMAttributes, FocusableElement, FocusEvents} from '@react-types/shared';
|
|
19
|
-
import {FocusEvent, useCallback} from 'react';
|
|
20
|
-
import {getActiveElement, getEventTarget, getOwnerDocument} from '@react-aria/utils';
|
|
21
|
-
import {useSyntheticBlurEvent} from './utils';
|
|
22
|
-
|
|
23
|
-
export interface FocusProps<Target = FocusableElement> extends FocusEvents<Target> {
|
|
24
|
-
/** Whether the focus events should be disabled. */
|
|
25
|
-
isDisabled?: boolean
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface FocusResult<Target = FocusableElement> {
|
|
29
|
-
/** Props to spread onto the target element. */
|
|
30
|
-
focusProps: DOMAttributes<Target>
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Handles focus events for the immediate target.
|
|
35
|
-
* Focus events on child elements will be ignored.
|
|
36
|
-
*/
|
|
37
|
-
export function useFocus<Target extends FocusableElement = FocusableElement>(props: FocusProps<Target>): FocusResult<Target> {
|
|
38
|
-
let {
|
|
39
|
-
isDisabled,
|
|
40
|
-
onFocus: onFocusProp,
|
|
41
|
-
onBlur: onBlurProp,
|
|
42
|
-
onFocusChange
|
|
43
|
-
} = props;
|
|
44
|
-
|
|
45
|
-
const onBlur: FocusProps<Target>['onBlur'] = useCallback((e: FocusEvent<Target>) => {
|
|
46
|
-
if (getEventTarget(e) === e.currentTarget) {
|
|
47
|
-
if (onBlurProp) {
|
|
48
|
-
onBlurProp(e);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (onFocusChange) {
|
|
52
|
-
onFocusChange(false);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
}, [onBlurProp, onFocusChange]);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const onSyntheticFocus = useSyntheticBlurEvent<Target>(onBlur);
|
|
61
|
-
|
|
62
|
-
const onFocus: FocusProps<Target>['onFocus'] = useCallback((e: FocusEvent<Target>) => {
|
|
63
|
-
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
64
|
-
// focus handler already moved focus somewhere else.
|
|
65
|
-
|
|
66
|
-
let eventTarget = getEventTarget(e);
|
|
67
|
-
const ownerDocument = getOwnerDocument(eventTarget);
|
|
68
|
-
const activeElement = ownerDocument ? getActiveElement(ownerDocument) : getActiveElement();
|
|
69
|
-
if (eventTarget === e.currentTarget && eventTarget === activeElement) {
|
|
70
|
-
if (onFocusProp) {
|
|
71
|
-
onFocusProp(e);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (onFocusChange) {
|
|
75
|
-
onFocusChange(true);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
onSyntheticFocus(e);
|
|
79
|
-
}
|
|
80
|
-
}, [onFocusChange, onFocusProp, onSyntheticFocus]);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
focusProps: {
|
|
84
|
-
onFocus: (!isDisabled && (onFocusProp || onFocusChange || onBlurProp)) ? onFocus : undefined,
|
|
85
|
-
onBlur: (!isDisabled && (onBlurProp || onFocusChange)) ? onBlur : undefined
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|