@react-aria/overlays 3.31.1 → 3.32.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.
Files changed (263) hide show
  1. package/dist/import.mjs +12 -12
  2. package/dist/main.js +28 -28
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +12 -12
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +19 -0
  7. package/package.json +15 -21
  8. package/src/index.ts +19 -22
  9. package/dist/DismissButton.main.js +0 -51
  10. package/dist/DismissButton.main.js.map +0 -1
  11. package/dist/DismissButton.mjs +0 -46
  12. package/dist/DismissButton.module.js +0 -46
  13. package/dist/DismissButton.module.js.map +0 -1
  14. package/dist/Overlay.main.js +0 -73
  15. package/dist/Overlay.main.js.map +0 -1
  16. package/dist/Overlay.mjs +0 -63
  17. package/dist/Overlay.module.js +0 -63
  18. package/dist/Overlay.module.js.map +0 -1
  19. package/dist/PortalProvider.main.js +0 -41
  20. package/dist/PortalProvider.main.js.map +0 -1
  21. package/dist/PortalProvider.mjs +0 -31
  22. package/dist/PortalProvider.module.js +0 -31
  23. package/dist/PortalProvider.module.js.map +0 -1
  24. package/dist/ar-AE.main.js +0 -6
  25. package/dist/ar-AE.main.js.map +0 -1
  26. package/dist/ar-AE.mjs +0 -8
  27. package/dist/ar-AE.module.js +0 -8
  28. package/dist/ar-AE.module.js.map +0 -1
  29. package/dist/ariaHideOutside.main.js +0 -152
  30. package/dist/ariaHideOutside.main.js.map +0 -1
  31. package/dist/ariaHideOutside.mjs +0 -146
  32. package/dist/ariaHideOutside.module.js +0 -146
  33. package/dist/ariaHideOutside.module.js.map +0 -1
  34. package/dist/bg-BG.main.js +0 -6
  35. package/dist/bg-BG.main.js.map +0 -1
  36. package/dist/bg-BG.mjs +0 -8
  37. package/dist/bg-BG.module.js +0 -8
  38. package/dist/bg-BG.module.js.map +0 -1
  39. package/dist/calculatePosition.main.js +0 -414
  40. package/dist/calculatePosition.main.js.map +0 -1
  41. package/dist/calculatePosition.mjs +0 -408
  42. package/dist/calculatePosition.module.js +0 -408
  43. package/dist/calculatePosition.module.js.map +0 -1
  44. package/dist/cs-CZ.main.js +0 -6
  45. package/dist/cs-CZ.main.js.map +0 -1
  46. package/dist/cs-CZ.mjs +0 -8
  47. package/dist/cs-CZ.module.js +0 -8
  48. package/dist/cs-CZ.module.js.map +0 -1
  49. package/dist/da-DK.main.js +0 -6
  50. package/dist/da-DK.main.js.map +0 -1
  51. package/dist/da-DK.mjs +0 -8
  52. package/dist/da-DK.module.js +0 -8
  53. package/dist/da-DK.module.js.map +0 -1
  54. package/dist/de-DE.main.js +0 -6
  55. package/dist/de-DE.main.js.map +0 -1
  56. package/dist/de-DE.mjs +0 -8
  57. package/dist/de-DE.module.js +0 -8
  58. package/dist/de-DE.module.js.map +0 -1
  59. package/dist/el-GR.main.js +0 -6
  60. package/dist/el-GR.main.js.map +0 -1
  61. package/dist/el-GR.mjs +0 -8
  62. package/dist/el-GR.module.js +0 -8
  63. package/dist/el-GR.module.js.map +0 -1
  64. package/dist/en-US.main.js +0 -6
  65. package/dist/en-US.main.js.map +0 -1
  66. package/dist/en-US.mjs +0 -8
  67. package/dist/en-US.module.js +0 -8
  68. package/dist/en-US.module.js.map +0 -1
  69. package/dist/es-ES.main.js +0 -6
  70. package/dist/es-ES.main.js.map +0 -1
  71. package/dist/es-ES.mjs +0 -8
  72. package/dist/es-ES.module.js +0 -8
  73. package/dist/es-ES.module.js.map +0 -1
  74. package/dist/et-EE.main.js +0 -6
  75. package/dist/et-EE.main.js.map +0 -1
  76. package/dist/et-EE.mjs +0 -8
  77. package/dist/et-EE.module.js +0 -8
  78. package/dist/et-EE.module.js.map +0 -1
  79. package/dist/fi-FI.main.js +0 -6
  80. package/dist/fi-FI.main.js.map +0 -1
  81. package/dist/fi-FI.mjs +0 -8
  82. package/dist/fi-FI.module.js +0 -8
  83. package/dist/fi-FI.module.js.map +0 -1
  84. package/dist/fr-FR.main.js +0 -6
  85. package/dist/fr-FR.main.js.map +0 -1
  86. package/dist/fr-FR.mjs +0 -8
  87. package/dist/fr-FR.module.js +0 -8
  88. package/dist/fr-FR.module.js.map +0 -1
  89. package/dist/he-IL.main.js +0 -6
  90. package/dist/he-IL.main.js.map +0 -1
  91. package/dist/he-IL.mjs +0 -8
  92. package/dist/he-IL.module.js +0 -8
  93. package/dist/he-IL.module.js.map +0 -1
  94. package/dist/hr-HR.main.js +0 -6
  95. package/dist/hr-HR.main.js.map +0 -1
  96. package/dist/hr-HR.mjs +0 -8
  97. package/dist/hr-HR.module.js +0 -8
  98. package/dist/hr-HR.module.js.map +0 -1
  99. package/dist/hu-HU.main.js +0 -6
  100. package/dist/hu-HU.main.js.map +0 -1
  101. package/dist/hu-HU.mjs +0 -8
  102. package/dist/hu-HU.module.js +0 -8
  103. package/dist/hu-HU.module.js.map +0 -1
  104. package/dist/intlStrings.main.js +0 -108
  105. package/dist/intlStrings.main.js.map +0 -1
  106. package/dist/intlStrings.mjs +0 -110
  107. package/dist/intlStrings.module.js +0 -110
  108. package/dist/intlStrings.module.js.map +0 -1
  109. package/dist/it-IT.main.js +0 -6
  110. package/dist/it-IT.main.js.map +0 -1
  111. package/dist/it-IT.mjs +0 -8
  112. package/dist/it-IT.module.js +0 -8
  113. package/dist/it-IT.module.js.map +0 -1
  114. package/dist/ja-JP.main.js +0 -6
  115. package/dist/ja-JP.main.js.map +0 -1
  116. package/dist/ja-JP.mjs +0 -8
  117. package/dist/ja-JP.module.js +0 -8
  118. package/dist/ja-JP.module.js.map +0 -1
  119. package/dist/ko-KR.main.js +0 -6
  120. package/dist/ko-KR.main.js.map +0 -1
  121. package/dist/ko-KR.mjs +0 -8
  122. package/dist/ko-KR.module.js +0 -8
  123. package/dist/ko-KR.module.js.map +0 -1
  124. package/dist/lt-LT.main.js +0 -6
  125. package/dist/lt-LT.main.js.map +0 -1
  126. package/dist/lt-LT.mjs +0 -8
  127. package/dist/lt-LT.module.js +0 -8
  128. package/dist/lt-LT.module.js.map +0 -1
  129. package/dist/lv-LV.main.js +0 -6
  130. package/dist/lv-LV.main.js.map +0 -1
  131. package/dist/lv-LV.mjs +0 -8
  132. package/dist/lv-LV.module.js +0 -8
  133. package/dist/lv-LV.module.js.map +0 -1
  134. package/dist/nb-NO.main.js +0 -6
  135. package/dist/nb-NO.main.js.map +0 -1
  136. package/dist/nb-NO.mjs +0 -8
  137. package/dist/nb-NO.module.js +0 -8
  138. package/dist/nb-NO.module.js.map +0 -1
  139. package/dist/nl-NL.main.js +0 -6
  140. package/dist/nl-NL.main.js.map +0 -1
  141. package/dist/nl-NL.mjs +0 -8
  142. package/dist/nl-NL.module.js +0 -8
  143. package/dist/nl-NL.module.js.map +0 -1
  144. package/dist/pl-PL.main.js +0 -6
  145. package/dist/pl-PL.main.js.map +0 -1
  146. package/dist/pl-PL.mjs +0 -8
  147. package/dist/pl-PL.module.js +0 -8
  148. package/dist/pl-PL.module.js.map +0 -1
  149. package/dist/pt-BR.main.js +0 -6
  150. package/dist/pt-BR.main.js.map +0 -1
  151. package/dist/pt-BR.mjs +0 -8
  152. package/dist/pt-BR.module.js +0 -8
  153. package/dist/pt-BR.module.js.map +0 -1
  154. package/dist/pt-PT.main.js +0 -6
  155. package/dist/pt-PT.main.js.map +0 -1
  156. package/dist/pt-PT.mjs +0 -8
  157. package/dist/pt-PT.module.js +0 -8
  158. package/dist/pt-PT.module.js.map +0 -1
  159. package/dist/ro-RO.main.js +0 -6
  160. package/dist/ro-RO.main.js.map +0 -1
  161. package/dist/ro-RO.mjs +0 -8
  162. package/dist/ro-RO.module.js +0 -8
  163. package/dist/ro-RO.module.js.map +0 -1
  164. package/dist/ru-RU.main.js +0 -6
  165. package/dist/ru-RU.main.js.map +0 -1
  166. package/dist/ru-RU.mjs +0 -8
  167. package/dist/ru-RU.module.js +0 -8
  168. package/dist/ru-RU.module.js.map +0 -1
  169. package/dist/sk-SK.main.js +0 -6
  170. package/dist/sk-SK.main.js.map +0 -1
  171. package/dist/sk-SK.mjs +0 -8
  172. package/dist/sk-SK.module.js +0 -8
  173. package/dist/sk-SK.module.js.map +0 -1
  174. package/dist/sl-SI.main.js +0 -6
  175. package/dist/sl-SI.main.js.map +0 -1
  176. package/dist/sl-SI.mjs +0 -8
  177. package/dist/sl-SI.module.js +0 -8
  178. package/dist/sl-SI.module.js.map +0 -1
  179. package/dist/sr-SP.main.js +0 -6
  180. package/dist/sr-SP.main.js.map +0 -1
  181. package/dist/sr-SP.mjs +0 -8
  182. package/dist/sr-SP.module.js +0 -8
  183. package/dist/sr-SP.module.js.map +0 -1
  184. package/dist/sv-SE.main.js +0 -6
  185. package/dist/sv-SE.main.js.map +0 -1
  186. package/dist/sv-SE.mjs +0 -8
  187. package/dist/sv-SE.module.js +0 -8
  188. package/dist/sv-SE.module.js.map +0 -1
  189. package/dist/tr-TR.main.js +0 -6
  190. package/dist/tr-TR.main.js.map +0 -1
  191. package/dist/tr-TR.mjs +0 -8
  192. package/dist/tr-TR.module.js +0 -8
  193. package/dist/tr-TR.module.js.map +0 -1
  194. package/dist/types.d.ts +0 -355
  195. package/dist/types.d.ts.map +0 -1
  196. package/dist/uk-UA.main.js +0 -6
  197. package/dist/uk-UA.main.js.map +0 -1
  198. package/dist/uk-UA.mjs +0 -8
  199. package/dist/uk-UA.module.js +0 -8
  200. package/dist/uk-UA.module.js.map +0 -1
  201. package/dist/useCloseOnScroll.main.js +0 -52
  202. package/dist/useCloseOnScroll.main.js.map +0 -1
  203. package/dist/useCloseOnScroll.mjs +0 -46
  204. package/dist/useCloseOnScroll.module.js +0 -46
  205. package/dist/useCloseOnScroll.module.js.map +0 -1
  206. package/dist/useModal.main.js +0 -118
  207. package/dist/useModal.main.js.map +0 -1
  208. package/dist/useModal.mjs +0 -105
  209. package/dist/useModal.module.js +0 -105
  210. package/dist/useModal.module.js.map +0 -1
  211. package/dist/useModalOverlay.main.js +0 -57
  212. package/dist/useModalOverlay.main.js.map +0 -1
  213. package/dist/useModalOverlay.mjs +0 -52
  214. package/dist/useModalOverlay.module.js +0 -52
  215. package/dist/useModalOverlay.module.js.map +0 -1
  216. package/dist/useOverlay.main.js +0 -111
  217. package/dist/useOverlay.main.js.map +0 -1
  218. package/dist/useOverlay.mjs +0 -106
  219. package/dist/useOverlay.module.js +0 -106
  220. package/dist/useOverlay.module.js.map +0 -1
  221. package/dist/useOverlayPosition.main.js +0 -225
  222. package/dist/useOverlayPosition.main.js.map +0 -1
  223. package/dist/useOverlayPosition.mjs +0 -220
  224. package/dist/useOverlayPosition.module.js +0 -220
  225. package/dist/useOverlayPosition.module.js.map +0 -1
  226. package/dist/useOverlayTrigger.main.js +0 -54
  227. package/dist/useOverlayTrigger.main.js.map +0 -1
  228. package/dist/useOverlayTrigger.mjs +0 -49
  229. package/dist/useOverlayTrigger.module.js +0 -49
  230. package/dist/useOverlayTrigger.module.js.map +0 -1
  231. package/dist/usePopover.main.js +0 -77
  232. package/dist/usePopover.main.js.map +0 -1
  233. package/dist/usePopover.mjs +0 -72
  234. package/dist/usePopover.module.js +0 -72
  235. package/dist/usePopover.module.js.map +0 -1
  236. package/dist/usePreventScroll.main.js +0 -215
  237. package/dist/usePreventScroll.main.js.map +0 -1
  238. package/dist/usePreventScroll.mjs +0 -210
  239. package/dist/usePreventScroll.module.js +0 -210
  240. package/dist/usePreventScroll.module.js.map +0 -1
  241. package/dist/zh-CN.main.js +0 -6
  242. package/dist/zh-CN.main.js.map +0 -1
  243. package/dist/zh-CN.mjs +0 -8
  244. package/dist/zh-CN.module.js +0 -8
  245. package/dist/zh-CN.module.js.map +0 -1
  246. package/dist/zh-TW.main.js +0 -6
  247. package/dist/zh-TW.main.js.map +0 -1
  248. package/dist/zh-TW.mjs +0 -8
  249. package/dist/zh-TW.module.js +0 -8
  250. package/dist/zh-TW.module.js.map +0 -1
  251. package/src/DismissButton.tsx +0 -52
  252. package/src/Overlay.tsx +0 -95
  253. package/src/PortalProvider.tsx +0 -41
  254. package/src/ariaHideOutside.ts +0 -221
  255. package/src/calculatePosition.ts +0 -680
  256. package/src/useCloseOnScroll.ts +0 -65
  257. package/src/useModal.tsx +0 -197
  258. package/src/useModalOverlay.ts +0 -69
  259. package/src/useOverlay.ts +0 -169
  260. package/src/useOverlayPosition.ts +0 -327
  261. package/src/useOverlayTrigger.ts +0 -72
  262. package/src/usePopover.ts +0 -138
  263. package/src/usePreventScroll.ts +0 -273
