Skip to content

Commit 21501b7

Browse files
authored
Add getOwnerDocument and getOwnerWindow utils and fix usePress (#5096)
* Add getOwnerDocument and getOwnerWindow utils and fix usePress
1 parent 9d0d443 commit 21501b7

File tree

7 files changed

+428
-15
lines changed

7 files changed

+428
-15
lines changed

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ module.exports = {
6565
'jsdoc/require-jsdoc': OFF,
6666
'jsdoc/require-description': OFF
6767
}
68+
}, {
69+
files: ['packages/@react-aria/interactions/**/*.ts', 'packages/@react-aria/interactions/**/*.tsx'],
70+
rules: {
71+
'no-restricted-globals': [
72+
WARN,
73+
{
74+
'name': 'window',
75+
'message': 'Use getOwnerWindow from @react-aria/utils instead.'
76+
},
77+
{
78+
'name': 'document',
79+
'message': 'Use getOwnerDocument from @react-aria/utils instead.'
80+
}
81+
]
82+
}
6883
}],
6984
env: {
7085
'browser': true,

packages/@react-aria/interactions/src/textSelection.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {isIOS, runAfterTransition} from '@react-aria/utils';
13+
import {getOwnerDocument, isIOS, runAfterTransition} from '@react-aria/utils';
1414

1515
// Safari on iOS starts selecting text on long press. The only way to avoid this, it seems,
1616
// is to add user-select: none to the entire page. Adding it to the pressable element prevents
@@ -36,8 +36,9 @@ let modifiedElementMap = new WeakMap<Element, string>();
3636
export function disableTextSelection(target?: Element) {
3737
if (isIOS()) {
3838
if (state === 'default') {
39-
savedUserSelect = document.documentElement.style.webkitUserSelect;
40-
document.documentElement.style.webkitUserSelect = 'none';
39+
const documentObject = getOwnerDocument(target);
40+
savedUserSelect = documentObject.documentElement.style.webkitUserSelect;
41+
documentObject.documentElement.style.webkitUserSelect = 'none';
4142
}
4243

4344
state = 'disabled';
@@ -67,8 +68,9 @@ export function restoreTextSelection(target?: Element) {
6768
runAfterTransition(() => {
6869
// Avoid race conditions
6970
if (state === 'restoring') {
70-
if (document.documentElement.style.webkitUserSelect === 'none') {
71-
document.documentElement.style.webkitUserSelect = savedUserSelect || '';
71+
const documentObject = getOwnerDocument(target);
72+
if (documentObject.documentElement.style.webkitUserSelect === 'none') {
73+
documentObject.documentElement.style.webkitUserSelect = savedUserSelect || '';
7274
}
7375

7476
savedUserSelect = '';

packages/@react-aria/interactions/src/usePress.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import {disableTextSelection, restoreTextSelection} from './textSelection';
1919
import {DOMAttributes, FocusableElement, PressEvent as IPressEvent, PointerType, PressEvents} from '@react-types/shared';
20-
import {focusWithoutScrolling, isMac, isVirtualClick, isVirtualPointerEvent, mergeProps, openLink, useEffectEvent, useGlobalListeners, useSyncRef} from '@react-aria/utils';
20+
import {focusWithoutScrolling, getOwnerDocument, getOwnerWindow, isMac, isVirtualClick, isVirtualPointerEvent, mergeProps, openLink, useEffectEvent, useGlobalListeners, useSyncRef} from '@react-aria/utils';
2121
import {PressResponderContext} from './context';
2222
import {RefObject, useContext, useEffect, useMemo, useRef, useState} from 'react';
2323

@@ -271,7 +271,7 @@ export function usePress(props: PressHookProps): PressResult {
271271

272272
// Focus may move before the key up event, so register the event on the document
273273
// instead of the same element where the key down event occurred.
274-
addGlobalListener(document, 'keyup', onKeyUp, false);
274+
addGlobalListener(getOwnerDocument(e.currentTarget), 'keyup', onKeyUp, false);
275275
}
276276

277277
if (shouldStopPropagation) {
@@ -410,9 +410,9 @@ export function usePress(props: PressHookProps): PressResult {
410410

411411
shouldStopPropagation = triggerPressStart(e, state.pointerType);
412412

413-
addGlobalListener(document, 'pointermove', onPointerMove, false);
414-
addGlobalListener(document, 'pointerup', onPointerUp, false);
415-
addGlobalListener(document, 'pointercancel', onPointerCancel, false);
413+
addGlobalListener(getOwnerDocument(e.currentTarget), 'pointermove', onPointerMove, false);
414+
addGlobalListener(getOwnerDocument(e.currentTarget), 'pointerup', onPointerUp, false);
415+
addGlobalListener(getOwnerDocument(e.currentTarget), 'pointercancel', onPointerCancel, false);
416416
}
417417

418418
if (shouldStopPropagation) {
@@ -534,7 +534,7 @@ export function usePress(props: PressHookProps): PressResult {
534534
e.stopPropagation();
535535
}
536536

537-
addGlobalListener(document, 'mouseup', onMouseUp, false);
537+
addGlobalListener(getOwnerDocument(e.currentTarget), 'mouseup', onMouseUp, false);
538538
};
539539

540540
pressProps.onMouseEnter = (e) => {
@@ -634,7 +634,7 @@ export function usePress(props: PressHookProps): PressResult {
634634
e.stopPropagation();
635635
}
636636

637-
addGlobalListener(window, 'scroll', onScroll, true);
637+
addGlobalListener(getOwnerWindow(e.currentTarget), 'scroll', onScroll, true);
638638
};
639639

640640
pressProps.onTouchMove = (e) => {
@@ -773,8 +773,8 @@ function isValidKeyboardEvent(event: KeyboardEvent, currentTarget: Element): boo
773773
// "Spacebar" is for IE 11
774774
return (
775775
(key === 'Enter' || key === ' ' || key === 'Spacebar' || code === 'Space') &&
776-
!((element instanceof HTMLInputElement && !isValidInputKey(element, key)) ||
777-
element instanceof HTMLTextAreaElement ||
776+
!((element instanceof getOwnerWindow(element).HTMLInputElement && !isValidInputKey(element, key)) ||
777+
element instanceof getOwnerWindow(element).HTMLTextAreaElement ||
778778
element.isContentEditable) &&
779779
// Links should only trigger with Enter key
780780
!((role === 'link' || (!role && isHTMLAnchorLink(element))) && key !== 'Enter')

0 commit comments

Comments
 (0)