@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,680 +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 {Axis, Placement, PlacementAxis, SizeAxis} from '@react-types/overlays';
14
- import {clamp, isWebKit, nodeContains} from '@react-aria/utils';
15
-
16
- interface Position {
17
- top?: number,
18
- left?: number,
19
- bottom?: number,
20
- right?: number
21
- }
22
-
23
- interface Dimensions {
24
- width: number,
25
- height: number,
26
- totalWidth: number,
27
- totalHeight: number,
28
- top: number,
29
- left: number,
30
- scroll: Position
31
- }
32
-
33
- interface ParsedPlacement {
34
- placement: PlacementAxis,
35
- crossPlacement: PlacementAxis,
36
- axis: Axis,
37
- crossAxis: Axis,
38
- size: SizeAxis,
39
- crossSize: SizeAxis
40
- }
41
-
42
- interface Offset {
43
- top: number,
44
- left: number,
45
- width: number,
46
- height: number
47
- }
48
-
49
- interface PositionOpts {
50
- arrowSize: number,
51
- placement: Placement,
52
- targetNode: Element,
53
- overlayNode: Element,
54
- scrollNode: Element,
55
- padding: number,
56
- shouldFlip: boolean,
57
- boundaryElement: Element,
58
- offset: number,
59
- crossOffset: number,
60
- maxHeight?: number,
61
- arrowBoundaryOffset?: number
62
- }
63
-
64
- type HeightGrowthDirection = 'top' | 'bottom';
65
-
66
- export interface PositionResult {
67
- position: Position,
68
- arrowOffsetLeft?: number,
69
- arrowOffsetTop?: number,
70
- triggerAnchorPoint: {x: number, y: number},
71
- maxHeight: number,
72
- placement: PlacementAxis
73
- }
74
-
75
- const AXIS = {
76
- top: 'top',
77
- bottom: 'top',
78
- left: 'left',
79
- right: 'left'
80
- };
81
-
82
- const FLIPPED_DIRECTION = {
83
- top: 'bottom',
84
- bottom: 'top',
85
- left: 'right',
86
- right: 'left'
87
- };
88
-
89
- const CROSS_AXIS = {
90
- top: 'left',
91
- left: 'top'
92
- };
93
-
94
- const AXIS_SIZE = {
95
- top: 'height',
96
- left: 'width'
97
- };
98
-
99
- const TOTAL_SIZE = {
100
- width: 'totalWidth',
101
- height: 'totalHeight'
102
- };
103
-
104
- const PARSED_PLACEMENT_CACHE = {};
105
-
106
- let getVisualViewport = () => typeof document !== 'undefined' ? window.visualViewport : null;
107
-
108
- function getContainerDimensions(containerNode: Element, visualViewport: VisualViewport | null): Dimensions {
109
- let width = 0, height = 0, totalWidth = 0, totalHeight = 0, top = 0, left = 0;
110
- let scroll: Position = {};
111
- let isPinchZoomedIn = (visualViewport?.scale ?? 1) > 1;
112
-
113
- // In the case where the container is `html` or `body` and the container doesn't have something like `position: relative`,
114
- // then position absolute will be positioned relative to the viewport, also known as the `initial containing block`.
115
- // That's why we use the visual viewport instead.
116
-
117
- if (containerNode.tagName === 'BODY' || containerNode.tagName === 'HTML') {
118
- let documentElement = document.documentElement;
119
- totalWidth = documentElement.clientWidth;
120
- totalHeight = documentElement.clientHeight;
121
- width = visualViewport?.width ?? totalWidth;
122
- height = visualViewport?.height ?? totalHeight;
123
- scroll.top = documentElement.scrollTop || containerNode.scrollTop;
124
- scroll.left = documentElement.scrollLeft || containerNode.scrollLeft;
125
-
126
- // The goal of the below is to get a top/left value that represents the top/left of the visual viewport with
127
- // respect to the layout viewport origin. This combined with the scrollTop/scrollLeft will allow us to calculate
128
- // coordinates/values with respect to the visual viewport or with respect to the layout viewport.
129
- if (visualViewport) {
130
- top = visualViewport.offsetTop;
131
- left = visualViewport.offsetLeft;
132
- }
133
- } else {
134
- ({width, height, top, left} = getOffset(containerNode, false));
135
- scroll.top = containerNode.scrollTop;
136
- scroll.left = containerNode.scrollLeft;
137
- totalWidth = width;
138
- totalHeight = height;
139
- }
140
-
141
- if (isWebKit() && (containerNode.tagName === 'BODY' || containerNode.tagName === 'HTML') && isPinchZoomedIn) {
142
- // Safari will report a non-zero scrollTop/Left for the non-scrolling body/HTML element when pinch zoomed in unlike other browsers.
143
- // Set to zero for parity calculations so we get consistent positioning of overlays across all browsers.
144
- // Also switch to visualViewport.pageTop/pageLeft so that we still accomodate for scroll positioning for body/HTML elements that are actually scrollable
145
- // before pinch zoom happens
146
- scroll.top = 0;
147
- scroll.left = 0;
148
- top = visualViewport?.pageTop ?? 0;
149
- left = visualViewport?.pageLeft ?? 0;
150
- }
151
-
152
- return {width, height, totalWidth, totalHeight, scroll, top, left};
153
- }
154
-
155
- function getScroll(node: Element): Offset {
156
- return {
157
- top: node.scrollTop,
158
- left: node.scrollLeft,
159
- width: node.scrollWidth,
160
- height: node.scrollHeight
161
- };
162
- }
163
-
164
- // Determines the amount of space required when moving the overlay to ensure it remains in the boundary
165
- function getDelta(
166
- axis: Axis,
167
- offset: number,
168
- size: number,
169
- // The dimensions of the boundary element that the popover is
170
- // positioned within (most of the time this is the <body>).
171
- boundaryDimensions: Dimensions,
172
- // The dimensions of the containing block element that the popover is
173
- // positioned relative to (e.g. parent with position: relative).
174
- // Usually this is the same as the boundary element, but if the popover
175
- // is portaled somewhere other than the body and has an ancestor with
176
- // position: relative/absolute, it will be different.
177
- containerDimensions: Dimensions,
178
- padding: number,
179
- containerOffsetWithBoundary: Offset
180
- ) {
181
- let containerScroll = containerDimensions.scroll[axis] ?? 0;
182
- // The height/width of the boundary. Matches the axis along which we are adjusting the overlay position
183
- let boundarySize = boundaryDimensions[AXIS_SIZE[axis]];
184
- // Calculate the edges of the boundary (accomodating for the boundary padding) and the edges of the overlay.
185
- // Note that these values are with respect to the visual viewport (aka 0,0 is the top left of the viewport)
186
-
187
- let boundaryStartEdge = containerOffsetWithBoundary[axis] + boundaryDimensions.scroll[AXIS[axis]] + padding;
188
- let boundaryEndEdge = containerOffsetWithBoundary[axis] + boundaryDimensions.scroll[AXIS[axis]] + boundarySize - padding;
189
- // transformed value of the left edge of the overlay
190
- let startEdgeOffset = offset - containerScroll + boundaryDimensions.scroll[AXIS[axis]] + containerOffsetWithBoundary[axis] - boundaryDimensions[AXIS[axis]];
191
- // transformed value of the right edge of the overlay
192
- let endEdgeOffset = offset - containerScroll + size + boundaryDimensions.scroll[AXIS[axis]] + containerOffsetWithBoundary[axis] - boundaryDimensions[AXIS[axis]];
193
-
194
- // If any of the overlay edges falls outside of the boundary, shift the overlay the required amount to align one of the overlay's
195
- // edges with the closest boundary edge.
196
- if (startEdgeOffset < boundaryStartEdge) {
197
- return boundaryStartEdge - startEdgeOffset;
198
- } else if (endEdgeOffset > boundaryEndEdge) {
199
- return Math.max(boundaryEndEdge - endEdgeOffset, boundaryStartEdge - startEdgeOffset);
200
- } else {
201
- return 0;
202
- }
203
- }
204
-
205
- function getMargins(node: Element): Position {
206
- let style = window.getComputedStyle(node);
207
- return {
208
- top: parseInt(style.marginTop, 10) || 0,
209
- bottom: parseInt(style.marginBottom, 10) || 0,
210
- left: parseInt(style.marginLeft, 10) || 0,
211
- right: parseInt(style.marginRight, 10) || 0
212
- };
213
- }
214
-
215
- function parsePlacement(input: Placement): ParsedPlacement {
216
- if (PARSED_PLACEMENT_CACHE[input]) {
217
- return PARSED_PLACEMENT_CACHE[input];
218
- }
219
-
220
- let [placement, crossPlacement] = input.split(' ');
221
- let axis: Axis = AXIS[placement] || 'right';
222
- let crossAxis: Axis = CROSS_AXIS[axis];
223
-
224
- if (!AXIS[crossPlacement]) {
225
- crossPlacement = 'center';
226
- }
227
-
228
- let size = AXIS_SIZE[axis];
229
- let crossSize = AXIS_SIZE[crossAxis];
230
- PARSED_PLACEMENT_CACHE[input] = {placement, crossPlacement, axis, crossAxis, size, crossSize};
231
- return PARSED_PLACEMENT_CACHE[input];
232
- }
233
-
234
- function computePosition(
235
- childOffset: Offset,
236
- boundaryDimensions: Dimensions,
237
- overlaySize: Offset,
238
- placementInfo: ParsedPlacement,
239
- offset: number,
240
- crossOffset: number,
241
- containerOffsetWithBoundary: Offset,
242
- isContainerPositioned: boolean,
243
- arrowSize: number,
244
- arrowBoundaryOffset: number,
245
- containerDimensions: Dimensions
246
- ) {
247
- let {placement, crossPlacement, axis, crossAxis, size, crossSize} = placementInfo;
248
- let position: Position = {};
249
-
250
- // button position
251
- position[crossAxis] = childOffset[crossAxis] ?? 0;
252
- if (crossPlacement === 'center') {
253
- // + (button size / 2) - (overlay size / 2)
254
- // at this point the overlay center should match the button center
255
- position[crossAxis]! += ((childOffset[crossSize] ?? 0) - (overlaySize[crossSize] ?? 0)) / 2;
256
- } else if (crossPlacement !== crossAxis) {
257
- // + (button size) - (overlay size)
258
- // at this point the overlay bottom should match the button bottom
259
- position[crossAxis]! += (childOffset[crossSize] ?? 0) - (overlaySize[crossSize] ?? 0);
260
- }/* else {
261
- the overlay top should match the button top
262
- } */
263
-
264
- position[crossAxis]! += crossOffset;
265
-
266
- // overlay top or left overlapping arrow with button bottom or right
267
- const minPosition = childOffset[crossAxis] - overlaySize[crossSize] + arrowSize + arrowBoundaryOffset;
268
- // overlay bottom or right overlapping arrow with button top or left
269
- const maxPosition = childOffset[crossAxis] + childOffset[crossSize] - arrowSize - arrowBoundaryOffset;
270
- position[crossAxis] = clamp(position[crossAxis]!, minPosition, maxPosition);
271
-
272
- // Floor these so the position isn't placed on a partial pixel, only whole pixels. Shouldn't matter if it was floored or ceiled, so chose one.
273
- if (placement === axis) {
274
- // If the container is positioned (non-static), then we use the container's actual
275
- // height, as `bottom` will be relative to this height. But if the container is static,
276
- // then it can only be the `document.body`, and `bottom` will be relative to _its_
277
- // container.
278
- let containerHeight = (isContainerPositioned ? containerDimensions[size] : containerDimensions[TOTAL_SIZE[size]]);
279
- position[FLIPPED_DIRECTION[axis]] = Math.floor(containerHeight - childOffset[axis] + offset);
280
- } else {
281
- position[axis] = Math.floor(childOffset[axis] + childOffset[size] + offset);
282
- }
283
- return position;
284
- }
285
-
286
- function getMaxHeight(
287
- position: Position,
288
- boundaryDimensions: Dimensions,
289
- containerOffsetWithBoundary: Offset,
290
- isContainerPositioned: boolean,
291
- margins: Position,
292
- padding: number,
293
- overlayHeight: number,
294
- heightGrowthDirection: HeightGrowthDirection,
295
- containerDimensions: Dimensions,
296
- isContainerDescendentOfBoundary: boolean,
297
- visualViewport: VisualViewport | null
298
- ) {
299
- // For cases where position is set via "bottom" instead of "top", we need to calculate the true overlay top
300
- // with respect to the container.
301
- let overlayTop = (position.top != null ? position.top : (containerDimensions[TOTAL_SIZE.height] - (position.bottom ?? 0) - overlayHeight)) - (containerDimensions.scroll.top ?? 0);
302
- // calculate the dimentions of the "boundingRect" which is most restrictive top/bottom of the boundaryRect and the visual view port
303
- let boundaryToContainerTransformOffset = isContainerDescendentOfBoundary ? containerOffsetWithBoundary.top : 0;
304
- let boundingRect = {
305
- // This should be boundary top in container coord system vs viewport top in container coord system
306
- // For the viewport top, there are several cases
307
- // 1. pinchzoom case where we want the viewports offset top as top here
308
- // 2. case where container is offset from the boundary and is contained by the boundary. In this case the top we want here is NOT 0, we want to take boundary's top even though is is a negative number OR the visual viewport, whichever is more restrictive
309
- top: Math.max(boundaryDimensions.top + boundaryToContainerTransformOffset, (visualViewport?.offsetTop ?? boundaryDimensions.top) + boundaryToContainerTransformOffset),
310
- bottom: Math.min((boundaryDimensions.top + boundaryDimensions.height + boundaryToContainerTransformOffset), (visualViewport?.offsetTop ?? 0) + (visualViewport?.height ?? 0))
311
- };
312
-
313
- let maxHeight = heightGrowthDirection !== 'top' ?
314
- // We want the distance between the top of the overlay to the bottom of the boundary
315
- Math.max(0,
316
- boundingRect.bottom // this is the bottom of the boundary
317
- - overlayTop // this is the top of the overlay
318
- - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
319
- )
320
- // We want the distance between the bottom of the overlay to the top of the boundary
321
- : Math.max(0,
322
- (overlayTop + overlayHeight) // this is the bottom of the overlay
323
- - boundingRect.top // this is the top of the boundary
324
- - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
325
- );
326
- return maxHeight;
327
- }
328
-
329
- function getAvailableSpace(
330
- boundaryDimensions: Dimensions, // boundary
331
- containerOffsetWithBoundary: Offset,
332
- childOffset: Offset, // trigger, position based of container's non-viewport 0,0
333
- margins: Position, // overlay
334
- padding: number, // overlay <-> boundary
335
- placementInfo: ParsedPlacement,
336
- containerDimensions: Dimensions,
337
- isContainerDescendentOfBoundary: boolean
338
- ) {
339
- let {placement, axis, size} = placementInfo;
340
- if (placement === axis) {
341
- return Math.max(0,
342
- childOffset[axis] // trigger start
343
- - (containerDimensions.scroll[axis] ?? 0) // transform trigger position to be with respect to viewport 0,0
344
- - (boundaryDimensions[axis] + (isContainerDescendentOfBoundary ? containerOffsetWithBoundary[axis] : 0)) // boundary start
345
- - (margins[axis] ?? 0) // margins usually for arrows or other decorations
346
- - margins[FLIPPED_DIRECTION[axis]]
347
- - padding); // padding between overlay and boundary
348
- }
349
-
350
- return Math.max(0,
351
- (boundaryDimensions[size]
352
- + boundaryDimensions[axis]
353
- + (isContainerDescendentOfBoundary ? containerOffsetWithBoundary[axis] : 0))
354
- - childOffset[axis]
355
- - childOffset[size]
356
- + (containerDimensions.scroll[axis] ?? 0)
357
- - (margins[axis] ?? 0)
358
- - margins[FLIPPED_DIRECTION[axis]]
359
- - padding);
360
- }
361
-
362
- export function calculatePositionInternal(
363
- placementInput: Placement,
364
- childOffset: Offset,
365
- overlaySize: Offset,
366
- scrollSize: Offset,
367
- margins: Position,
368
- padding: number,
369
- flip: boolean,
370
- boundaryDimensions: Dimensions,
371
- containerDimensions: Dimensions,
372
- containerOffsetWithBoundary: Offset,
373
- offset: number,
374
- crossOffset: number,
375
- isContainerPositioned: boolean,
376
- userSetMaxHeight: number | undefined,
377
- arrowSize: number,
378
- arrowBoundaryOffset: number,
379
- isContainerDescendentOfBoundary: boolean,
380
- visualViewport: VisualViewport | null
381
- ): PositionResult {
382
- let placementInfo = parsePlacement(placementInput);
383
- let {size, crossAxis, crossSize, placement, crossPlacement} = placementInfo;
384
- let position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
385
- let normalizedOffset = offset;
386
- let space = getAvailableSpace(
387
- boundaryDimensions,
388
- containerOffsetWithBoundary,
389
- childOffset,
390
- margins,
391
- padding + offset,
392
- placementInfo,
393
- containerDimensions,
394
- isContainerDescendentOfBoundary
395
- );
396
-
397
- // Check if the scroll size of the overlay is greater than the available space to determine if we need to flip
398
- if (flip && overlaySize[size] > space) {
399
- let flippedPlacementInfo = parsePlacement(`${FLIPPED_DIRECTION[placement]} ${crossPlacement}` as Placement);
400
- let flippedPosition = computePosition(childOffset, boundaryDimensions, overlaySize, flippedPlacementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
401
-
402
- let flippedSpace = getAvailableSpace(
403
- boundaryDimensions,
404
- containerOffsetWithBoundary,
405
- childOffset,
406
- margins,
407
- padding + offset,
408
- flippedPlacementInfo,
409
- containerDimensions,
410
- isContainerDescendentOfBoundary
411
- );
412
-
413
- // If the available space for the flipped position is greater than the original available space, flip.
414
- if (flippedSpace > space) {
415
- placementInfo = flippedPlacementInfo;
416
- position = flippedPosition;
417
- normalizedOffset = offset;
418
- }
419
- }
420
-
421
- // Determine the direction the height of the overlay can grow so that we can choose how to calculate the max height
422
- let heightGrowthDirection: HeightGrowthDirection = 'bottom';
423
- if (placementInfo.axis === 'top') {
424
- if (placementInfo.placement === 'top') {
425
- heightGrowthDirection = 'top';
426
- } else if (placementInfo.placement === 'bottom') {
427
- heightGrowthDirection = 'bottom';
428
- }
429
- } else if (placementInfo.crossAxis === 'top') {
430
- if (placementInfo.crossPlacement === 'top') {
431
- heightGrowthDirection = 'bottom';
432
- } else if (placementInfo.crossPlacement === 'bottom') {
433
- heightGrowthDirection = 'top';
434
- }
435
- }
436
-
437
- let delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary);
438
- position[crossAxis]! += delta;
439
-
440
- let maxHeight = getMaxHeight(
441
- position,
442
- boundaryDimensions,
443
- containerOffsetWithBoundary,
444
- isContainerPositioned,
445
- margins,
446
- padding,
447
- overlaySize.height,
448
- heightGrowthDirection,
449
- containerDimensions,
450
- isContainerDescendentOfBoundary,
451
- visualViewport
452
- );
453
-
454
- if (userSetMaxHeight && userSetMaxHeight < maxHeight) {
455
- maxHeight = userSetMaxHeight;
456
- }
457
-
458
- overlaySize.height = Math.min(overlaySize.height, maxHeight);
459
-
460
- position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, normalizedOffset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
461
- delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary);
462
- position[crossAxis]! += delta;
463
-
464
- let arrowPosition: Position = {};
465
-
466
- // All values are transformed so that 0 is at the top/left of the overlay depending on the orientation
467
- // Prefer the arrow being in the center of the trigger/overlay anchor element
468
- // childOffset[crossAxis] + .5 * childOffset[crossSize] = absolute position with respect to the trigger's coordinate system that would place the arrow in the center of the trigger
469
- // position[crossAxis] - margins[AXIS[crossAxis]] = value use to transform the position to a value with respect to the overlay's coordinate system. A child element's (aka arrow) position absolute's "0"
470
- // is positioned after the margin of its parent (aka overlay) so we need to subtract it to get the proper coordinate transform
471
- let origin = childOffset[crossAxis] - position[crossAxis]! - margins[AXIS[crossAxis]];
472
- let preferredArrowPosition = origin + .5 * childOffset[crossSize];
473
-
474
- // Min/Max position limits for the arrow with respect to the overlay
475
- const arrowMinPosition = arrowSize / 2 + arrowBoundaryOffset;
476
- // overlaySize[crossSize] - margins = true size of the overlay
477
- const overlayMargin = AXIS[crossAxis] === 'left' ? (margins.left ?? 0) + (margins.right ?? 0) : (margins.top ?? 0) + (margins.bottom ?? 0);
478
- const arrowMaxPosition = overlaySize[crossSize] - overlayMargin - (arrowSize / 2) - arrowBoundaryOffset;
479
-
480
- // Min/Max position limits for the arrow with respect to the trigger/overlay anchor element
481
- // Same margin accomodation done here as well as for the preferredArrowPosition
482
- const arrowOverlappingChildMinEdge = childOffset[crossAxis] + (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]);
483
- const arrowOverlappingChildMaxEdge = childOffset[crossAxis] + childOffset[crossSize] - (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]);
484
-
485
- // Clamp the arrow positioning so that it always is within the bounds of the anchor and the overlay
486
- const arrowPositionOverlappingChild = clamp(preferredArrowPosition, arrowOverlappingChildMinEdge, arrowOverlappingChildMaxEdge);
487
- arrowPosition[crossAxis] = clamp(arrowPositionOverlappingChild, arrowMinPosition, arrowMaxPosition);
488
-
489
- // If there is an arrow, use that as the origin so that animations are smooth.
490
- // Otherwise use the target edge.
491
- ({placement, crossPlacement} = placementInfo);
492
- if (arrowSize) {
493
- origin = arrowPosition[crossAxis];
494
- } else if (crossPlacement === 'right') {
495
- origin += childOffset[crossSize];
496
- } else if (crossPlacement === 'center') {
497
- origin += childOffset[crossSize] / 2;
498
- }
499
-
500
- let crossOrigin = placement === 'left' || placement === 'top' ? overlaySize[size] : 0;
501
- let triggerAnchorPoint = {
502
- x: placement === 'top' || placement === 'bottom' ? origin : crossOrigin,
503
- y: placement === 'left' || placement === 'right' ? origin : crossOrigin
504
- };
505
-
506
- return {
507
- position,
508
- maxHeight: maxHeight,
509
- arrowOffsetLeft: arrowPosition.left,
510
- arrowOffsetTop: arrowPosition.top,
511
- placement,
512
- triggerAnchorPoint
513
- };
514
- }
515
-
516
- /**
517
- * Determines where to place the overlay with regards to the target and the position of an optional indicator.
518
- */
519
- export function calculatePosition(opts: PositionOpts): PositionResult {
520
- let {
521
- placement,
522
- targetNode,
523
- overlayNode,
524
- scrollNode,
525
- padding,
526
- shouldFlip,
527
- boundaryElement,
528
- offset,
529
- crossOffset,
530
- maxHeight,
531
- arrowSize = 0,
532
- arrowBoundaryOffset = 0
533
- } = opts;
534
-
535
- let visualViewport = getVisualViewport();
536
- let container = overlayNode instanceof HTMLElement ? getContainingBlock(overlayNode) : document.documentElement;
537
- let isViewportContainer = container === document.documentElement;
538
- const containerPositionStyle = window.getComputedStyle(container).position;
539
- let isContainerPositioned = !!containerPositionStyle && containerPositionStyle !== 'static';
540
- let childOffset: Offset = isViewportContainer ? getOffset(targetNode, false) : getPosition(targetNode, container, false);
541
-
542
- if (!isViewportContainer) {
543
- let {marginTop, marginLeft} = window.getComputedStyle(targetNode);
544
- childOffset.top += parseInt(marginTop, 10) || 0;
545
- childOffset.left += parseInt(marginLeft, 10) || 0;
546
- }
547
-
548
- let overlaySize: Offset = getOffset(overlayNode, true);
549
- let margins = getMargins(overlayNode);
550
- overlaySize.width += (margins.left ?? 0) + (margins.right ?? 0);
551
- overlaySize.height += (margins.top ?? 0) + (margins.bottom ?? 0);
552
-
553
- let scrollSize = getScroll(scrollNode);
554
-
555
- // Note that due to logic inside getContainerDimensions, for cases where the boundary element is the body, we will return
556
- // a height/width that matches the visual viewport size rather than the body's height/width (aka for zoom it will be zoom adjusted size)
557
- // and a top/left that is adjusted as well (will return the top/left of the zoomed in viewport, or 0,0 for a non-zoomed body)
558
- // Otherwise this returns the height/width of a arbitrary boundary element, and its top/left with respect to the viewport (NOTE THIS MEANS IT DOESNT INCLUDE SCROLL)
559
- let boundaryDimensions = getContainerDimensions(boundaryElement, visualViewport);
560
- let containerDimensions = getContainerDimensions(container, visualViewport);
561
- // If the container is the HTML element wrapping the body element, the retrieved scrollTop/scrollLeft will be equal to the
562
- // body element's scroll. Set the container's scroll values to 0 since the overlay's edge position value in getDelta don't then need to be further offset
563
- // by the container scroll since they are essentially the same containing element and thus in the same coordinate system
564
- let containerOffsetWithBoundary: Offset = getPosition(boundaryElement, container, false);
565
-
566
- let isContainerDescendentOfBoundary = nodeContains(boundaryElement, container);
567
- return calculatePositionInternal(
568
- placement,
569
- childOffset,
570
- overlaySize,
571
- scrollSize,
572
- margins,
573
- padding,
574
- shouldFlip,
575
- boundaryDimensions,
576
- containerDimensions,
577
- containerOffsetWithBoundary,
578
- offset,
579
- crossOffset,
580
- isContainerPositioned,
581
- maxHeight,
582
- arrowSize,
583
- arrowBoundaryOffset,
584
- isContainerDescendentOfBoundary,
585
- visualViewport
586
- );
587
- }
588
-
589
- export function getRect(node: Element, ignoreScale: boolean) {
590
- let {top, left, width, height} = node.getBoundingClientRect();
591
-
592
- // Use offsetWidth and offsetHeight if this is an HTML element, so that
593
- // the size is not affected by scale transforms.
594
- if (ignoreScale && node instanceof node.ownerDocument.defaultView!.HTMLElement) {
595
- width = node.offsetWidth;
596
- height = node.offsetHeight;
597
- }
598
-
599
- return {top, left, width, height};
600
- }
601
-
602
- function getOffset(node: Element, ignoreScale: boolean): Offset {
603
- let {top, left, width, height} = getRect(node, ignoreScale);
604
- let {scrollTop, scrollLeft, clientTop, clientLeft} = document.documentElement;
605
- return {
606
- top: top + scrollTop - clientTop,
607
- left: left + scrollLeft - clientLeft,
608
- width,
609
- height
610
- };
611
- }
612
-
613
- function getPosition(node: Element, parent: Element, ignoreScale: boolean): Offset {
614
- let style = window.getComputedStyle(node);
615
- let offset: Offset;
616
- if (style.position === 'fixed') {
617
- offset = getRect(node, ignoreScale);
618
- } else {
619
- offset = getOffset(node, ignoreScale);
620
- let parentOffset = getOffset(parent, ignoreScale);
621
- let parentStyle = window.getComputedStyle(parent);
622
- parentOffset.top += (parseInt(parentStyle.borderTopWidth, 10) || 0) - parent.scrollTop;
623
- parentOffset.left += (parseInt(parentStyle.borderLeftWidth, 10) || 0) - parent.scrollLeft;
624
- offset.top -= parentOffset.top;
625
- offset.left -= parentOffset.left;
626
- }
627
-
628
- offset.top -= parseInt(style.marginTop, 10) || 0;
629
- offset.left -= parseInt(style.marginLeft, 10) || 0;
630
- return offset;
631
- }
632
-
633
- // Returns the containing block of an element, which is the element that
634
- // this element will be positioned relative to.
635
- // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block
636
- function getContainingBlock(node: HTMLElement): Element {
637
- // The offsetParent of an element in most cases equals the containing block.
638
- // https://w3c.github.io/csswg-drafts/cssom-view/#dom-htmlelement-offsetparent
639
- let offsetParent = node.offsetParent;
640
-
641
- // The offsetParent algorithm terminates at the document body,
642
- // even if the body is not a containing block. Double check that
643
- // and use the documentElement if so.
644
- if (
645
- offsetParent &&
646
- offsetParent === document.body &&
647
- window.getComputedStyle(offsetParent).position === 'static' &&
648
- !isContainingBlock(offsetParent)
649
- ) {
650
- offsetParent = document.documentElement;
651
- }
652
-
653
- // TODO(later): handle table elements?
654
-
655
- // The offsetParent can be null if the element has position: fixed, or a few other cases.
656
- // We have to walk up the tree manually in this case because fixed positioned elements
657
- // are still positioned relative to their containing block, which is not always the viewport.
658
- if (offsetParent == null) {
659
- offsetParent = node.parentElement;
660
- while (offsetParent && !isContainingBlock(offsetParent)) {
661
- offsetParent = offsetParent.parentElement;
662
- }
663
- }
664
-
665
- // Fall back to the viewport.
666
- return offsetParent || document.documentElement;
667
- }
668
-
669
- // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
670
- function isContainingBlock(node: Element): boolean {
671
- let style = window.getComputedStyle(node);
672
- return (
673
- style.transform !== 'none' ||
674
- /transform|perspective/.test(style.willChange) ||
675
- style.filter !== 'none' ||
676
- style.contain === 'paint' ||
677
- ('backdropFilter' in style && style.backdropFilter !== 'none') ||
678
- ('WebkitBackdropFilter' in style && style.WebkitBackdropFilter !== 'none')
679
- );
680
- }