@react-aria/overlays 3.31.2 → 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 -22
  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 -203
  30. package/dist/ariaHideOutside.main.js.map +0 -1
  31. package/dist/ariaHideOutside.mjs +0 -197
  32. package/dist/ariaHideOutside.module.js +0 -197
  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 -438
  40. package/dist/calculatePosition.main.js.map +0 -1
  41. package/dist/calculatePosition.mjs +0 -432
  42. package/dist/calculatePosition.module.js +0 -432
  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 -113
  217. package/dist/useOverlay.main.js.map +0 -1
  218. package/dist/useOverlay.mjs +0 -108
  219. package/dist/useOverlay.module.js +0 -108
  220. package/dist/useOverlay.module.js.map +0 -1
  221. package/dist/useOverlayPosition.main.js +0 -226
  222. package/dist/useOverlayPosition.main.js.map +0 -1
  223. package/dist/useOverlayPosition.mjs +0 -221
  224. package/dist/useOverlayPosition.module.js +0 -221
  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 -220
  237. package/dist/usePreventScroll.main.js.map +0 -1
  238. package/dist/usePreventScroll.mjs +0 -215
  239. package/dist/usePreventScroll.module.js +0 -215
  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 -300
  255. package/src/calculatePosition.ts +0 -702
  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 -170
  260. package/src/useOverlayPosition.ts +0 -328
  261. package/src/useOverlayTrigger.ts +0 -72
  262. package/src/usePopover.ts +0 -138
  263. package/src/usePreventScroll.ts +0 -279
