UNPKG

4.26 kBPlain TextView Raw
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
13import {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).
28type 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
32let state: State = 'default';
33let savedUserSelect = '';
34let modifiedElementMap = new WeakMap<Element, string>();
35
36export function disableTextSelection(target?: Element) {
37 if (isIOS()) {
38 if (state === 'default') {
39 savedUserSelect = document.documentElement.style.webkitUserSelect;
40 document.documentElement.style.webkitUserSelect = 'none';
41 }
42
43 state = 'disabled';
44 } else if (target instanceof HTMLElement || target instanceof SVGElement) {
45 // If not iOS, store the target's original user-select and change to user-select: none
46 // Ignore state since it doesn't apply for non iOS
47 modifiedElementMap.set(target, target.style.userSelect);
48 target.style.userSelect = 'none';
49 }
50}
51
52export function restoreTextSelection(target?: Element) {
53 if (isIOS()) {
54 // If the state is already default, there's nothing to do.
55 // If it is restoring, then there's no need to queue a second restore.
56 if (state !== 'disabled') {
57 return;
58 }
59
60 state = 'restoring';
61
62 // There appears to be a delay on iOS where selection still might occur
63 // after pointer up, so wait a bit before removing user-select.
64 setTimeout(() => {
65 // Wait for any CSS transitions to complete so we don't recompute style
66 // for the whole page in the middle of the animation and cause jank.
67 runAfterTransition(() => {
68 // Avoid race conditions
69 if (state === 'restoring') {
70 if (document.documentElement.style.webkitUserSelect === 'none') {
71 document.documentElement.style.webkitUserSelect = savedUserSelect || '';
72 }
73
74 savedUserSelect = '';
75 state = 'default';
76 }
77 });
78 }, 300);
79 } else if (target instanceof HTMLElement || target instanceof SVGElement) {
80 // If not iOS, restore the target's original user-select if any
81 // Ignore state since it doesn't apply for non iOS
82 if (target && modifiedElementMap.has(target)) {
83 let targetOldUserSelect = modifiedElementMap.get(target);
84
85 if (target.style.userSelect === 'none') {
86 target.style.userSelect = targetOldUserSelect;
87 }
88
89 if (target.getAttribute('style') === '') {
90 target.removeAttribute('style');
91 }
92 modifiedElementMap.delete(target);
93 }
94 }
95}