@cloudscape-design/components 3.0.490 → 3.0.491

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.
@@ -131,15 +131,6 @@ const RECTANGLE_CALCULATIONS = {
131
131
  };
132
132
  },
133
133
  };
134
- /**
135
- * Returns whether one rectangle fits in another.
136
- */
137
- function canRectFit(inner, outer) {
138
- return (inner.left >= outer.left &&
139
- inner.top >= outer.top &&
140
- inner.left + inner.width <= outer.left + outer.width &&
141
- inner.top + inner.height <= outer.top + outer.height);
142
- }
143
134
  function fitIntoContainer(inner, outer) {
144
135
  let { left, width, top, height } = inner;
145
136
  // Adjust left boundary.
@@ -162,72 +153,99 @@ function fitIntoContainer(inner, outer) {
162
153
  }
163
154
  return { left, width, top, height };
164
155
  }
165
- /**
166
- * Returns the area of the intersection of passed in rectangles or a null, if there is no intersection
167
- */
168
- export function intersectRectangles(rectangles) {
169
- let boundingOffset = null;
156
+ function getTallestRect(rect1, rect2) {
157
+ return rect1.height >= rect2.height ? rect1 : rect2;
158
+ }
159
+ function getIntersection(rectangles) {
160
+ let boundingBox = null;
170
161
  for (const currentRect of rectangles) {
171
- if (!boundingOffset) {
172
- boundingOffset = currentRect;
162
+ if (!boundingBox) {
163
+ boundingBox = currentRect;
173
164
  continue;
174
165
  }
175
- const left = Math.max(boundingOffset.left, currentRect.left);
176
- const top = Math.max(boundingOffset.top, currentRect.top);
177
- const right = Math.min(boundingOffset.left + boundingOffset.width, currentRect.left + currentRect.width);
178
- const bottom = Math.min(boundingOffset.top + boundingOffset.height, currentRect.top + currentRect.height);
166
+ const left = Math.max(boundingBox.left, currentRect.left);
167
+ const top = Math.max(boundingBox.top, currentRect.top);
168
+ const right = Math.min(boundingBox.left + boundingBox.width, currentRect.left + currentRect.width);
169
+ const bottom = Math.min(boundingBox.top + boundingBox.height, currentRect.top + currentRect.height);
179
170
  if (right < left || bottom < top) {
180
171
  return null;
181
172
  }
182
- boundingOffset = {
173
+ boundingBox = {
183
174
  left,
184
175
  top,
185
176
  width: right - left,
186
177
  height: bottom - top,
187
178
  };
188
179
  }
189
- return boundingOffset && boundingOffset.height * boundingOffset.width;
180
+ return boundingBox;
181
+ }
182
+ /**
183
+ * Returns the area of the intersection of passed in rectangles or a null, if there is no intersection
184
+ */
185
+ export function intersectRectangles(rectangles) {
186
+ const boundingBox = getIntersection(rectangles);
187
+ return boundingBox && boundingBox.height * boundingBox.width;
190
188
  }
191
189
  /**
192
190
  * A functions that returns the correct popover position based on screen dimensions.
193
191
  */
194
192
  export function calculatePosition({ preferredPosition, fixedInternalPosition, trigger, arrow, body, container, viewport,
195
193
  // the popover is only bound by the viewport if it is rendered in a portal
196
- renderWithPortal, }) {
197
- let bestPositionOutsideViewport = null;
198
- let largestArea = 0;
194
+ renderWithPortal, allowVerticalOverflow, }) {
195
+ let bestOption = null;
199
196
  // If a fixed internal position is passed, only consider this one.
200
197
  const preferredInternalPositions = fixedInternalPosition
201
198
  ? [fixedInternalPosition]
202
199
  : PRIORITY_MAPPING[preferredPosition];
203
- // Attempt to position the popover based on the priority list for this position,
204
- // trying to fit it inside the container and inside the viewport.
200
+ // Attempt to position the popover based on the priority list for this position.
205
201
  for (const internalPosition of preferredInternalPositions) {
206
- const boundingOffset = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
207
- const fitsInContainer = renderWithPortal || canRectFit(boundingOffset, container);
208
- const fitsInViewport = canRectFit(boundingOffset, viewport);
209
- if (fitsInContainer && fitsInViewport) {
210
- return { internalPosition, boundingOffset };
211
- }
212
- const boundingRectangles = [boundingOffset, viewport];
213
- if (!renderWithPortal) {
214
- boundingRectangles.push(container);
215
- }
216
- const availableArea = intersectRectangles(boundingRectangles);
217
- if (availableArea && availableArea > largestArea) {
218
- bestPositionOutsideViewport = { internalPosition, boundingOffset };
219
- largestArea = availableArea;
202
+ const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
203
+ const visibleArea = renderWithPortal
204
+ ? getIntersection([rect, viewport])
205
+ : getIntersection([rect, viewport, container]);
206
+ const fitsWithoutOverflow = visibleArea && visibleArea.width === body.width && visibleArea.height === body.height;
207
+ if (fitsWithoutOverflow) {
208
+ return { internalPosition, rect };
220
209
  }
210
+ const newOption = { rect, internalPosition, visibleArea };
211
+ bestOption = getBestOption(newOption, bestOption);
221
212
  }
222
213
  // Use best possible placement.
223
- const internalPosition = (bestPositionOutsideViewport === null || bestPositionOutsideViewport === void 0 ? void 0 : bestPositionOutsideViewport.internalPosition) || 'right-top';
214
+ const internalPosition = (bestOption === null || bestOption === void 0 ? void 0 : bestOption.internalPosition) || 'right-top';
224
215
  // Get default rect for that placement.
225
- const defaultOffset = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
226
- // Get largest possible rect that fits into viewport.
227
- const optimisedOffset = fitIntoContainer(defaultOffset, viewport);
228
- // If largest possible rect is smaller than original - set body scroll.
229
- const scrollable = optimisedOffset.height < defaultOffset.height;
230
- return { internalPosition, boundingOffset: optimisedOffset, scrollable };
216
+ const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
217
+ // Get largest possible rect that fits into the viewport or container.
218
+ // We allow the popover to overflow the viewport if allowVerticalOverflow is true _and_ the popover will be anchored to the top or the bottom.
219
+ // If it is anchored to the right or left, we consider that it should have enough vertical space so that applying scroll to it is a better option.
220
+ const tallestBoundingContainer = getTallestRect(viewport, container);
221
+ const boundingContainer = allowVerticalOverflow && isTopOrBottom(internalPosition)
222
+ ? {
223
+ top: tallestBoundingContainer.top,
224
+ height: tallestBoundingContainer.height,
225
+ left: viewport.left,
226
+ width: viewport.width,
227
+ }
228
+ : viewport;
229
+ const optimizedRect = fitIntoContainer(rect, boundingContainer);
230
+ // If largest possible rect is shorter than original - set body scroll.
231
+ const scrollable = optimizedRect.height < rect.height;
232
+ return { internalPosition, rect: optimizedRect, scrollable };
233
+ }
234
+ function getBestOption(option1, option2) {
235
+ // Within calculatePosition, the only case where option2 will not be defined will be in the first call.
236
+ // The only case where the visibleArea of an option will be null is when the popover is totally outside of the viewport.
237
+ if (!(option2 === null || option2 === void 0 ? void 0 : option2.visibleArea)) {
238
+ return option1;
239
+ }
240
+ if (!option1.visibleArea) {
241
+ return option2;
242
+ }
243
+ // Only if none of the two options overflows horizontally, choose the best based on the visible height.
244
+ if (option1.visibleArea.width === option2.visibleArea.width) {
245
+ return option1.visibleArea.height > option2.visibleArea.height ? option1 : option2;
246
+ }
247
+ // Otherwise, choose the option that is less cut off horizontally.
248
+ return option1.visibleArea.width > option2.visibleArea.width ? option1 : option2;
231
249
  }
232
250
  export function getOffsetDimensions(element) {
233
251
  return { offsetHeight: element.offsetHeight, offsetWidth: element.offsetWidth };
@@ -239,4 +257,7 @@ export function getDimensions(element) {
239
257
  height: parseFloat(computedStyle.height),
240
258
  };
241
259
  }
260
+ function isTopOrBottom(internalPosition) {
261
+ return ['top', 'bottom'].includes(internalPosition.split('-')[0]);
262
+ }
242
263
  //# sourceMappingURL=positions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"positions.js","sourceRoot":"lib/default/","sources":["popover/utils/positions.ts"],"names":[],"mappings":"AAiBA,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB,MAAM,CAAC,MAAM,gBAAgB,GAAsD;IACjF,GAAG,EAAE;QACH,YAAY;QACZ,WAAW;QACX,UAAU;QACV,eAAe;QACf,cAAc;QACd,aAAa;QACb,WAAW;QACX,cAAc;QACd,UAAU;QACV,aAAa;KACd;IACD,MAAM,EAAE;QACN,eAAe;QACf,cAAc;QACd,aAAa;QACb,YAAY;QACZ,WAAW;QACX,UAAU;QACV,WAAW;QACX,cAAc;QACd,UAAU;QACV,aAAa;KACd;IACD,IAAI,EAAE;QACJ,UAAU;QACV,aAAa;QACb,WAAW;QACX,cAAc;QACd,eAAe;QACf,YAAY;QACZ,aAAa;QACb,UAAU;QACV,cAAc;QACd,WAAW;KACZ;IACD,KAAK,EAAE;QACL,WAAW;QACX,cAAc;QACd,UAA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BAAkB,CAAC,CAAC;QAC9D,IAAI,aAAa,IAAI,aAAa,GAAG,WAAW,EAAE;YAChD,2BAA2B,GAAG,EAAE,gBAAgB,EAAE,cAAc,EAAE,CAAC;YACnE,WAAW,GAAG,aAAa,CAAC;SAC7B;KACF;IAED,+BAA+B;IAC/B,MAAM,gBAAgB,GAAG,CAAA,2BAA2B,aAA3B,2BAA2B,uBAA3B,2BAA2B,CAAE,gBAAgB,KAAI,WAAW,CAAC;IACtF,uCAAuC;IACvC,MAAM,aAAa,GAAG,sBAAsB,CAAC,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IACzF,qDAAqD;IACrD,MAAM,eAAe,GAAG,gBAAgB,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;IAClE,uEAAuE;IACvE,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC;IAEjE,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;AAC3E,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,OAAoB;IACtD,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;AAClF,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,MAAM,aAAa,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAChD,OAAO;QACL,KAAK,EAAE,UAAU,CAAC,aAAa,CAAC,KAAK,CAAC;QACtC,MAAM,EAAE,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC;KACzC,CAAC;AACJ,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { PopoverProps, InternalPosition, BoundingOffset, BoundingBox } from '../interfaces';\n\n// A structure describing how the popover should be positioned\ninterface CalculatedPosition {\n scrollable?: boolean;\n internalPosition: InternalPosition;\n boundingOffset: BoundingOffset;\n}\n\ninterface ElementGroup {\n body: BoundingBox;\n trigger: BoundingOffset;\n arrow: BoundingBox;\n}\n\nconst ARROW_OFFSET = 12;\n\nexport const PRIORITY_MAPPING: Record<PopoverProps.Position, InternalPosition[]> = {\n top: [\n 'top-center',\n 'top-right',\n 'top-left',\n 'bottom-center',\n 'bottom-right',\n 'bottom-left',\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n ],\n bottom: [\n 'bottom-center',\n 'bottom-right',\n 'bottom-left',\n 'top-center',\n 'top-right',\n 'top-left',\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n ],\n left: [\n 'left-top',\n 'left-bottom',\n 'right-top',\n 'right-bottom',\n 'bottom-center',\n 'top-center',\n 'bottom-left',\n 'top-left',\n 'bottom-right',\n 'top-right',\n ],\n right: [\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n 'bottom-center',\n 'top-center',\n 'bottom-right',\n 'top-right',\n 'bottom-left',\n 'top-left',\n ],\n};\n\nconst RECTANGLE_CALCULATIONS: Record<InternalPosition, (r: ElementGroup) => BoundingOffset> = {\n 'top-center': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 - body.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'top-right': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 - ARROW_OFFSET - arrow.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'top-left': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 + ARROW_OFFSET + arrow.width / 2 - body.width,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-center': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 - body.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-right': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 - ARROW_OFFSET - arrow.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-left': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 + ARROW_OFFSET + arrow.width / 2 - body.width,\n width: body.width,\n height: body.height,\n };\n },\n 'right-top': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - ARROW_OFFSET - arrow.height,\n left: trigger.left + trigger.width + arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'right-bottom': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - body.height + ARROW_OFFSET + arrow.height,\n left: trigger.left + trigger.width + arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'left-top': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - ARROW_OFFSET - arrow.height,\n left: trigger.left - body.width - arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'left-bottom': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - body.height + ARROW_OFFSET + arrow.height,\n left: trigger.left - body.width - arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n};\n\n/**\n * Returns whether one rectangle fits in another.\n */\nfunction canRectFit(inner: BoundingOffset, outer: BoundingOffset): boolean {\n return (\n inner.left >= outer.left &&\n inner.top >= outer.top &&\n inner.left + inner.width <= outer.left + outer.width &&\n inner.top + inner.height <= outer.top + outer.height\n );\n}\n\nfunction fitIntoContainer(inner: BoundingOffset, outer: BoundingOffset): BoundingOffset {\n let { left, width, top, height } = inner;\n\n // Adjust left boundary.\n if (left < outer.left) {\n width = left + width - outer.left;\n left = outer.left;\n }\n // Adjust right boundary.\n else if (left + width > outer.left + outer.width) {\n width = outer.left + outer.width - left;\n }\n // Adjust top boundary.\n if (top < outer.top) {\n height = top + height - outer.top;\n top = outer.top;\n }\n // Adjust bottom boundary.\n else if (top + height > outer.top + outer.height) {\n height = outer.top + outer.height - top;\n }\n\n return { left, width, top, height };\n}\n\n/**\n * Returns the area of the intersection of passed in rectangles or a null, if there is no intersection\n */\nexport function intersectRectangles(rectangles: BoundingOffset[]): number | null {\n let boundingOffset: BoundingOffset | null = null;\n for (const currentRect of rectangles) {\n if (!boundingOffset) {\n boundingOffset = currentRect;\n continue;\n }\n const left = Math.max(boundingOffset.left, currentRect.left);\n const top = Math.max(boundingOffset.top, currentRect.top);\n const right = Math.min(boundingOffset.left + boundingOffset.width, currentRect.left + currentRect.width);\n const bottom = Math.min(boundingOffset.top + boundingOffset.height, currentRect.top + currentRect.height);\n if (right < left || bottom < top) {\n return null;\n }\n boundingOffset = {\n left,\n top,\n width: right - left,\n height: bottom - top,\n };\n }\n return boundingOffset && boundingOffset.height * boundingOffset.width;\n}\n\n/**\n * A functions that returns the correct popover position based on screen dimensions.\n */\nexport function calculatePosition({\n preferredPosition,\n fixedInternalPosition,\n trigger,\n arrow,\n body,\n container,\n viewport,\n // the popover is only bound by the viewport if it is rendered in a portal\n renderWithPortal,\n}: {\n preferredPosition: PopoverProps.Position;\n fixedInternalPosition?: InternalPosition;\n trigger: BoundingOffset;\n arrow: BoundingBox;\n body: BoundingBox;\n container: BoundingOffset;\n viewport: BoundingOffset;\n // the popover is only bound by the viewport if it is rendered in a portal\n renderWithPortal?: boolean;\n}): CalculatedPosition {\n let bestPositionOutsideViewport: CalculatedPosition | null = null;\n let largestArea = 0;\n\n // If a fixed internal position is passed, only consider this one.\n const preferredInternalPositions = fixedInternalPosition\n ? [fixedInternalPosition]\n : PRIORITY_MAPPING[preferredPosition];\n\n // Attempt to position the popover based on the priority list for this position,\n // trying to fit it inside the container and inside the viewport.\n for (const internalPosition of preferredInternalPositions) {\n const boundingOffset = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });\n const fitsInContainer = renderWithPortal || canRectFit(boundingOffset, container);\n const fitsInViewport = canRectFit(boundingOffset, viewport);\n if (fitsInContainer && fitsInViewport) {\n return { internalPosition, boundingOffset };\n }\n const boundingRectangles = [boundingOffset, viewport];\n if (!renderWithPortal) {\n boundingRectangles.push(container);\n }\n const availableArea = intersectRectangles(boundingRectangles);\n if (availableArea && availableArea > largestArea) {\n bestPositionOutsideViewport = { internalPosition, boundingOffset };\n largestArea = availableArea;\n }\n }\n\n // Use best possible placement.\n const internalPosition = bestPositionOutsideViewport?.internalPosition || 'right-top';\n // Get default rect for that placement.\n const defaultOffset = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });\n // Get largest possible rect that fits into viewport.\n const optimisedOffset = fitIntoContainer(defaultOffset, viewport);\n // If largest possible rect is smaller than original - set body scroll.\n const scrollable = optimisedOffset.height < defaultOffset.height;\n\n return { internalPosition, boundingOffset: optimisedOffset, scrollable };\n}\n\nexport function getOffsetDimensions(element: HTMLElement) {\n return { offsetHeight: element.offsetHeight, offsetWidth: element.offsetWidth };\n}\n\nexport function getDimensions(element: HTMLElement) {\n const computedStyle = getComputedStyle(element);\n return {\n width: parseFloat(computedStyle.width),\n height: parseFloat(computedStyle.height),\n };\n}\n"]}
1
+ {"version":3,"file":"positions.js","sourceRoot":"lib/default/","sources":["popover/utils/positions.ts"],"names":[],"mappings":"AAiBA,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB,MAAM,CAAC,MAAM,gBAAgB,GAAsD;IACjF,GAAG,EAAE;QACH,YAAY;QACZ,WAAW;QACX,UAAU;QACV,eAAe;QACf,cAAc;QACd,aAAa;QACb,WAAW;QACX,cAAc;QACd,UAAU;QACV,aAAa;KACd;IACD,MAAM,EAAE;QACN,eAAe;QACf,cAAc;QACd,aAAa;QACb,YAAY;QACZ,WAAW;QACX,UAAU;QACV,WAAW;QACX,cAAc;QACd,UAAU;QACV,aAAa;KACd;IACD,IAAI,EAAE;QACJ,UAAU;QACV,aAAa;QACb,WAAW;QACX,cAAc;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,uGAAuG;IACvG,IAAI,OAAO,CAAC,WAAW,CAAC,KAAK,KAAK,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE;QAC3D,OAAO,OAAO,CAAC,WAAW,CAAC,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC;KACpF;IACD,kEAAkE;IAClE,OAAO,OAAO,CAAC,WAAW,CAAC,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC;AACnF,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,OAAoB;IACtD,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;AAClF,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,MAAM,aAAa,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAChD,OAAO;QACL,KAAK,EAAE,UAAU,CAAC,aAAa,CAAC,KAAK,CAAC;QACtC,MAAM,EAAE,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC;KACzC,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,gBAAkC;IACvD,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,gBAAgB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpE,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { PopoverProps, InternalPosition, BoundingBox, Dimensions } from '../interfaces';\n\n// A structure describing how the popover should be positioned\ninterface CalculatedPosition {\n scrollable?: boolean;\n internalPosition: InternalPosition;\n rect: BoundingBox;\n}\n\ninterface ElementGroup {\n body: Dimensions;\n trigger: BoundingBox;\n arrow: Dimensions;\n}\n\nconst ARROW_OFFSET = 12;\n\nexport const PRIORITY_MAPPING: Record<PopoverProps.Position, InternalPosition[]> = {\n top: [\n 'top-center',\n 'top-right',\n 'top-left',\n 'bottom-center',\n 'bottom-right',\n 'bottom-left',\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n ],\n bottom: [\n 'bottom-center',\n 'bottom-right',\n 'bottom-left',\n 'top-center',\n 'top-right',\n 'top-left',\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n ],\n left: [\n 'left-top',\n 'left-bottom',\n 'right-top',\n 'right-bottom',\n 'bottom-center',\n 'top-center',\n 'bottom-left',\n 'top-left',\n 'bottom-right',\n 'top-right',\n ],\n right: [\n 'right-top',\n 'right-bottom',\n 'left-top',\n 'left-bottom',\n 'bottom-center',\n 'top-center',\n 'bottom-right',\n 'top-right',\n 'bottom-left',\n 'top-left',\n ],\n};\n\nconst RECTANGLE_CALCULATIONS: Record<InternalPosition, (r: ElementGroup) => BoundingBox> = {\n 'top-center': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 - body.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'top-right': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 - ARROW_OFFSET - arrow.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'top-left': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top - body.height - arrow.height,\n left: trigger.left + trigger.width / 2 + ARROW_OFFSET + arrow.width / 2 - body.width,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-center': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 - body.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-right': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 - ARROW_OFFSET - arrow.width / 2,\n width: body.width,\n height: body.height,\n };\n },\n 'bottom-left': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height + arrow.height,\n left: trigger.left + trigger.width / 2 + ARROW_OFFSET + arrow.width / 2 - body.width,\n width: body.width,\n height: body.height,\n };\n },\n 'right-top': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - ARROW_OFFSET - arrow.height,\n left: trigger.left + trigger.width + arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'right-bottom': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - body.height + ARROW_OFFSET + arrow.height,\n left: trigger.left + trigger.width + arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'left-top': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - ARROW_OFFSET - arrow.height,\n left: trigger.left - body.width - arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n 'left-bottom': ({ body, trigger, arrow }) => {\n return {\n top: trigger.top + trigger.height / 2 - body.height + ARROW_OFFSET + arrow.height,\n left: trigger.left - body.width - arrow.height,\n width: body.width,\n height: body.height,\n };\n },\n};\n\nfunction fitIntoContainer(inner: BoundingBox, outer: BoundingBox): BoundingBox {\n let { left, width, top, height } = inner;\n\n // Adjust left boundary.\n if (left < outer.left) {\n width = left + width - outer.left;\n left = outer.left;\n }\n // Adjust right boundary.\n else if (left + width > outer.left + outer.width) {\n width = outer.left + outer.width - left;\n }\n // Adjust top boundary.\n if (top < outer.top) {\n height = top + height - outer.top;\n top = outer.top;\n }\n // Adjust bottom boundary.\n else if (top + height > outer.top + outer.height) {\n height = outer.top + outer.height - top;\n }\n\n return { left, width, top, height };\n}\n\nfunction getTallestRect(rect1: BoundingBox, rect2: BoundingBox): BoundingBox {\n return rect1.height >= rect2.height ? rect1 : rect2;\n}\n\nfunction getIntersection(rectangles: BoundingBox[]): BoundingBox | null {\n let boundingBox: BoundingBox | null = null;\n for (const currentRect of rectangles) {\n if (!boundingBox) {\n boundingBox = currentRect;\n continue;\n }\n const left = Math.max(boundingBox.left, currentRect.left);\n const top = Math.max(boundingBox.top, currentRect.top);\n const right = Math.min(boundingBox.left + boundingBox.width, currentRect.left + currentRect.width);\n const bottom = Math.min(boundingBox.top + boundingBox.height, currentRect.top + currentRect.height);\n if (right < left || bottom < top) {\n return null;\n }\n boundingBox = {\n left,\n top,\n width: right - left,\n height: bottom - top,\n };\n }\n return boundingBox;\n}\n\n/**\n * Returns the area of the intersection of passed in rectangles or a null, if there is no intersection\n */\nexport function intersectRectangles(rectangles: BoundingBox[]): number | null {\n const boundingBox: BoundingBox | null = getIntersection(rectangles);\n return boundingBox && boundingBox.height * boundingBox.width;\n}\n\ntype CandidatePosition = CalculatedPosition & { visibleArea: BoundingBox | null };\n\n/**\n * A functions that returns the correct popover position based on screen dimensions.\n */\nexport function calculatePosition({\n preferredPosition,\n fixedInternalPosition,\n trigger,\n arrow,\n body,\n container,\n viewport,\n // the popover is only bound by the viewport if it is rendered in a portal\n renderWithPortal,\n allowVerticalOverflow,\n}: {\n preferredPosition: PopoverProps.Position;\n fixedInternalPosition?: InternalPosition;\n trigger: BoundingBox;\n arrow: Dimensions;\n body: Dimensions;\n container: BoundingBox;\n viewport: BoundingBox;\n // the popover is only bound by the viewport if it is rendered in a portal\n renderWithPortal?: boolean;\n allowVerticalOverflow?: boolean;\n}): CalculatedPosition {\n let bestOption: CandidatePosition | null = null;\n\n // If a fixed internal position is passed, only consider this one.\n const preferredInternalPositions = fixedInternalPosition\n ? [fixedInternalPosition]\n : PRIORITY_MAPPING[preferredPosition];\n\n // Attempt to position the popover based on the priority list for this position.\n for (const internalPosition of preferredInternalPositions) {\n const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });\n const visibleArea = renderWithPortal\n ? getIntersection([rect, viewport])\n : getIntersection([rect, viewport, container]);\n\n const fitsWithoutOverflow = visibleArea && visibleArea.width === body.width && visibleArea.height === body.height;\n\n if (fitsWithoutOverflow) {\n return { internalPosition, rect };\n }\n\n const newOption = { rect, internalPosition, visibleArea };\n bestOption = getBestOption(newOption, bestOption);\n }\n\n // Use best possible placement.\n const internalPosition = bestOption?.internalPosition || 'right-top';\n // Get default rect for that placement.\n const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });\n\n // Get largest possible rect that fits into the viewport or container.\n\n // We allow the popover to overflow the viewport if allowVerticalOverflow is true _and_ the popover will be anchored to the top or the bottom.\n // If it is anchored to the right or left, we consider that it should have enough vertical space so that applying scroll to it is a better option.\n const tallestBoundingContainer = getTallestRect(viewport, container);\n const boundingContainer =\n allowVerticalOverflow && isTopOrBottom(internalPosition)\n ? {\n top: tallestBoundingContainer.top,\n height: tallestBoundingContainer.height,\n left: viewport.left,\n width: viewport.width,\n }\n : viewport;\n\n const optimizedRect = fitIntoContainer(rect, boundingContainer);\n\n // If largest possible rect is shorter than original - set body scroll.\n const scrollable = optimizedRect.height < rect.height;\n\n return { internalPosition, rect: optimizedRect, scrollable };\n}\n\nfunction getBestOption(option1: CandidatePosition, option2: CandidatePosition | null) {\n // Within calculatePosition, the only case where option2 will not be defined will be in the first call.\n // The only case where the visibleArea of an option will be null is when the popover is totally outside of the viewport.\n if (!option2?.visibleArea) {\n return option1;\n }\n if (!option1.visibleArea) {\n return option2;\n }\n // Only if none of the two options overflows horizontally, choose the best based on the visible height.\n if (option1.visibleArea.width === option2.visibleArea.width) {\n return option1.visibleArea.height > option2.visibleArea.height ? option1 : option2;\n }\n // Otherwise, choose the option that is less cut off horizontally.\n return option1.visibleArea.width > option2.visibleArea.width ? option1 : option2;\n}\n\nexport function getOffsetDimensions(element: HTMLElement) {\n return { offsetHeight: element.offsetHeight, offsetWidth: element.offsetWidth };\n}\n\nexport function getDimensions(element: HTMLElement) {\n const computedStyle = getComputedStyle(element);\n return {\n width: parseFloat(computedStyle.width),\n height: parseFloat(computedStyle.height),\n };\n}\n\nfunction isTopOrBottom(internalPosition: InternalPosition) {\n return ['top', 'bottom'].includes(internalPosition.split('-')[0]);\n}\n"]}