forked from cloudscape-design/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositions.ts
More file actions
361 lines (330 loc) · 13 KB
/
positions.ts
File metadata and controls
361 lines (330 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { BoundingBox, Dimensions, InternalPosition, PopoverProps, Rect } from '../interfaces';
// A structure describing how the popover should be positioned
interface CalculatedPosition {
scrollable?: boolean;
internalPosition: InternalPosition;
rect: BoundingBox;
}
interface ElementGroup {
body: Dimensions;
trigger: BoundingBox;
arrow: Dimensions;
}
const ARROW_OFFSET = 12;
export const PRIORITY_MAPPING: Record<PopoverProps.Position, InternalPosition[]> = {
top: [
'top-center',
'top-right',
'top-left',
'bottom-center',
'bottom-right',
'bottom-left',
'right-top',
'right-bottom',
'left-top',
'left-bottom',
],
bottom: [
'bottom-center',
'bottom-right',
'bottom-left',
'top-center',
'top-right',
'top-left',
'right-top',
'right-bottom',
'left-top',
'left-bottom',
],
left: [
'left-top',
'left-bottom',
'right-top',
'right-bottom',
'bottom-center',
'top-center',
'bottom-left',
'top-left',
'bottom-right',
'top-right',
],
right: [
'right-top',
'right-bottom',
'left-top',
'left-bottom',
'bottom-center',
'top-center',
'bottom-right',
'top-right',
'bottom-left',
'top-left',
],
};
const RECTANGLE_CALCULATIONS: Record<InternalPosition, (r: ElementGroup) => BoundingBox> = {
'top-center': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart - body.blockSize - arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize / 2 - body.inlineSize / 2,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'top-right': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart - body.blockSize - arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize / 2 - ARROW_OFFSET - arrow.inlineSize / 2,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'top-left': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart - body.blockSize - arrow.blockSize,
insetInlineStart:
trigger.insetInlineStart + trigger.inlineSize / 2 + ARROW_OFFSET + arrow.inlineSize / 2 - body.inlineSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'bottom-center': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart + trigger.blockSize + arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize / 2 - body.inlineSize / 2,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'bottom-right': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart + trigger.blockSize + arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize / 2 - ARROW_OFFSET - arrow.inlineSize / 2,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'bottom-left': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart + trigger.blockSize + arrow.blockSize,
insetInlineStart:
trigger.insetInlineStart + trigger.inlineSize / 2 + ARROW_OFFSET + arrow.inlineSize / 2 - body.inlineSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'right-top': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart + trigger.blockSize / 2 - ARROW_OFFSET - arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize + arrow.blockSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'right-bottom': ({ body, trigger, arrow }) => {
return {
insetBlockStart:
trigger.insetBlockStart + trigger.blockSize / 2 - body.blockSize + ARROW_OFFSET + arrow.blockSize,
insetInlineStart: trigger.insetInlineStart + trigger.inlineSize + arrow.blockSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'left-top': ({ body, trigger, arrow }) => {
return {
insetBlockStart: trigger.insetBlockStart + trigger.blockSize / 2 - ARROW_OFFSET - arrow.blockSize,
insetInlineStart: trigger.insetInlineStart - body.inlineSize - arrow.blockSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
'left-bottom': ({ body, trigger, arrow }) => {
return {
insetBlockStart:
trigger.insetBlockStart + trigger.blockSize / 2 - body.blockSize + ARROW_OFFSET + arrow.blockSize,
insetInlineStart: trigger.insetInlineStart - body.inlineSize - arrow.blockSize,
inlineSize: body.inlineSize,
blockSize: body.blockSize,
};
},
};
function fitIntoContainer(inner: BoundingBox, outer: BoundingBox): BoundingBox {
let { insetInlineStart, inlineSize, insetBlockStart, blockSize } = inner;
// Adjust left boundary.
if (insetInlineStart < outer.insetInlineStart) {
inlineSize = insetInlineStart + inlineSize - outer.insetInlineStart;
insetInlineStart = outer.insetInlineStart;
}
// Adjust right boundary.
else if (insetInlineStart + inlineSize > outer.insetInlineStart + outer.inlineSize) {
inlineSize = outer.insetInlineStart + outer.inlineSize - insetInlineStart;
}
// Adjust top boundary.
if (insetBlockStart < outer.insetBlockStart) {
blockSize = insetBlockStart + blockSize - outer.insetBlockStart;
insetBlockStart = outer.insetBlockStart;
}
// Adjust bottom boundary.
else if (insetBlockStart + blockSize > outer.insetBlockStart + outer.blockSize) {
blockSize = outer.insetBlockStart + outer.blockSize - insetBlockStart;
}
return { insetInlineStart, inlineSize, insetBlockStart, blockSize };
}
function getTallestRect(rect1: BoundingBox, rect2: BoundingBox): BoundingBox {
return rect1.blockSize >= rect2.blockSize ? rect1 : rect2;
}
function getIntersection(rectangles: BoundingBox[]): BoundingBox | null {
let boundingBox: BoundingBox | null = null;
for (const currentRect of rectangles) {
if (!boundingBox) {
boundingBox = currentRect;
continue;
}
const insetInlineStart = Math.max(boundingBox.insetInlineStart, currentRect.insetInlineStart);
const insetBlockStart = Math.max(boundingBox.insetBlockStart, currentRect.insetBlockStart);
const insetInlineEnd = Math.min(
boundingBox.insetInlineStart + boundingBox.inlineSize,
currentRect.insetInlineStart + currentRect.inlineSize
);
const insetBlockEnd = Math.min(
boundingBox.insetBlockStart + boundingBox.blockSize,
currentRect.insetBlockStart + currentRect.blockSize
);
if (insetInlineEnd < insetInlineStart || insetBlockEnd < insetBlockStart) {
return null;
}
boundingBox = {
insetInlineStart,
insetBlockStart,
inlineSize: insetInlineEnd - insetInlineStart,
blockSize: insetBlockEnd - insetBlockStart,
};
}
return boundingBox;
}
/**
* Returns the area of the intersection of passed in rectangles or a null, if there is no intersection
*/
export function intersectRectangles(rectangles: BoundingBox[]): number | null {
const boundingBox: BoundingBox | null = getIntersection(rectangles);
return boundingBox && boundingBox.blockSize * boundingBox.inlineSize;
}
type CandidatePosition = CalculatedPosition & { visibleArea: BoundingBox | null };
/**
* A functions that returns the correct popover position based on screen dimensions.
*/
export function calculatePosition({
preferredPosition,
fixedInternalPosition,
trigger,
arrow,
body,
container,
viewport,
// the popover is only bound by the viewport if it is rendered in a portal
renderWithPortal,
allowVerticalOverflow,
minVisibleBlockSize,
}: {
preferredPosition: PopoverProps.Position;
fixedInternalPosition?: InternalPosition;
trigger: BoundingBox;
arrow: Dimensions;
body: Dimensions;
container: BoundingBox;
viewport: BoundingBox;
// the popover is only bound by the viewport if it is rendered in a portal
renderWithPortal?: boolean;
allowVerticalOverflow?: boolean;
minVisibleBlockSize?: number;
}): CalculatedPosition {
let bestOption: CandidatePosition | null = null;
// If a fixed internal position is passed, only consider this one.
const preferredInternalPositions = fixedInternalPosition
? [fixedInternalPosition]
: PRIORITY_MAPPING[preferredPosition];
// Attempt to position the popover based on the priority list for this position.
for (const internalPosition of preferredInternalPositions) {
const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
const visibleArea = renderWithPortal
? getIntersection([rect, viewport])
: getIntersection([rect, viewport, container]);
// When min visible block size is set, the popover is considered fitting the container if the available space
// is the same or larger than min allowed, even if it means the scrollbar is needed.
const fitsBlockSize =
minVisibleBlockSize === undefined
? visibleArea && visibleArea.blockSize === body.blockSize
: visibleArea && visibleArea.blockSize >= Math.min(body.blockSize, minVisibleBlockSize);
const fitsInlineSize = visibleArea && visibleArea.inlineSize === body.inlineSize;
if (fitsBlockSize && fitsInlineSize) {
const scrollable = visibleArea && visibleArea.blockSize < body.blockSize;
return { internalPosition, rect: scrollable ? fitIntoContainer(rect, viewport) : rect, scrollable };
}
const newOption = { rect, internalPosition, visibleArea };
bestOption = getBestOption(newOption, bestOption);
}
// Use best possible placement.
const internalPosition = bestOption?.internalPosition || 'right-top';
// Get default rect for that placement.
const rect = RECTANGLE_CALCULATIONS[internalPosition]({ body, trigger, arrow });
// Get largest possible rect that fits into the viewport or container.
// We allow the popover to overflow the viewport if allowVerticalOverflow is true _and_ the popover will be anchored to the top or the bottom.
// 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.
const tallestBoundingContainer = getTallestRect(viewport, container);
const boundingContainer =
allowVerticalOverflow && isTopOrBottom(internalPosition)
? {
insetBlockStart: tallestBoundingContainer.insetBlockStart,
blockSize: tallestBoundingContainer.blockSize,
insetInlineStart: viewport.insetInlineStart,
inlineSize: viewport.inlineSize,
}
: viewport;
const optimizedRect = fitIntoContainer(rect, boundingContainer);
// If largest possible rect is shorter than original - set body scroll.
const scrollable = optimizedRect.blockSize < rect.blockSize;
return { internalPosition, rect: optimizedRect, scrollable };
}
function getBestOption(option1: CandidatePosition, option2: CandidatePosition | null) {
// Within calculatePosition, the only case where option2 will not be defined will be in the first call.
// The only case where the visibleArea of an option will be null is when the popover is totally outside of the viewport.
if (!option2?.visibleArea) {
return option1;
}
if (!option1.visibleArea) {
return option2;
}
// Only if none of the two options overflows horizontally, choose the best based on the visible height.
if (option1.visibleArea.inlineSize === option2.visibleArea.inlineSize) {
return option1.visibleArea.blockSize > option2.visibleArea.blockSize ? option1 : option2;
}
// Otherwise, choose the option that is less cut off horizontally.
return option1.visibleArea.inlineSize > option2.visibleArea.inlineSize ? option1 : option2;
}
export function getOffsetDimensions(element: HTMLElement) {
return { offsetHeight: element.offsetHeight, offsetWidth: element.offsetWidth };
}
export function getDimensions(element: HTMLElement) {
const computedStyle = getComputedStyle(element);
return {
inlineSize: parseFloat(computedStyle.inlineSize),
blockSize: parseFloat(computedStyle.blockSize),
};
}
function isTopOrBottom(internalPosition: InternalPosition) {
return ['top', 'bottom'].includes(internalPosition.split('-')[0]);
}
export function clampRectStart(rect: Rect, bounds: BoundingBox) {
const parentInlineEnd = bounds.insetInlineStart + bounds.inlineSize;
const parentBlockEnd = bounds.insetBlockStart + bounds.blockSize;
rect.insetInlineStart = Math.max(bounds.insetInlineStart, Math.min(rect.insetInlineStart, parentInlineEnd));
rect.insetBlockStart = Math.max(bounds.insetBlockStart, Math.min(rect.insetBlockStart, parentBlockEnd));
return rect;
}
export function isCenterOutside(child: Rect, parent: Rect) {
const childCenter = child.insetBlockStart + child.blockSize / 2;
const overflowsBlockStart = childCenter < parent.insetBlockStart;
const overflowsBlockEnd = childCenter > parent.insetBlockEnd;
return overflowsBlockStart || overflowsBlockEnd;
}