-
Notifications
You must be signed in to change notification settings - Fork 1
/
glomex-dialog.js
1225 lines (1115 loc) · 40.5 KB
/
glomex-dialog.js
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { RotataToFullscreen } from './rotate-to-fullscreen.js';
const NON_VISIBLE_WIDTH = window.innerWidth < 720 ? 320 : 640;
const DEFAULT_DOCK_TARGET_INSET = '0px 10px auto auto';
const DEFAULT_TRANSITION_DURATION = 300;
const LIGHTBOX_Z_INDEX = 2147483647;
const DOCK_Z_INDEX = 2147483646;
const MIN_DOCK_WIDTH = 192;
const MAX_DOCK_WIDTH = 400;
const PHONE_MAX_WIDTH = 480;
const STICKY_TOP_SELECTOR_THRESHOLD = 200;
let allowRotateToFullscreen = false;
if (window.matchMedia(
`(max-device-width: ${PHONE_MAX_WIDTH}px) and (pointer: coarse)`,
).matches) {
allowRotateToFullscreen = true;
}
const updateViewPortWidth = (element) => {
let viewPortWidth = window.innerWidth * 0.3;
if (viewPortWidth < MIN_DOCK_WIDTH) {
viewPortWidth = MIN_DOCK_WIDTH;
} else if (viewPortWidth > MAX_DOCK_WIDTH) {
viewPortWidth = MAX_DOCK_WIDTH;
}
element.shadowRoot.querySelector('.dock-target').style.width = `${viewPortWidth}px`;
};
const updatePlaceholderAspectRatio = (element, aspectRatio) => {
element.shadowRoot.querySelector('.placeholder.aspect-ratio-box')
.style.paddingTop = `${(1 / aspectRatio) * 100}%`;
};
const updateDockAspectRatio = (element, aspectRatio) => {
element.shadowRoot.querySelector('.dock-target .aspect-ratio-box')
.style.paddingTop = `${(1 / aspectRatio) * 100}%`;
};
const updateDockStickyAspectRatio = (element, aspectRatio) => {
element.shadowRoot.querySelector('.dock-sticky-target .aspect-ratio-box')
.style.paddingTop = `${(1 / aspectRatio) * 100}%`;
};
const getAlternativeDockTarget = (element) => {
const dockTarget = element.getAttribute('dock-target');
let dockTargetElements;
if (dockTarget) {
try {
dockTargetElements = document.querySelectorAll(dockTarget);
} catch (e) {
// invalid selector strings should allow falling back to the default handling
return null;
}
if (dockTargetElements.length === 0) return null;
// pick the innermost node (querySelectorAll sorts by order in DOM)
const dockTargetElement = dockTargetElements[dockTargetElements.length - 1];
const intersection = getViewportIntersection(dockTargetElement);
if (intersection && intersection.width > 0 && intersection.height > 0) {
return dockTargetElement;
}
}
return null;
};
const getDefaultDockTarget = (element) => element.shadowRoot.querySelector('.dock-target');
const getDockStickyTarget = (element) => element.shadowRoot.querySelector('.dock-sticky-target');
const updateDockTargetState = (element) => {
const alternativeDockTarget = getAlternativeDockTarget(element);
if (alternativeDockTarget && !element.getAttribute('alternative-dock-target')) {
element.setAttribute('alternative-dock-target', '');
} else {
element.removeAttribute('alternative-dock-target');
}
};
const getAspectRatioFromStrings = (aspectRatioStrings = []) => {
aspectRatioStrings.push('16:9');
return aspectRatioStrings.map((aspectRatio) => {
if (!aspectRatio) return NaN;
const ratioSplit = aspectRatio.split(':');
if (ratioSplit.length === 1) return Number(aspectRatio);
return ratioSplit[0] / ratioSplit[1];
}).filter((aspectRatio) => !!aspectRatio)[0];
};
const moveFromTo = (element, {
from, to, animate = false, aspectRatio,
initialAspectRatio, downscale = false,
transitionDuration,
} = {}) => new Promise((resolve) => {
const fromRect = from.getBoundingClientRect();
const toRect = to.getBoundingClientRect();
const visualViewport = getVisualViewport();
const width = fromRect.width === 0 ? NON_VISIBLE_WIDTH : fromRect.width;
const height = fromRect.height === 0
? (NON_VISIBLE_WIDTH / initialAspectRatio)
: fromRect.height;
const toHeight = width / aspectRatio;
const elementInnerContainer = element.firstElementChild;
element.style.position = 'fixed';
element.style.width = `${width}px`;
element.style.height = `${height}px`;
element.style.transform = 'scale(1)';
element.style.top = `${fromRect.top + visualViewport.offsetTop}px`;
element.style.left = `${fromRect.left + visualViewport.offsetLeft}px`;
// avoid CLS further
element.style.display = 'grid';
const deltaX = toRect.left - fromRect.left;
const deltaY = toRect.top - fromRect.top;
const deltaScale = toRect.width / width;
const moveDialog = () => {
// avoid CLS (see setting to "display: grid" before requestAnimationFrame)
element.style.display = null;
element.style.height = `${toHeight}px`;
element.style.transform = `translate(${(deltaX / width) * 100}%, ${(deltaY / toHeight) * 100}%) scale(${deltaScale})`;
element.style.transitionProperty = 'transform';
element.style.transformOrigin = 'top left';
element.style.transitionTimingFunction = 'ease-out';
if (animate) {
element.style.transitionDuration = `${transitionDuration}ms`;
} else {
element.style.transitionDuration = null;
}
if (!downscale) {
// avoid as best as possible that the contained element is shortly scaled too large
// somehow width+scale is not applied at the same time when the motion is too fast
elementInnerContainer.style.transitionDuration = '0s';
elementInnerContainer.style.transitionProperty = 'transform';
elementInnerContainer.style.width = `${toRect.width}px`;
elementInnerContainer.style.height = `${toRect.height}px`;
elementInnerContainer.style.transform = `scale(${1 / deltaScale})`;
elementInnerContainer.style.transformOrigin = 'top left';
}
};
if (!animate) {
moveDialog();
resolve({
scale: downscale ? deltaScale : 1,
});
return;
}
window.requestAnimationFrame(() => {
moveDialog();
resolve({
scale: downscale ? deltaScale : 1,
});
});
});
const toPositions = (insetString) => {
const insetParts = insetString.split(' ');
if (!insetString) {
return undefined;
}
if (insetParts.length === 1) {
return {
top: insetParts[0], right: 'auto', bottom: 'auto', left: 'auto',
};
}
if (insetParts.length === 2) {
return {
top: insetParts[0], right: insetParts[1], bottom: insetParts[0], left: insetParts[1],
};
}
if (insetParts.length === 3) {
return {
top: insetParts[0], right: insetParts[1], bottom: insetParts[1], left: insetParts[2],
};
}
if (insetParts.length === 4) {
return {
top: insetParts[0], right: insetParts[1], bottom: insetParts[2], left: insetParts[3],
};
}
return undefined;
};
function pointerCoords(e) {
const coords = e.touches ? e.touches[0] : e;
return {
x: 'x' in coords ? coords.x : coords.clientX,
y: 'y' in coords ? coords.y : coords.clientY,
};
}
function getActiveElement(root) {
const activeEl = root.activeElement;
if (!activeEl) {
return null;
}
if (activeEl.shadowRoot) {
return getActiveElement(activeEl.shadowRoot);
}
return activeEl;
}
function isInDocument(element, document) {
let currentElement = element;
while (currentElement && currentElement.parentNode) {
if (currentElement.parentNode === document) {
return true;
} if (currentElement.parentNode instanceof window.DocumentFragment) {
currentElement = currentElement.parentNode.host;
} else {
currentElement = currentElement.parentNode;
}
}
return false;
}
/**
* A dialog web component that allows docking a video player or
* putting it in a lightbox. It allows implementing a similar
* feature as amp-video-docking but without using AMP.
*
* @attr {string} mode - Can take the values "hidden", "inline", "dock" or "lightbox".
* @attr {string} aspect-ratio - The aspect-ratio for the inline element. Default is 16:9
* @attr {string} dock-target - A dom-element with position:fixed where mode=dock should animate to
* @attr {string} dock-target-inset - Defines the position of the dock using inset
* @attr {string} dock-aspect-ratio - The aspect-ratio when the element is mode=dock
* @attr {string} dock-mode - When set to "sticky" it behaves similar to "position: sticky" in CSS
* (with a max width of 400px). If undefined docks the content to a corner.
* @attr {string} dock-sticky-target-top - The top distance for dock-mode=sticky in pixels
* (defaults to 0)
* @attr {string} dock-sticky-aspect-ratio - The aspect-ratio when the element is docked
* for dock-mode=sticky
* @attr {string} dock-downscale - Do you want to scale the element when mode=dock
*/
class GlomexDialogElement extends window.HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `<style>
:host {
display: block;
position: relative;
}
.dock-target {
position: fixed;
pointer-events: none;
visibility: hidden;
}
.dock-sticky-target {
position: fixed;
pointer-events: none;
visibility: hidden;
}
.aspect-ratio-box {
height: 0;
}
.placeholder {
display: block;
cursor: pointer;
}
.placeholder-inner {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: block;
}
.placeholder-default {
background-color: var(--placeholder-background-color, rgba(200, 200, 200, 0.8));
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' width='24px' version='1.1' viewBox='0 0 24 24'%3E%3Cg fill='none' fill-rule='evenodd' stroke='none' stroke-width='1'%3E%3Cg transform='scale(1, 1)'%3E%3Cpath d='M19,19 L5,19 L5,5 L12,5 L13,3 L5,3 L3,3 3,3.9 3,5 L3,19 L3,20.1 3,21 5,21 L19,21 L21,21 21,20.1 21,19 L21,12 L19,12 L19,19 Z M14,3 L14,5 L17.59,5 L7.76,14.83 L9.17,16.24 L19,6.41 L19,10 L21,10 L21,3 L14,3 Z' fill='%23fff' fill-rule='nonzero'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: center;
background-size: 10%;
width: 100%;
height: 100%;
}
:host([mode=hidden]) .dialog-content,
:host([mode=hidden]) .placeholder {
display: none;
}
.popover {
border: 0;
margin: 0;
padding: 0;
background: transparent;
display: block;
}
:host([mode=inline]) .popover {
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
overflow: visible;
}
.dialog-content {
display: block;
width: 100%;
height: 100%;
border: 0;
padding: 0;
margin: 0;
background: transparent;
will-change: transform, transition, width, height, top, left, opacity;
}
.dialog-inner-wrapper {
width: 100%;
height: 100%;
position: absolute;
top 0;
left: 0;
max-width: 100%;
will-change: transition, transform, width, height;
}
/*
fixes an issue with fullscreen in safari,
"will-change: transform" lets external elements shine through
*/
.dialog-content:-webkit-full-screen-ancestor:not(iframe),
.dialog-inner-wrapper:-webkit-full-screen-ancestor:not(iframe) {
will-change: auto;
}
.drag-handle {
position: absolute;
top: 0;
left: 0;
width: 1em;
height: 1em;
padding: 0.25em;
fill-color: white;
border-radius: 2px;
background-color: rgba(0, 0, 0, 0.7);
transition: background 300ms ease-in-out, opacity 300ms ease-in-out;
opacity: 0;
z-index: 1;
}
:host([mode=dock]) .dialog-content {
position: absolute;
}
:host([alternative-dock-target]) .drag-handle,
:host([dock-mode=sticky]) .drag-handle {
display: none;
}
.drag-handle:hover, .drag-handle:active {
background-color: rgba(0, 0, 0, 0.4);
}
.drag-handle:active {
opacity: 1;
cursor: move;
}
.drag-handle svg {
fill: white;
width: 100%;
height: 100%;
}
:host([mode=dock]) .dialog-content:hover .drag-handle {
opacity: 1;
cursor: move;
}
.dialog-content ::slotted([slot=dock-background]) {
visibility: hidden;
}
.dialog-content slot[name="dock-background"] {
touch-action: none;
cursor: move;
position: absolute;
display: block;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
:host([dock-mode=sticky]) slot[name="dock-background"] {
cursor: unset;
}
:host([mode=dock]) .dialog-content ::slotted([slot=dock-background]) {
visibility: visible;
}
:host([mode=dock]) .dialog-content {
z-index: ${DOCK_Z_INDEX};
}
:host([mode=lightbox]) .dialog-content {
display: flex;
max-height: 100dvh;
max-width: 100dvw;
align-items: center;
justify-content: center;
animation-name: fade-in;
animation-duration: 200ms;
animation-timing-function: ease-in;
position: fixed;
top: 0;
left: 0;
z-index: ${LIGHTBOX_Z_INDEX};
overflow: hidden;
}
:host([mode=lightbox]) .dialog-content::backdrop {
width: 100%;
height: 100%;
background: var(--lightbox-background, rgba(0, 0, 0, 0.5));
backdrop-filter: blur(4px) saturate(50%);
}
:host([mode=lightbox]) .dialog-content slot[name="dock-background"] {
display: none;
}
:host([mode=lightbox]) .dialog-inner-wrapper {
max-width: 80%;
position: unset;
height: unset;
}
@media (max-width: 1024px) {
:host([mode=lightbox]) .dialog-inner-wrapper {
max-width: 95%;
}
}
:host([mode=lightbox]) slot[name="dialog-element"] {
display: flex;
aspect-ratio: 16 / 9;
margin: 0 auto;
max-height: 100dvh;
}
@media (orientation: portrait) {
:host([mode=lightbox]) slot[name="dialog-element"] {
aspect-ratio: 9 / 16;
}
:host([mode=lightbox]) .dialog-inner-wrapper {
max-width: unset;
}
}
@media (max-device-width: ${PHONE_MAX_WIDTH}px) and (pointer: coarse) and (orientation: landscape) {
:host([mode=lightbox]) .dialog-content {
animation-name: none;
height: 100%;
width: 100%;
}
:host([mode=lightbox]) .dialog-inner-wrapper {
height: 100% !important;
max-width: 100% !important;
margin: 0 auto !important;
min-height: -webkit-fill-available !important;
}
:host([mode=lightbox]):before {
background: #000;
}
}
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
</style>
<div class="placeholder aspect-ratio-box">
<slot name="placeholder" class="placeholder-inner">
<div class="placeholder-default"></div>
</slot>
</div>
<div class="popover" popover="manual">
<div class="dock-target">
<div class="aspect-ratio-box"></div>
</div>
<div class="dock-sticky-target">
<div class="aspect-ratio-box"></div>
</div>
<dialog class="dialog-content" part="dialog-content">
<div class="dialog-inner-wrapper">
<slot name="dock-background">
<div class="drag-handle">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrows-move" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M7.646.146a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 1.707V5.5a.5.5 0 0 1-1 0V1.707L6.354 2.854a.5.5 0 1 1-.708-.708l2-2zM8 10a.5.5 0 0 1 .5.5v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 0 1 .708-.708L7.5 14.293V10.5A.5.5 0 0 1 8 10zM.146 8.354a.5.5 0 0 1 0-.708l2-2a.5.5 0 1 1 .708.708L1.707 7.5H5.5a.5.5 0 0 1 0 1H1.707l1.147 1.146a.5.5 0 0 1-.708.708l-2-2zM10 8a.5.5 0 0 1 .5-.5h3.793l-1.147-1.146a.5.5 0 0 1 .708-.708l2 2a.5.5 0 0 1 0 .708l-2 2a.5.5 0 0 1-.708-.708L14.293 8.5H10.5A.5.5 0 0 1 10 8z"/></svg>
</div>
</slot>
<slot name="dialog-element"></slot>
</div>
</dialog>
</div>`;
const dockTarget = this.shadowRoot.querySelector('.dock-target');
const positions = toPositions(DEFAULT_DOCK_TARGET_INSET);
const dockStickyTarget = this.shadowRoot.querySelector('.dock-sticky-target');
const placeholderSlot = this.shadowRoot.querySelector('.placeholder-inner');
dockStickyTarget.style.top = positions.top;
Object.assign(dockTarget.style, positions);
this._wasInHiddenMode = false;
this._internalModeChange = false;
this._dockTargetResizeObserver = undefined;
this._bodyStyleAdjusted = false;
this.addEventListener('click', ({ target }) => {
if (this.classList.contains('dragging')) return;
// allows clicks within GlomexDialog and on a slotted placeholder
if (!(target instanceof GlomexDialogElement)
&& target.assignedSlot !== placeholderSlot
) return;
this._internalModeChange = true;
if (this._wasInHiddenMode) {
this.setAttribute('mode', 'hidden');
} else {
this.setAttribute('mode', 'inline');
}
});
}
connectedCallback() {
if (this._bodyStyleAdjusted) {
window.document.body.style.height = null;
window.document.body.style.overflow = null;
}
this._bodyStyleAdjusted = false;
updateViewPortWidth(this);
updatePlaceholderAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('aspect-ratio'),
]));
updateDockAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('dock-aspect-ratio'),
this.getAttribute('aspect-ratio'),
]));
updateDockStickyAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('dock-sticky-aspect-ratio'),
this.getAttribute('aspect-ratio'),
]));
if (!this.hasAttribute('mode')) {
// default-mode is "inline"
this.setAttribute('mode', 'inline');
}
if (this._disconnectDragAndDrop) this._disconnectDragAndDrop();
this._disconnectDragAndDrop = connectDragAndDrop(this);
const onResize = () => {
this._adjustLightboxModeForLandscapeOnMobile();
updateViewPortWidth(this);
this.refreshDockDialog();
};
onResize();
window.addEventListener('resize', onResize);
this._disconnectResize = () => {
window.removeEventListener('resize', onResize);
};
const onKeyup = (event) => {
const currentMode = this.getAttribute('mode');
if (currentMode !== 'lightbox') return;
if (event.key === 'Escape' || event.key === 'Esc') {
this._internalModeChange = true;
if (this._wasInHiddenMode) {
this.setAttribute('mode', 'hidden');
} else {
this.setAttribute('mode', 'inline');
}
}
// restrict tab behavior in lightbox mode
if (event.key === 'Tab') {
const activeElement = getActiveElement(window.document);
if (activeElement !== this && !isInDocument(activeElement, this)) {
const dialogInnerWrapper = this.shadowRoot.querySelector('.dialog-inner-wrapper');
dialogInnerWrapper.focus();
}
}
};
window.document.addEventListener('keyup', onKeyup);
this._disconnectKeyup = () => {
window.document.removeEventListener('keyup', onKeyup);
};
}
disconnectedCallback() {
if (this._disconnectDragAndDrop) this._disconnectDragAndDrop();
this._disconnectResize();
this._disconnectKeyup();
if (this._rotateToFullscreen) {
this._rotateToFullscreen.disable();
this._rotateToFullscreen.removeEventListener('exit', this._onRotateToFullscreenExit);
this._rotateToFullscreen.removeEventListener('enter', this._onRotateToFullscreenEnter);
this._rotateToFullscreen = undefined;
}
if (this._dockTargetResizeObserver) {
this._dockTargetResizeObserver.disconnect();
this._dockTargetResizeObserver = undefined;
}
if (this._bodyStyleAdjusted) {
window.document.body.style.height = null;
window.document.body.style.overflow = null;
}
this._bodyStyleAdjusted = false;
}
static get observedAttributes() {
return [
'mode',
'aspect-ratio',
'dock-target',
'dock-target-inset',
'dock-aspect-ratio',
'dock-mode',
'dock-sticky-target-top',
'dock-sticky-aspect-ratio',
'dock-downscale',
'rotate-to-fullscreen',
];
}
attributeChangedCallback(name, oldValue, newValue) {
const dialogContent = this.shadowRoot.querySelector('.dialog-content');
const dialogInnerWrapper = dialogContent.querySelector('.dialog-inner-wrapper');
const placeholder = this.shadowRoot.querySelector('.placeholder');
const popover = this.shadowRoot.querySelector('.popover');
const dockMode = this.getAttribute('dock-mode');
const transitionDuration = DEFAULT_TRANSITION_DURATION;
const aspectRatios = [
dockMode === 'sticky'
? this.getAttribute('dock-sticky-aspect-ratio')
: this.getAttribute('dock-aspect-ratio'),
this.getAttribute('aspect-ratio'),
];
const moveTo = dockMode === 'sticky'
? getDockStickyTarget(this)
: getAlternativeDockTarget(this) || getDefaultDockTarget(this);
if (name === 'dock-target') {
const dockTarget = getAlternativeDockTarget(this);
if (!newValue) {
if (this._dockTargetResizeObserver) {
this._dockTargetResizeObserver.disconnect();
}
} else if (window.ResizeObserver && dockTarget) {
this._dockTargetResizeObserver = new window.ResizeObserver(
() => this.refreshDockDialog(),
);
this._dockTargetResizeObserver.observe(dockTarget);
}
}
if (name === 'mode') {
if (this._wasInHiddenMode && (
newValue === 'lightbox'
|| newValue === 'dock'
|| newValue === 'inline'
)) {
if (newValue !== 'inline') {
placeholder.style.display = 'none';
}
placeholder.style.visibility = 'hidden';
} else {
placeholder.style.display = null;
placeholder.style.visibility = null;
}
window.removeEventListener('touchmove', this._onNonPassiveTouchMove, {
passive: false,
});
if (newValue === 'dock') {
if (popover.showPopover) popover.showPopover();
// ensure to refresh dock-target states before transition
this.refreshDockDialog();
dialogContent.style.zIndex = DOCK_Z_INDEX;
updateDockTargetState(this);
moveFromTo(dialogContent, {
from: placeholder,
to: moveTo,
animate: !this._wasInHiddenMode,
initialAspectRatio: getAspectRatioFromStrings([
this.getAttribute('aspect-ratio'),
]),
aspectRatio: getAspectRatioFromStrings(aspectRatios),
downscale: this.getAttribute('dock-downscale'),
transitionDuration,
}).then(({ scale }) => {
if (oldValue === 'lightbox') {
this.refreshDockDialog();
}
if (this.getAttribute('dock-downscale')) {
this.dispatchEvent(
new CustomEvent('dockscale', {
detail: { scale },
bubbles: true,
composed: true,
}),
);
}
});
} else if (newValue === 'inline') {
const goToInline = () => {
// also ensure that "absolute" positioning works
if (popover.hidePopover) popover.hidePopover();
// somehow this avoids CLS when switching between
// position "fixed" => "absolute"
dialogContent.style.display = 'grid';
dialogContent.style.position = 'absolute';
dialogContent.style.transform = 'scale(1)';
dialogInnerWrapper.style.transform = null;
dialogInnerWrapper.style.width = null;
dialogContent.style.top = null;
dialogContent.style.left = null;
if (!this._wasInHiddenMode && oldValue === 'dock') {
dialogContent.style.transitionDuration = `${transitionDuration}ms`;
dialogInnerWrapper.style.transitionDuration = null;
dialogContent.style.transitionTimingFunction = 'ease-out';
}
setTimeout(() => {
if (this.getAttribute('mode') === 'inline') {
dialogContent.setAttribute('style', '');
dialogInnerWrapper.setAttribute('style', '');
placeholder.style.visibility = 'hidden';
if (popover.hidePopover) popover.hidePopover();
}
}, transitionDuration);
};
if (oldValue === 'dock') {
// in order to transition with animation back from dock to inline
dialogContent.style.transitionDuration = `${transitionDuration}ms`;
// reposition element without animation
// so that new position with scroll gets calculated
moveFromTo(dialogContent, {
from: placeholder,
to: moveTo,
animate: false,
initialAspectRatio: getAspectRatioFromStrings([
this.getAttribute('aspect-ratio'),
]),
aspectRatio: getAspectRatioFromStrings(aspectRatios),
downscale: this.getAttribute('dock-downscale'),
transitionDuration,
}).then(() => {
window.requestAnimationFrame(() => {
goToInline();
});
});
} else if (oldValue !== 'inline') {
goToInline();
}
this._wasInHiddenMode = false;
} else if (newValue === 'lightbox') {
// prevent scrolling
window.document.body.style.height = '100%';
window.document.body.style.overflow = 'hidden';
this._bodyStyleAdjusted = true;
dialogContent.setAttribute('style', '');
dialogInnerWrapper.setAttribute('style', '');
dialogInnerWrapper.setAttribute('tabindex', '-1');
dialogInnerWrapper.focus();
// prevent document scrolling on iOS
this._onNonPassiveTouchMove = (event) => {
event.preventDefault();
};
window.addEventListener('touchmove', this._onNonPassiveTouchMove, {
passive: false,
});
if (popover.showPopover) popover.showPopover();
dialogContent.showModal();
this._adjustLightboxModeForLandscapeOnMobile();
if (this._rotateToFullscreen) {
this._rotateToFullscreen.enable();
}
} else if (newValue === 'hidden') {
this._wasInHiddenMode = true;
}
if (newValue !== 'lightbox') {
// reset prevent scrolling
if (this._bodyStyleAdjusted) {
window.document.body.style.height = null;
window.document.body.style.overflow = null;
}
dialogContent.close();
dialogInnerWrapper.removeAttribute('tabindex');
}
if (oldValue === 'lightbox' && this._rotateToFullscreen) {
this._rotateToFullscreen.disable();
dialogContent.close();
}
this.dispatchEvent(
new CustomEvent('modechange', {
detail: {
previousMode: oldValue,
mode: newValue,
internal: this._internalModeChange,
},
bubbles: true,
composed: true,
}),
);
this._internalModeChange = false;
}
if (name === 'dock-target') {
if (this._disconnectDragAndDrop) this._disconnectDragAndDrop();
this._disconnectDragAndDrop = connectDragAndDrop(this);
this.refreshDockDialog();
}
if (name === 'dock-target-inset') {
const positions = toPositions(newValue) || toPositions(DEFAULT_DOCK_TARGET_INSET);
Object.assign(
this.shadowRoot.querySelector('.dock-target').style,
positions,
);
// ensure to position from left by X% (e.g. 50% = center it)
const transform = [];
if (positions.top.match(/%$/)) {
transform.push(`translateY(-${positions.top})`);
} else if (positions.bottom.match(/%$/)) {
transform.push(`translateY(${positions.bottom})`);
}
if (positions.left.match(/%$/)) {
transform.push(`translateX(-${positions.left})`);
} else if (positions.right.match(/%$/)) {
transform.push(`translateX(${positions.right})`);
}
if (transform.length > 0) {
this.shadowRoot.querySelector('.dock-target').style.transform = transform.join(' ');
}
this.refreshDockDialog();
}
if (name === 'dock-sticky-target-top') {
this.refreshDockDialog();
}
if (name === 'dock-mode') {
if (this._disconnectDragAndDrop) this._disconnectDragAndDrop();
if (newValue !== 'sticky') {
this._disconnectDragAndDrop = connectDragAndDrop(this);
}
this.refreshDockDialog();
}
if (name === 'aspect-ratio') {
updatePlaceholderAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('aspect-ratio'),
]));
}
if (name === 'dock-aspect-ratio') {
updateDockAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('dock-aspect-ratio'),
this.getAttribute('aspect-ratio'),
]));
}
if (name === 'dock-sticky-aspect-ratio') {
updateDockStickyAspectRatio(this, getAspectRatioFromStrings([
this.getAttribute('dock-sticky-aspect-ratio'),
this.getAttribute('aspect-ratio'),
]));
}
if (name === 'dock-downscale' && newValue) {
if (newValue) {
dialogInnerWrapper.setAttribute('style', '');
} else {
// No implementation when dock-downscale gets reset
}
}
if (name === 'rotate-to-fullscreen') {
if (newValue == null && this._rotateToFullscreen) {
this._rotateToFullscreen.disable();
this._rotateToFullscreen.removeEventListener('exit', this._onRotateToFullscreenExit);
this._rotateToFullscreen.removeEventListener('enter', this._onRotateToFullscreenEnter);
this._rotateToFullscreen = undefined;
} else if (allowRotateToFullscreen) {
this._onRotateToFullscreenExit = (event) => {
if (event.detail.orientation.indexOf('landscape') === 0) {
this._internalModeChange = true;
this.setAttribute('mode', this._wasInHiddenMode ? 'hidden' : 'inline');
}
this.removeAttribute('fullscreen');
};
this._onRotateToFullscreenEnter = () => {
// expose fullscreen to the light-dom so we can apply fullscreen styles there
// (device-mode: fullscreen) media queries somehow don't work in combination
// with shadow dom
this.setAttribute('fullscreen', '');
};
try {
// iOS < 14 will fail extending EventTarget constructor
this._rotateToFullscreen = new RotataToFullscreen(window, dialogInnerWrapper);
this._rotateToFullscreen.addEventListener('exit', this._onRotateToFullscreenExit);
this._rotateToFullscreen.addEventListener('enter', this._onRotateToFullscreenEnter);
} catch (err) {
// ignore
}
}
}
}
_adjustLightboxModeForLandscapeOnMobile() {
if (this.getAttribute('mode') !== 'lightbox') return;
const mobileLandscapeSelector = `(max-device-width: ${PHONE_MAX_WIDTH}px) and (pointer: coarse) and (orientation: landscape)`;
if (window.matchMedia(mobileLandscapeSelector).matches) {
// allow scrolling in mobile landscape
// so that the user can scroll down to remove the browser bar
if (this._bodyStyleAdjusted) {
window.document.body.style.overflow = null;
}
} else {
window.document.body.style.overflow = 'hidden';
this._bodyStyleAdjusted = true;
}
}
/**
* Forces repositioning docked dialog element.
* Should be called when an external "dock-target" changed
* its size or position.
*/
refreshDockDialog() {
const dialogContent = this.shadowRoot.querySelector('.dialog-content');
const dockStickyTarget = this.shadowRoot.querySelector('.dock-sticky-target');
const dockStickyTargetTop = this.getAttribute('dock-sticky-target-top');
const clientRect = this.getBoundingClientRect();
const mode = this.getAttribute('mode');
const dockMode = this.getAttribute('dock-mode');
const aspectRatios = [
dockMode === 'sticky'
? this.getAttribute('dock-sticky-aspect-ratio')
: this.getAttribute('dock-aspect-ratio'),
this.getAttribute('aspect-ratio'),
];
if (dockMode === 'sticky') {
const alternativeDockTarget = getAlternativeDockTarget(this);
if (alternativeDockTarget) {
// adjust the ".dock-sticky-target" top value based on selector
const { height } = getViewportIntersection(alternativeDockTarget);
// in case we attach to navigation bars that can be expanded
// we ignore to adjust sticky position below a certain threshold
// based on given external selector
if (height < STICKY_TOP_SELECTOR_THRESHOLD && height > 0) {
dockStickyTarget.style.top = `${height || 0}px`;
} else {
// when not visible adjust to dock-sticky-target-top value
dockStickyTarget.style.top = `${dockStickyTargetTop || 0}px`;
}
} else {
// when no alternative dock target adjust to dock-sticky-target-top value
dockStickyTarget.style.top = `${dockStickyTargetTop || 0}px`;
}
}
const moveTo = dockMode === 'sticky'
? getDockStickyTarget(this)
: getAlternativeDockTarget(this) || getDefaultDockTarget(this);
let stickyWidth = clientRect.width;
if (stickyWidth === 0) {
stickyWidth = window.innerWidth * 0.9;
}
dockStickyTarget.style.left = `${clientRect.left}px`;
dockStickyTarget.style.width = `${stickyWidth}px`;
dockStickyTarget.style.height = `${stickyWidth / getAspectRatioFromStrings(aspectRatios)}px`;