UNPKG

4.27 kBJavaScriptView Raw
1import {isIOS as $7R18e$isIOS, getOwnerDocument as $7R18e$getOwnerDocument, runAfterTransition as $7R18e$runAfterTransition} from "@react-aria/utils";
2
3/*
4 * Copyright 2020 Adobe. All rights reserved.
5 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License. You may obtain a copy
7 * of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under
10 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11 * OF ANY KIND, either express or implied. See the License for the specific language
12 * governing permissions and limitations under the License.
13 */
14// Note that state only matters here for iOS. Non-iOS gets user-select: none applied to the target element
15// rather than at the document level so we just need to apply/remove user-select: none for each pressed element individually
16let $14c0b72509d70225$var$state = 'default';
17let $14c0b72509d70225$var$savedUserSelect = '';
18let $14c0b72509d70225$var$modifiedElementMap = new WeakMap();
19function $14c0b72509d70225$export$16a4697467175487(target) {
20 if ((0, $7R18e$isIOS)()) {
21 if ($14c0b72509d70225$var$state === 'default') {
22 // eslint-disable-next-line no-restricted-globals
23 const documentObject = (0, $7R18e$getOwnerDocument)(target);
24 $14c0b72509d70225$var$savedUserSelect = documentObject.documentElement.style.webkitUserSelect;
25 documentObject.documentElement.style.webkitUserSelect = 'none';
26 }
27 $14c0b72509d70225$var$state = 'disabled';
28 } else if (target instanceof HTMLElement || target instanceof SVGElement) {
29 // If not iOS, store the target's original user-select and change to user-select: none
30 // Ignore state since it doesn't apply for non iOS
31 $14c0b72509d70225$var$modifiedElementMap.set(target, target.style.userSelect);
32 target.style.userSelect = 'none';
33 }
34}
35function $14c0b72509d70225$export$b0d6fa1ab32e3295(target) {
36 if ((0, $7R18e$isIOS)()) {
37 // If the state is already default, there's nothing to do.
38 // If it is restoring, then there's no need to queue a second restore.
39 if ($14c0b72509d70225$var$state !== 'disabled') return;
40 $14c0b72509d70225$var$state = 'restoring';
41 // There appears to be a delay on iOS where selection still might occur
42 // after pointer up, so wait a bit before removing user-select.
43 setTimeout(()=>{
44 // Wait for any CSS transitions to complete so we don't recompute style
45 // for the whole page in the middle of the animation and cause jank.
46 (0, $7R18e$runAfterTransition)(()=>{
47 // Avoid race conditions
48 if ($14c0b72509d70225$var$state === 'restoring') {
49 // eslint-disable-next-line no-restricted-globals
50 const documentObject = (0, $7R18e$getOwnerDocument)(target);
51 if (documentObject.documentElement.style.webkitUserSelect === 'none') documentObject.documentElement.style.webkitUserSelect = $14c0b72509d70225$var$savedUserSelect || '';
52 $14c0b72509d70225$var$savedUserSelect = '';
53 $14c0b72509d70225$var$state = 'default';
54 }
55 });
56 }, 300);
57 } else if (target instanceof HTMLElement || target instanceof SVGElement) // If not iOS, restore the target's original user-select if any
58 // Ignore state since it doesn't apply for non iOS
59 {
60 if (target && $14c0b72509d70225$var$modifiedElementMap.has(target)) {
61 let targetOldUserSelect = $14c0b72509d70225$var$modifiedElementMap.get(target);
62 if (target.style.userSelect === 'none') target.style.userSelect = targetOldUserSelect;
63 if (target.getAttribute('style') === '') target.removeAttribute('style');
64 $14c0b72509d70225$var$modifiedElementMap.delete(target);
65 }
66 }
67}
68
69
70export {$14c0b72509d70225$export$16a4697467175487 as disableTextSelection, $14c0b72509d70225$export$b0d6fa1ab32e3295 as restoreTextSelection};
71//# sourceMappingURL=textSelection.module.js.map