@spscommerce/positioning 5.0.0-rc1 → 5.0.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.
package/lib/index.esm.js DELETED
@@ -1,511 +0,0 @@
1
- import 'intersection-observer';
2
- import { lockedToAnimationFrames, onNextTick } from '@spscommerce/utils';
3
- import { EventEmitter } from 'eventemitter3';
4
-
5
- /** These are the possibilities for the `position` option of
6
- * `PositioningOptions` when using `relativeTo`. */
7
- var Position;
8
- (function (Position) {
9
- Position["TOP_LEFT"] = "top left";
10
- Position["TOP_MIDDLE"] = "top middle";
11
- Position["TOP_RIGHT"] = "top right";
12
- Position["RIGHT_TOP"] = "right top";
13
- Position["RIGHT_MIDDLE"] = "right middle";
14
- Position["RIGHT_BOTTOM"] = "right bottom";
15
- Position["BOTTOM_RIGHT"] = "bottom right";
16
- Position["BOTTOM_MIDDLE"] = "bottom middle";
17
- Position["BOTTOM_LEFT"] = "bottom left";
18
- Position["LEFT_BOTTOM"] = "left bottom";
19
- Position["LEFT_MIDDLE"] = "left middle";
20
- Position["LEFT_TOP"] = "left top";
21
- })(Position || (Position = {}));
22
-
23
- /** Indicates which point on a positioned element it should be positioned by. */
24
- var PositionAnchor;
25
- (function (PositionAnchor) {
26
- PositionAnchor["TOP_LEFT"] = "top left";
27
- PositionAnchor["TOP_RIGHT"] = "top right";
28
- PositionAnchor["BOTTOM_LEFT"] = "bottom left";
29
- PositionAnchor["BOTTOM_RIGHT"] = "bottom right";
30
- })(PositionAnchor || (PositionAnchor = {}));
31
-
32
- var DEFAULT_POSITIONING_OPTIONS = {
33
- anchor: PositionAnchor.TOP_LEFT,
34
- offsets: [],
35
- position: Position.TOP_LEFT,
36
- };
37
-
38
- /*! *****************************************************************************
39
- Copyright (c) Microsoft Corporation.
40
-
41
- Permission to use, copy, modify, and/or distribute this software for any
42
- purpose with or without fee is hereby granted.
43
-
44
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
45
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
46
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
47
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
48
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
49
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
50
- PERFORMANCE OF THIS SOFTWARE.
51
- ***************************************************************************** */
52
-
53
- var __assign = function() {
54
- __assign = Object.assign || function __assign(t) {
55
- for (var s, i = 1, n = arguments.length; i < n; i++) {
56
- s = arguments[i];
57
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
58
- }
59
- return t;
60
- };
61
- return __assign.apply(this, arguments);
62
- };
63
-
64
- function __decorate(decorators, target, key, desc) {
65
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
66
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
67
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
68
- return c > 3 && r && Object.defineProperty(target, key, r), r;
69
- }
70
-
71
- function __metadata(metadataKey, metadataValue) {
72
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
73
- }
74
-
75
- function __values(o) {
76
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
77
- if (m) return m.call(o);
78
- if (o && typeof o.length === "number") return {
79
- next: function () {
80
- if (o && i >= o.length) o = void 0;
81
- return { value: o && o[i++], done: !o };
82
- }
83
- };
84
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
85
- }
86
-
87
- function __read(o, n) {
88
- var m = typeof Symbol === "function" && o[Symbol.iterator];
89
- if (!m) return o;
90
- var i = m.call(o), r, ar = [], e;
91
- try {
92
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
93
- }
94
- catch (error) { e = { error: error }; }
95
- finally {
96
- try {
97
- if (r && !r.done && (m = i["return"])) m.call(i);
98
- }
99
- finally { if (e) throw e.error; }
100
- }
101
- return ar;
102
- }
103
-
104
- var NAVBAR_HEIGHT = 60;
105
- /** This class follows a singleton pattern where you can create an instance by calling
106
- * `new PositioningService()` if you wish, but all instance methods pass through to
107
- * static methods, so there's really only one set of elements being positioned that's
108
- * shared between all instances and static usages of PositioningService.
109
- */
110
- var PositioningService = /** @class */ (function () {
111
- function PositioningService() {
112
- }
113
- PositioningService.on = function (eventName, handler) {
114
- PositioningService.events.on(eventName, handler);
115
- };
116
- PositioningService.off = function (eventName, handler) {
117
- PositioningService.events.off(eventName, handler);
118
- };
119
- PositioningService.once = function (eventName, handler) {
120
- PositioningService.events.once(eventName, handler);
121
- };
122
- PositioningService.onViewportIntersection = function (entries) {
123
- var e_1, _a;
124
- var _loop_1 = function (entry) {
125
- // This is how you make a copy of an IntersectionObserverEntry that
126
- // you can modify; it's read-only and incompatible with simply using
127
- // destructuring or Object.assign. Before sending it out we make the
128
- // copy read-only by passing it to Object.freeze
129
- var entryCopy = Object.keys(IntersectionObserverEntry.prototype).reduce(function (copy, key) {
130
- var _a;
131
- return (__assign(__assign({}, copy), (_a = {}, _a[key] = entry[key], _a)));
132
- }, {});
133
- entryCopy.rootBounds = entryCopy.rootBounds || PositioningService.getRootBounds();
134
- PositioningService.events.emit('viewportIntersection', Object.freeze(entryCopy));
135
- };
136
- try {
137
- for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
138
- var entry = entries_1_1.value;
139
- _loop_1(entry);
140
- }
141
- }
142
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
143
- finally {
144
- try {
145
- if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
146
- }
147
- finally { if (e_1) throw e_1.error; }
148
- }
149
- };
150
- PositioningService.onElementIntersection = function (target, intersectingWith) {
151
- PositioningService.events.emit('elementIntersection', { target: target, intersectingWith: intersectingWith });
152
- };
153
- PositioningService.getRootBounds = function () {
154
- var rootWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
155
- var rootHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
156
- return {
157
- x: 0,
158
- y: NAVBAR_HEIGHT,
159
- left: 0,
160
- top: NAVBAR_HEIGHT,
161
- right: rootWidth,
162
- bottom: rootHeight,
163
- height: rootHeight - NAVBAR_HEIGHT,
164
- width: rootWidth,
165
- };
166
- };
167
- PositioningService.clearStyles = function (element) {
168
- Object.assign(element.style, {
169
- position: '',
170
- width: '',
171
- top: '',
172
- left: '',
173
- right: '',
174
- bottom: '',
175
- visibility: '',
176
- zIndex: '',
177
- });
178
- };
179
- PositioningService.fixElementPosition = function (element, options) {
180
- if (!this.elements.has(element)) {
181
- return;
182
- }
183
- var opts = options || this.elements.get(element);
184
- var rootBounds = this.getRootBounds();
185
- var positionedElementBounds = element.getBoundingClientRect();
186
- var relativeTargetBounds = opts.relativeTo.getBoundingClientRect();
187
- var width = (opts.useRelativeTargetWidth
188
- ? relativeTargetBounds
189
- : positionedElementBounds).width;
190
- Object.assign(element.style, {
191
- minWidth: width + "px",
192
- position: 'fixed',
193
- zIndex: opts.zIndex || '',
194
- });
195
- var top;
196
- var left;
197
- var positionRelativeToTarget = opts.position.split(' ');
198
- var offsetA = opts.offsets[0] || 0;
199
- var offsetB = opts.offsets[1] || 0;
200
- top = 0;
201
- left = 0;
202
- switch (positionRelativeToTarget[0]) {
203
- case 'top':
204
- top = relativeTargetBounds.top - positionedElementBounds.height - offsetA;
205
- break;
206
- case 'left':
207
- left = relativeTargetBounds.left - width - offsetA;
208
- break;
209
- case 'right':
210
- left = relativeTargetBounds.right + offsetA;
211
- break;
212
- case 'bottom':
213
- top = relativeTargetBounds.bottom + offsetA;
214
- break;
215
- default:
216
- throw new Error(opts.position + " is not a valid position");
217
- }
218
- switch (positionRelativeToTarget[1]) {
219
- case 'left':
220
- left = relativeTargetBounds.left - offsetB;
221
- break;
222
- case 'top':
223
- top = relativeTargetBounds.top - offsetB;
224
- break;
225
- case 'middle':
226
- if (positionRelativeToTarget[0] === 'top'
227
- || positionRelativeToTarget[0] === 'bottom') {
228
- left = relativeTargetBounds.left
229
- + relativeTargetBounds.width / 2
230
- - width / 2
231
- + offsetB;
232
- }
233
- else if (positionRelativeToTarget[0] === 'left'
234
- || positionRelativeToTarget[0] === 'right') {
235
- top = relativeTargetBounds.top
236
- + relativeTargetBounds.height / 2
237
- - positionedElementBounds.height / 2
238
- + offsetB;
239
- }
240
- break;
241
- case 'bottom':
242
- top = relativeTargetBounds.bottom - positionedElementBounds.height + offsetB;
243
- break;
244
- case 'right':
245
- left = relativeTargetBounds.right - width + offsetB;
246
- break;
247
- default:
248
- throw new Error(opts.position + " is not a valid position");
249
- }
250
- var bottom = top + positionedElementBounds.height;
251
- var right = left + positionedElementBounds.width;
252
- var topPx = Math.round(top) + "px";
253
- var bottomPx = Math.round(rootBounds.bottom - bottom) + "px";
254
- var leftPx = Math.round(left) + "px";
255
- var rightPx = Math.round(rootBounds.right - right) + "px";
256
- switch (opts.anchor) {
257
- case PositionAnchor.TOP_LEFT:
258
- Object.assign(element.style, {
259
- top: topPx,
260
- bottom: 'auto',
261
- left: leftPx,
262
- right: 'auto',
263
- });
264
- break;
265
- case PositionAnchor.TOP_RIGHT:
266
- Object.assign(element.style, {
267
- top: topPx,
268
- bottom: 'auto',
269
- left: 'auto',
270
- right: rightPx,
271
- });
272
- break;
273
- case PositionAnchor.BOTTOM_LEFT:
274
- Object.assign(element.style, {
275
- top: 'auto',
276
- bottom: bottomPx,
277
- left: leftPx,
278
- right: 'auto',
279
- });
280
- break;
281
- case PositionAnchor.BOTTOM_RIGHT:
282
- Object.assign(element.style, {
283
- top: 'auto',
284
- bottom: bottomPx,
285
- left: 'auto',
286
- right: rightPx,
287
- });
288
- break;
289
- default:
290
- throw new Error(opts.anchor + " is not a valid positioning anchor");
291
- }
292
- };
293
- PositioningService.update = function (event) {
294
- var e_2, _a;
295
- var elements = PositioningService.elements.entries();
296
- var updatedElements = [];
297
- try {
298
- for (var elements_1 = __values(elements), elements_1_1 = elements_1.next(); !elements_1_1.done; elements_1_1 = elements_1.next()) {
299
- var _b = __read(elements_1_1.value, 2), element = _b[0], options = _b[1];
300
- if (!event
301
- || event.target === document
302
- || event.target === options.scrollParent
303
- || event.target === window) {
304
- PositioningService.fixElementPosition(element, options);
305
- PositioningService.checkCollisions(element, updatedElements);
306
- updatedElements.push(element);
307
- }
308
- }
309
- }
310
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
311
- finally {
312
- try {
313
- if (elements_1_1 && !elements_1_1.done && (_a = elements_1.return)) _a.call(elements_1);
314
- }
315
- finally { if (e_2) throw e_2.error; }
316
- }
317
- };
318
- PositioningService.checkCollisions = function (element, otherElements) {
319
- var e_3, _a;
320
- var otherElementsList = otherElements || Array.from(PositioningService.elements.keys());
321
- var bounds = element.getBoundingClientRect();
322
- var collisions = [];
323
- try {
324
- for (var otherElementsList_1 = __values(otherElementsList), otherElementsList_1_1 = otherElementsList_1.next(); !otherElementsList_1_1.done; otherElementsList_1_1 = otherElementsList_1.next()) {
325
- var otherElement = otherElementsList_1_1.value;
326
- if (otherElement !== element) {
327
- var otherBounds = otherElement.getBoundingClientRect();
328
- if (otherBounds.left <= bounds.right
329
- && otherBounds.right >= bounds.left
330
- && otherBounds.top <= bounds.bottom
331
- && otherBounds.bottom >= bounds.top) {
332
- collisions.push(otherElement);
333
- }
334
- }
335
- }
336
- }
337
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
338
- finally {
339
- try {
340
- if (otherElementsList_1_1 && !otherElementsList_1_1.done && (_a = otherElementsList_1.return)) _a.call(otherElementsList_1);
341
- }
342
- finally { if (e_3) throw e_3.error; }
343
- }
344
- if (collisions.length) {
345
- PositioningService.onElementIntersection(element, collisions);
346
- }
347
- };
348
- /** Returns `true` if the element is currently being positioned by the service
349
- * and `false` otherwise. */
350
- PositioningService.isPositioned = function (element) {
351
- return PositioningService.elements.has(element);
352
- };
353
- /** Returns the options currently beinng used for positioning the element,
354
- * if it is indeed being positioned by the service. */
355
- PositioningService.getPositioningOptions = function (element) {
356
- if (PositioningService.elements.has(element)) {
357
- return PositioningService.elements.get(element);
358
- }
359
- return null;
360
- };
361
- /** PositioningService will start controlling the position of the given element. */
362
- PositioningService.position = function (element, options) {
363
- if (options === void 0) { options = {}; }
364
- var opts = __assign(__assign({}, DEFAULT_POSITIONING_OPTIONS), options);
365
- var e = element;
366
- while (e !== document.body) {
367
- e = e.parentElement;
368
- if (e.classList.contains('sps-focused-task')) {
369
- opts.scrollParent = e;
370
- if (!PositioningService.registeredScrollParents.has(e)) {
371
- PositioningService.registeredScrollParents.add(e);
372
- e.addEventListener('scroll', PositioningService.update);
373
- }
374
- break;
375
- }
376
- }
377
- // eslint-disable-next-line no-param-reassign
378
- element.style.visibility = 'hidden';
379
- if (PositioningService.elements.size === 0) {
380
- window.addEventListener('resize', PositioningService.update);
381
- window.addEventListener('scroll', PositioningService.update);
382
- }
383
- if (!options.relativeTo) {
384
- throw new Error('You must provide an element for the relativeTo option to position an element.');
385
- }
386
- PositioningService.elements.set(element, options);
387
- onNextTick(function () {
388
- PositioningService.fixElementPosition(element);
389
- PositioningService.viewportObserver.observe(element);
390
- // eslint-disable-next-line no-param-reassign
391
- element.style.visibility = '';
392
- PositioningService.checkCollisions(element);
393
- });
394
- };
395
- /** PositioningService will stop controlling the position of the given element. */
396
- PositioningService.release = function (element) {
397
- if (PositioningService.elements.has(element)) {
398
- PositioningService.clearStyles(element);
399
- var options = PositioningService.elements.get(element);
400
- if (options.scrollParent) {
401
- options.scrollParent.removeEventListener('scroll', PositioningService.update);
402
- PositioningService.registeredScrollParents.delete(options.scrollParent);
403
- }
404
- PositioningService.elements.delete(element);
405
- if (PositioningService.elements.size === 0) {
406
- window.removeEventListener('resize', PositioningService.update);
407
- window.removeEventListener('scroll', PositioningService.update);
408
- }
409
- PositioningService.viewportObserver.unobserve(element);
410
- }
411
- };
412
- PositioningService.releaseAll = function () {
413
- var e_4, _a;
414
- try {
415
- for (var _b = __values(PositioningService.elements.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
416
- var element = _c.value;
417
- PositioningService.release(element);
418
- }
419
- }
420
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
421
- finally {
422
- try {
423
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
424
- }
425
- finally { if (e_4) throw e_4.error; }
426
- }
427
- };
428
- /** PositioningService will refresh the positioning of the given element */
429
- PositioningService.refresh = function (element) {
430
- if (PositioningService.elements.has(element)) {
431
- PositioningService.clearStyles(element);
432
- PositioningService.fixElementPosition(element);
433
- PositioningService.checkCollisions(element);
434
- }
435
- };
436
- PositioningService.refreshAll = function () {
437
- var e_5, _a;
438
- try {
439
- for (var _b = __values(PositioningService.elements.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
440
- var element = _c.value;
441
- PositioningService.refresh(element);
442
- }
443
- }
444
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
445
- finally {
446
- try {
447
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
448
- }
449
- finally { if (e_5) throw e_5.error; }
450
- }
451
- };
452
- /** Update the positioning options of a currently positioned element */
453
- PositioningService.reposition = function (element, newOptions) {
454
- if (PositioningService.elements.has(element)) {
455
- var options = PositioningService.elements.get(element);
456
- PositioningService.elements.set(element, Object.assign(options, newOptions));
457
- PositioningService.refresh(element);
458
- }
459
- };
460
- /* eslint class-methods-use-this: "off" */
461
- PositioningService.prototype.on = function (eventName, handler) {
462
- PositioningService.on(eventName, handler);
463
- };
464
- PositioningService.prototype.off = function (eventName, handler) {
465
- PositioningService.off(eventName, handler);
466
- };
467
- PositioningService.prototype.once = function (eventName, handler) {
468
- PositioningService.once(eventName, handler);
469
- };
470
- PositioningService.prototype.isPositioned = function (element) {
471
- return PositioningService.isPositioned(element);
472
- };
473
- PositioningService.prototype.getPositioningOptions = function (element) {
474
- return PositioningService.getPositioningOptions(element);
475
- };
476
- PositioningService.prototype.position = function (element, options) {
477
- if (options === void 0) { options = {}; }
478
- PositioningService.position(element, options);
479
- };
480
- PositioningService.prototype.release = function (element) {
481
- PositioningService.release(element);
482
- };
483
- PositioningService.prototype.releaseAll = function () {
484
- PositioningService.releaseAll();
485
- };
486
- PositioningService.prototype.refresh = function (element) {
487
- PositioningService.refresh(element);
488
- };
489
- PositioningService.prototype.refreshAll = function () {
490
- PositioningService.refreshAll();
491
- };
492
- PositioningService.prototype.reposition = function (element, newOptions) {
493
- PositioningService.reposition(element, newOptions);
494
- };
495
- PositioningService.elements = new Map();
496
- PositioningService.registeredScrollParents = new Set();
497
- PositioningService.events = new EventEmitter();
498
- PositioningService.viewportObserver = new IntersectionObserver(PositioningService.onViewportIntersection, {
499
- rootMargin: "-" + NAVBAR_HEIGHT + "px 0px 0px",
500
- threshold: 1,
501
- });
502
- __decorate([
503
- lockedToAnimationFrames,
504
- __metadata("design:type", Function),
505
- __metadata("design:paramtypes", [Event]),
506
- __metadata("design:returntype", void 0)
507
- ], PositioningService, "update", null);
508
- return PositioningService;
509
- }());
510
-
511
- export { DEFAULT_POSITIONING_OPTIONS, Position, PositionAnchor, PositioningService };
@@ -1 +0,0 @@
1
- export {};