@react-aria/overlays 3.29.1 → 3.30.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/types.d.ts.map +1 -1
- package/dist/usePreventScroll.main.js +81 -83
- package/dist/usePreventScroll.main.js.map +1 -1
- package/dist/usePreventScroll.mjs +82 -84
- package/dist/usePreventScroll.module.js +82 -84
- package/dist/usePreventScroll.module.js.map +1 -1
- package/package.json +11 -11
- package/src/usePreventScroll.ts +94 -109
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;AEoBA,kCAAmC,SAAQ,aAAa;IACtD;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;OAEG;IACH,SAAS,EAAE,UAAU,OAAO,GAAG,IAAI,CAAC,CAAC;IACrC;;OAEG;IACH,UAAU,EAAE,UAAU,OAAO,GAAG,IAAI,CAAC,CAAC;IACtC;;OAEG;IACH,QAAQ,CAAC,EAAE,UAAU,OAAO,GAAG,IAAI,CAAC,CAAC;IACrC;;;OAGG;IACH,SAAS,CAAC,EAAE,UAAU,OAAO,GAAG,IAAI,CAAC,CAAC;IACtC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,4DAA4D;IAC5D,OAAO,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC;IAC9B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED;IACE,+CAA+C;IAC/C,YAAY,EAAE,aAAa,CAAC;IAC5B,8CAA8C;IAC9C,UAAU,EAAE,aAAa,CAAC;IAC1B,oEAAoE;IACpE,SAAS,EAAE,aAAa,GAAG,IAAI,CAAC;IAChC,0FAA0F;IAC1F,kBAAkB,EAAE;QA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AiCrE;AAED,eAAe;AACf,0CAA0C,IAAI,CAM7C;ACzED,sCAAuC,SAAQ,IAAI,CAAC,gBAAgB,EAAE,8BAA8B,CAAC;IACnG;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;OAGG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAA;CACpC;AAED;IACE,mCAAmC;IACnC,UAAU,EAAE,aAAa,CAAC;IAC1B,sCAAsC;IACtC,aAAa,EAAE,aAAa,CAAA;CAC7B;AAED;;;GAGG;AACH,gCAAgC,KAAK,EAAE,qBAAqB,EAAE,KAAK,EAAE,mBAAmB,EAAE,GAAG,EAAE,UAAU,WAAW,GAAG,IAAI,CAAC,GAAG,gBAAgB,CAuB9I;ACrCD,YAAY,EAAC,SAAS,EAAE,aAAa,EAAE,aAAa,EAAC,MAAM,uBAAuB,CAAC","sources":["packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/calculatePosition.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useCloseOnScroll.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useOverlayPosition.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useOverlay.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useOverlayTrigger.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/usePreventScroll.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/PortalProvider.tsx","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useModal.tsx","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/DismissButton.tsx","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/ariaHideOutside.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/usePopover.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/Overlay.tsx","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/useModalOverlay.ts","packages/@react-aria/overlays/src/packages/@react-aria/overlays/src/index.ts","packages/@react-aria/overlays/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,"/*\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 */\nexport {useOverlayPosition} from './useOverlayPosition';\nexport {useOverlay} from './useOverlay';\nexport {useOverlayTrigger} from './useOverlayTrigger';\nexport {usePreventScroll} from './usePreventScroll';\nexport {ModalProvider, useModalProvider, OverlayProvider, OverlayContainer, useModal} from './useModal';\nexport {DismissButton} from './DismissButton';\nexport {ariaHideOutside} from './ariaHideOutside';\nexport {usePopover} from './usePopover';\nexport {useModalOverlay} from './useModalOverlay';\nexport {Overlay, useOverlayFocusContain} from './Overlay';\nexport {UNSAFE_PortalProvider, useUNSAFE_PortalContext} from './PortalProvider';\n\nexport type {AriaPositionProps, PositionAria} from './useOverlayPosition';\nexport type {AriaOverlayProps, OverlayAria} from './useOverlay';\nexport type {OverlayTriggerAria, OverlayTriggerProps} from './useOverlayTrigger';\nexport type {AriaModalOptions, ModalAria, ModalProviderAria, ModalProviderProps, OverlayContainerProps} from './useModal';\nexport type {DismissButtonProps} from './DismissButton';\nexport type {AriaPopoverProps, PopoverAria} from './usePopover';\nexport type {AriaModalOverlayProps, ModalOverlayAria} from './useModalOverlay';\nexport type {OverlayProps} from './Overlay';\nexport type {Placement, PlacementAxis, PositionProps} from '@react-types/overlays';\nexport type {PortalProviderProps, PortalProviderContextValue} from './PortalProvider';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -18,18 +18,6 @@ $parcel$export(module.exports, "usePreventScroll", () => $5c2f5cd01815d369$expor
|
|
|
18
18
|
* governing permissions and limitations under the License.
|
|
19
19
|
*/
|
|
20
20
|
const $5c2f5cd01815d369$var$visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
21
|
-
// HTML input types that do not cause the software keyboard to appear.
|
|
22
|
-
const $5c2f5cd01815d369$var$nonTextInputTypes = new Set([
|
|
23
|
-
'checkbox',
|
|
24
|
-
'radio',
|
|
25
|
-
'range',
|
|
26
|
-
'color',
|
|
27
|
-
'file',
|
|
28
|
-
'image',
|
|
29
|
-
'button',
|
|
30
|
-
'submit',
|
|
31
|
-
'reset'
|
|
32
|
-
]);
|
|
33
21
|
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
34
22
|
let $5c2f5cd01815d369$var$preventScrollCount = 0;
|
|
35
23
|
let $5c2f5cd01815d369$var$restore;
|
|
@@ -74,29 +62,41 @@ function $5c2f5cd01815d369$var$preventScrollStandard() {
|
|
|
74
62
|
// on the window.
|
|
75
63
|
// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
|
|
76
64
|
// the top or bottom. Work around a bug where this does not work when the element does not actually overflow
|
|
77
|
-
// by preventing default in a `touchmove` event.
|
|
65
|
+
// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch
|
|
66
|
+
// zooming or when an element contains text selection, which may allow scrolling in some cases.
|
|
78
67
|
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
79
|
-
// 4. When
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
83
|
-
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
84
|
-
// above work or Safari will still try to scroll the page when focusing an input.
|
|
85
|
-
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
86
|
-
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
68
|
+
// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents
|
|
69
|
+
// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view
|
|
70
|
+
// ourselves, without scrolling the whole page.
|
|
87
71
|
function $5c2f5cd01815d369$var$preventScrollMobileSafari() {
|
|
88
72
|
let scrollable;
|
|
89
|
-
let
|
|
73
|
+
let allowTouchMove = false;
|
|
90
74
|
let onTouchStart = (e)=>{
|
|
91
75
|
// Store the nearest scrollable parent element from the element that the user touched.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
if (
|
|
76
|
+
let target = e.target;
|
|
77
|
+
scrollable = (0, $59kHH$reactariautils.isScrollable)(target) ? target : (0, $59kHH$reactariautils.getScrollParent)(target, true);
|
|
78
|
+
allowTouchMove = false;
|
|
79
|
+
// If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.
|
|
80
|
+
let selection = target.ownerDocument.defaultView.getSelection();
|
|
81
|
+
if (selection && !selection.isCollapsed && selection.containsNode(target, true)) allowTouchMove = true;
|
|
82
|
+
// If this is a focused input element with a selected range, allow user to drag the selection handles.
|
|
83
|
+
if ('selectionStart' in target && 'selectionEnd' in target && target.selectionStart < target.selectionEnd && target.ownerDocument.activeElement === target) allowTouchMove = true;
|
|
98
84
|
};
|
|
85
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
86
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
87
|
+
// the window instead.
|
|
88
|
+
// This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
|
|
89
|
+
let style = document.createElement('style');
|
|
90
|
+
style.textContent = `
|
|
91
|
+
@layer {
|
|
92
|
+
* {
|
|
93
|
+
overscroll-behavior: contain;
|
|
94
|
+
}
|
|
95
|
+
}`.trim();
|
|
96
|
+
document.head.prepend(style);
|
|
99
97
|
let onTouchMove = (e)=>{
|
|
98
|
+
// Allow pinch-zooming.
|
|
99
|
+
if (e.touches.length === 2 || allowTouchMove) return;
|
|
100
100
|
// Prevent scrolling the window.
|
|
101
101
|
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
102
102
|
e.preventDefault();
|
|
@@ -110,53 +110,39 @@ function $5c2f5cd01815d369$var$preventScrollMobileSafari() {
|
|
|
110
110
|
// because it must be set before the touchstart event.
|
|
111
111
|
if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) e.preventDefault();
|
|
112
112
|
};
|
|
113
|
-
let
|
|
114
|
-
if (restoreScrollableStyles) restoreScrollableStyles();
|
|
115
|
-
};
|
|
116
|
-
let onFocus = (e)=>{
|
|
113
|
+
let onBlur = (e)=>{
|
|
117
114
|
let target = e.target;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
$5c2f5cd01815d369$var$visualViewport.addEventListener('resize', ()=>$5c2f5cd01815d369$var$scrollIntoView(target), {
|
|
136
|
-
once: true
|
|
137
|
-
});
|
|
138
|
-
}
|
|
115
|
+
let relatedTarget = e.relatedTarget;
|
|
116
|
+
if (relatedTarget && (0, $59kHH$reactariautils.willOpenKeyboard)(relatedTarget)) {
|
|
117
|
+
// Focus without scrolling the whole page, and then scroll into view manually.
|
|
118
|
+
relatedTarget.focus({
|
|
119
|
+
preventScroll: true
|
|
120
|
+
});
|
|
121
|
+
$5c2f5cd01815d369$var$scrollIntoViewWhenReady(relatedTarget, (0, $59kHH$reactariautils.willOpenKeyboard)(target));
|
|
122
|
+
} else if (!relatedTarget) {
|
|
123
|
+
var _target_parentElement;
|
|
124
|
+
// When tapping the Done button on the keyboard, focus moves to the body.
|
|
125
|
+
// FocusScope will then restore focus back to the input. Later when tapping
|
|
126
|
+
// the same input again, it is already focused, so no blur event will fire,
|
|
127
|
+
// resulting in the flow above never running and Safari's native scrolling occurring.
|
|
128
|
+
// Instead, move focus to the parent focusable element (e.g. the dialog).
|
|
129
|
+
let focusable = (_target_parentElement = target.parentElement) === null || _target_parentElement === void 0 ? void 0 : _target_parentElement.closest('[tabindex]');
|
|
130
|
+
focusable === null || focusable === void 0 ? void 0 : focusable.focus({
|
|
131
|
+
preventScroll: true
|
|
139
132
|
});
|
|
140
133
|
}
|
|
141
134
|
};
|
|
142
|
-
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
152
|
-
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
153
|
-
let scrollX = window.pageXOffset;
|
|
154
|
-
let scrollY = window.pageYOffset;
|
|
155
|
-
restoreStyles = (0, $59kHH$reactariautils.chain)($5c2f5cd01815d369$var$addEvent(window, 'scroll', onWindowScroll), $5c2f5cd01815d369$var$setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`), $5c2f5cd01815d369$var$setStyle(document.documentElement, 'overflow', 'hidden'), $5c2f5cd01815d369$var$setStyle(document.body, 'marginTop', `-${scrollY}px`), ()=>{
|
|
156
|
-
window.scrollTo(scrollX, scrollY);
|
|
135
|
+
// Override programmatic focus to scroll into view without scrolling the whole page.
|
|
136
|
+
let focus = HTMLElement.prototype.focus;
|
|
137
|
+
HTMLElement.prototype.focus = function(opts) {
|
|
138
|
+
// Track whether the keyboard was already visible before.
|
|
139
|
+
let wasKeyboardVisible = document.activeElement != null && (0, $59kHH$reactariautils.willOpenKeyboard)(document.activeElement);
|
|
140
|
+
// Focus the element without scrolling the page.
|
|
141
|
+
focus.call(this, {
|
|
142
|
+
...opts,
|
|
143
|
+
preventScroll: true
|
|
157
144
|
});
|
|
158
|
-
|
|
159
|
-
window.scrollTo(0, 0);
|
|
145
|
+
if (!opts || !opts.preventScroll) $5c2f5cd01815d369$var$scrollIntoViewWhenReady(this, wasKeyboardVisible);
|
|
160
146
|
};
|
|
161
147
|
let removeEvents = (0, $59kHH$reactariautils.chain)($5c2f5cd01815d369$var$addEvent(document, 'touchstart', onTouchStart, {
|
|
162
148
|
passive: false,
|
|
@@ -164,15 +150,11 @@ function $5c2f5cd01815d369$var$preventScrollMobileSafari() {
|
|
|
164
150
|
}), $5c2f5cd01815d369$var$addEvent(document, 'touchmove', onTouchMove, {
|
|
165
151
|
passive: false,
|
|
166
152
|
capture: true
|
|
167
|
-
}), $5c2f5cd01815d369$var$addEvent(document, '
|
|
168
|
-
passive: false,
|
|
169
|
-
capture: true
|
|
170
|
-
}), $5c2f5cd01815d369$var$addEvent(document, 'focus', onFocus, true));
|
|
153
|
+
}), $5c2f5cd01815d369$var$addEvent(document, 'blur', onBlur, true));
|
|
171
154
|
return ()=>{
|
|
172
|
-
// Restore styles and scroll the page back to where it was.
|
|
173
|
-
restoreScrollableStyles === null || restoreScrollableStyles === void 0 ? void 0 : restoreScrollableStyles();
|
|
174
|
-
restoreStyles === null || restoreStyles === void 0 ? void 0 : restoreStyles();
|
|
175
155
|
removeEvents();
|
|
156
|
+
style.remove();
|
|
157
|
+
HTMLElement.prototype.focus = focus;
|
|
176
158
|
};
|
|
177
159
|
}
|
|
178
160
|
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
@@ -193,6 +175,15 @@ function $5c2f5cd01815d369$var$addEvent(target, event, handler, options) {
|
|
|
193
175
|
target.removeEventListener(event, handler, options);
|
|
194
176
|
};
|
|
195
177
|
}
|
|
178
|
+
function $5c2f5cd01815d369$var$scrollIntoViewWhenReady(target, wasKeyboardVisible) {
|
|
179
|
+
if (wasKeyboardVisible || !$5c2f5cd01815d369$var$visualViewport) // If the keyboard was already visible, scroll the target into view immediately.
|
|
180
|
+
$5c2f5cd01815d369$var$scrollIntoView(target);
|
|
181
|
+
else // Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
182
|
+
// measure the correct position to scroll to.
|
|
183
|
+
$5c2f5cd01815d369$var$visualViewport.addEventListener('resize', ()=>$5c2f5cd01815d369$var$scrollIntoView(target), {
|
|
184
|
+
once: true
|
|
185
|
+
});
|
|
186
|
+
}
|
|
196
187
|
function $5c2f5cd01815d369$var$scrollIntoView(target) {
|
|
197
188
|
let root = document.scrollingElement || document.documentElement;
|
|
198
189
|
let nextTarget = target;
|
|
@@ -200,16 +191,23 @@ function $5c2f5cd01815d369$var$scrollIntoView(target) {
|
|
|
200
191
|
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
201
192
|
let scrollable = (0, $59kHH$reactariautils.getScrollParent)(nextTarget);
|
|
202
193
|
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
|
|
203
|
-
let
|
|
204
|
-
let
|
|
205
|
-
if (
|
|
194
|
+
let scrollableRect = scrollable.getBoundingClientRect();
|
|
195
|
+
let targetRect = nextTarget.getBoundingClientRect();
|
|
196
|
+
if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
|
|
197
|
+
let bottom = scrollableRect.bottom;
|
|
198
|
+
if ($5c2f5cd01815d369$var$visualViewport) bottom = Math.min(bottom, $5c2f5cd01815d369$var$visualViewport.offsetTop + $5c2f5cd01815d369$var$visualViewport.height);
|
|
199
|
+
// Center within the viewport.
|
|
200
|
+
let adjustment = targetRect.top - scrollableRect.top - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
|
|
201
|
+
scrollable.scrollTo({
|
|
202
|
+
// Clamp to the valid range to prevent over-scrolling.
|
|
203
|
+
top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
|
|
204
|
+
behavior: 'smooth'
|
|
205
|
+
});
|
|
206
|
+
}
|
|
206
207
|
}
|
|
207
208
|
nextTarget = scrollable.parentElement;
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
|
-
function $5c2f5cd01815d369$var$willOpenKeyboard(target) {
|
|
211
|
-
return target instanceof HTMLInputElement && !$5c2f5cd01815d369$var$nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
|
|
212
|
-
}
|
|
213
211
|
|
|
214
212
|
|
|
215
213
|
//# sourceMappingURL=usePreventScroll.main.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AASD,MAAM,uCAAiB,OAAO,aAAa,eAAe,OAAO,cAAc;AAE/E,sEAAsE;AACtE,MAAM,0CAAoB,IAAI,IAAI;IAChC;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD;AAED,mIAAmI;AACnI,IAAI,2CAAqB;AACzB,IAAI;AAOG,SAAS,0CAAiB,UAAgC,CAAC,CAAC;IACjE,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,CAAA,GAAA,qCAAc,EAAE;QACd,IAAI,YACF;QAGF;QACA,IAAI,6CAAuB;YACzB,IAAI,CAAA,GAAA,2BAAI,KACN,gCAAU;iBAEV,gCAAU;;QAId,OAAO;YACL;YACA,IAAI,6CAAuB,GACzB;QAEJ;IACF,GAAG;QAAC;KAAW;AACjB;AAEA,0FAA0F;AAC1F,mFAAmF;AACnF,SAAS;IACP,IAAI,iBAAiB,OAAO,UAAU,GAAG,SAAS,eAAe,CAAC,WAAW;IAC7E,OAAO,CAAA,GAAA,2BAAI,EACT,iBAAiB,KACf,2FAA2F;IAC1F,CAAA,qBAAqB,SAAS,eAAe,CAAC,KAAK,GAChD,+BAAS,SAAS,eAAe,EAAE,mBAAmB,YACtD,+BAAS,SAAS,eAAe,EAAE,gBAAgB,GAAG,eAAe,EAAE,CAAC,CAAA,GAC9E,+BAAS,SAAS,eAAe,EAAE,YAAY;AAEnD;AAEA,wEAAwE;AACxE,gDAAgD;AAChD,EAAE;AACF,8FAA8F;AAC9F,sGAAsG;AACtG,mCAAmC;AACnC,6GAA6G;AAC7G,2EAA2E;AAC3E,4GAA4G;AAC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AS,OAAoB,EAAE,KAAa,EAAE,KAAa;IAClE,IAAI,MAAM,QAAQ,KAAK,CAAC,MAAM;IAC9B,QAAQ,KAAK,CAAC,MAAM,GAAG;IAEvB,OAAO;QACL,QAAQ,KAAK,CAAC,MAAM,GAAG;IACzB;AACF;AAEA,6EAA6E;AAC7E,SAAS,+BACP,MAAyB,EACzB,KAAQ,EACR,OAA6E,EAC7E,OAA2C;IAE3C,0EAA0E;IAC1E,aAAa;IACb,OAAO,gBAAgB,CAAC,OAAO,SAAS;IACxC,OAAO;QACL,aAAa;QACb,OAAO,mBAAmB,CAAC,OAAO,SAAS;IAC7C;AACF;AAEA,SAAS,qCAAe,MAAe;IACrC,IAAI,OAAO,SAAS,gBAAgB,IAAI,SAAS,eAAe;IAChE,IAAI,aAA6B;IACjC,MAAO,cAAc,eAAe,KAAM;QACxC,0GAA0G;QAC1G,IAAI,aAAa,CAAA,GAAA,qCAAc,EAAE;QACjC,IAAI,eAAe,SAAS,eAAe,IAAI,eAAe,SAAS,IAAI,IAAI,eAAe,YAAY;YACxG,IAAI,gBAAgB,WAAW,qBAAqB,GAAG,GAAG;YAC1D,IAAI,YAAY,WAAW,qBAAqB,GAAG,GAAG;YACtD,IAAI,YAAY,gBAAgB,WAAW,YAAY,EACrD,WAAW,SAAS,IAAI,YAAY;QAExC;QAEA,aAAa,WAAW,aAAa;IACvC;AACF;AAEA,SAAS,uCAAiB,MAAe;IACvC,OACE,AAAC,kBAAkB,oBAAoB,CAAC,wCAAkB,GAAG,CAAC,OAAO,IAAI,KACzE,kBAAkB,uBACjB,kBAAkB,eAAe,OAAO,iBAAiB;AAE9D","sources":["packages/@react-aria/overlays/src/usePreventScroll.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 {chain, getScrollParent, isIOS, useLayoutEffect} from '@react-aria/utils';\n\ninterface PreventScrollOptions {\n /** Whether the scroll lock is disabled. */\n isDisabled?: boolean\n}\n\nconst visualViewport = typeof document !== 'undefined' && window.visualViewport;\n\n// HTML input types that do not cause the software keyboard to appear.\nconst nonTextInputTypes = new Set([\n 'checkbox',\n 'radio',\n 'range',\n 'color',\n 'file',\n 'image',\n 'button',\n 'submit',\n 'reset'\n]);\n\n// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position\nlet preventScrollCount = 0;\nlet restore;\n\n/**\n * Prevents scrolling on the document body on mount, and\n * restores it on unmount. Also ensures that content does not\n * shift due to the scrollbars disappearing.\n */\nexport function usePreventScroll(options: PreventScrollOptions = {}): void {\n let {isDisabled} = options;\n\n useLayoutEffect(() => {\n if (isDisabled) {\n return;\n }\n\n preventScrollCount++;\n if (preventScrollCount === 1) {\n if (isIOS()) {\n restore = preventScrollMobileSafari();\n } else {\n restore = preventScrollStandard();\n }\n }\n\n return () => {\n preventScrollCount--;\n if (preventScrollCount === 0) {\n restore();\n }\n };\n }, [isDisabled]);\n}\n\n// For most browsers, all we need to do is set `overflow: hidden` on the root element, and\n// add some padding to prevent the page from shifting when the scrollbar is hidden.\nfunction preventScrollStandard() {\n let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;\n return chain(\n scrollbarWidth > 0 &&\n // Use scrollbar-gutter when supported because it also works for fixed positioned elements.\n ('scrollbarGutter' in document.documentElement.style\n ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')\n : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),\n setStyle(document.documentElement, 'overflow', 'hidden')\n );\n}\n\n// Mobile Safari is a whole different beast. Even with overflow: hidden,\n// it still scrolls the page in many situations:\n//\n// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.\n// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of\n// it, so it becomes scrollable.\n// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.\n// This may cause even fixed position elements to scroll off the screen.\n// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always\n// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.\n//\n// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:\n//\n// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling\n// on the window.\n// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at\n// the top or bottom. Work around a bug where this does not work when the element does not actually overflow\n// by preventing default in a `touchmove` event.\n// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.\n// 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top\n// of the page, which prevents it from scrolling the page. After the input is focused, scroll the element\n// into view ourselves, without scrolling the whole page.\n// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the\n// same visually, but makes the actual scroll position always zero. This is required to make all of the\n// above work or Safari will still try to scroll the page when focusing an input.\n// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting\n// to navigate to an input with the next/previous buttons that's outside a modal.\nfunction preventScrollMobileSafari() {\n let scrollable: Element;\n let restoreScrollableStyles;\n let onTouchStart = (e: TouchEvent) => {\n // Store the nearest scrollable parent element from the element that the user touched.\n scrollable = getScrollParent(e.target as Element, true);\n if (scrollable === document.documentElement && scrollable === document.body) {\n return;\n }\n\n // Prevent scrolling up when at the top and scrolling down when at the bottom\n // of a nested scrollable area, otherwise mobile Safari will start scrolling\n // the window instead.\n if (scrollable instanceof HTMLElement && window.getComputedStyle(scrollable).overscrollBehavior === 'auto') {\n restoreScrollableStyles = setStyle(scrollable, 'overscrollBehavior', 'contain');\n }\n };\n\n let onTouchMove = (e: TouchEvent) => {\n // Prevent scrolling the window.\n if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {\n e.preventDefault();\n return;\n }\n\n // overscroll-behavior should prevent scroll chaining, but currently does not\n // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452\n // This checks that both the width and height do not overflow, otherwise we might\n // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to\n // the element will prevent vertical page scrolling. We can't add that automatically\n // because it must be set before the touchstart event.\n if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {\n e.preventDefault();\n }\n };\n\n let onTouchEnd = () => {\n if (restoreScrollableStyles) {\n restoreScrollableStyles();\n }\n };\n\n let onFocus = (e: FocusEvent) => {\n let target = e.target as HTMLElement;\n if (willOpenKeyboard(target)) {\n setupStyles();\n\n // Apply a transform to trick Safari into thinking the input is at the top of the page\n // so it doesn't try to scroll it into view.\n target.style.transform = 'translateY(-2000px)';\n requestAnimationFrame(() => {\n target.style.transform = '';\n\n // This will have prevented the browser from scrolling the focused element into view,\n // so we need to do this ourselves in a way that doesn't cause the whole page to scroll.\n if (visualViewport) {\n if (visualViewport.height < window.innerHeight) {\n // If the keyboard is already visible, do this after one additional frame\n // to wait for the transform to be removed.\n requestAnimationFrame(() => {\n scrollIntoView(target);\n });\n } else {\n // Otherwise, wait for the visual viewport to resize before scrolling so we can\n // measure the correct position to scroll to.\n visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});\n }\n }\n });\n }\n };\n\n let restoreStyles: null | (() => void) = null;\n let setupStyles = () => {\n if (restoreStyles) {\n return;\n }\n\n let onWindowScroll = () => {\n // Last resort. If the window scrolled, scroll it back to the top.\n // It should always be at the top because the body will have a negative margin (see below).\n window.scrollTo(0, 0);\n };\n\n // Record the original scroll position so we can restore it.\n // Then apply a negative margin to the body to offset it by the scroll position. This will\n // enable us to scroll the window to the top, which is required for the rest of this to work.\n let scrollX = window.pageXOffset;\n let scrollY = window.pageYOffset;\n\n restoreStyles = chain(\n addEvent(window, 'scroll', onWindowScroll),\n setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`),\n setStyle(document.documentElement, 'overflow', 'hidden'),\n setStyle(document.body, 'marginTop', `-${scrollY}px`),\n () => {\n window.scrollTo(scrollX, scrollY);\n }\n );\n\n // Scroll to the top. The negative margin on the body will make this appear the same.\n window.scrollTo(0, 0);\n };\n\n let removeEvents = chain(\n addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),\n addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),\n addEvent(document, 'touchend', onTouchEnd, {passive: false, capture: true}),\n addEvent(document, 'focus', onFocus, true)\n );\n\n return () => {\n // Restore styles and scroll the page back to where it was.\n restoreScrollableStyles?.();\n restoreStyles?.();\n removeEvents();\n };\n}\n\n// Sets a CSS property on an element, and returns a function to revert it to the previous value.\nfunction setStyle(element: HTMLElement, style: string, value: string) {\n let cur = element.style[style];\n element.style[style] = value;\n\n return () => {\n element.style[style] = cur;\n };\n}\n\n// Adds an event listener to an element, and returns a function to remove it.\nfunction addEvent<K extends keyof GlobalEventHandlersEventMap>(\n target: Document | Window,\n event: K,\n handler: (this: Document | Window, ev: GlobalEventHandlersEventMap[K]) => any,\n options?: boolean | AddEventListenerOptions\n) {\n // internal function, so it's ok to ignore the difficult to fix type error\n // @ts-ignore\n target.addEventListener(event, handler, options);\n return () => {\n // @ts-ignore\n target.removeEventListener(event, handler, options);\n };\n}\n\nfunction scrollIntoView(target: Element) {\n let root = document.scrollingElement || document.documentElement;\n let nextTarget: Element | null = target;\n while (nextTarget && nextTarget !== root) {\n // Find the parent scrollable element and adjust the scroll position if the target is not already in view.\n let scrollable = getScrollParent(nextTarget);\n if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {\n let scrollableTop = scrollable.getBoundingClientRect().top;\n let targetTop = nextTarget.getBoundingClientRect().top;\n if (targetTop > scrollableTop + nextTarget.clientHeight) {\n scrollable.scrollTop += targetTop - scrollableTop;\n }\n }\n\n nextTarget = scrollable.parentElement;\n }\n}\n\nfunction willOpenKeyboard(target: Element) {\n return (\n (target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) ||\n target instanceof HTMLTextAreaElement ||\n (target instanceof HTMLElement && target.isContentEditable)\n );\n}\n"],"names":[],"version":3,"file":"usePreventScroll.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AASD,MAAM,uCAAiB,OAAO,aAAa,eAAe,OAAO,cAAc;AAE/E,mIAAmI;AACnI,IAAI,2CAAqB;AACzB,IAAI;AAOG,SAAS,0CAAiB,UAAgC,CAAC,CAAC;IACjE,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,CAAA,GAAA,qCAAc,EAAE;QACd,IAAI,YACF;QAGF;QACA,IAAI,6CAAuB;YACzB,IAAI,CAAA,GAAA,2BAAI,KACN,gCAAU;iBAEV,gCAAU;;QAId,OAAO;YACL;YACA,IAAI,6CAAuB,GACzB;QAEJ;IACF,GAAG;QAAC;KAAW;AACjB;AAEA,0FAA0F;AAC1F,mFAAmF;AACnF,SAAS;IACP,IAAI,iBAAiB,OAAO,UAAU,GAAG,SAAS,eAAe,CAAC,WAAW;IAC7E,OAAO,CAAA,GAAA,2BAAI,EACT,iBAAiB,KACf,2FAA2F;IAC1F,CAAA,qBAAqB,SAAS,eAAe,CAAC,KAAK,GAChD,+BAAS,SAAS,eAAe,EAAE,mBAAmB,YACtD,+BAAS,SAAS,eAAe,EAAE,gBAAgB,GAAG,eAAe,EAAE,CAAC,CAAA,GAC9E,+BAAS,SAAS,eAAe,EAAE,YAAY;AAEnD;AAEA,wEAAwE;AACxE,gDAAgD;AAChD,EAAE;AACF,8FAA8F;AAC9F,sGAAsG;AACtG,mCAAmC;AACnC,6GAA6G;AAC7G,2EAA2E;AAC3E,4GAA4G;AAC5G,sGAAsG;AACtG,EAAE;AACF,oGAAoG;AACpG,EAAE;AACF,+GAA+G;AAC/G,oBAAoB;AACpB,4GAA4G;AAC5G,+GAA+G;AAC/G,4GAA4G;AAC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sD;oBACtD,KAAK,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,WAAW,YAAY,GAAG,WAAW,YAAY,EAAE,WAAW,SAAS,GAAG;oBACpG,UAAU;gBACZ;YACF;QACF;QAEA,aAAa,WAAW,aAAa;IACvC;AACF","sources":["packages/@react-aria/overlays/src/usePreventScroll.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 {chain, getScrollParent, isIOS, isScrollable, useLayoutEffect, willOpenKeyboard} from '@react-aria/utils';\n\ninterface PreventScrollOptions {\n /** Whether the scroll lock is disabled. */\n isDisabled?: boolean\n}\n\nconst visualViewport = typeof document !== 'undefined' && window.visualViewport;\n\n// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position\nlet preventScrollCount = 0;\nlet restore;\n\n/**\n * Prevents scrolling on the document body on mount, and\n * restores it on unmount. Also ensures that content does not\n * shift due to the scrollbars disappearing.\n */\nexport function usePreventScroll(options: PreventScrollOptions = {}): void {\n let {isDisabled} = options;\n\n useLayoutEffect(() => {\n if (isDisabled) {\n return;\n }\n\n preventScrollCount++;\n if (preventScrollCount === 1) {\n if (isIOS()) {\n restore = preventScrollMobileSafari();\n } else {\n restore = preventScrollStandard();\n }\n }\n\n return () => {\n preventScrollCount--;\n if (preventScrollCount === 0) {\n restore();\n }\n };\n }, [isDisabled]);\n}\n\n// For most browsers, all we need to do is set `overflow: hidden` on the root element, and\n// add some padding to prevent the page from shifting when the scrollbar is hidden.\nfunction preventScrollStandard() {\n let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;\n return chain(\n scrollbarWidth > 0 &&\n // Use scrollbar-gutter when supported because it also works for fixed positioned elements.\n ('scrollbarGutter' in document.documentElement.style\n ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')\n : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),\n setStyle(document.documentElement, 'overflow', 'hidden')\n );\n}\n\n// Mobile Safari is a whole different beast. Even with overflow: hidden,\n// it still scrolls the page in many situations:\n//\n// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.\n// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of\n// it, so it becomes scrollable.\n// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.\n// This may cause even fixed position elements to scroll off the screen.\n// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always\n// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.\n//\n// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:\n//\n// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling\n// on the window.\n// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at\n// the top or bottom. Work around a bug where this does not work when the element does not actually overflow\n// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch\n// zooming or when an element contains text selection, which may allow scrolling in some cases.\n// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.\n// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents \n// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view\n// ourselves, without scrolling the whole page.\nfunction preventScrollMobileSafari() {\n let scrollable: Element;\n let allowTouchMove = false;\n let onTouchStart = (e: TouchEvent) => {\n // Store the nearest scrollable parent element from the element that the user touched.\n let target = e.target as Element;\n scrollable = isScrollable(target) ? target : getScrollParent(target, true);\n allowTouchMove = false;\n \n // If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.\n let selection = target.ownerDocument.defaultView!.getSelection();\n if (selection && !selection.isCollapsed && selection.containsNode(target, true)) {\n allowTouchMove = true;\n }\n\n // If this is a focused input element with a selected range, allow user to drag the selection handles.\n if (\n 'selectionStart' in target && \n 'selectionEnd' in target &&\n (target.selectionStart as number) < (target.selectionEnd as number) &&\n target.ownerDocument.activeElement === target\n ) {\n allowTouchMove = true;\n }\n };\n\n // Prevent scrolling up when at the top and scrolling down when at the bottom\n // of a nested scrollable area, otherwise mobile Safari will start scrolling\n // the window instead.\n // This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.\n let style = document.createElement('style');\n style.textContent = `\n@layer {\n * {\n overscroll-behavior: contain;\n }\n}`.trim();\n document.head.prepend(style);\n\n let onTouchMove = (e: TouchEvent) => {\n // Allow pinch-zooming.\n if (e.touches.length === 2 || allowTouchMove) {\n return;\n }\n\n // Prevent scrolling the window.\n if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {\n e.preventDefault();\n return;\n }\n\n // overscroll-behavior should prevent scroll chaining, but currently does not\n // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452\n // This checks that both the width and height do not overflow, otherwise we might\n // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to\n // the element will prevent vertical page scrolling. We can't add that automatically\n // because it must be set before the touchstart event.\n if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {\n e.preventDefault();\n }\n };\n\n let onBlur = (e: FocusEvent) => {\n let target = e.target as HTMLElement;\n let relatedTarget = e.relatedTarget as HTMLElement | null;\n if (relatedTarget && willOpenKeyboard(relatedTarget)) {\n // Focus without scrolling the whole page, and then scroll into view manually.\n relatedTarget.focus({preventScroll: true});\n scrollIntoViewWhenReady(relatedTarget, willOpenKeyboard(target));\n } else if (!relatedTarget) {\n // When tapping the Done button on the keyboard, focus moves to the body.\n // FocusScope will then restore focus back to the input. Later when tapping\n // the same input again, it is already focused, so no blur event will fire,\n // resulting in the flow above never running and Safari's native scrolling occurring.\n // Instead, move focus to the parent focusable element (e.g. the dialog).\n let focusable = target.parentElement?.closest('[tabindex]') as HTMLElement | null;\n focusable?.focus({preventScroll: true});\n }\n };\n\n // Override programmatic focus to scroll into view without scrolling the whole page.\n let focus = HTMLElement.prototype.focus;\n HTMLElement.prototype.focus = function (opts) {\n // Track whether the keyboard was already visible before.\n let wasKeyboardVisible = document.activeElement != null && willOpenKeyboard(document.activeElement);\n\n // Focus the element without scrolling the page.\n focus.call(this, {...opts, preventScroll: true});\n\n if (!opts || !opts.preventScroll) {\n scrollIntoViewWhenReady(this, wasKeyboardVisible);\n }\n };\n\n let removeEvents = chain(\n addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),\n addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),\n addEvent(document, 'blur', onBlur, true)\n );\n\n return () => {\n removeEvents();\n style.remove();\n HTMLElement.prototype.focus = focus;\n };\n}\n\n// Sets a CSS property on an element, and returns a function to revert it to the previous value.\nfunction setStyle(element: HTMLElement, style: string, value: string) {\n let cur = element.style[style];\n element.style[style] = value;\n\n return () => {\n element.style[style] = cur;\n };\n}\n\n// Adds an event listener to an element, and returns a function to remove it.\nfunction addEvent<K extends keyof GlobalEventHandlersEventMap>(\n target: Document | Window,\n event: K,\n handler: (this: Document | Window, ev: GlobalEventHandlersEventMap[K]) => any,\n options?: boolean | AddEventListenerOptions\n) {\n // internal function, so it's ok to ignore the difficult to fix type error\n // @ts-ignore\n target.addEventListener(event, handler, options);\n return () => {\n // @ts-ignore\n target.removeEventListener(event, handler, options);\n };\n}\n\nfunction scrollIntoViewWhenReady(target: Element, wasKeyboardVisible: boolean) {\n if (wasKeyboardVisible || !visualViewport) {\n // If the keyboard was already visible, scroll the target into view immediately.\n scrollIntoView(target);\n } else {\n // Otherwise, wait for the visual viewport to resize before scrolling so we can\n // measure the correct position to scroll to.\n visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});\n }\n}\n\nfunction scrollIntoView(target: Element) {\n let root = document.scrollingElement || document.documentElement;\n let nextTarget: Element | null = target;\n while (nextTarget && nextTarget !== root) {\n // Find the parent scrollable element and adjust the scroll position if the target is not already in view.\n let scrollable = getScrollParent(nextTarget);\n if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {\n let scrollableRect = scrollable.getBoundingClientRect();\n let targetRect = nextTarget.getBoundingClientRect();\n if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {\n let bottom = scrollableRect.bottom;\n if (visualViewport) {\n bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);\n }\n\n // Center within the viewport.\n let adjustment = (targetRect.top - scrollableRect.top) - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);\n scrollable.scrollTo({\n // Clamp to the valid range to prevent over-scrolling.\n top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),\n behavior: 'smooth'\n });\n }\n }\n\n nextTarget = scrollable.parentElement;\n }\n}\n"],"names":[],"version":3,"file":"usePreventScroll.main.js.map"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain as $7mMvr$chain, getScrollParent as $7mMvr$getScrollParent} from "@react-aria/utils";
|
|
1
|
+
import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain as $7mMvr$chain, isScrollable as $7mMvr$isScrollable, getScrollParent as $7mMvr$getScrollParent, willOpenKeyboard as $7mMvr$willOpenKeyboard} from "@react-aria/utils";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -12,18 +12,6 @@ import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain
|
|
|
12
12
|
* governing permissions and limitations under the License.
|
|
13
13
|
*/
|
|
14
14
|
const $49c51c25361d4cd2$var$visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
15
|
-
// HTML input types that do not cause the software keyboard to appear.
|
|
16
|
-
const $49c51c25361d4cd2$var$nonTextInputTypes = new Set([
|
|
17
|
-
'checkbox',
|
|
18
|
-
'radio',
|
|
19
|
-
'range',
|
|
20
|
-
'color',
|
|
21
|
-
'file',
|
|
22
|
-
'image',
|
|
23
|
-
'button',
|
|
24
|
-
'submit',
|
|
25
|
-
'reset'
|
|
26
|
-
]);
|
|
27
15
|
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
28
16
|
let $49c51c25361d4cd2$var$preventScrollCount = 0;
|
|
29
17
|
let $49c51c25361d4cd2$var$restore;
|
|
@@ -68,29 +56,41 @@ function $49c51c25361d4cd2$var$preventScrollStandard() {
|
|
|
68
56
|
// on the window.
|
|
69
57
|
// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
|
|
70
58
|
// the top or bottom. Work around a bug where this does not work when the element does not actually overflow
|
|
71
|
-
// by preventing default in a `touchmove` event.
|
|
59
|
+
// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch
|
|
60
|
+
// zooming or when an element contains text selection, which may allow scrolling in some cases.
|
|
72
61
|
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
73
|
-
// 4. When
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
77
|
-
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
78
|
-
// above work or Safari will still try to scroll the page when focusing an input.
|
|
79
|
-
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
80
|
-
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
62
|
+
// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents
|
|
63
|
+
// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view
|
|
64
|
+
// ourselves, without scrolling the whole page.
|
|
81
65
|
function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
82
66
|
let scrollable;
|
|
83
|
-
let
|
|
67
|
+
let allowTouchMove = false;
|
|
84
68
|
let onTouchStart = (e)=>{
|
|
85
69
|
// Store the nearest scrollable parent element from the element that the user touched.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
if (
|
|
70
|
+
let target = e.target;
|
|
71
|
+
scrollable = (0, $7mMvr$isScrollable)(target) ? target : (0, $7mMvr$getScrollParent)(target, true);
|
|
72
|
+
allowTouchMove = false;
|
|
73
|
+
// If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.
|
|
74
|
+
let selection = target.ownerDocument.defaultView.getSelection();
|
|
75
|
+
if (selection && !selection.isCollapsed && selection.containsNode(target, true)) allowTouchMove = true;
|
|
76
|
+
// If this is a focused input element with a selected range, allow user to drag the selection handles.
|
|
77
|
+
if ('selectionStart' in target && 'selectionEnd' in target && target.selectionStart < target.selectionEnd && target.ownerDocument.activeElement === target) allowTouchMove = true;
|
|
92
78
|
};
|
|
79
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
80
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
81
|
+
// the window instead.
|
|
82
|
+
// This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
|
|
83
|
+
let style = document.createElement('style');
|
|
84
|
+
style.textContent = `
|
|
85
|
+
@layer {
|
|
86
|
+
* {
|
|
87
|
+
overscroll-behavior: contain;
|
|
88
|
+
}
|
|
89
|
+
}`.trim();
|
|
90
|
+
document.head.prepend(style);
|
|
93
91
|
let onTouchMove = (e)=>{
|
|
92
|
+
// Allow pinch-zooming.
|
|
93
|
+
if (e.touches.length === 2 || allowTouchMove) return;
|
|
94
94
|
// Prevent scrolling the window.
|
|
95
95
|
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
96
96
|
e.preventDefault();
|
|
@@ -104,53 +104,39 @@ function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
|
104
104
|
// because it must be set before the touchstart event.
|
|
105
105
|
if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) e.preventDefault();
|
|
106
106
|
};
|
|
107
|
-
let
|
|
108
|
-
if (restoreScrollableStyles) restoreScrollableStyles();
|
|
109
|
-
};
|
|
110
|
-
let onFocus = (e)=>{
|
|
107
|
+
let onBlur = (e)=>{
|
|
111
108
|
let target = e.target;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
$49c51c25361d4cd2$var$visualViewport.addEventListener('resize', ()=>$49c51c25361d4cd2$var$scrollIntoView(target), {
|
|
130
|
-
once: true
|
|
131
|
-
});
|
|
132
|
-
}
|
|
109
|
+
let relatedTarget = e.relatedTarget;
|
|
110
|
+
if (relatedTarget && (0, $7mMvr$willOpenKeyboard)(relatedTarget)) {
|
|
111
|
+
// Focus without scrolling the whole page, and then scroll into view manually.
|
|
112
|
+
relatedTarget.focus({
|
|
113
|
+
preventScroll: true
|
|
114
|
+
});
|
|
115
|
+
$49c51c25361d4cd2$var$scrollIntoViewWhenReady(relatedTarget, (0, $7mMvr$willOpenKeyboard)(target));
|
|
116
|
+
} else if (!relatedTarget) {
|
|
117
|
+
var _target_parentElement;
|
|
118
|
+
// When tapping the Done button on the keyboard, focus moves to the body.
|
|
119
|
+
// FocusScope will then restore focus back to the input. Later when tapping
|
|
120
|
+
// the same input again, it is already focused, so no blur event will fire,
|
|
121
|
+
// resulting in the flow above never running and Safari's native scrolling occurring.
|
|
122
|
+
// Instead, move focus to the parent focusable element (e.g. the dialog).
|
|
123
|
+
let focusable = (_target_parentElement = target.parentElement) === null || _target_parentElement === void 0 ? void 0 : _target_parentElement.closest('[tabindex]');
|
|
124
|
+
focusable === null || focusable === void 0 ? void 0 : focusable.focus({
|
|
125
|
+
preventScroll: true
|
|
133
126
|
});
|
|
134
127
|
}
|
|
135
128
|
};
|
|
136
|
-
|
|
137
|
-
let
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
146
|
-
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
147
|
-
let scrollX = window.pageXOffset;
|
|
148
|
-
let scrollY = window.pageYOffset;
|
|
149
|
-
restoreStyles = (0, $7mMvr$chain)($49c51c25361d4cd2$var$addEvent(window, 'scroll', onWindowScroll), $49c51c25361d4cd2$var$setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`), $49c51c25361d4cd2$var$setStyle(document.documentElement, 'overflow', 'hidden'), $49c51c25361d4cd2$var$setStyle(document.body, 'marginTop', `-${scrollY}px`), ()=>{
|
|
150
|
-
window.scrollTo(scrollX, scrollY);
|
|
129
|
+
// Override programmatic focus to scroll into view without scrolling the whole page.
|
|
130
|
+
let focus = HTMLElement.prototype.focus;
|
|
131
|
+
HTMLElement.prototype.focus = function(opts) {
|
|
132
|
+
// Track whether the keyboard was already visible before.
|
|
133
|
+
let wasKeyboardVisible = document.activeElement != null && (0, $7mMvr$willOpenKeyboard)(document.activeElement);
|
|
134
|
+
// Focus the element without scrolling the page.
|
|
135
|
+
focus.call(this, {
|
|
136
|
+
...opts,
|
|
137
|
+
preventScroll: true
|
|
151
138
|
});
|
|
152
|
-
|
|
153
|
-
window.scrollTo(0, 0);
|
|
139
|
+
if (!opts || !opts.preventScroll) $49c51c25361d4cd2$var$scrollIntoViewWhenReady(this, wasKeyboardVisible);
|
|
154
140
|
};
|
|
155
141
|
let removeEvents = (0, $7mMvr$chain)($49c51c25361d4cd2$var$addEvent(document, 'touchstart', onTouchStart, {
|
|
156
142
|
passive: false,
|
|
@@ -158,15 +144,11 @@ function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
|
158
144
|
}), $49c51c25361d4cd2$var$addEvent(document, 'touchmove', onTouchMove, {
|
|
159
145
|
passive: false,
|
|
160
146
|
capture: true
|
|
161
|
-
}), $49c51c25361d4cd2$var$addEvent(document, '
|
|
162
|
-
passive: false,
|
|
163
|
-
capture: true
|
|
164
|
-
}), $49c51c25361d4cd2$var$addEvent(document, 'focus', onFocus, true));
|
|
147
|
+
}), $49c51c25361d4cd2$var$addEvent(document, 'blur', onBlur, true));
|
|
165
148
|
return ()=>{
|
|
166
|
-
// Restore styles and scroll the page back to where it was.
|
|
167
|
-
restoreScrollableStyles === null || restoreScrollableStyles === void 0 ? void 0 : restoreScrollableStyles();
|
|
168
|
-
restoreStyles === null || restoreStyles === void 0 ? void 0 : restoreStyles();
|
|
169
149
|
removeEvents();
|
|
150
|
+
style.remove();
|
|
151
|
+
HTMLElement.prototype.focus = focus;
|
|
170
152
|
};
|
|
171
153
|
}
|
|
172
154
|
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
@@ -187,6 +169,15 @@ function $49c51c25361d4cd2$var$addEvent(target, event, handler, options) {
|
|
|
187
169
|
target.removeEventListener(event, handler, options);
|
|
188
170
|
};
|
|
189
171
|
}
|
|
172
|
+
function $49c51c25361d4cd2$var$scrollIntoViewWhenReady(target, wasKeyboardVisible) {
|
|
173
|
+
if (wasKeyboardVisible || !$49c51c25361d4cd2$var$visualViewport) // If the keyboard was already visible, scroll the target into view immediately.
|
|
174
|
+
$49c51c25361d4cd2$var$scrollIntoView(target);
|
|
175
|
+
else // Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
176
|
+
// measure the correct position to scroll to.
|
|
177
|
+
$49c51c25361d4cd2$var$visualViewport.addEventListener('resize', ()=>$49c51c25361d4cd2$var$scrollIntoView(target), {
|
|
178
|
+
once: true
|
|
179
|
+
});
|
|
180
|
+
}
|
|
190
181
|
function $49c51c25361d4cd2$var$scrollIntoView(target) {
|
|
191
182
|
let root = document.scrollingElement || document.documentElement;
|
|
192
183
|
let nextTarget = target;
|
|
@@ -194,16 +185,23 @@ function $49c51c25361d4cd2$var$scrollIntoView(target) {
|
|
|
194
185
|
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
195
186
|
let scrollable = (0, $7mMvr$getScrollParent)(nextTarget);
|
|
196
187
|
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
|
|
197
|
-
let
|
|
198
|
-
let
|
|
199
|
-
if (
|
|
188
|
+
let scrollableRect = scrollable.getBoundingClientRect();
|
|
189
|
+
let targetRect = nextTarget.getBoundingClientRect();
|
|
190
|
+
if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
|
|
191
|
+
let bottom = scrollableRect.bottom;
|
|
192
|
+
if ($49c51c25361d4cd2$var$visualViewport) bottom = Math.min(bottom, $49c51c25361d4cd2$var$visualViewport.offsetTop + $49c51c25361d4cd2$var$visualViewport.height);
|
|
193
|
+
// Center within the viewport.
|
|
194
|
+
let adjustment = targetRect.top - scrollableRect.top - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
|
|
195
|
+
scrollable.scrollTo({
|
|
196
|
+
// Clamp to the valid range to prevent over-scrolling.
|
|
197
|
+
top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
|
|
198
|
+
behavior: 'smooth'
|
|
199
|
+
});
|
|
200
|
+
}
|
|
200
201
|
}
|
|
201
202
|
nextTarget = scrollable.parentElement;
|
|
202
203
|
}
|
|
203
204
|
}
|
|
204
|
-
function $49c51c25361d4cd2$var$willOpenKeyboard(target) {
|
|
205
|
-
return target instanceof HTMLInputElement && !$49c51c25361d4cd2$var$nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
|
|
206
|
-
}
|
|
207
205
|
|
|
208
206
|
|
|
209
207
|
export {$49c51c25361d4cd2$export$ee0f7cc6afcd1c18 as usePreventScroll};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain as $7mMvr$chain, getScrollParent as $7mMvr$getScrollParent} from "@react-aria/utils";
|
|
1
|
+
import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain as $7mMvr$chain, isScrollable as $7mMvr$isScrollable, getScrollParent as $7mMvr$getScrollParent, willOpenKeyboard as $7mMvr$willOpenKeyboard} from "@react-aria/utils";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -12,18 +12,6 @@ import {useLayoutEffect as $7mMvr$useLayoutEffect, isIOS as $7mMvr$isIOS, chain
|
|
|
12
12
|
* governing permissions and limitations under the License.
|
|
13
13
|
*/
|
|
14
14
|
const $49c51c25361d4cd2$var$visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
15
|
-
// HTML input types that do not cause the software keyboard to appear.
|
|
16
|
-
const $49c51c25361d4cd2$var$nonTextInputTypes = new Set([
|
|
17
|
-
'checkbox',
|
|
18
|
-
'radio',
|
|
19
|
-
'range',
|
|
20
|
-
'color',
|
|
21
|
-
'file',
|
|
22
|
-
'image',
|
|
23
|
-
'button',
|
|
24
|
-
'submit',
|
|
25
|
-
'reset'
|
|
26
|
-
]);
|
|
27
15
|
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
28
16
|
let $49c51c25361d4cd2$var$preventScrollCount = 0;
|
|
29
17
|
let $49c51c25361d4cd2$var$restore;
|
|
@@ -68,29 +56,41 @@ function $49c51c25361d4cd2$var$preventScrollStandard() {
|
|
|
68
56
|
// on the window.
|
|
69
57
|
// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
|
|
70
58
|
// the top or bottom. Work around a bug where this does not work when the element does not actually overflow
|
|
71
|
-
// by preventing default in a `touchmove` event.
|
|
59
|
+
// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch
|
|
60
|
+
// zooming or when an element contains text selection, which may allow scrolling in some cases.
|
|
72
61
|
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
73
|
-
// 4. When
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
77
|
-
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
78
|
-
// above work or Safari will still try to scroll the page when focusing an input.
|
|
79
|
-
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
80
|
-
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
62
|
+
// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents
|
|
63
|
+
// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view
|
|
64
|
+
// ourselves, without scrolling the whole page.
|
|
81
65
|
function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
82
66
|
let scrollable;
|
|
83
|
-
let
|
|
67
|
+
let allowTouchMove = false;
|
|
84
68
|
let onTouchStart = (e)=>{
|
|
85
69
|
// Store the nearest scrollable parent element from the element that the user touched.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
if (
|
|
70
|
+
let target = e.target;
|
|
71
|
+
scrollable = (0, $7mMvr$isScrollable)(target) ? target : (0, $7mMvr$getScrollParent)(target, true);
|
|
72
|
+
allowTouchMove = false;
|
|
73
|
+
// If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.
|
|
74
|
+
let selection = target.ownerDocument.defaultView.getSelection();
|
|
75
|
+
if (selection && !selection.isCollapsed && selection.containsNode(target, true)) allowTouchMove = true;
|
|
76
|
+
// If this is a focused input element with a selected range, allow user to drag the selection handles.
|
|
77
|
+
if ('selectionStart' in target && 'selectionEnd' in target && target.selectionStart < target.selectionEnd && target.ownerDocument.activeElement === target) allowTouchMove = true;
|
|
92
78
|
};
|
|
79
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
80
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
81
|
+
// the window instead.
|
|
82
|
+
// This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
|
|
83
|
+
let style = document.createElement('style');
|
|
84
|
+
style.textContent = `
|
|
85
|
+
@layer {
|
|
86
|
+
* {
|
|
87
|
+
overscroll-behavior: contain;
|
|
88
|
+
}
|
|
89
|
+
}`.trim();
|
|
90
|
+
document.head.prepend(style);
|
|
93
91
|
let onTouchMove = (e)=>{
|
|
92
|
+
// Allow pinch-zooming.
|
|
93
|
+
if (e.touches.length === 2 || allowTouchMove) return;
|
|
94
94
|
// Prevent scrolling the window.
|
|
95
95
|
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
96
96
|
e.preventDefault();
|
|
@@ -104,53 +104,39 @@ function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
|
104
104
|
// because it must be set before the touchstart event.
|
|
105
105
|
if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) e.preventDefault();
|
|
106
106
|
};
|
|
107
|
-
let
|
|
108
|
-
if (restoreScrollableStyles) restoreScrollableStyles();
|
|
109
|
-
};
|
|
110
|
-
let onFocus = (e)=>{
|
|
107
|
+
let onBlur = (e)=>{
|
|
111
108
|
let target = e.target;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
$49c51c25361d4cd2$var$visualViewport.addEventListener('resize', ()=>$49c51c25361d4cd2$var$scrollIntoView(target), {
|
|
130
|
-
once: true
|
|
131
|
-
});
|
|
132
|
-
}
|
|
109
|
+
let relatedTarget = e.relatedTarget;
|
|
110
|
+
if (relatedTarget && (0, $7mMvr$willOpenKeyboard)(relatedTarget)) {
|
|
111
|
+
// Focus without scrolling the whole page, and then scroll into view manually.
|
|
112
|
+
relatedTarget.focus({
|
|
113
|
+
preventScroll: true
|
|
114
|
+
});
|
|
115
|
+
$49c51c25361d4cd2$var$scrollIntoViewWhenReady(relatedTarget, (0, $7mMvr$willOpenKeyboard)(target));
|
|
116
|
+
} else if (!relatedTarget) {
|
|
117
|
+
var _target_parentElement;
|
|
118
|
+
// When tapping the Done button on the keyboard, focus moves to the body.
|
|
119
|
+
// FocusScope will then restore focus back to the input. Later when tapping
|
|
120
|
+
// the same input again, it is already focused, so no blur event will fire,
|
|
121
|
+
// resulting in the flow above never running and Safari's native scrolling occurring.
|
|
122
|
+
// Instead, move focus to the parent focusable element (e.g. the dialog).
|
|
123
|
+
let focusable = (_target_parentElement = target.parentElement) === null || _target_parentElement === void 0 ? void 0 : _target_parentElement.closest('[tabindex]');
|
|
124
|
+
focusable === null || focusable === void 0 ? void 0 : focusable.focus({
|
|
125
|
+
preventScroll: true
|
|
133
126
|
});
|
|
134
127
|
}
|
|
135
128
|
};
|
|
136
|
-
|
|
137
|
-
let
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
146
|
-
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
147
|
-
let scrollX = window.pageXOffset;
|
|
148
|
-
let scrollY = window.pageYOffset;
|
|
149
|
-
restoreStyles = (0, $7mMvr$chain)($49c51c25361d4cd2$var$addEvent(window, 'scroll', onWindowScroll), $49c51c25361d4cd2$var$setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`), $49c51c25361d4cd2$var$setStyle(document.documentElement, 'overflow', 'hidden'), $49c51c25361d4cd2$var$setStyle(document.body, 'marginTop', `-${scrollY}px`), ()=>{
|
|
150
|
-
window.scrollTo(scrollX, scrollY);
|
|
129
|
+
// Override programmatic focus to scroll into view without scrolling the whole page.
|
|
130
|
+
let focus = HTMLElement.prototype.focus;
|
|
131
|
+
HTMLElement.prototype.focus = function(opts) {
|
|
132
|
+
// Track whether the keyboard was already visible before.
|
|
133
|
+
let wasKeyboardVisible = document.activeElement != null && (0, $7mMvr$willOpenKeyboard)(document.activeElement);
|
|
134
|
+
// Focus the element without scrolling the page.
|
|
135
|
+
focus.call(this, {
|
|
136
|
+
...opts,
|
|
137
|
+
preventScroll: true
|
|
151
138
|
});
|
|
152
|
-
|
|
153
|
-
window.scrollTo(0, 0);
|
|
139
|
+
if (!opts || !opts.preventScroll) $49c51c25361d4cd2$var$scrollIntoViewWhenReady(this, wasKeyboardVisible);
|
|
154
140
|
};
|
|
155
141
|
let removeEvents = (0, $7mMvr$chain)($49c51c25361d4cd2$var$addEvent(document, 'touchstart', onTouchStart, {
|
|
156
142
|
passive: false,
|
|
@@ -158,15 +144,11 @@ function $49c51c25361d4cd2$var$preventScrollMobileSafari() {
|
|
|
158
144
|
}), $49c51c25361d4cd2$var$addEvent(document, 'touchmove', onTouchMove, {
|
|
159
145
|
passive: false,
|
|
160
146
|
capture: true
|
|
161
|
-
}), $49c51c25361d4cd2$var$addEvent(document, '
|
|
162
|
-
passive: false,
|
|
163
|
-
capture: true
|
|
164
|
-
}), $49c51c25361d4cd2$var$addEvent(document, 'focus', onFocus, true));
|
|
147
|
+
}), $49c51c25361d4cd2$var$addEvent(document, 'blur', onBlur, true));
|
|
165
148
|
return ()=>{
|
|
166
|
-
// Restore styles and scroll the page back to where it was.
|
|
167
|
-
restoreScrollableStyles === null || restoreScrollableStyles === void 0 ? void 0 : restoreScrollableStyles();
|
|
168
|
-
restoreStyles === null || restoreStyles === void 0 ? void 0 : restoreStyles();
|
|
169
149
|
removeEvents();
|
|
150
|
+
style.remove();
|
|
151
|
+
HTMLElement.prototype.focus = focus;
|
|
170
152
|
};
|
|
171
153
|
}
|
|
172
154
|
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
@@ -187,6 +169,15 @@ function $49c51c25361d4cd2$var$addEvent(target, event, handler, options) {
|
|
|
187
169
|
target.removeEventListener(event, handler, options);
|
|
188
170
|
};
|
|
189
171
|
}
|
|
172
|
+
function $49c51c25361d4cd2$var$scrollIntoViewWhenReady(target, wasKeyboardVisible) {
|
|
173
|
+
if (wasKeyboardVisible || !$49c51c25361d4cd2$var$visualViewport) // If the keyboard was already visible, scroll the target into view immediately.
|
|
174
|
+
$49c51c25361d4cd2$var$scrollIntoView(target);
|
|
175
|
+
else // Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
176
|
+
// measure the correct position to scroll to.
|
|
177
|
+
$49c51c25361d4cd2$var$visualViewport.addEventListener('resize', ()=>$49c51c25361d4cd2$var$scrollIntoView(target), {
|
|
178
|
+
once: true
|
|
179
|
+
});
|
|
180
|
+
}
|
|
190
181
|
function $49c51c25361d4cd2$var$scrollIntoView(target) {
|
|
191
182
|
let root = document.scrollingElement || document.documentElement;
|
|
192
183
|
let nextTarget = target;
|
|
@@ -194,16 +185,23 @@ function $49c51c25361d4cd2$var$scrollIntoView(target) {
|
|
|
194
185
|
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
195
186
|
let scrollable = (0, $7mMvr$getScrollParent)(nextTarget);
|
|
196
187
|
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
|
|
197
|
-
let
|
|
198
|
-
let
|
|
199
|
-
if (
|
|
188
|
+
let scrollableRect = scrollable.getBoundingClientRect();
|
|
189
|
+
let targetRect = nextTarget.getBoundingClientRect();
|
|
190
|
+
if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
|
|
191
|
+
let bottom = scrollableRect.bottom;
|
|
192
|
+
if ($49c51c25361d4cd2$var$visualViewport) bottom = Math.min(bottom, $49c51c25361d4cd2$var$visualViewport.offsetTop + $49c51c25361d4cd2$var$visualViewport.height);
|
|
193
|
+
// Center within the viewport.
|
|
194
|
+
let adjustment = targetRect.top - scrollableRect.top - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
|
|
195
|
+
scrollable.scrollTo({
|
|
196
|
+
// Clamp to the valid range to prevent over-scrolling.
|
|
197
|
+
top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
|
|
198
|
+
behavior: 'smooth'
|
|
199
|
+
});
|
|
200
|
+
}
|
|
200
201
|
}
|
|
201
202
|
nextTarget = scrollable.parentElement;
|
|
202
203
|
}
|
|
203
204
|
}
|
|
204
|
-
function $49c51c25361d4cd2$var$willOpenKeyboard(target) {
|
|
205
|
-
return target instanceof HTMLInputElement && !$49c51c25361d4cd2$var$nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
|
|
206
|
-
}
|
|
207
205
|
|
|
208
206
|
|
|
209
207
|
export {$49c51c25361d4cd2$export$ee0f7cc6afcd1c18 as usePreventScroll};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAAA;;;;;;;;;;CAUC;AASD,MAAM,uCAAiB,OAAO,aAAa,eAAe,OAAO,cAAc;AAE/E,sEAAsE;AACtE,MAAM,0CAAoB,IAAI,IAAI;IAChC;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD;AAED,mIAAmI;AACnI,IAAI,2CAAqB;AACzB,IAAI;AAOG,SAAS,0CAAiB,UAAgC,CAAC,CAAC;IACjE,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,CAAA,GAAA,sBAAc,EAAE;QACd,IAAI,YACF;QAGF;QACA,IAAI,6CAAuB;YACzB,IAAI,CAAA,GAAA,YAAI,KACN,gCAAU;iBAEV,gCAAU;;QAId,OAAO;YACL;YACA,IAAI,6CAAuB,GACzB;QAEJ;IACF,GAAG;QAAC;KAAW;AACjB;AAEA,0FAA0F;AAC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YAAY,gBAAgB,WAAW,YAAY,EACrD,WAAW,SAAS,IAAI,YAAY;QAExC;QAEA,aAAa,WAAW,aAAa;IACvC;AACF;AAEA,SAAS,uCAAiB,MAAe;IACvC,OACE,AAAC,kBAAkB,oBAAoB,CAAC,wCAAkB,GAAG,CAAC,OAAO,IAAI,KACzE,kBAAkB,uBACjB,kBAAkB,eAAe,OAAO,iBAAiB;AAE9D","sources":["packages/@react-aria/overlays/src/usePreventScroll.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 {chain, getScrollParent, isIOS, useLayoutEffect} from '@react-aria/utils';\n\ninterface PreventScrollOptions {\n /** Whether the scroll lock is disabled. */\n isDisabled?: boolean\n}\n\nconst visualViewport = typeof document !== 'undefined' && window.visualViewport;\n\n// HTML input types that do not cause the software keyboard to appear.\nconst nonTextInputTypes = new Set([\n 'checkbox',\n 'radio',\n 'range',\n 'color',\n 'file',\n 'image',\n 'button',\n 'submit',\n 'reset'\n]);\n\n// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position\nlet preventScrollCount = 0;\nlet restore;\n\n/**\n * Prevents scrolling on the document body on mount, and\n * restores it on unmount. Also ensures that content does not\n * shift due to the scrollbars disappearing.\n */\nexport function usePreventScroll(options: PreventScrollOptions = {}): void {\n let {isDisabled} = options;\n\n useLayoutEffect(() => {\n if (isDisabled) {\n return;\n }\n\n preventScrollCount++;\n if (preventScrollCount === 1) {\n if (isIOS()) {\n restore = preventScrollMobileSafari();\n } else {\n restore = preventScrollStandard();\n }\n }\n\n return () => {\n preventScrollCount--;\n if (preventScrollCount === 0) {\n restore();\n }\n };\n }, [isDisabled]);\n}\n\n// For most browsers, all we need to do is set `overflow: hidden` on the root element, and\n// add some padding to prevent the page from shifting when the scrollbar is hidden.\nfunction preventScrollStandard() {\n let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;\n return chain(\n scrollbarWidth > 0 &&\n // Use scrollbar-gutter when supported because it also works for fixed positioned elements.\n ('scrollbarGutter' in document.documentElement.style\n ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')\n : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),\n setStyle(document.documentElement, 'overflow', 'hidden')\n );\n}\n\n// Mobile Safari is a whole different beast. Even with overflow: hidden,\n// it still scrolls the page in many situations:\n//\n// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.\n// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of\n// it, so it becomes scrollable.\n// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.\n// This may cause even fixed position elements to scroll off the screen.\n// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always\n// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.\n//\n// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:\n//\n// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling\n// on the window.\n// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at\n// the top or bottom. Work around a bug where this does not work when the element does not actually overflow\n// by preventing default in a `touchmove` event.\n// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.\n// 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top\n// of the page, which prevents it from scrolling the page. After the input is focused, scroll the element\n// into view ourselves, without scrolling the whole page.\n// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the\n// same visually, but makes the actual scroll position always zero. This is required to make all of the\n// above work or Safari will still try to scroll the page when focusing an input.\n// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting\n// to navigate to an input with the next/previous buttons that's outside a modal.\nfunction preventScrollMobileSafari() {\n let scrollable: Element;\n let restoreScrollableStyles;\n let onTouchStart = (e: TouchEvent) => {\n // Store the nearest scrollable parent element from the element that the user touched.\n scrollable = getScrollParent(e.target as Element, true);\n if (scrollable === document.documentElement && scrollable === document.body) {\n return;\n }\n\n // Prevent scrolling up when at the top and scrolling down when at the bottom\n // of a nested scrollable area, otherwise mobile Safari will start scrolling\n // the window instead.\n if (scrollable instanceof HTMLElement && window.getComputedStyle(scrollable).overscrollBehavior === 'auto') {\n restoreScrollableStyles = setStyle(scrollable, 'overscrollBehavior', 'contain');\n }\n };\n\n let onTouchMove = (e: TouchEvent) => {\n // Prevent scrolling the window.\n if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {\n e.preventDefault();\n return;\n }\n\n // overscroll-behavior should prevent scroll chaining, but currently does not\n // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452\n // This checks that both the width and height do not overflow, otherwise we might\n // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to\n // the element will prevent vertical page scrolling. We can't add that automatically\n // because it must be set before the touchstart event.\n if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {\n e.preventDefault();\n }\n };\n\n let onTouchEnd = () => {\n if (restoreScrollableStyles) {\n restoreScrollableStyles();\n }\n };\n\n let onFocus = (e: FocusEvent) => {\n let target = e.target as HTMLElement;\n if (willOpenKeyboard(target)) {\n setupStyles();\n\n // Apply a transform to trick Safari into thinking the input is at the top of the page\n // so it doesn't try to scroll it into view.\n target.style.transform = 'translateY(-2000px)';\n requestAnimationFrame(() => {\n target.style.transform = '';\n\n // This will have prevented the browser from scrolling the focused element into view,\n // so we need to do this ourselves in a way that doesn't cause the whole page to scroll.\n if (visualViewport) {\n if (visualViewport.height < window.innerHeight) {\n // If the keyboard is already visible, do this after one additional frame\n // to wait for the transform to be removed.\n requestAnimationFrame(() => {\n scrollIntoView(target);\n });\n } else {\n // Otherwise, wait for the visual viewport to resize before scrolling so we can\n // measure the correct position to scroll to.\n visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});\n }\n }\n });\n }\n };\n\n let restoreStyles: null | (() => void) = null;\n let setupStyles = () => {\n if (restoreStyles) {\n return;\n }\n\n let onWindowScroll = () => {\n // Last resort. If the window scrolled, scroll it back to the top.\n // It should always be at the top because the body will have a negative margin (see below).\n window.scrollTo(0, 0);\n };\n\n // Record the original scroll position so we can restore it.\n // Then apply a negative margin to the body to offset it by the scroll position. This will\n // enable us to scroll the window to the top, which is required for the rest of this to work.\n let scrollX = window.pageXOffset;\n let scrollY = window.pageYOffset;\n\n restoreStyles = chain(\n addEvent(window, 'scroll', onWindowScroll),\n setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`),\n setStyle(document.documentElement, 'overflow', 'hidden'),\n setStyle(document.body, 'marginTop', `-${scrollY}px`),\n () => {\n window.scrollTo(scrollX, scrollY);\n }\n );\n\n // Scroll to the top. The negative margin on the body will make this appear the same.\n window.scrollTo(0, 0);\n };\n\n let removeEvents = chain(\n addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),\n addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),\n addEvent(document, 'touchend', onTouchEnd, {passive: false, capture: true}),\n addEvent(document, 'focus', onFocus, true)\n );\n\n return () => {\n // Restore styles and scroll the page back to where it was.\n restoreScrollableStyles?.();\n restoreStyles?.();\n removeEvents();\n };\n}\n\n// Sets a CSS property on an element, and returns a function to revert it to the previous value.\nfunction setStyle(element: HTMLElement, style: string, value: string) {\n let cur = element.style[style];\n element.style[style] = value;\n\n return () => {\n element.style[style] = cur;\n };\n}\n\n// Adds an event listener to an element, and returns a function to remove it.\nfunction addEvent<K extends keyof GlobalEventHandlersEventMap>(\n target: Document | Window,\n event: K,\n handler: (this: Document | Window, ev: GlobalEventHandlersEventMap[K]) => any,\n options?: boolean | AddEventListenerOptions\n) {\n // internal function, so it's ok to ignore the difficult to fix type error\n // @ts-ignore\n target.addEventListener(event, handler, options);\n return () => {\n // @ts-ignore\n target.removeEventListener(event, handler, options);\n };\n}\n\nfunction scrollIntoView(target: Element) {\n let root = document.scrollingElement || document.documentElement;\n let nextTarget: Element | null = target;\n while (nextTarget && nextTarget !== root) {\n // Find the parent scrollable element and adjust the scroll position if the target is not already in view.\n let scrollable = getScrollParent(nextTarget);\n if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {\n let scrollableTop = scrollable.getBoundingClientRect().top;\n let targetTop = nextTarget.getBoundingClientRect().top;\n if (targetTop > scrollableTop + nextTarget.clientHeight) {\n scrollable.scrollTop += targetTop - scrollableTop;\n }\n }\n\n nextTarget = scrollable.parentElement;\n }\n}\n\nfunction willOpenKeyboard(target: Element) {\n return (\n (target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) ||\n target instanceof HTMLTextAreaElement ||\n (target instanceof HTMLElement && target.isContentEditable)\n );\n}\n"],"names":[],"version":3,"file":"usePreventScroll.module.js.map"}
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AASD,MAAM,uCAAiB,OAAO,aAAa,eAAe,OAAO,cAAc;AAE/E,mIAAmI;AACnI,IAAI,2CAAqB;AACzB,IAAI;AAOG,SAAS,0CAAiB,UAAgC,CAAC,CAAC;IACjE,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,CAAA,GAAA,sBAAc,EAAE;QACd,IAAI,YACF;QAGF;QACA,IAAI,6CAAuB;YACzB,IAAI,CAAA,GAAA,YAAI,KACN,gCAAU;iBAEV,gCAAU;;QAId,OAAO;YACL;YACA,IAAI,6CAAuB,GACzB;QAEJ;IACF,GAAG;QAAC;KAAW;AACjB;AAEA,0FAA0F;AAC1F,mFAAmF;AACnF,SAAS;IACP,IAAI,iBAAiB,OAAO,UAAU,GAAG,SAAS,eAAe,CAAC,WAAW;IAC7E,OAAO,CAAA,GAAA,YAAI,EACT,iBAAiB,KACf,2FAA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iB,WAAW,qBAAqB;YACrD,IAAI,aAAa,WAAW,qBAAqB;YACjD,IAAI,WAAW,GAAG,GAAG,eAAe,GAAG,IAAI,WAAW,MAAM,GAAG,eAAe,GAAG,GAAG,WAAW,YAAY,EAAE;gBAC3G,IAAI,SAAS,eAAe,MAAM;gBAClC,IAAI,sCACF,SAAS,KAAK,GAAG,CAAC,QAAQ,qCAAe,SAAS,GAAG,qCAAe,MAAM;gBAG5E,8BAA8B;gBAC9B,IAAI,aAAa,AAAC,WAAW,GAAG,GAAG,eAAe,GAAG,GAAK,CAAA,AAAC,CAAA,SAAS,eAAe,GAAG,AAAD,IAAK,IAAI,WAAW,MAAM,GAAG,CAAA;gBAClH,WAAW,QAAQ,CAAC;oBAClB,sDAAsD;oBACtD,KAAK,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,WAAW,YAAY,GAAG,WAAW,YAAY,EAAE,WAAW,SAAS,GAAG;oBACpG,UAAU;gBACZ;YACF;QACF;QAEA,aAAa,WAAW,aAAa;IACvC;AACF","sources":["packages/@react-aria/overlays/src/usePreventScroll.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 {chain, getScrollParent, isIOS, isScrollable, useLayoutEffect, willOpenKeyboard} from '@react-aria/utils';\n\ninterface PreventScrollOptions {\n /** Whether the scroll lock is disabled. */\n isDisabled?: boolean\n}\n\nconst visualViewport = typeof document !== 'undefined' && window.visualViewport;\n\n// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position\nlet preventScrollCount = 0;\nlet restore;\n\n/**\n * Prevents scrolling on the document body on mount, and\n * restores it on unmount. Also ensures that content does not\n * shift due to the scrollbars disappearing.\n */\nexport function usePreventScroll(options: PreventScrollOptions = {}): void {\n let {isDisabled} = options;\n\n useLayoutEffect(() => {\n if (isDisabled) {\n return;\n }\n\n preventScrollCount++;\n if (preventScrollCount === 1) {\n if (isIOS()) {\n restore = preventScrollMobileSafari();\n } else {\n restore = preventScrollStandard();\n }\n }\n\n return () => {\n preventScrollCount--;\n if (preventScrollCount === 0) {\n restore();\n }\n };\n }, [isDisabled]);\n}\n\n// For most browsers, all we need to do is set `overflow: hidden` on the root element, and\n// add some padding to prevent the page from shifting when the scrollbar is hidden.\nfunction preventScrollStandard() {\n let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;\n return chain(\n scrollbarWidth > 0 &&\n // Use scrollbar-gutter when supported because it also works for fixed positioned elements.\n ('scrollbarGutter' in document.documentElement.style\n ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')\n : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),\n setStyle(document.documentElement, 'overflow', 'hidden')\n );\n}\n\n// Mobile Safari is a whole different beast. Even with overflow: hidden,\n// it still scrolls the page in many situations:\n//\n// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.\n// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of\n// it, so it becomes scrollable.\n// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.\n// This may cause even fixed position elements to scroll off the screen.\n// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always\n// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.\n//\n// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:\n//\n// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling\n// on the window.\n// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at\n// the top or bottom. Work around a bug where this does not work when the element does not actually overflow\n// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch\n// zooming or when an element contains text selection, which may allow scrolling in some cases.\n// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.\n// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents \n// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view\n// ourselves, without scrolling the whole page.\nfunction preventScrollMobileSafari() {\n let scrollable: Element;\n let allowTouchMove = false;\n let onTouchStart = (e: TouchEvent) => {\n // Store the nearest scrollable parent element from the element that the user touched.\n let target = e.target as Element;\n scrollable = isScrollable(target) ? target : getScrollParent(target, true);\n allowTouchMove = false;\n \n // If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.\n let selection = target.ownerDocument.defaultView!.getSelection();\n if (selection && !selection.isCollapsed && selection.containsNode(target, true)) {\n allowTouchMove = true;\n }\n\n // If this is a focused input element with a selected range, allow user to drag the selection handles.\n if (\n 'selectionStart' in target && \n 'selectionEnd' in target &&\n (target.selectionStart as number) < (target.selectionEnd as number) &&\n target.ownerDocument.activeElement === target\n ) {\n allowTouchMove = true;\n }\n };\n\n // Prevent scrolling up when at the top and scrolling down when at the bottom\n // of a nested scrollable area, otherwise mobile Safari will start scrolling\n // the window instead.\n // This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.\n let style = document.createElement('style');\n style.textContent = `\n@layer {\n * {\n overscroll-behavior: contain;\n }\n}`.trim();\n document.head.prepend(style);\n\n let onTouchMove = (e: TouchEvent) => {\n // Allow pinch-zooming.\n if (e.touches.length === 2 || allowTouchMove) {\n return;\n }\n\n // Prevent scrolling the window.\n if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {\n e.preventDefault();\n return;\n }\n\n // overscroll-behavior should prevent scroll chaining, but currently does not\n // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452\n // This checks that both the width and height do not overflow, otherwise we might\n // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to\n // the element will prevent vertical page scrolling. We can't add that automatically\n // because it must be set before the touchstart event.\n if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {\n e.preventDefault();\n }\n };\n\n let onBlur = (e: FocusEvent) => {\n let target = e.target as HTMLElement;\n let relatedTarget = e.relatedTarget as HTMLElement | null;\n if (relatedTarget && willOpenKeyboard(relatedTarget)) {\n // Focus without scrolling the whole page, and then scroll into view manually.\n relatedTarget.focus({preventScroll: true});\n scrollIntoViewWhenReady(relatedTarget, willOpenKeyboard(target));\n } else if (!relatedTarget) {\n // When tapping the Done button on the keyboard, focus moves to the body.\n // FocusScope will then restore focus back to the input. Later when tapping\n // the same input again, it is already focused, so no blur event will fire,\n // resulting in the flow above never running and Safari's native scrolling occurring.\n // Instead, move focus to the parent focusable element (e.g. the dialog).\n let focusable = target.parentElement?.closest('[tabindex]') as HTMLElement | null;\n focusable?.focus({preventScroll: true});\n }\n };\n\n // Override programmatic focus to scroll into view without scrolling the whole page.\n let focus = HTMLElement.prototype.focus;\n HTMLElement.prototype.focus = function (opts) {\n // Track whether the keyboard was already visible before.\n let wasKeyboardVisible = document.activeElement != null && willOpenKeyboard(document.activeElement);\n\n // Focus the element without scrolling the page.\n focus.call(this, {...opts, preventScroll: true});\n\n if (!opts || !opts.preventScroll) {\n scrollIntoViewWhenReady(this, wasKeyboardVisible);\n }\n };\n\n let removeEvents = chain(\n addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),\n addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),\n addEvent(document, 'blur', onBlur, true)\n );\n\n return () => {\n removeEvents();\n style.remove();\n HTMLElement.prototype.focus = focus;\n };\n}\n\n// Sets a CSS property on an element, and returns a function to revert it to the previous value.\nfunction setStyle(element: HTMLElement, style: string, value: string) {\n let cur = element.style[style];\n element.style[style] = value;\n\n return () => {\n element.style[style] = cur;\n };\n}\n\n// Adds an event listener to an element, and returns a function to remove it.\nfunction addEvent<K extends keyof GlobalEventHandlersEventMap>(\n target: Document | Window,\n event: K,\n handler: (this: Document | Window, ev: GlobalEventHandlersEventMap[K]) => any,\n options?: boolean | AddEventListenerOptions\n) {\n // internal function, so it's ok to ignore the difficult to fix type error\n // @ts-ignore\n target.addEventListener(event, handler, options);\n return () => {\n // @ts-ignore\n target.removeEventListener(event, handler, options);\n };\n}\n\nfunction scrollIntoViewWhenReady(target: Element, wasKeyboardVisible: boolean) {\n if (wasKeyboardVisible || !visualViewport) {\n // If the keyboard was already visible, scroll the target into view immediately.\n scrollIntoView(target);\n } else {\n // Otherwise, wait for the visual viewport to resize before scrolling so we can\n // measure the correct position to scroll to.\n visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});\n }\n}\n\nfunction scrollIntoView(target: Element) {\n let root = document.scrollingElement || document.documentElement;\n let nextTarget: Element | null = target;\n while (nextTarget && nextTarget !== root) {\n // Find the parent scrollable element and adjust the scroll position if the target is not already in view.\n let scrollable = getScrollParent(nextTarget);\n if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {\n let scrollableRect = scrollable.getBoundingClientRect();\n let targetRect = nextTarget.getBoundingClientRect();\n if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {\n let bottom = scrollableRect.bottom;\n if (visualViewport) {\n bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);\n }\n\n // Center within the viewport.\n let adjustment = (targetRect.top - scrollableRect.top) - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);\n scrollable.scrollTo({\n // Clamp to the valid range to prevent over-scrolling.\n top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),\n behavior: 'smooth'\n });\n }\n }\n\n nextTarget = scrollable.parentElement;\n }\n}\n"],"names":[],"version":3,"file":"usePreventScroll.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/overlays",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.30.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
"url": "https://github.com/adobe/react-spectrum"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@react-aria/focus": "^3.21.
|
|
30
|
-
"@react-aria/i18n": "^3.12.
|
|
31
|
-
"@react-aria/interactions": "^3.25.
|
|
29
|
+
"@react-aria/focus": "^3.21.2",
|
|
30
|
+
"@react-aria/i18n": "^3.12.13",
|
|
31
|
+
"@react-aria/interactions": "^3.25.6",
|
|
32
32
|
"@react-aria/ssr": "^3.9.10",
|
|
33
|
-
"@react-aria/utils": "^3.
|
|
34
|
-
"@react-aria/visually-hidden": "^3.8.
|
|
35
|
-
"@react-stately/overlays": "^3.6.
|
|
36
|
-
"@react-types/button": "^3.14.
|
|
37
|
-
"@react-types/overlays": "^3.9.
|
|
38
|
-
"@react-types/shared": "^3.32.
|
|
33
|
+
"@react-aria/utils": "^3.31.0",
|
|
34
|
+
"@react-aria/visually-hidden": "^3.8.28",
|
|
35
|
+
"@react-stately/overlays": "^3.6.20",
|
|
36
|
+
"@react-types/button": "^3.14.1",
|
|
37
|
+
"@react-types/overlays": "^3.9.2",
|
|
38
|
+
"@react-types/shared": "^3.32.1",
|
|
39
39
|
"@swc/helpers": "^0.5.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "0bda51183baa23306342af32a82012ea0fe0f2dc"
|
|
49
49
|
}
|
package/src/usePreventScroll.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {chain, getScrollParent, isIOS, useLayoutEffect} from '@react-aria/utils';
|
|
13
|
+
import {chain, getScrollParent, isIOS, isScrollable, useLayoutEffect, willOpenKeyboard} from '@react-aria/utils';
|
|
14
14
|
|
|
15
15
|
interface PreventScrollOptions {
|
|
16
16
|
/** Whether the scroll lock is disabled. */
|
|
@@ -19,19 +19,6 @@ interface PreventScrollOptions {
|
|
|
19
19
|
|
|
20
20
|
const visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
21
21
|
|
|
22
|
-
// HTML input types that do not cause the software keyboard to appear.
|
|
23
|
-
const nonTextInputTypes = new Set([
|
|
24
|
-
'checkbox',
|
|
25
|
-
'radio',
|
|
26
|
-
'range',
|
|
27
|
-
'color',
|
|
28
|
-
'file',
|
|
29
|
-
'image',
|
|
30
|
-
'button',
|
|
31
|
-
'submit',
|
|
32
|
-
'reset'
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
22
|
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
36
23
|
let preventScrollCount = 0;
|
|
37
24
|
let restore;
|
|
@@ -98,35 +85,57 @@ function preventScrollStandard() {
|
|
|
98
85
|
// on the window.
|
|
99
86
|
// 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
|
|
100
87
|
// the top or bottom. Work around a bug where this does not work when the element does not actually overflow
|
|
101
|
-
// by preventing default in a `touchmove` event.
|
|
88
|
+
// by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch
|
|
89
|
+
// zooming or when an element contains text selection, which may allow scrolling in some cases.
|
|
102
90
|
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
103
|
-
// 4. When
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
107
|
-
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
108
|
-
// above work or Safari will still try to scroll the page when focusing an input.
|
|
109
|
-
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
110
|
-
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
91
|
+
// 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents
|
|
92
|
+
// Safari from scrolling the page. After a small delay, focus the real input and scroll it into view
|
|
93
|
+
// ourselves, without scrolling the whole page.
|
|
111
94
|
function preventScrollMobileSafari() {
|
|
112
95
|
let scrollable: Element;
|
|
113
|
-
let
|
|
96
|
+
let allowTouchMove = false;
|
|
114
97
|
let onTouchStart = (e: TouchEvent) => {
|
|
115
98
|
// Store the nearest scrollable parent element from the element that the user touched.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
99
|
+
let target = e.target as Element;
|
|
100
|
+
scrollable = isScrollable(target) ? target : getScrollParent(target, true);
|
|
101
|
+
allowTouchMove = false;
|
|
102
|
+
|
|
103
|
+
// If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.
|
|
104
|
+
let selection = target.ownerDocument.defaultView!.getSelection();
|
|
105
|
+
if (selection && !selection.isCollapsed && selection.containsNode(target, true)) {
|
|
106
|
+
allowTouchMove = true;
|
|
119
107
|
}
|
|
120
108
|
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
109
|
+
// If this is a focused input element with a selected range, allow user to drag the selection handles.
|
|
110
|
+
if (
|
|
111
|
+
'selectionStart' in target &&
|
|
112
|
+
'selectionEnd' in target &&
|
|
113
|
+
(target.selectionStart as number) < (target.selectionEnd as number) &&
|
|
114
|
+
target.ownerDocument.activeElement === target
|
|
115
|
+
) {
|
|
116
|
+
allowTouchMove = true;
|
|
126
117
|
}
|
|
127
118
|
};
|
|
128
119
|
|
|
120
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
121
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
122
|
+
// the window instead.
|
|
123
|
+
// This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
|
|
124
|
+
let style = document.createElement('style');
|
|
125
|
+
style.textContent = `
|
|
126
|
+
@layer {
|
|
127
|
+
* {
|
|
128
|
+
overscroll-behavior: contain;
|
|
129
|
+
}
|
|
130
|
+
}`.trim();
|
|
131
|
+
document.head.prepend(style);
|
|
132
|
+
|
|
129
133
|
let onTouchMove = (e: TouchEvent) => {
|
|
134
|
+
// Allow pinch-zooming.
|
|
135
|
+
if (e.touches.length === 2 || allowTouchMove) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
130
139
|
// Prevent scrolling the window.
|
|
131
140
|
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
132
141
|
e.preventDefault();
|
|
@@ -144,86 +153,48 @@ function preventScrollMobileSafari() {
|
|
|
144
153
|
}
|
|
145
154
|
};
|
|
146
155
|
|
|
147
|
-
let
|
|
148
|
-
if (restoreScrollableStyles) {
|
|
149
|
-
restoreScrollableStyles();
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
let onFocus = (e: FocusEvent) => {
|
|
156
|
+
let onBlur = (e: FocusEvent) => {
|
|
154
157
|
let target = e.target as HTMLElement;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// If the keyboard is already visible, do this after one additional frame
|
|
169
|
-
// to wait for the transform to be removed.
|
|
170
|
-
requestAnimationFrame(() => {
|
|
171
|
-
scrollIntoView(target);
|
|
172
|
-
});
|
|
173
|
-
} else {
|
|
174
|
-
// Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
175
|
-
// measure the correct position to scroll to.
|
|
176
|
-
visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
});
|
|
158
|
+
let relatedTarget = e.relatedTarget as HTMLElement | null;
|
|
159
|
+
if (relatedTarget && willOpenKeyboard(relatedTarget)) {
|
|
160
|
+
// Focus without scrolling the whole page, and then scroll into view manually.
|
|
161
|
+
relatedTarget.focus({preventScroll: true});
|
|
162
|
+
scrollIntoViewWhenReady(relatedTarget, willOpenKeyboard(target));
|
|
163
|
+
} else if (!relatedTarget) {
|
|
164
|
+
// When tapping the Done button on the keyboard, focus moves to the body.
|
|
165
|
+
// FocusScope will then restore focus back to the input. Later when tapping
|
|
166
|
+
// the same input again, it is already focused, so no blur event will fire,
|
|
167
|
+
// resulting in the flow above never running and Safari's native scrolling occurring.
|
|
168
|
+
// Instead, move focus to the parent focusable element (e.g. the dialog).
|
|
169
|
+
let focusable = target.parentElement?.closest('[tabindex]') as HTMLElement | null;
|
|
170
|
+
focusable?.focus({preventScroll: true});
|
|
180
171
|
}
|
|
181
172
|
};
|
|
182
173
|
|
|
183
|
-
|
|
184
|
-
let
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
let onWindowScroll = () => {
|
|
190
|
-
// Last resort. If the window scrolled, scroll it back to the top.
|
|
191
|
-
// It should always be at the top because the body will have a negative margin (see below).
|
|
192
|
-
window.scrollTo(0, 0);
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
// Record the original scroll position so we can restore it.
|
|
196
|
-
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
197
|
-
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
198
|
-
let scrollX = window.pageXOffset;
|
|
199
|
-
let scrollY = window.pageYOffset;
|
|
174
|
+
// Override programmatic focus to scroll into view without scrolling the whole page.
|
|
175
|
+
let focus = HTMLElement.prototype.focus;
|
|
176
|
+
HTMLElement.prototype.focus = function (opts) {
|
|
177
|
+
// Track whether the keyboard was already visible before.
|
|
178
|
+
let wasKeyboardVisible = document.activeElement != null && willOpenKeyboard(document.activeElement);
|
|
200
179
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`),
|
|
204
|
-
setStyle(document.documentElement, 'overflow', 'hidden'),
|
|
205
|
-
setStyle(document.body, 'marginTop', `-${scrollY}px`),
|
|
206
|
-
() => {
|
|
207
|
-
window.scrollTo(scrollX, scrollY);
|
|
208
|
-
}
|
|
209
|
-
);
|
|
180
|
+
// Focus the element without scrolling the page.
|
|
181
|
+
focus.call(this, {...opts, preventScroll: true});
|
|
210
182
|
|
|
211
|
-
|
|
212
|
-
|
|
183
|
+
if (!opts || !opts.preventScroll) {
|
|
184
|
+
scrollIntoViewWhenReady(this, wasKeyboardVisible);
|
|
185
|
+
}
|
|
213
186
|
};
|
|
214
187
|
|
|
215
188
|
let removeEvents = chain(
|
|
216
189
|
addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),
|
|
217
190
|
addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),
|
|
218
|
-
addEvent(document, '
|
|
219
|
-
addEvent(document, 'focus', onFocus, true)
|
|
191
|
+
addEvent(document, 'blur', onBlur, true)
|
|
220
192
|
);
|
|
221
193
|
|
|
222
194
|
return () => {
|
|
223
|
-
// Restore styles and scroll the page back to where it was.
|
|
224
|
-
restoreScrollableStyles?.();
|
|
225
|
-
restoreStyles?.();
|
|
226
195
|
removeEvents();
|
|
196
|
+
style.remove();
|
|
197
|
+
HTMLElement.prototype.focus = focus;
|
|
227
198
|
};
|
|
228
199
|
}
|
|
229
200
|
|
|
@@ -253,6 +224,17 @@ function addEvent<K extends keyof GlobalEventHandlersEventMap>(
|
|
|
253
224
|
};
|
|
254
225
|
}
|
|
255
226
|
|
|
227
|
+
function scrollIntoViewWhenReady(target: Element, wasKeyboardVisible: boolean) {
|
|
228
|
+
if (wasKeyboardVisible || !visualViewport) {
|
|
229
|
+
// If the keyboard was already visible, scroll the target into view immediately.
|
|
230
|
+
scrollIntoView(target);
|
|
231
|
+
} else {
|
|
232
|
+
// Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
233
|
+
// measure the correct position to scroll to.
|
|
234
|
+
visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
256
238
|
function scrollIntoView(target: Element) {
|
|
257
239
|
let root = document.scrollingElement || document.documentElement;
|
|
258
240
|
let nextTarget: Element | null = target;
|
|
@@ -260,21 +242,24 @@ function scrollIntoView(target: Element) {
|
|
|
260
242
|
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
261
243
|
let scrollable = getScrollParent(nextTarget);
|
|
262
244
|
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
|
|
263
|
-
let
|
|
264
|
-
let
|
|
265
|
-
if (
|
|
266
|
-
|
|
245
|
+
let scrollableRect = scrollable.getBoundingClientRect();
|
|
246
|
+
let targetRect = nextTarget.getBoundingClientRect();
|
|
247
|
+
if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
|
|
248
|
+
let bottom = scrollableRect.bottom;
|
|
249
|
+
if (visualViewport) {
|
|
250
|
+
bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Center within the viewport.
|
|
254
|
+
let adjustment = (targetRect.top - scrollableRect.top) - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
|
|
255
|
+
scrollable.scrollTo({
|
|
256
|
+
// Clamp to the valid range to prevent over-scrolling.
|
|
257
|
+
top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
|
|
258
|
+
behavior: 'smooth'
|
|
259
|
+
});
|
|
267
260
|
}
|
|
268
261
|
}
|
|
269
262
|
|
|
270
263
|
nextTarget = scrollable.parentElement;
|
|
271
264
|
}
|
|
272
265
|
}
|
|
273
|
-
|
|
274
|
-
function willOpenKeyboard(target: Element) {
|
|
275
|
-
return (
|
|
276
|
-
(target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) ||
|
|
277
|
-
target instanceof HTMLTextAreaElement ||
|
|
278
|
-
(target instanceof HTMLElement && target.isContentEditable)
|
|
279
|
-
);
|
|
280
|
-
}
|