@@ -1,702 +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
- let maxHeight = heightGrowthDirection !== 'top' ?
313
- // We want the distance between the top of the overlay to the bottom of the boundary
314
- Math.max(0,
315
- boundingRect.bottom // this is the bottom of the boundary
316
- - overlayTop // this is the top of the overlay
317
- - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
318
- )
319
- // We want the distance between the bottom of the overlay to the top of the boundary
320
- : Math.max(0,
321
- (overlayTop + overlayHeight) // this is the bottom of the overlay
322
- - boundingRect.top // this is the top of the boundary
323
- - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
324
- );
325
- return maxHeight;
326
- }
327
-
328
- function getAvailableSpace(
329
- boundaryDimensions: Dimensions, // boundary
330
- containerOffsetWithBoundary: Offset,
331
- childOffset: Offset, // trigger, position based of container's non-viewport 0,0
332
- margins: Position, // overlay
333
- padding: number, // overlay <-> boundary
334
- placementInfo: ParsedPlacement,
335
- containerDimensions: Dimensions,
336
- isContainerDescendentOfBoundary: boolean
337
- ) {
338
- let {placement, axis, size} = placementInfo;
339
- if (placement === axis) {
340
- return Math.max(0,
341
- childOffset[axis] // trigger start
342
- - (containerDimensions.scroll[axis] ?? 0) // transform trigger position to be with respect to viewport 0,0
343
- - (boundaryDimensions[axis] + (isContainerDescendentOfBoundary ? containerOffsetWithBoundary[axis] : 0)) // boundary start
344
- - (margins[axis] ?? 0) // margins usually for arrows or other decorations
345
- - margins[FLIPPED_DIRECTION[axis]]
346
- - padding); // padding between overlay and boundary
347
- }
348
-
349
- return Math.max(0,
350
- (boundaryDimensions[size]
351
- + boundaryDimensions[axis]
352
- + (isContainerDescendentOfBoundary ? containerOffsetWithBoundary[axis] : 0))
353
- - childOffset[axis]
354
- - childOffset[size]
355
- + (containerDimensions.scroll[axis] ?? 0)
356
- - (margins[axis] ?? 0)
357
- - margins[FLIPPED_DIRECTION[axis]]
358
- - padding);
359
- }
360
-
361
- export function calculatePositionInternal(
362
- placementInput: Placement,
363
- childOffset: Offset,
364
- overlaySize: Offset,
365
- scrollSize: Offset,
366
- margins: Position,
367
- padding: number,
368
- flip: boolean,
369
- boundaryDimensions: Dimensions,
370
- containerDimensions: Dimensions,
371
- containerOffsetWithBoundary: Offset,
372
- offset: number,
373
- crossOffset: number,
374
- isContainerPositioned: boolean,
375
- userSetMaxHeight: number | undefined,
376
- arrowSize: number,
377
- arrowBoundaryOffset: number,
378
- isContainerDescendentOfBoundary: boolean,
379
- visualViewport: VisualViewport | null
380
- ): PositionResult {
381
- let placementInfo = parsePlacement(placementInput);
382
- let {size, crossAxis, crossSize, placement, crossPlacement} = placementInfo;
383
- let position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
384
- let normalizedOffset = offset;
385
- let space = getAvailableSpace(
386
- boundaryDimensions,
387
- containerOffsetWithBoundary,
388
- childOffset,
389
- margins,
390
- padding + offset,
391
- placementInfo,
392
- containerDimensions,
393
- isContainerDescendentOfBoundary
394
- );
395
-
396
- // Check if the scroll size of the overlay is greater than the available space to determine if we need to flip
397
- if (flip && overlaySize[size] > space) {
398
- let flippedPlacementInfo = parsePlacement(`${FLIPPED_DIRECTION[placement]} ${crossPlacement}` as Placement);
399
- let flippedPosition = computePosition(childOffset, boundaryDimensions, overlaySize, flippedPlacementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
400
-
401
- let flippedSpace = getAvailableSpace(
402
- boundaryDimensions,
403
- containerOffsetWithBoundary,
404
- childOffset,
405
- margins,
406
- padding + offset,
407
- flippedPlacementInfo,
408
- containerDimensions,
409
- isContainerDescendentOfBoundary
410
- );
411
-
412
- // If the available space for the flipped position is greater than the original available space, flip.
413
- if (flippedSpace > space) {
414
- placementInfo = flippedPlacementInfo;
415
- position = flippedPosition;
416
- normalizedOffset = offset;
417
- }
418
- }
419
-
420
- // Determine the direction the height of the overlay can grow so that we can choose how to calculate the max height
421
- let heightGrowthDirection: HeightGrowthDirection = 'bottom';
422
- if (placementInfo.axis === 'top') {
423
- if (placementInfo.placement === 'top') {
424
- heightGrowthDirection = 'top';
425
- } else if (placementInfo.placement === 'bottom') {
426
- heightGrowthDirection = 'bottom';
427
- }
428
- } else if (placementInfo.crossAxis === 'top') {
429
- if (placementInfo.crossPlacement === 'top') {
430
- heightGrowthDirection = 'bottom';
431
- } else if (placementInfo.crossPlacement === 'bottom') {
432
- heightGrowthDirection = 'top';
433
- }
434
- }
435
-
436
- let delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary);
437
- position[crossAxis]! += delta;
438
-
439
- let maxHeight = getMaxHeight(
440
- position,
441
- boundaryDimensions,
442
- containerOffsetWithBoundary,
443
- isContainerPositioned,
444
- margins,
445
- padding,
446
- overlaySize.height,
447
- heightGrowthDirection,
448
- containerDimensions,
449
- isContainerDescendentOfBoundary,
450
- visualViewport
451
- );
452
-
453
- if (userSetMaxHeight && userSetMaxHeight < maxHeight) {
454
- maxHeight = userSetMaxHeight;
455
- }
456
-
457
- overlaySize.height = Math.min(overlaySize.height, maxHeight);
458
-
459
- position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, normalizedOffset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset, containerDimensions);
460
- delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary);
461
- position[crossAxis]! += delta;
462
-
463
- let arrowPosition: Position = {};
464
-
465
- // All values are transformed so that 0 is at the top/left of the overlay depending on the orientation
466
- // Prefer the arrow being in the center of the trigger/overlay anchor element
467
- // 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
468
- // 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"
469
- // is positioned after the margin of its parent (aka overlay) so we need to subtract it to get the proper coordinate transform
470
- let origin = childOffset[crossAxis] - position[crossAxis]! - margins[AXIS[crossAxis]];
471
- let preferredArrowPosition = origin + .5 * childOffset[crossSize];
472
-
473
- // Min/Max position limits for the arrow with respect to the overlay
474
- const arrowMinPosition = arrowSize / 2 + arrowBoundaryOffset;
475
- // overlaySize[crossSize] - margins = true size of the overlay
476
- const overlayMargin = AXIS[crossAxis] === 'left' ? (margins.left ?? 0) + (margins.right ?? 0) : (margins.top ?? 0) + (margins.bottom ?? 0);
477
- const arrowMaxPosition = overlaySize[crossSize] - overlayMargin - (arrowSize / 2) - arrowBoundaryOffset;
478
-
479
- // Min/Max position limits for the arrow with respect to the trigger/overlay anchor element
480
- // Same margin accomodation done here as well as for the preferredArrowPosition
481
- const arrowOverlappingChildMinEdge = childOffset[crossAxis] + (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]);
482
- const arrowOverlappingChildMaxEdge = childOffset[crossAxis] + childOffset[crossSize] - (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]);
483
-
484
- // Clamp the arrow positioning so that it always is within the bounds of the anchor and the overlay
485
- const arrowPositionOverlappingChild = clamp(preferredArrowPosition, arrowOverlappingChildMinEdge, arrowOverlappingChildMaxEdge);
486
- arrowPosition[crossAxis] = clamp(arrowPositionOverlappingChild, arrowMinPosition, arrowMaxPosition);
487
-
488
- // If there is an arrow, use that as the origin so that animations are smooth.
489
- // Otherwise use the target edge.
490
- ({placement, crossPlacement} = placementInfo);
491
- if (arrowSize) {
492
- origin = arrowPosition[crossAxis];
493
- } else if (crossPlacement === 'right') {
494
- origin += childOffset[crossSize];
495
- } else if (crossPlacement === 'center') {
496
- origin += childOffset[crossSize] / 2;
497
- }
498
-
499
- let crossOrigin = placement === 'left' || placement === 'top' ? overlaySize[size] : 0;
500
- let triggerAnchorPoint = {
501
- x: placement === 'top' || placement === 'bottom' ? origin : crossOrigin,
502
- y: placement === 'left' || placement === 'right' ? origin : crossOrigin
503
- };
504
-
505
- return {
506
- position,
507
- maxHeight: maxHeight,
508
- arrowOffsetLeft: arrowPosition.left,
509
- arrowOffsetTop: arrowPosition.top,
510
- placement,
511
- triggerAnchorPoint
512
- };
513
- }
514
-
515
- /**
516
- * Determines where to place the overlay with regards to the target and the position of an optional indicator.
517
- */
518
- export function calculatePosition(opts: PositionOpts): PositionResult {
519
- let {
520
- placement,
521
- targetNode,
522
- overlayNode,
523
- scrollNode,
524
- padding,
525
- shouldFlip,
526
- boundaryElement,
527
- offset,
528
- crossOffset,
529
- maxHeight,
530
- arrowSize = 0,
531
- arrowBoundaryOffset = 0
532
- } = opts;
533
-
534
- let visualViewport = getVisualViewport();
535
- let container = overlayNode instanceof HTMLElement ? getContainingBlock(overlayNode) : document.documentElement;
536
- let isViewportContainer = container === document.documentElement;
537
- const containerPositionStyle = window.getComputedStyle(container).position;
538
- let isContainerPositioned = !!containerPositionStyle && containerPositionStyle !== 'static';
539
- let childOffset: Offset = isViewportContainer ? getOffset(targetNode, false) : getPosition(targetNode, container, false);
540
-
541
- if (!isViewportContainer) {
542
- let {marginTop, marginLeft} = window.getComputedStyle(targetNode);
543
- childOffset.top += parseInt(marginTop, 10) || 0;
544
- childOffset.left += parseInt(marginLeft, 10) || 0;
545
- }
546
-
547
- let overlaySize: Offset = getOffset(overlayNode, true);
548
- let margins = getMargins(overlayNode);
549
- overlaySize.width += (margins.left ?? 0) + (margins.right ?? 0);
550
- overlaySize.height += (margins.top ?? 0) + (margins.bottom ?? 0);
551
-
552
- let scrollSize = getScroll(scrollNode);
553
-
554
- // Note that due to logic inside getContainerDimensions, for cases where the boundary element is the body, we will return
555
- // 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)
556
- // 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)
557
- // 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)
558
- let boundaryDimensions = getContainerDimensions(boundaryElement, visualViewport);
559
- let containerDimensions = getContainerDimensions(container, visualViewport);
560
-
561
- // There are several difference cases of how to calculate the containerOffsetWithBoundary:
562
- // - boundaryElement is body or HTML and the container is an arbitrary element in the boundary (aka submenu with parent menu as container in v3)
563
- // - boundaryElement and container are both body or HTML element (aka standard popover case)
564
- // - boundaryElement is customized by the user. Container can also be arbitrary (either body/HTML or some other element)
565
- // containerOffsetWithBoundary should always return a value that is the boundary's coordinate offset with respect to the container coord system (container is 0, 0)
566
- let containerOffsetWithBoundary: Offset;
567
- if ((boundaryElement.tagName === 'BODY' || boundaryElement.tagName === 'HTML') && !isViewportContainer) {
568
- // Use getRect instead of getOffset because boundaryDimensions for BODY/HTML is in viewport coordinate space,
569
- // not document coordinate space
570
- let containerRect = getRect(container, false);
571
- // the offset should be negative because if container is at viewport position x,y, then viewport top (aka 0)
572
- // is at position -x,y in container-relative coordinates
573
- containerOffsetWithBoundary = {
574
- top: -(containerRect.top - boundaryDimensions.top),
575
- left: -(containerRect.left - boundaryDimensions.left),
576
- width: 0,
577
- height: 0
578
- };
579
- } else if ((boundaryElement.tagName === 'BODY' || boundaryElement.tagName === 'HTML') && isViewportContainer) {
580
- // both are the same viewport container, no offset needed
581
- containerOffsetWithBoundary = {top: 0, left: 0, width: 0, height: 0};
582
- } else {
583
- // This returns the boundary's coordinate with respect to the container. This case captures cases such as when you provide a custom boundary
584
- // like in ScrollingBoundaryContainerExample in Popover.stories.
585
- containerOffsetWithBoundary = getPosition(boundaryElement, container, false);
586
- }
587
-
588
- let isContainerDescendentOfBoundary = nodeContains(boundaryElement, container);
589
- return calculatePositionInternal(
590
- placement,
591
- childOffset,
592
- overlaySize,
593
- scrollSize,
594
- margins,
595
- padding,
596
- shouldFlip,
597
- boundaryDimensions,
598
- containerDimensions,
599
- containerOffsetWithBoundary,
600
- offset,
601
- crossOffset,
602
- isContainerPositioned,
603
- maxHeight,
604
- arrowSize,
605
- arrowBoundaryOffset,
606
- isContainerDescendentOfBoundary,
607
- visualViewport
608
- );
609
- }
610
-
611
- export function getRect(node: Element, ignoreScale: boolean) {
612
- let {top, left, width, height} = node.getBoundingClientRect();
613
-
614
- // Use offsetWidth and offsetHeight if this is an HTML element, so that
615
- // the size is not affected by scale transforms.
616
- if (ignoreScale && node instanceof node.ownerDocument.defaultView!.HTMLElement) {
617
- width = node.offsetWidth;
618
- height = node.offsetHeight;
619
- }
620
-
621
- return {top, left, width, height};
622
- }
623
-
624
- function getOffset(node: Element, ignoreScale: boolean): Offset {
625
- let {top, left, width, height} = getRect(node, ignoreScale);
626
- let {scrollTop, scrollLeft, clientTop, clientLeft} = document.documentElement;
627
- return {
628
- top: top + scrollTop - clientTop,
629
- left: left + scrollLeft - clientLeft,
630
- width,
631
- height
632
- };
633
- }
634
-
635
- function getPosition(node: Element, parent: Element, ignoreScale: boolean): Offset {
636
- let style = window.getComputedStyle(node);
637
- let offset: Offset;
638
- if (style.position === 'fixed') {
639
- offset = getRect(node, ignoreScale);
640
- } else {
641
- offset = getOffset(node, ignoreScale);
642
- let parentOffset = getOffset(parent, ignoreScale);
643
- let parentStyle = window.getComputedStyle(parent);
644
- parentOffset.top += (parseInt(parentStyle.borderTopWidth, 10) || 0) - parent.scrollTop;
645
- parentOffset.left += (parseInt(parentStyle.borderLeftWidth, 10) || 0) - parent.scrollLeft;
646
- offset.top -= parentOffset.top;
647
- offset.left -= parentOffset.left;
648
- }
649
-
650
- offset.top -= parseInt(style.marginTop, 10) || 0;
651
- offset.left -= parseInt(style.marginLeft, 10) || 0;
652
- return offset;
653
- }
654
-
655
- // Returns the containing block of an element, which is the element that
656
- // this element will be positioned relative to.
657
- // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block
658
- function getContainingBlock(node: HTMLElement): Element {
659
- // The offsetParent of an element in most cases equals the containing block.
660
- // https://w3c.github.io/csswg-drafts/cssom-view/#dom-htmlelement-offsetparent
661
- let offsetParent = node.offsetParent;
662
-
663
- // The offsetParent algorithm terminates at the document body,
664
- // even if the body is not a containing block. Double check that
665
- // and use the documentElement if so.
666
- if (
667
- offsetParent &&
668
- offsetParent === document.body &&
669
- window.getComputedStyle(offsetParent).position === 'static' &&
670
- !isContainingBlock(offsetParent)
671
- ) {
672
- offsetParent = document.documentElement;
673
- }
674
-
675
- // TODO(later): handle table elements?
676
-
677
- // The offsetParent can be null if the element has position: fixed, or a few other cases.
678
- // We have to walk up the tree manually in this case because fixed positioned elements
679
- // are still positioned relative to their containing block, which is not always the viewport.
680
- if (offsetParent == null) {
681
- offsetParent = node.parentElement;
682
- while (offsetParent && !isContainingBlock(offsetParent)) {
683
- offsetParent = offsetParent.parentElement;
684
- }
685
- }
686
-
687
- // Fall back to the viewport.
688
- return offsetParent || document.documentElement;
689
- }
690
-
691
- // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
692
- function isContainingBlock(node: Element): boolean {
693
- let style = window.getComputedStyle(node);
694
- return (
695
- style.transform !== 'none' ||
696
- /transform|perspective/.test(style.willChange) ||
697
- style.filter !== 'none' ||
698
- style.contain === 'paint' ||
699
- ('backdropFilter' in style && style.backdropFilter !== 'none') ||
700
- ('WebkitBackdropFilter' in style && style.WebkitBackdropFilter !== 'none')
701
- );
702
- }