Skip to content

Commit 3010555

Browse files
committed
Delay user-select: none reset
1 parent d6f340e commit 3010555

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
cursorPointer,
88
elementSupportsDisabled,
99
classNameToString,
10-
setUserSelectOnBody,
10+
setUserSelectNone,
11+
resetUserSelect,
1112
} from './utils';
1213
import {
1314
PolymorphicForwardRefExoticComponent,
@@ -666,8 +667,8 @@ const InteractiveNotMemoized: PolymorphicForwardRefExoticComponent<
666667
// because iOS will still select nearby text if it is only set on the element
667668
React.useEffect(() => {
668669
if (inTouchActiveState && useExtendedTouchActive) {
669-
setUserSelectOnBody('none');
670-
return () => setUserSelectOnBody('');
670+
setUserSelectNone();
671+
return resetUserSelect;
671672
}
672673
return;
673674
}, [inTouchActiveState, useExtendedTouchActive]);

src/utils.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,22 @@ export const classNameToString = (className: any) =>
6161
// to prevent the browser from selecting text on long touch
6262
// note that it needs to be set on the body not the RI element
6363
// because iOS will still select nearby text
64-
export const setUserSelectOnBody = (value: 'none' | ''): void => {
65-
document.body.style.userSelect = value;
66-
document.body.style.webkitUserSelect = value;
64+
let pendingUserSelectReset = false;
65+
66+
export const setUserSelectNone = (): void => {
67+
pendingUserSelectReset = false;
68+
document.body.style.userSelect = 'none';
69+
document.body.style.webkitUserSelect = 'none';
70+
};
71+
72+
export const resetUserSelect = (): void => {
73+
pendingUserSelectReset = true;
74+
// use setTimeout delay for reset because iOS will select text shortly after touch end
75+
// when the touch end event occurs near the time that iOS would normally select text
76+
window.setTimeout(() => {
77+
if (pendingUserSelectReset) {
78+
document.body.style.userSelect = '';
79+
document.body.style.webkitUserSelect = '';
80+
}
81+
}, 250);
6782
};

0 commit comments

Comments
 (0)