@vtex/ads-stories 0.2.0 → 0.4.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.
@@ -26,6 +26,542 @@ __export(index_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(index_exports);
28
28
 
29
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg
30
+ var close_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M205.66,194.34a8,8,0,0,1-11.32,11.32L128,139.31,61.66,205.66a8,8,0,0,1-11.32-11.32L116.69,128,50.34,61.66A8,8,0,0,1,61.66,50.34L128,116.69l66.34-66.35a8,8,0,0,1,11.32,11.32L139.31,128Z"/></svg>\n';
31
+
32
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/collapse.svg
33
+ var collapse_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M152,96V48a8,8,0,0,1,16,0V88h40a8,8,0,0,1,0,16H160A8,8,0,0,1,152,96ZM96,152H48a8,8,0,0,0,0,16H88v40a8,8,0,0,0,16,0V160A8,8,0,0,0,96,152Zm112,0H160a8,8,0,0,0-8,8v48a8,8,0,0,0,16,0V168h40a8,8,0,0,0,0-16ZM96,40a8,8,0,0,0-8,8V88H48a8,8,0,0,0,0,16H96a8,8,0,0,0,8-8V48A8,8,0,0,0,96,40Z"/></svg>\n';
34
+
35
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg
36
+ var expand_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48V88a8,8,0,0,1-16,0V56H168a8,8,0,0,1,0-16h40A8,8,0,0,1,216,48ZM88,200H56V168a8,8,0,0,0-16,0v40a8,8,0,0,0,8,8H88a8,8,0,0,0,0-16Zm120-40a8,8,0,0,0-8,8v32H168a8,8,0,0,0,0,16h40a8,8,0,0,0,8-8V168A8,8,0,0,0,208,160ZM88,40H48a8,8,0,0,0-8,8V88a8,8,0,0,0,16,0V56H88a8,8,0,0,0,0-16Z"/></svg>\n';
37
+
38
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/pause-fill.svg
39
+ var pause_fill_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48V208a16,16,0,0,1-16,16H160a16,16,0,0,1-16-16V48a16,16,0,0,1,16-16h40A16,16,0,0,1,216,48ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Z"/></svg>\n';
40
+
41
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg
42
+ var play_fill_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M240,128a15.74,15.74,0,0,1-7.6,13.51L88.32,229.65a16,16,0,0,1-16.2.3A15.86,15.86,0,0,1,64,216.13V39.87a15.86,15.86,0,0,1,8.12-13.82,16,16,0,0,1,16.2.3L232.4,114.49A15.74,15.74,0,0,1,240,128Z"/></svg>\n';
43
+
44
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-x-fill.svg
45
+ var speaker_x_fill_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M245.66,146.34a8,8,0,0,1-11.32,11.32L216,139.31l-18.34,18.35a8,8,0,0,1-11.32-11.32L204.69,128l-18.35-18.34a8,8,0,0,1,11.32-11.32L216,116.69l18.34-18.35a8,8,0,0,1,11.32,11.32L227.31,128ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm97.15-54.15a8,8,0,0,0-10-.16l-65.57,51A4,4,0,0,0,80,79.84v96.32a4,4,0,0,0,1.55,3.15l65.57,51a8,8,0,0,0,9,.56,8.29,8.29,0,0,0,3.91-7.18V32.25A8.27,8.27,0,0,0,157.12,25.85Z"/></svg>\n';
46
+
47
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-high-fill.svg
48
+ var speaker_high_fill_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M160,32.25V223.69a8.29,8.29,0,0,1-3.91,7.18,8,8,0,0,1-9-.56l-65.57-51A4,4,0,0,1,80,176.16V79.84a4,4,0,0,1,1.55-3.15l65.57-51a8,8,0,0,1,10,.16A8.27,8.27,0,0,1,160,32.25ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm126.77,20.84a8,8,0,0,0-.72,11.3,24,24,0,0,1,0,31.72,8,8,0,1,0,12,10.58,40,40,0,0,0,0-52.88A8,8,0,0,0,186.74,100.84Zm40.89-26.17a8,8,0,1,0-11.92,10.66,64,64,0,0,1,0,85.34,8,8,0,1,0,11.92,10.66,80,80,0,0,0,0-106.66Z"/></svg>\n';
49
+
50
+ // src/components/icons/index.ts
51
+ var icons = {
52
+ close: close_default,
53
+ collapse: collapse_default,
54
+ expand: expand_default,
55
+ pause: pause_fill_default,
56
+ play: play_fill_default,
57
+ soundOff: speaker_x_fill_default,
58
+ soundOn: speaker_high_fill_default
59
+ };
60
+ function createIcon(name) {
61
+ const template = document.createElement("template");
62
+ template.innerHTML = icons[name];
63
+ const svg = template.content.firstElementChild;
64
+ svg.classList.add("icon");
65
+ svg.setAttribute("aria-hidden", "true");
66
+ svg.setAttribute("focusable", "false");
67
+ return svg;
68
+ }
69
+
70
+ // src/components/card/card.ts
71
+ var cardStyles = `
72
+ .card {
73
+ position: relative;
74
+ box-sizing: border-box;
75
+ width: calc(500px * 9 / 16);
76
+ height: 500px;
77
+ overflow: hidden;
78
+ border-radius: var(--vtex-ads-story-radius, 20px);
79
+ background: #000;
80
+ }
81
+
82
+ /* The card's button covers it. The focus ring goes on the card, since the
83
+ card's overflow would clip the button's own. */
84
+ .card-open {
85
+ position: absolute;
86
+ inset: 0;
87
+ width: 100%;
88
+ height: 100%;
89
+ padding: 0;
90
+ border: 0;
91
+ background: transparent;
92
+ cursor: pointer;
93
+ }
94
+
95
+ .card-open:focus-visible {
96
+ outline: none;
97
+ }
98
+
99
+ .card:has(> .card-open:focus-visible) {
100
+ outline: auto;
101
+ }
102
+
103
+ .poster,
104
+ .video {
105
+ position: absolute;
106
+ inset: 0;
107
+ width: 100%;
108
+ height: 100%;
109
+ object-fit: cover;
110
+ }
111
+
112
+ .play {
113
+ position: absolute;
114
+ top: 50%;
115
+ left: 50%;
116
+ display: grid;
117
+ width: 66px;
118
+ height: 66px;
119
+ place-items: center;
120
+ border-radius: 50%;
121
+ background:
122
+ linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),
123
+ rgba(255, 255, 255, 0.15);
124
+ backdrop-filter: blur(2px);
125
+ box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);
126
+ transform: translate(-50%, -50%);
127
+ transition:
128
+ opacity 0.3s ease,
129
+ transform 0.3s ease;
130
+ }
131
+
132
+ /* The icon's triangle is 18px tall and already sits slightly right of its
133
+ box, so it looks centered in the circle. */
134
+ .play .icon {
135
+ width: 22px;
136
+ height: 22px;
137
+ color: #fff;
138
+ }
139
+
140
+ /* Sound on/off, at the top right of the card (as in the prototype). */
141
+ .card-sound {
142
+ position: absolute;
143
+ top: 22px;
144
+ right: 12px;
145
+ z-index: 1;
146
+ display: grid;
147
+ width: 28px;
148
+ height: 28px;
149
+ place-items: center;
150
+ padding: 0;
151
+ border: 0;
152
+ background: transparent;
153
+ color: #fff;
154
+ cursor: pointer;
155
+ filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
156
+ }
157
+
158
+ .card-sound .icon {
159
+ width: 20px;
160
+ height: 20px;
161
+ }
162
+
163
+ /* With a mouse, the play button shows only on hover or keyboard focus.
164
+ Touch screens have no hover, so it stays visible there. */
165
+ @media (hover: hover) and (pointer: fine) {
166
+ .play {
167
+ opacity: 0;
168
+ transform: translate(-50%, -50%) scale(0.9);
169
+ }
170
+
171
+ .card:hover .play,
172
+ .card-open:focus-visible .play {
173
+ opacity: 1;
174
+ transform: translate(-50%, -50%);
175
+ }
176
+ }
177
+ `;
178
+ function httpUrl(value) {
179
+ if (!value) return null;
180
+ try {
181
+ const { protocol } = new URL(value);
182
+ return protocol === "https:" || protocol === "http:" ? value : null;
183
+ } catch (e) {
184
+ return null;
185
+ }
186
+ }
187
+ function prefersReducedMotion() {
188
+ var _a2, _b;
189
+ return (_b = (_a2 = globalThis.matchMedia) == null ? void 0 : _a2.call(globalThis, "(prefers-reduced-motion: reduce)").matches) != null ? _b : false;
190
+ }
191
+ function playVideo(video) {
192
+ var _a2;
193
+ (_a2 = video.play()) == null ? void 0 : _a2.catch(() => void 0);
194
+ }
195
+ function createVideo(src, poster) {
196
+ const video = document.createElement("video");
197
+ video.className = "video";
198
+ video.muted = true;
199
+ video.setAttribute("muted", "");
200
+ video.setAttribute("playsinline", "");
201
+ video.loop = true;
202
+ video.preload = "metadata";
203
+ video.autoplay = !prefersReducedMotion();
204
+ if (poster) video.poster = poster;
205
+ video.src = src;
206
+ video.addEventListener("error", () => video.remove());
207
+ return video;
208
+ }
209
+ function createSoundButton(video, options) {
210
+ const button = document.createElement("button");
211
+ button.type = "button";
212
+ button.className = "card-sound";
213
+ function sync() {
214
+ button.setAttribute(
215
+ "aria-label",
216
+ video.muted ? options.soundOnLabel : options.soundOffLabel
217
+ );
218
+ button.replaceChildren(createIcon(video.muted ? "soundOff" : "soundOn"));
219
+ }
220
+ video.addEventListener("volumechange", sync);
221
+ video.addEventListener("error", () => button.remove());
222
+ button.addEventListener("click", () => {
223
+ video.muted = !video.muted;
224
+ });
225
+ sync();
226
+ return button;
227
+ }
228
+ function createCard(story, options) {
229
+ const card = document.createElement("div");
230
+ card.className = "card";
231
+ const posterUrl = httpUrl(story.poster_url);
232
+ if (posterUrl) {
233
+ const poster = document.createElement("img");
234
+ poster.className = "poster";
235
+ poster.alt = "";
236
+ poster.src = posterUrl;
237
+ card.append(poster);
238
+ }
239
+ const mediaUrl = httpUrl(story.media_url);
240
+ const video = mediaUrl ? createVideo(mediaUrl, posterUrl) : null;
241
+ if (video) card.append(video);
242
+ if (options) {
243
+ const open = document.createElement("button");
244
+ open.type = "button";
245
+ open.className = "card-open";
246
+ open.setAttribute("aria-label", options.label);
247
+ const play = document.createElement("span");
248
+ play.className = "play";
249
+ play.setAttribute("aria-hidden", "true");
250
+ play.append(createIcon("play"));
251
+ open.append(play);
252
+ open.addEventListener("click", options.onSelect);
253
+ card.append(open);
254
+ if (video) card.append(createSoundButton(video, options));
255
+ }
256
+ return card;
257
+ }
258
+
259
+ // src/components/player/player.ts
260
+ var playerStyles = `
261
+ .player {
262
+ position: fixed;
263
+ inset: auto 16px 16px auto;
264
+ z-index: 2147483000;
265
+ box-sizing: border-box;
266
+ width: calc(min(587px, 100vh - 80px) * 9 / 16);
267
+ height: min(587px, 100vh - 80px);
268
+ margin: 0;
269
+ padding: 0;
270
+ overflow: visible;
271
+ border: 0;
272
+ background: transparent;
273
+ color: inherit;
274
+ font: inherit;
275
+ animation: player-in 0.25s ease;
276
+ }
277
+
278
+ /* A <dialog> shown as a popover has no open attribute, which the browser
279
+ hides by default. */
280
+ .player:popover-open {
281
+ display: block;
282
+ }
283
+
284
+ /* Switching modes shows the dialog again; only the first opening fades in. */
285
+ .player[data-switched] {
286
+ animation: none;
287
+ }
288
+
289
+ .player::backdrop {
290
+ background: transparent;
291
+ }
292
+
293
+ .player-frame {
294
+ position: relative;
295
+ width: 100%;
296
+ height: 100%;
297
+ }
298
+
299
+ .player .card {
300
+ width: 100%;
301
+ height: 100%;
302
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
303
+ }
304
+
305
+ .player .card:has(.video) {
306
+ cursor: pointer;
307
+ }
308
+
309
+ /* Play/pause reuses the card's glass .play button, as a real button. */
310
+ .player-toggle {
311
+ padding: 0;
312
+ border: 0;
313
+ color: #fff;
314
+ font: inherit;
315
+ cursor: pointer;
316
+ }
317
+
318
+ .player[data-paused] .player-toggle,
319
+ .player-toggle:focus-visible {
320
+ opacity: 1;
321
+ transform: translate(-50%, -50%);
322
+ }
323
+
324
+ /* On touch, tapping the video pauses it; the button shows while paused. */
325
+ @media (hover: none) {
326
+ .player:not([data-paused]) .player-toggle {
327
+ opacity: 0;
328
+ }
329
+ }
330
+
331
+ .player-sound {
332
+ position: absolute;
333
+ top: 12px;
334
+ right: 12px;
335
+ z-index: 1;
336
+ border-color: transparent;
337
+ background: rgba(0, 0, 0, 0.3);
338
+ }
339
+
340
+ .player-sound .icon {
341
+ width: 20px;
342
+ height: 20px;
343
+ }
344
+
345
+ /* Above the card: on narrow screens the expanded card can cover them. */
346
+ .player-controls {
347
+ position: absolute;
348
+ right: 0;
349
+ bottom: calc(100% + 8px);
350
+ z-index: 1;
351
+ display: flex;
352
+ gap: 8px;
353
+ }
354
+
355
+ .player-button {
356
+ display: grid;
357
+ width: 36px;
358
+ height: 36px;
359
+ place-items: center;
360
+ padding: 0;
361
+ border: 1px solid rgba(133, 133, 133, 0.3);
362
+ border-radius: 50%;
363
+ background: rgba(0, 0, 0, 0.32);
364
+ backdrop-filter: blur(5px);
365
+ color: #fff;
366
+ cursor: pointer;
367
+ transition: background-color 0.2s;
368
+ }
369
+
370
+ .player-button:hover {
371
+ background: rgba(0, 0, 0, 0.55);
372
+ }
373
+
374
+ .player-button .icon {
375
+ width: 18px;
376
+ height: 18px;
377
+ }
378
+
379
+ .player[data-expanded] {
380
+ inset: 0;
381
+ display: grid;
382
+ width: 100%;
383
+ max-width: none;
384
+ height: 100%;
385
+ max-height: none;
386
+ place-items: center;
387
+ background: rgba(0, 0, 0, 0.85);
388
+ animation: none;
389
+ }
390
+
391
+ .player[data-expanded] .player-frame {
392
+ width: calc(min(78vh, 720px) * 9 / 16);
393
+ height: min(78vh, 720px);
394
+ }
395
+
396
+ .player[data-expanded] .player-controls {
397
+ position: fixed;
398
+ top: 24px;
399
+ right: 24px;
400
+ bottom: auto;
401
+ }
402
+
403
+ .player[data-expanded] .player-button {
404
+ width: 40px;
405
+ height: 40px;
406
+ }
407
+
408
+ @media (max-width: 576px) {
409
+ .player {
410
+ right: 12px;
411
+ width: 220px;
412
+ height: 391px;
413
+ }
414
+
415
+ .player[data-expanded] {
416
+ background: #000;
417
+ }
418
+
419
+ /* The video stays 9:16 and centered; the rest of the screen is black. */
420
+ .player[data-expanded] .player-frame {
421
+ width: min(100vw, 100vh * 9 / 16);
422
+ height: min(100vh, 100vw * 16 / 9);
423
+ }
424
+
425
+ .player[data-expanded] .card {
426
+ border-radius: 0;
427
+ }
428
+
429
+ /* Below the sound button, at the top right of the video. Placed in the
430
+ video frame, not the screen: the frame is centered, so on screens
431
+ taller than 9:16 its top is below the screen's. */
432
+ .player[data-expanded] .player-controls {
433
+ position: absolute;
434
+ top: 60px;
435
+ right: 12px;
436
+ }
437
+ }
438
+
439
+ @keyframes player-in {
440
+ from {
441
+ opacity: 0;
442
+ transform: translateY(8px);
443
+ }
444
+ }
445
+
446
+ @media (prefers-reduced-motion: reduce) {
447
+ .player {
448
+ animation: none;
449
+ }
450
+ }
451
+ `;
452
+ function createButton(className, label, icon) {
453
+ const button = document.createElement("button");
454
+ button.type = "button";
455
+ button.className = `player-button ${className}`;
456
+ button.setAttribute("aria-label", label);
457
+ button.append(createIcon(icon));
458
+ return button;
459
+ }
460
+ function supportsPopover(player) {
461
+ return typeof player.showPopover === "function";
462
+ }
463
+ function showPlayer(player) {
464
+ if (supportsPopover(player)) player.showPopover();
465
+ else player.show();
466
+ }
467
+ function playPlayer(player) {
468
+ const video = player.querySelector("video");
469
+ if (video) playVideo(video);
470
+ }
471
+ function addVideoControls(player, card, video, options) {
472
+ const toggle = document.createElement("button");
473
+ toggle.type = "button";
474
+ toggle.className = "play player-toggle";
475
+ const sound = createButton("player-sound", "", "soundOff");
476
+ function syncPlayback() {
477
+ player.toggleAttribute("data-paused", video.paused);
478
+ toggle.setAttribute(
479
+ "aria-label",
480
+ video.paused ? options.playLabel : options.pauseLabel
481
+ );
482
+ toggle.replaceChildren(createIcon(video.paused ? "play" : "pause"));
483
+ }
484
+ function syncSound() {
485
+ sound.setAttribute(
486
+ "aria-label",
487
+ video.muted ? options.soundOnLabel : options.soundOffLabel
488
+ );
489
+ sound.replaceChildren(createIcon(video.muted ? "soundOff" : "soundOn"));
490
+ }
491
+ function togglePlayback() {
492
+ if (video.paused) playVideo(video);
493
+ else video.pause();
494
+ }
495
+ video.addEventListener("play", syncPlayback);
496
+ video.addEventListener("pause", syncPlayback);
497
+ video.addEventListener("volumechange", syncSound);
498
+ video.addEventListener("error", () => {
499
+ toggle.remove();
500
+ sound.remove();
501
+ delete player.dataset.paused;
502
+ });
503
+ toggle.addEventListener("click", (event) => {
504
+ event.stopPropagation();
505
+ togglePlayback();
506
+ });
507
+ sound.addEventListener("click", (event) => {
508
+ event.stopPropagation();
509
+ video.muted = !video.muted;
510
+ });
511
+ card.addEventListener("click", () => {
512
+ if (video.isConnected) togglePlayback();
513
+ });
514
+ syncPlayback();
515
+ syncSound();
516
+ card.append(toggle, sound);
517
+ }
518
+ function createPlayer(story, options) {
519
+ const player = document.createElement("dialog");
520
+ player.className = "player";
521
+ player.setAttribute("popover", "manual");
522
+ player.setAttribute("aria-label", options.label);
523
+ const frame = document.createElement("div");
524
+ frame.className = "player-frame";
525
+ const controls = document.createElement("div");
526
+ controls.className = "player-controls";
527
+ const expand = createButton("player-expand", options.expandLabel, "expand");
528
+ const close = createButton("player-close", options.closeLabel, "close");
529
+ controls.append(expand, close);
530
+ let expanded = false;
531
+ function hide() {
532
+ if (!expanded && supportsPopover(player)) player.hidePopover();
533
+ else player.close();
534
+ }
535
+ expand.addEventListener("click", () => {
536
+ hide();
537
+ expanded = !expanded;
538
+ player.toggleAttribute("data-expanded", expanded);
539
+ player.dataset.switched = "";
540
+ expand.setAttribute(
541
+ "aria-label",
542
+ expanded ? options.collapseLabel : options.expandLabel
543
+ );
544
+ expand.replaceChildren(createIcon(expanded ? "collapse" : "expand"));
545
+ if (expanded) player.showModal();
546
+ else showPlayer(player);
547
+ expand.focus();
548
+ });
549
+ close.addEventListener("click", options.onClose);
550
+ player.addEventListener("click", (event) => {
551
+ if (expanded && event.target === player) options.onClose();
552
+ });
553
+ player.addEventListener("cancel", (event) => {
554
+ event.preventDefault();
555
+ options.onClose();
556
+ });
557
+ const card = createCard(story);
558
+ const video = card.querySelector("video");
559
+ if (video) addVideoControls(player, card, video, options);
560
+ frame.append(controls, card);
561
+ player.append(frame);
562
+ return player;
563
+ }
564
+
29
565
  // src/story/story.ts
30
566
  var TAG_NAME = "vtex-ads-story";
31
567
  var _a;
@@ -36,44 +572,94 @@ var styles = `
36
572
  font-family: var(--vtex-ads-story-font-family, inherit);
37
573
  color: var(--vtex-ads-story-color-text, inherit);
38
574
  }
575
+ ${cardStyles}
576
+ ${playerStyles}
39
577
  `;
40
- var DEFAULT_NAME = "world";
578
+ var PLACEHOLDER_STORY = {
579
+ poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
580
+ media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4"
581
+ };
582
+ var LABELS = {
583
+ watch: "Assistir v\xEDdeo",
584
+ player: "V\xEDdeo",
585
+ expand: "Expandir v\xEDdeo",
586
+ collapse: "Reduzir v\xEDdeo",
587
+ close: "Fechar v\xEDdeo",
588
+ play: "Reproduzir v\xEDdeo",
589
+ pause: "Pausar v\xEDdeo",
590
+ soundOn: "Ativar som",
591
+ soundOff: "Desativar som"
592
+ };
41
593
  var VtexAdsStoryElement = class extends BaseElement {
42
- static get observedAttributes() {
43
- return ["name"];
44
- }
45
- get name() {
46
- var _a2, _b;
47
- return (_b = (_a2 = this.getAttribute("name")) == null ? void 0 : _a2.trim()) != null ? _b : "";
48
- }
49
- set name(value) {
50
- this.setAttribute("name", value);
594
+ constructor() {
595
+ super(...arguments);
596
+ this.player = null;
597
+ this.onKeyDown = (event) => {
598
+ if (event.key === "Escape") this.closePlayer();
599
+ };
51
600
  }
52
601
  render() {
53
602
  var _a2;
54
- const container = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector("[part='root']");
603
+ const container = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root");
55
604
  if (!container) return;
56
- container.textContent = `Hello ${this.name || DEFAULT_NAME}!`;
605
+ container.replaceChildren(
606
+ createCard(PLACEHOLDER_STORY, {
607
+ label: LABELS.watch,
608
+ soundOnLabel: LABELS.soundOn,
609
+ soundOffLabel: LABELS.soundOff,
610
+ onSelect: () => this.openPlayer()
611
+ })
612
+ );
613
+ }
614
+ cardVideo() {
615
+ var _a2;
616
+ return (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(
617
+ ".root .card video"
618
+ );
619
+ }
620
+ openPlayer() {
621
+ var _a2, _b;
622
+ if (this.player || !this.shadowRoot) return;
623
+ this.player = createPlayer(PLACEHOLDER_STORY, {
624
+ label: LABELS.player,
625
+ expandLabel: LABELS.expand,
626
+ collapseLabel: LABELS.collapse,
627
+ closeLabel: LABELS.close,
628
+ playLabel: LABELS.play,
629
+ pauseLabel: LABELS.pause,
630
+ soundOnLabel: LABELS.soundOn,
631
+ soundOffLabel: LABELS.soundOff,
632
+ onClose: () => this.closePlayer()
633
+ });
634
+ this.shadowRoot.append(this.player);
635
+ showPlayer(this.player);
636
+ (_a2 = this.cardVideo()) == null ? void 0 : _a2.pause();
637
+ playPlayer(this.player);
638
+ document.addEventListener("keydown", this.onKeyDown);
639
+ (_b = this.player.querySelector(".player-close")) == null ? void 0 : _b.focus();
640
+ }
641
+ // `removed`: the element is leaving the page, so there is no card to give
642
+ // focus back to or to resume playing (it would play out of sight).
643
+ closePlayer({ removed = false } = {}) {
644
+ var _a2, _b;
645
+ if (!this.player) return;
646
+ document.removeEventListener("keydown", this.onKeyDown);
647
+ this.player.remove();
648
+ this.player = null;
649
+ if (removed) return;
650
+ const video = this.cardVideo();
651
+ if (video == null ? void 0 : video.autoplay) playVideo(video);
652
+ (_b = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root .card-open")) == null ? void 0 : _b.focus();
57
653
  }
58
654
  connectedCallback() {
59
655
  if (!this.shadowRoot) {
60
656
  const root = this.attachShadow({ mode: "open" });
61
- root.innerHTML = `<style>${styles}</style><div part="root"></div>`;
657
+ root.innerHTML = `<style>${styles}</style><div class="root"></div>`;
62
658
  }
63
- this.upgradeProperty("name");
64
659
  this.render();
65
660
  }
66
- // A property set before the element is defined (e.g. while the CDN script
67
- // loads) becomes an own property that shadows the class getter/setter.
68
- // Remove it and set it again so it goes through the setter.
69
- upgradeProperty(prop) {
70
- if (!Object.prototype.hasOwnProperty.call(this, prop)) return;
71
- const value = this[prop];
72
- Reflect.deleteProperty(this, prop);
73
- this[prop] = value;
74
- }
75
- attributeChangedCallback() {
76
- this.render();
661
+ disconnectedCallback() {
662
+ this.closePlayer({ removed: true });
77
663
  }
78
664
  };
79
665
  function defineVtexAdsStories() {
@@ -87,4 +673,5 @@ function defineVtexAdsStories() {
87
673
  VtexAdsStoryElement,
88
674
  defineVtexAdsStories
89
675
  });
676
+ /*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */
90
677
  //# sourceMappingURL=index.cjs.map