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