@@ -1,273 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {chain, getScrollParent, isIOS, isScrollable, useLayoutEffect, willOpenKeyboard} from '@react-aria/utils';
14
-
15
- interface PreventScrollOptions {
16
- /** Whether the scroll lock is disabled. */
17
- isDisabled?: boolean
18
- }
19
-
20
- const visualViewport = typeof document !== 'undefined' && window.visualViewport;
21
-
22
- // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
23
- let preventScrollCount = 0;
24
- let restore;
25
-
26
- /**
27
- * Prevents scrolling on the document body on mount, and
28
- * restores it on unmount. Also ensures that content does not
29
- * shift due to the scrollbars disappearing.
30
- */
31
- export function usePreventScroll(options: PreventScrollOptions = {}): void {
32
- let {isDisabled} = options;
33
-
34
- useLayoutEffect(() => {
35
- if (isDisabled) {
36
- return;
37
- }
38
-
39
- preventScrollCount++;
40
- if (preventScrollCount === 1) {
41
- if (isIOS()) {
42
- restore = preventScrollMobileSafari();
43
- } else {
44
- restore = preventScrollStandard();
45
- }
46
- }
47
-
48
- return () => {
49
- preventScrollCount--;
50
- if (preventScrollCount === 0) {
51
- restore();
52
- }
53
- };
54
- }, [isDisabled]);
55
- }
56
-
57
- // For most browsers, all we need to do is set `overflow: hidden` on the root element, and
58
- // add some padding to prevent the page from shifting when the scrollbar is hidden.
59
- function preventScrollStandard() {
60
- let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
61
- return chain(
62
- scrollbarWidth > 0 &&
63
- // Use scrollbar-gutter when supported because it also works for fixed positioned elements.
64
- ('scrollbarGutter' in document.documentElement.style
65
- ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')
66
- : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),
67
- setStyle(document.documentElement, 'overflow', 'hidden')
68
- );
69
- }
70
-
71
- // Mobile Safari is a whole different beast. Even with overflow: hidden,
72
- // it still scrolls the page in many situations:
73
- //
74
- // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
75
- // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
76
- // it, so it becomes scrollable.
77
- // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
78
- // This may cause even fixed position elements to scroll off the screen.
79
- // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
80
- // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
81
- //
82
- // In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
83
- //
84
- // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
85
- // on the window.
86
- // 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
87
- // the top or bottom. Work around a bug where this does not work when the element does not actually overflow
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.
90
- // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
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.
94
- function preventScrollMobileSafari() {
95
- let scrollable: Element;
96
- let allowTouchMove = false;
97
- let onTouchStart = (e: TouchEvent) => {
98
- // Store the nearest scrollable parent element from the element that the user touched.
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;
107
- }
108
-
109
- // If this is a range input, allow touch move to allow user to adjust the slider value
110
- if (e.composedPath().some((el) =>
111
- el instanceof HTMLInputElement &&
112
- el.type === 'range'
113
- )) {
114
- allowTouchMove = true;
115
- }
116
-
117
- // If this is a focused input element with a selected range, allow user to drag the selection handles.
118
- if (
119
- 'selectionStart' in target &&
120
- 'selectionEnd' in target &&
121
- (target.selectionStart as number) < (target.selectionEnd as number) &&
122
- target.ownerDocument.activeElement === target
123
- ) {
124
- allowTouchMove = true;
125
- }
126
- };
127
-
128
- // Prevent scrolling up when at the top and scrolling down when at the bottom
129
- // of a nested scrollable area, otherwise mobile Safari will start scrolling
130
- // the window instead.
131
- // This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
132
- let style = document.createElement('style');
133
- style.textContent = `
134
- @layer {
135
- * {
136
- overscroll-behavior: contain;
137
- }
138
- }`.trim();
139
- document.head.prepend(style);
140
-
141
- let onTouchMove = (e: TouchEvent) => {
142
- // Allow pinch-zooming.
143
- if (e.touches.length === 2 || allowTouchMove) {
144
- return;
145
- }
146
-
147
- // Prevent scrolling the window.
148
- if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
149
- e.preventDefault();
150
- return;
151
- }
152
-
153
- // overscroll-behavior should prevent scroll chaining, but currently does not
154
- // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452
155
- // This checks that both the width and height do not overflow, otherwise we might
156
- // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to
157
- // the element will prevent vertical page scrolling. We can't add that automatically
158
- // because it must be set before the touchstart event.
159
- if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {
160
- e.preventDefault();
161
- }
162
- };
163
-
164
- let onBlur = (e: FocusEvent) => {
165
- let target = e.target as HTMLElement;
166
- let relatedTarget = e.relatedTarget as HTMLElement | null;
167
- if (relatedTarget && willOpenKeyboard(relatedTarget)) {
168
- // Focus without scrolling the whole page, and then scroll into view manually.
169
- relatedTarget.focus({preventScroll: true});
170
- scrollIntoViewWhenReady(relatedTarget, willOpenKeyboard(target));
171
- } else if (!relatedTarget) {
172
- // When tapping the Done button on the keyboard, focus moves to the body.
173
- // FocusScope will then restore focus back to the input. Later when tapping
174
- // the same input again, it is already focused, so no blur event will fire,
175
- // resulting in the flow above never running and Safari's native scrolling occurring.
176
- // Instead, move focus to the parent focusable element (e.g. the dialog).
177
- let focusable = target.parentElement?.closest('[tabindex]') as HTMLElement | null;
178
- focusable?.focus({preventScroll: true});
179
- }
180
- };
181
-
182
- // Override programmatic focus to scroll into view without scrolling the whole page.
183
- let focus = HTMLElement.prototype.focus;
184
- HTMLElement.prototype.focus = function (opts) {
185
- // Track whether the keyboard was already visible before.
186
- let wasKeyboardVisible = document.activeElement != null && willOpenKeyboard(document.activeElement);
187
-
188
- // Focus the element without scrolling the page.
189
- focus.call(this, {...opts, preventScroll: true});
190
-
191
- if (!opts || !opts.preventScroll) {
192
- scrollIntoViewWhenReady(this, wasKeyboardVisible);
193
- }
194
- };
195
-
196
- let removeEvents = chain(
197
- addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),
198
- addEvent(document, 'touchmove', onTouchMove, {passive: false, capture: true}),
199
- addEvent(document, 'blur', onBlur, true)
200
- );
201
-
202
- return () => {
203
- removeEvents();
204
- style.remove();
205
- HTMLElement.prototype.focus = focus;
206
- };
207
- }
208
-
209
- // Sets a CSS property on an element, and returns a function to revert it to the previous value.
210
- function setStyle(element: HTMLElement, style: string, value: string) {
211
- let cur = element.style[style];
212
- element.style[style] = value;
213
-
214
- return () => {
215
- element.style[style] = cur;
216
- };
217
- }
218
-
219
- // Adds an event listener to an element, and returns a function to remove it.
220
- function addEvent<K extends keyof GlobalEventHandlersEventMap>(
221
- target: Document | Window,
222
- event: K,
223
- handler: (this: Document | Window, ev: GlobalEventHandlersEventMap[K]) => any,
224
- options?: boolean | AddEventListenerOptions
225
- ) {
226
- // internal function, so it's ok to ignore the difficult to fix type error
227
- // @ts-ignore
228
- target.addEventListener(event, handler, options);
229
- return () => {
230
- // @ts-ignore
231
- target.removeEventListener(event, handler, options);
232
- };
233
- }
234
-
235
- function scrollIntoViewWhenReady(target: Element, wasKeyboardVisible: boolean) {
236
- if (wasKeyboardVisible || !visualViewport) {
237
- // If the keyboard was already visible, scroll the target into view immediately.
238
- scrollIntoView(target);
239
- } else {
240
- // Otherwise, wait for the visual viewport to resize before scrolling so we can
241
- // measure the correct position to scroll to.
242
- visualViewport.addEventListener('resize', () => scrollIntoView(target), {once: true});
243
- }
244
- }
245
-
246
- function scrollIntoView(target: Element) {
247
- let root = document.scrollingElement || document.documentElement;
248
- let nextTarget: Element | null = target;
249
- while (nextTarget && nextTarget !== root) {
250
- // Find the parent scrollable element and adjust the scroll position if the target is not already in view.
251
- let scrollable = getScrollParent(nextTarget);
252
- if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
253
- let scrollableRect = scrollable.getBoundingClientRect();
254
- let targetRect = nextTarget.getBoundingClientRect();
255
- if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
256
- let bottom = scrollableRect.bottom;
257
- if (visualViewport) {
258
- bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);
259
- }
260
-
261
- // Center within the viewport.
262
- let adjustment = (targetRect.top - scrollableRect.top) - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
263
- scrollable.scrollTo({
264
- // Clamp to the valid range to prevent over-scrolling.
265
- top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
266
- behavior: 'smooth'
267
- });
268
- }
269
- }
270
-
271
- nextTarget = scrollable.parentElement;
272
- }
273
- }