@vtex/ads-stories 0.3.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.
@@ -35,11 +35,28 @@ var collapse_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256
35
35
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg
36
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
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
+
38
41
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg
39
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';
40
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
+
41
50
  // src/components/icons/index.ts
42
- var icons = { close: close_default, collapse: collapse_default, expand: expand_default, play: play_fill_default };
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
+ };
43
60
  function createIcon(name) {
44
61
  const template = document.createElement("template");
45
62
  template.innerHTML = icons[name];
@@ -62,11 +79,29 @@ var cardStyles = `
62
79
  background: #000;
63
80
  }
64
81
 
65
- .card[role="button"] {
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;
66
92
  cursor: pointer;
67
93
  }
68
94
 
69
- .poster {
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 {
70
105
  position: absolute;
71
106
  inset: 0;
72
107
  width: 100%;
@@ -102,6 +137,29 @@ var cardStyles = `
102
137
  color: #fff;
103
138
  }
104
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
+
105
163
  /* With a mouse, the play button shows only on hover or keyboard focus.
106
164
  Touch screens have no hover, so it stays visible there. */
107
165
  @media (hover: hover) and (pointer: fine) {
@@ -111,45 +169,89 @@ var cardStyles = `
111
169
  }
112
170
 
113
171
  .card:hover .play,
114
- .card:focus-visible .play {
172
+ .card-open:focus-visible .play {
115
173
  opacity: 1;
116
174
  transform: translate(-50%, -50%);
117
175
  }
118
176
  }
119
177
  `;
120
- function isHttpUrl(value) {
178
+ function httpUrl(value) {
179
+ if (!value) return null;
121
180
  try {
122
181
  const { protocol } = new URL(value);
123
- return protocol === "https:" || protocol === "http:";
182
+ return protocol === "https:" || protocol === "http:" ? value : null;
124
183
  } catch (e) {
125
- return false;
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"));
126
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;
127
227
  }
128
228
  function createCard(story, options) {
129
229
  const card = document.createElement("div");
130
230
  card.className = "card";
131
- if (story.poster_url && isHttpUrl(story.poster_url)) {
231
+ const posterUrl = httpUrl(story.poster_url);
232
+ if (posterUrl) {
132
233
  const poster = document.createElement("img");
133
234
  poster.className = "poster";
134
235
  poster.alt = "";
135
- poster.src = story.poster_url;
236
+ poster.src = posterUrl;
136
237
  card.append(poster);
137
238
  }
239
+ const mediaUrl = httpUrl(story.media_url);
240
+ const video = mediaUrl ? createVideo(mediaUrl, posterUrl) : null;
241
+ if (video) card.append(video);
138
242
  if (options) {
139
- card.setAttribute("role", "button");
140
- card.setAttribute("aria-label", options.label);
141
- card.tabIndex = 0;
243
+ const open = document.createElement("button");
244
+ open.type = "button";
245
+ open.className = "card-open";
246
+ open.setAttribute("aria-label", options.label);
142
247
  const play = document.createElement("span");
143
248
  play.className = "play";
144
249
  play.setAttribute("aria-hidden", "true");
145
250
  play.append(createIcon("play"));
146
- card.append(play);
147
- card.addEventListener("click", options.onSelect);
148
- card.addEventListener("keydown", (event) => {
149
- if (event.key !== "Enter" && event.key !== " ") return;
150
- event.preventDefault();
151
- options.onSelect();
152
- });
251
+ open.append(play);
252
+ open.addEventListener("click", options.onSelect);
253
+ card.append(open);
254
+ if (video) card.append(createSoundButton(video, options));
153
255
  }
154
256
  return card;
155
257
  }
@@ -200,6 +302,46 @@ var playerStyles = `
200
302
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
201
303
  }
202
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
+
203
345
  /* Above the card: on narrow screens the expanded card can cover them. */
204
346
  .player-controls {
205
347
  position: absolute;
@@ -284,8 +426,12 @@ var playerStyles = `
284
426
  border-radius: 0;
285
427
  }
286
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. */
287
432
  .player[data-expanded] .player-controls {
288
- top: 12px;
433
+ position: absolute;
434
+ top: 60px;
289
435
  right: 12px;
290
436
  }
291
437
  }
@@ -318,6 +464,57 @@ function showPlayer(player) {
318
464
  if (supportsPopover(player)) player.showPopover();
319
465
  else player.show();
320
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
+ }
321
518
  function createPlayer(story, options) {
322
519
  const player = document.createElement("dialog");
323
520
  player.className = "player";
@@ -357,7 +554,10 @@ function createPlayer(story, options) {
357
554
  event.preventDefault();
358
555
  options.onClose();
359
556
  });
360
- frame.append(controls, createCard(story));
557
+ const card = createCard(story);
558
+ const video = card.querySelector("video");
559
+ if (video) addVideoControls(player, card, video, options);
560
+ frame.append(controls, card);
361
561
  player.append(frame);
362
562
  return player;
363
563
  }
@@ -376,15 +576,19 @@ var styles = `
376
576
  ${playerStyles}
377
577
  `;
378
578
  var PLACEHOLDER_STORY = {
379
- ad_id: "placeholder",
380
- poster_url: "https://dummyimage.com/1080x1920/e31c57/fff"
579
+ poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
580
+ media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4"
381
581
  };
382
582
  var LABELS = {
383
583
  watch: "Assistir v\xEDdeo",
384
584
  player: "V\xEDdeo",
385
585
  expand: "Expandir v\xEDdeo",
386
586
  collapse: "Reduzir v\xEDdeo",
387
- close: "Fechar 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"
388
592
  };
389
593
  var VtexAdsStoryElement = class extends BaseElement {
390
594
  constructor() {
@@ -401,34 +605,51 @@ var VtexAdsStoryElement = class extends BaseElement {
401
605
  container.replaceChildren(
402
606
  createCard(PLACEHOLDER_STORY, {
403
607
  label: LABELS.watch,
608
+ soundOnLabel: LABELS.soundOn,
609
+ soundOffLabel: LABELS.soundOff,
404
610
  onSelect: () => this.openPlayer()
405
611
  })
406
612
  );
407
613
  }
408
- openPlayer() {
614
+ cardVideo() {
409
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;
410
622
  if (this.player || !this.shadowRoot) return;
411
623
  this.player = createPlayer(PLACEHOLDER_STORY, {
412
624
  label: LABELS.player,
413
625
  expandLabel: LABELS.expand,
414
626
  collapseLabel: LABELS.collapse,
415
627
  closeLabel: LABELS.close,
628
+ playLabel: LABELS.play,
629
+ pauseLabel: LABELS.pause,
630
+ soundOnLabel: LABELS.soundOn,
631
+ soundOffLabel: LABELS.soundOff,
416
632
  onClose: () => this.closePlayer()
417
633
  });
418
634
  this.shadowRoot.append(this.player);
419
635
  showPlayer(this.player);
636
+ (_a2 = this.cardVideo()) == null ? void 0 : _a2.pause();
637
+ playPlayer(this.player);
420
638
  document.addEventListener("keydown", this.onKeyDown);
421
- (_a2 = this.player.querySelector(".player-close")) == null ? void 0 : _a2.focus();
639
+ (_b = this.player.querySelector(".player-close")) == null ? void 0 : _b.focus();
422
640
  }
423
- closePlayer({ restoreFocus = true } = {}) {
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 } = {}) {
424
644
  var _a2, _b;
425
645
  if (!this.player) return;
426
646
  document.removeEventListener("keydown", this.onKeyDown);
427
647
  this.player.remove();
428
648
  this.player = null;
429
- if (restoreFocus) {
430
- (_b = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root .card")) == null ? void 0 : _b.focus();
431
- }
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();
432
653
  }
433
654
  connectedCallback() {
434
655
  if (!this.shadowRoot) {
@@ -438,7 +659,7 @@ var VtexAdsStoryElement = class extends BaseElement {
438
659
  this.render();
439
660
  }
440
661
  disconnectedCallback() {
441
- this.closePlayer({ restoreFocus: false });
662
+ this.closePlayer({ removed: true });
442
663
  }
443
664
  };
444
665
  function defineVtexAdsStories() {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/collapse.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg","../../src/components/icons/index.ts","../../src/components/card/card.ts","../../src/components/player/player.ts","../../src/story/story.ts"],"sourcesContent":["export { VtexAdsStoryElement, defineVtexAdsStories, TAG_NAME } from \"./story\";\n","<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","<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","<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","<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","/*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */\n\n// SVG files as downloaded from Phosphor: regular weight, or the weight in the\n// file name. They are bundled as text: `?raw` is handled by Vite in dev and\n// tests, and by the raw plugin in tsup.config.ts.\nimport close from \"./assets/close.svg?raw\";\nimport collapse from \"./assets/collapse.svg?raw\";\nimport expand from \"./assets/expand.svg?raw\";\nimport play from \"./assets/play-fill.svg?raw\";\n\nexport const icons = { close, collapse, expand, play };\n\nexport type IconName = keyof typeof icons;\n\n/** Renders a bundled icon. It is decorative: name the control around it. */\nexport function createIcon(name: IconName) {\n const template = document.createElement(\"template\");\n template.innerHTML = icons[name];\n\n const svg = template.content.firstElementChild as SVGSVGElement;\n svg.classList.add(\"icon\");\n svg.setAttribute(\"aria-hidden\", \"true\");\n svg.setAttribute(\"focusable\", \"false\");\n return svg;\n}\n","import { createIcon } from \"../icons\";\nimport type { StoryAd } from \"../../types\";\n\nexport const cardStyles = `\n .card {\n position: relative;\n box-sizing: border-box;\n width: calc(500px * 9 / 16);\n height: 500px;\n overflow: hidden;\n border-radius: var(--vtex-ads-story-radius, 20px);\n background: #000;\n }\n\n .card[role=\"button\"] {\n cursor: pointer;\n }\n\n .poster {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n }\n\n .play {\n position: absolute;\n top: 50%;\n left: 50%;\n display: grid;\n width: 66px;\n height: 66px;\n place-items: center;\n border-radius: 50%;\n background:\n linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),\n rgba(255, 255, 255, 0.15);\n backdrop-filter: blur(2px);\n box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);\n transform: translate(-50%, -50%);\n transition:\n opacity 0.3s ease,\n transform 0.3s ease;\n }\n\n /* The icon's triangle is 18px tall and already sits slightly right of its\n box, so it looks centered in the circle. */\n .play .icon {\n width: 22px;\n height: 22px;\n color: #fff;\n }\n\n /* With a mouse, the play button shows only on hover or keyboard focus.\n Touch screens have no hover, so it stays visible there. */\n @media (hover: hover) and (pointer: fine) {\n .play {\n opacity: 0;\n transform: translate(-50%, -50%) scale(0.9);\n }\n\n .card:hover .play,\n .card:focus-visible .play {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n }\n`;\n\nexport interface CardOptions {\n /** Accessible name of the card. */\n label: string;\n /** Called on click, Enter or Space. */\n onSelect: () => void;\n}\n\n// Only absolute http(s) URLs reach the DOM.\nfunction isHttpUrl(value: string) {\n try {\n const { protocol } = new URL(value);\n return protocol === \"https:\" || protocol === \"http:\";\n } catch {\n return false;\n }\n}\n\n/**\n * A 9:16 card with the Story cover. With options, the card is a button with\n * a play icon in the middle.\n */\nexport function createCard(story: StoryAd, options?: CardOptions) {\n const card = document.createElement(\"div\");\n card.className = \"card\";\n\n if (story.poster_url && isHttpUrl(story.poster_url)) {\n const poster = document.createElement(\"img\");\n poster.className = \"poster\";\n poster.alt = \"\";\n poster.src = story.poster_url;\n card.append(poster);\n }\n\n if (options) {\n card.setAttribute(\"role\", \"button\");\n card.setAttribute(\"aria-label\", options.label);\n card.tabIndex = 0;\n\n const play = document.createElement(\"span\");\n play.className = \"play\";\n play.setAttribute(\"aria-hidden\", \"true\");\n play.append(createIcon(\"play\"));\n card.append(play);\n\n card.addEventListener(\"click\", options.onSelect);\n card.addEventListener(\"keydown\", (event) => {\n if (event.key !== \"Enter\" && event.key !== \" \") return;\n event.preventDefault();\n options.onSelect();\n });\n }\n\n return card;\n}\n","import { createCard } from \"../card\";\nimport { createIcon, type IconName } from \"../icons\";\nimport type { StoryAd } from \"../../types\";\n\n// The player is a <dialog> shown in the top layer, so no store z-index,\n// overflow or transform can hide or move it.\n//\n// Mini: a manual popover (non-modal, the page stays usable), with the card at\n// the bottom right and the controls above it. Without popover support it is a\n// non-modal dialog, fixed with a high z-index.\n// Expanded: a modal dialog (showModal), so the browser makes the page behind\n// it inert. The card is centered over a dark backdrop, with the controls at\n// the top right of the screen. On narrow screens it fills the screen.\nexport const playerStyles = `\n .player {\n position: fixed;\n inset: auto 16px 16px auto;\n z-index: 2147483000;\n box-sizing: border-box;\n width: calc(min(587px, 100vh - 80px) * 9 / 16);\n height: min(587px, 100vh - 80px);\n margin: 0;\n padding: 0;\n overflow: visible;\n border: 0;\n background: transparent;\n color: inherit;\n font: inherit;\n animation: player-in 0.25s ease;\n }\n\n /* A <dialog> shown as a popover has no open attribute, which the browser\n hides by default. */\n .player:popover-open {\n display: block;\n }\n\n /* Switching modes shows the dialog again; only the first opening fades in. */\n .player[data-switched] {\n animation: none;\n }\n\n .player::backdrop {\n background: transparent;\n }\n\n .player-frame {\n position: relative;\n width: 100%;\n height: 100%;\n }\n\n .player .card {\n width: 100%;\n height: 100%;\n box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);\n }\n\n /* Above the card: on narrow screens the expanded card can cover them. */\n .player-controls {\n position: absolute;\n right: 0;\n bottom: calc(100% + 8px);\n z-index: 1;\n display: flex;\n gap: 8px;\n }\n\n .player-button {\n display: grid;\n width: 36px;\n height: 36px;\n place-items: center;\n padding: 0;\n border: 1px solid rgba(133, 133, 133, 0.3);\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.32);\n backdrop-filter: blur(5px);\n color: #fff;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .player-button:hover {\n background: rgba(0, 0, 0, 0.55);\n }\n\n .player-button .icon {\n width: 18px;\n height: 18px;\n }\n\n .player[data-expanded] {\n inset: 0;\n display: grid;\n width: 100%;\n max-width: none;\n height: 100%;\n max-height: none;\n place-items: center;\n background: rgba(0, 0, 0, 0.85);\n animation: none;\n }\n\n .player[data-expanded] .player-frame {\n width: calc(min(78vh, 720px) * 9 / 16);\n height: min(78vh, 720px);\n }\n\n .player[data-expanded] .player-controls {\n position: fixed;\n top: 24px;\n right: 24px;\n bottom: auto;\n }\n\n .player[data-expanded] .player-button {\n width: 40px;\n height: 40px;\n }\n\n @media (max-width: 576px) {\n .player {\n right: 12px;\n width: 220px;\n height: 391px;\n }\n\n .player[data-expanded] {\n background: #000;\n }\n\n /* The video stays 9:16 and centered; the rest of the screen is black. */\n .player[data-expanded] .player-frame {\n width: min(100vw, 100vh * 9 / 16);\n height: min(100vh, 100vw * 16 / 9);\n }\n\n .player[data-expanded] .card {\n border-radius: 0;\n }\n\n .player[data-expanded] .player-controls {\n top: 12px;\n right: 12px;\n }\n }\n\n @keyframes player-in {\n from {\n opacity: 0;\n transform: translateY(8px);\n }\n }\n\n @media (prefers-reduced-motion: reduce) {\n .player {\n animation: none;\n }\n }\n`;\n\nexport interface PlayerOptions {\n /** Accessible name of the player. */\n label: string;\n /** Accessible names of the buttons. */\n expandLabel: string;\n collapseLabel: string;\n closeLabel: string;\n onClose: () => void;\n}\n\nfunction createButton(className: string, label: string, icon: IconName) {\n const button = document.createElement(\"button\");\n button.type = \"button\";\n button.className = `player-button ${className}`;\n button.setAttribute(\"aria-label\", label);\n button.append(createIcon(icon));\n return button;\n}\n\nfunction supportsPopover(player: HTMLDialogElement) {\n return typeof player.showPopover === \"function\";\n}\n\n/** Shows the player as the mini player. It must already be in the page. */\nexport function showPlayer(player: HTMLDialogElement) {\n if (supportsPopover(player)) player.showPopover();\n else player.show();\n}\n\n/**\n * The Story player. It opens as the mini player (see showPlayer); expand\n * switches to the expanded view, a modal dialog, and back. Clicking the\n * backdrop of the expanded view or pressing Escape in it closes the player.\n */\nexport function createPlayer(story: StoryAd, options: PlayerOptions) {\n const player = document.createElement(\"dialog\");\n player.className = \"player\";\n player.setAttribute(\"popover\", \"manual\");\n player.setAttribute(\"aria-label\", options.label);\n\n const frame = document.createElement(\"div\");\n frame.className = \"player-frame\";\n\n const controls = document.createElement(\"div\");\n controls.className = \"player-controls\";\n\n const expand = createButton(\"player-expand\", options.expandLabel, \"expand\");\n const close = createButton(\"player-close\", options.closeLabel, \"close\");\n controls.append(expand, close);\n\n let expanded = false;\n\n // A dialog can't be shown as a popover and as a modal at the same time, so\n // switching modes hides it first, the same way it was shown.\n function hide() {\n if (!expanded && supportsPopover(player)) player.hidePopover();\n else player.close();\n }\n\n expand.addEventListener(\"click\", () => {\n hide();\n expanded = !expanded;\n player.toggleAttribute(\"data-expanded\", expanded);\n player.dataset.switched = \"\";\n expand.setAttribute(\n \"aria-label\",\n expanded ? options.collapseLabel : options.expandLabel,\n );\n expand.replaceChildren(createIcon(expanded ? \"collapse\" : \"expand\"));\n\n if (expanded) player.showModal();\n else showPlayer(player);\n expand.focus();\n });\n\n close.addEventListener(\"click\", options.onClose);\n\n // Clicks on the backdrop of the expanded view reach the dialog itself.\n player.addEventListener(\"click\", (event) => {\n if (expanded && event.target === player) options.onClose();\n });\n\n // Escape on a modal dialog would only hide it: close the player instead.\n player.addEventListener(\"cancel\", (event) => {\n event.preventDefault();\n options.onClose();\n });\n\n frame.append(controls, createCard(story));\n player.append(frame);\n return player;\n}\n","import { cardStyles, createCard } from \"../components/card\";\nimport { createPlayer, playerStyles, showPlayer } from \"../components/player\";\nimport type { StoryAd } from \"../types\";\n\nexport const TAG_NAME = \"vtex-ads-story\";\n\n// Keeps the module importable where HTMLElement doesn't exist (SSR, Node).\nconst BaseElement: typeof HTMLElement = globalThis.HTMLElement ?? Object;\n\nconst styles = `\n :host {\n display: block;\n font-family: var(--vtex-ads-story-font-family, inherit);\n color: var(--vtex-ads-story-color-text, inherit);\n }\n ${cardStyles}\n ${playerStyles}\n`;\n\n// Temporary: rendered until the element reads Stories from the ad API.\nconst PLACEHOLDER_STORY: StoryAd = {\n ad_id: \"placeholder\",\n poster_url: \"https://dummyimage.com/1080x1920/e31c57/fff\",\n};\n\n// Temporary: fixed texts until the store can set them by attribute.\nconst LABELS = {\n watch: \"Assistir vídeo\",\n player: \"Vídeo\",\n expand: \"Expandir vídeo\",\n collapse: \"Reduzir vídeo\",\n close: \"Fechar vídeo\",\n};\n\nexport class VtexAdsStoryElement extends BaseElement {\n private player: HTMLDialogElement | null = null;\n\n private readonly onKeyDown = (event: KeyboardEvent) => {\n if (event.key === \"Escape\") this.closePlayer();\n };\n\n private render() {\n const container = this.shadowRoot?.querySelector(\".root\");\n if (!container) return;\n\n container.replaceChildren(\n createCard(PLACEHOLDER_STORY, {\n label: LABELS.watch,\n onSelect: () => this.openPlayer(),\n }),\n );\n }\n\n private openPlayer() {\n if (this.player || !this.shadowRoot) return;\n\n this.player = createPlayer(PLACEHOLDER_STORY, {\n label: LABELS.player,\n expandLabel: LABELS.expand,\n collapseLabel: LABELS.collapse,\n closeLabel: LABELS.close,\n onClose: () => this.closePlayer(),\n });\n this.shadowRoot.append(this.player);\n showPlayer(this.player);\n document.addEventListener(\"keydown\", this.onKeyDown);\n\n this.player.querySelector<HTMLElement>(\".player-close\")?.focus();\n }\n\n private closePlayer({ restoreFocus = true } = {}) {\n if (!this.player) return;\n\n document.removeEventListener(\"keydown\", this.onKeyDown);\n this.player.remove();\n this.player = null;\n\n if (restoreFocus) {\n this.shadowRoot?.querySelector<HTMLElement>(\".root .card\")?.focus();\n }\n }\n\n connectedCallback() {\n if (!this.shadowRoot) {\n const root = this.attachShadow({ mode: \"open\" });\n root.innerHTML = `<style>${styles}</style><div class=\"root\"></div>`;\n }\n\n this.render();\n }\n\n disconnectedCallback() {\n this.closePlayer({ restoreFocus: false });\n }\n}\n\n// Types `document.createElement(\"vtex-ads-story\")` and `querySelector` results\n// as VtexAdsStoryElement in any TypeScript project, no framework needed.\ndeclare global {\n interface HTMLElementTagNameMap {\n \"vtex-ads-story\": VtexAdsStoryElement;\n }\n}\n\nexport function defineVtexAdsStories() {\n if (typeof customElements === \"undefined\") return;\n if (customElements.get(TAG_NAME)) return;\n\n customElements.define(TAG_NAME, VtexAdsStoryElement);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;;;ACAA;;;ACAA;;;ACAA;;;ACUO,IAAM,QAAQ,EAAE,sBAAO,4BAAU,wBAAQ,wBAAK;AAK9C,SAAS,WAAW,MAAgB;AACzC,QAAM,WAAW,SAAS,cAAc,UAAU;AAClD,WAAS,YAAY,MAAM,IAAI;AAE/B,QAAM,MAAM,SAAS,QAAQ;AAC7B,MAAI,UAAU,IAAI,MAAM;AACxB,MAAI,aAAa,eAAe,MAAM;AACtC,MAAI,aAAa,aAAa,OAAO;AACrC,SAAO;AACT;;;ACrBO,IAAM,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2E1B,SAAS,UAAU,OAAe;AAChC,MAAI;AACF,UAA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AAE,MAAM,OAAO,CAAC;AAC/C,WAAK,YAAY,UAAU,MAAM;AAAA,IACnC;AAEA,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,uBAAuB;AACrB,SAAK,YAAY,EAAE,cAAc,MAAM,CAAC;AAAA,EAC1C;AACF;AAUO,SAAS,uBAAuB;AACrC,MAAI,OAAO,mBAAmB,YAAa;AAC3C,MAAI,eAAe,IAAI,QAAQ,EAAG;AAElC,iBAAe,OAAO,UAAU,mBAAmB;AACrD;","names":["_a"]}
1
+ {"version":3,"sources":["../../src/index.ts","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/collapse.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/pause-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-x-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-high-fill.svg","../../src/components/icons/index.ts","../../src/components/card/card.ts","../../src/components/player/player.ts","../../src/story/story.ts"],"sourcesContent":["export { VtexAdsStoryElement, defineVtexAdsStories, TAG_NAME } from \"./story\";\n","<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","<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","<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","<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","<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","<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","<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","/*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */\n\n// SVG files as downloaded from Phosphor: regular weight, or the weight in the\n// file name. They are bundled as text: `?raw` is handled by Vite in dev and\n// tests, and by the raw plugin in tsup.config.ts.\nimport close from \"./assets/close.svg?raw\";\nimport collapse from \"./assets/collapse.svg?raw\";\nimport expand from \"./assets/expand.svg?raw\";\nimport pause from \"./assets/pause-fill.svg?raw\";\nimport play from \"./assets/play-fill.svg?raw\";\nimport soundOff from \"./assets/speaker-x-fill.svg?raw\";\nimport soundOn from \"./assets/speaker-high-fill.svg?raw\";\n\nexport const icons = {\n close,\n collapse,\n expand,\n pause,\n play,\n soundOff,\n soundOn,\n};\n\nexport type IconName = keyof typeof icons;\n\n/** Renders a bundled icon. It is decorative: name the control around it. */\nexport function createIcon(name: IconName) {\n const template = document.createElement(\"template\");\n template.innerHTML = icons[name];\n\n const svg = template.content.firstElementChild as SVGSVGElement;\n svg.classList.add(\"icon\");\n svg.setAttribute(\"aria-hidden\", \"true\");\n svg.setAttribute(\"focusable\", \"false\");\n return svg;\n}\n","import { createIcon } from \"../icons\";\nimport type { StoryMedia } from \"../../types\";\n\nexport const cardStyles = `\n .card {\n position: relative;\n box-sizing: border-box;\n width: calc(500px * 9 / 16);\n height: 500px;\n overflow: hidden;\n border-radius: var(--vtex-ads-story-radius, 20px);\n background: #000;\n }\n\n /* The card's button covers it. The focus ring goes on the card, since the\n card's overflow would clip the button's own. */\n .card-open {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n padding: 0;\n border: 0;\n background: transparent;\n cursor: pointer;\n }\n\n .card-open:focus-visible {\n outline: none;\n }\n\n .card:has(> .card-open:focus-visible) {\n outline: auto;\n }\n\n .poster,\n .video {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n }\n\n .play {\n position: absolute;\n top: 50%;\n left: 50%;\n display: grid;\n width: 66px;\n height: 66px;\n place-items: center;\n border-radius: 50%;\n background:\n linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),\n rgba(255, 255, 255, 0.15);\n backdrop-filter: blur(2px);\n box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);\n transform: translate(-50%, -50%);\n transition:\n opacity 0.3s ease,\n transform 0.3s ease;\n }\n\n /* The icon's triangle is 18px tall and already sits slightly right of its\n box, so it looks centered in the circle. */\n .play .icon {\n width: 22px;\n height: 22px;\n color: #fff;\n }\n\n /* Sound on/off, at the top right of the card (as in the prototype). */\n .card-sound {\n position: absolute;\n top: 22px;\n right: 12px;\n z-index: 1;\n display: grid;\n width: 28px;\n height: 28px;\n place-items: center;\n padding: 0;\n border: 0;\n background: transparent;\n color: #fff;\n cursor: pointer;\n filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));\n }\n\n .card-sound .icon {\n width: 20px;\n height: 20px;\n }\n\n /* With a mouse, the play button shows only on hover or keyboard focus.\n Touch screens have no hover, so it stays visible there. */\n @media (hover: hover) and (pointer: fine) {\n .play {\n opacity: 0;\n transform: translate(-50%, -50%) scale(0.9);\n }\n\n .card:hover .play,\n .card-open:focus-visible .play {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n }\n`;\n\nexport interface CardOptions {\n /** Accessible name of the card. */\n label: string;\n /** Called on click, Enter or Space. */\n onSelect: () => void;\n /** Accessible names of the sound button, shown when the card has a video. */\n soundOnLabel: string;\n soundOffLabel: string;\n}\n\n// Only absolute http(s) URLs reach the DOM. Relative and protocol-relative\n// ones (\"/a.jpg\", \"//host/a.jpg\") don't parse without a base, so they're out.\nfunction httpUrl(value: string | undefined) {\n if (!value) return null;\n try {\n const { protocol } = new URL(value);\n return protocol === \"https:\" || protocol === \"http:\" ? value : null;\n } catch {\n return null;\n }\n}\n\nfunction prefersReducedMotion() {\n return (\n globalThis.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches ?? false\n );\n}\n\n/**\n * Plays a video. A blocked play is ignored: the cover or the paused state\n * stays.\n */\nexport function playVideo(video: HTMLVideoElement) {\n video.play()?.catch(() => undefined);\n}\n\nfunction createVideo(src: string, poster: string | null) {\n const video = document.createElement(\"video\");\n video.className = \"video\";\n // Muted and inline is what browsers, iOS included, allow to autoplay.\n video.muted = true;\n video.setAttribute(\"muted\", \"\");\n video.setAttribute(\"playsinline\", \"\");\n video.loop = true;\n video.preload = \"metadata\";\n // No autoplay for people who asked for reduced motion.\n video.autoplay = !prefersReducedMotion();\n if (poster) video.poster = poster;\n video.src = src;\n\n // If the video can't load, the cover underneath stays.\n video.addEventListener(\"error\", () => video.remove());\n return video;\n}\n\n/** Sound on/off for the card's video, which starts muted. */\nfunction createSoundButton(video: HTMLVideoElement, options: CardOptions) {\n const button = document.createElement(\"button\");\n button.type = \"button\";\n button.className = \"card-sound\";\n\n function sync() {\n button.setAttribute(\n \"aria-label\",\n video.muted ? options.soundOnLabel : options.soundOffLabel,\n );\n button.replaceChildren(createIcon(video.muted ? \"soundOff\" : \"soundOn\"));\n }\n\n video.addEventListener(\"volumechange\", sync);\n // Without the video, the cover stays and there is no sound to control.\n video.addEventListener(\"error\", () => button.remove());\n button.addEventListener(\"click\", () => {\n video.muted = !video.muted;\n });\n\n sync();\n return button;\n}\n\n/**\n * A 9:16 card with the Story cover and, with a media_url, the video looping\n * muted over it. With options, a button with a play icon in the middle covers\n * the card, and a card with a video gets a sound button. The sound button is\n * beside the card's button, not inside it: a button can't contain another.\n */\nexport function createCard(story: StoryMedia, options?: CardOptions) {\n const card = document.createElement(\"div\");\n card.className = \"card\";\n\n const posterUrl = httpUrl(story.poster_url);\n if (posterUrl) {\n const poster = document.createElement(\"img\");\n poster.className = \"poster\";\n poster.alt = \"\";\n poster.src = posterUrl;\n card.append(poster);\n }\n\n const mediaUrl = httpUrl(story.media_url);\n const video = mediaUrl ? createVideo(mediaUrl, posterUrl) : null;\n if (video) card.append(video);\n\n if (options) {\n const open = document.createElement(\"button\");\n open.type = \"button\";\n open.className = \"card-open\";\n open.setAttribute(\"aria-label\", options.label);\n\n const play = document.createElement(\"span\");\n play.className = \"play\";\n play.setAttribute(\"aria-hidden\", \"true\");\n play.append(createIcon(\"play\"));\n open.append(play);\n open.addEventListener(\"click\", options.onSelect);\n\n card.append(open);\n if (video) card.append(createSoundButton(video, options));\n }\n\n return card;\n}\n","import { createCard, playVideo } from \"../card\";\nimport { createIcon, type IconName } from \"../icons\";\nimport type { StoryMedia } from \"../../types\";\n\n// The player is a <dialog> shown in the top layer, so no store z-index,\n// overflow or transform can hide or move it.\n//\n// Mini: a manual popover (non-modal, the page stays usable), with the card at\n// the bottom right and the controls above it. Without popover support it is a\n// non-modal dialog, fixed with a high z-index.\n// Expanded: a modal dialog (showModal), so the browser makes the page behind\n// it inert. The card is centered over a dark backdrop, with the controls at\n// the top right of the screen. On narrow screens it fills the screen.\nexport const playerStyles = `\n .player {\n position: fixed;\n inset: auto 16px 16px auto;\n z-index: 2147483000;\n box-sizing: border-box;\n width: calc(min(587px, 100vh - 80px) * 9 / 16);\n height: min(587px, 100vh - 80px);\n margin: 0;\n padding: 0;\n overflow: visible;\n border: 0;\n background: transparent;\n color: inherit;\n font: inherit;\n animation: player-in 0.25s ease;\n }\n\n /* A <dialog> shown as a popover has no open attribute, which the browser\n hides by default. */\n .player:popover-open {\n display: block;\n }\n\n /* Switching modes shows the dialog again; only the first opening fades in. */\n .player[data-switched] {\n animation: none;\n }\n\n .player::backdrop {\n background: transparent;\n }\n\n .player-frame {\n position: relative;\n width: 100%;\n height: 100%;\n }\n\n .player .card {\n width: 100%;\n height: 100%;\n box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);\n }\n\n .player .card:has(.video) {\n cursor: pointer;\n }\n\n /* Play/pause reuses the card's glass .play button, as a real button. */\n .player-toggle {\n padding: 0;\n border: 0;\n color: #fff;\n font: inherit;\n cursor: pointer;\n }\n\n .player[data-paused] .player-toggle,\n .player-toggle:focus-visible {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n\n /* On touch, tapping the video pauses it; the button shows while paused. */\n @media (hover: none) {\n .player:not([data-paused]) .player-toggle {\n opacity: 0;\n }\n }\n\n .player-sound {\n position: absolute;\n top: 12px;\n right: 12px;\n z-index: 1;\n border-color: transparent;\n background: rgba(0, 0, 0, 0.3);\n }\n\n .player-sound .icon {\n width: 20px;\n height: 20px;\n }\n\n /* Above the card: on narrow screens the expanded card can cover them. */\n .player-controls {\n position: absolute;\n right: 0;\n bottom: calc(100% + 8px);\n z-index: 1;\n display: flex;\n gap: 8px;\n }\n\n .player-button {\n display: grid;\n width: 36px;\n height: 36px;\n place-items: center;\n padding: 0;\n border: 1px solid rgba(133, 133, 133, 0.3);\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.32);\n backdrop-filter: blur(5px);\n color: #fff;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .player-button:hover {\n background: rgba(0, 0, 0, 0.55);\n }\n\n .player-button .icon {\n width: 18px;\n height: 18px;\n }\n\n .player[data-expanded] {\n inset: 0;\n display: grid;\n width: 100%;\n max-width: none;\n height: 100%;\n max-height: none;\n place-items: center;\n background: rgba(0, 0, 0, 0.85);\n animation: none;\n }\n\n .player[data-expanded] .player-frame {\n width: calc(min(78vh, 720px) * 9 / 16);\n height: min(78vh, 720px);\n }\n\n .player[data-expanded] .player-controls {\n position: fixed;\n top: 24px;\n right: 24px;\n bottom: auto;\n }\n\n .player[data-expanded] .player-button {\n width: 40px;\n height: 40px;\n }\n\n @media (max-width: 576px) {\n .player {\n right: 12px;\n width: 220px;\n height: 391px;\n }\n\n .player[data-expanded] {\n background: #000;\n }\n\n /* The video stays 9:16 and centered; the rest of the screen is black. */\n .player[data-expanded] .player-frame {\n width: min(100vw, 100vh * 9 / 16);\n height: min(100vh, 100vw * 16 / 9);\n }\n\n .player[data-expanded] .card {\n border-radius: 0;\n }\n\n /* Below the sound button, at the top right of the video. Placed in the\n video frame, not the screen: the frame is centered, so on screens\n taller than 9:16 its top is below the screen's. */\n .player[data-expanded] .player-controls {\n position: absolute;\n top: 60px;\n right: 12px;\n }\n }\n\n @keyframes player-in {\n from {\n opacity: 0;\n transform: translateY(8px);\n }\n }\n\n @media (prefers-reduced-motion: reduce) {\n .player {\n animation: none;\n }\n }\n`;\n\nexport interface PlayerOptions {\n /** Accessible name of the player. */\n label: string;\n /** Accessible names of the buttons. */\n expandLabel: string;\n collapseLabel: string;\n closeLabel: string;\n playLabel: string;\n pauseLabel: string;\n soundOnLabel: string;\n soundOffLabel: string;\n onClose: () => void;\n}\n\nfunction createButton(className: string, label: string, icon: IconName) {\n const button = document.createElement(\"button\");\n button.type = \"button\";\n button.className = `player-button ${className}`;\n button.setAttribute(\"aria-label\", label);\n button.append(createIcon(icon));\n return button;\n}\n\nfunction supportsPopover(player: HTMLDialogElement) {\n return typeof player.showPopover === \"function\";\n}\n\n/** Shows the player as the mini player. It must already be in the page. */\nexport function showPlayer(player: HTMLDialogElement) {\n if (supportsPopover(player)) player.showPopover();\n else player.show();\n}\n\n/** Plays the player's video, if it has one. */\nexport function playPlayer(player: HTMLDialogElement) {\n const video = player.querySelector(\"video\");\n if (video) playVideo(video);\n}\n\n/**\n * Play/pause in the middle of the video and sound on/off at its top right.\n * Clicking the video also plays or pauses it. The buttons follow the video's\n * own events, so a blocked autoplay shows as paused.\n */\nfunction addVideoControls(\n player: HTMLDialogElement,\n card: HTMLElement,\n video: HTMLVideoElement,\n options: PlayerOptions,\n) {\n // Not a .player-button: it keeps the card's larger glass look.\n const toggle = document.createElement(\"button\");\n toggle.type = \"button\";\n toggle.className = \"play player-toggle\";\n const sound = createButton(\"player-sound\", \"\", \"soundOff\");\n\n function syncPlayback() {\n player.toggleAttribute(\"data-paused\", video.paused);\n toggle.setAttribute(\n \"aria-label\",\n video.paused ? options.playLabel : options.pauseLabel,\n );\n toggle.replaceChildren(createIcon(video.paused ? \"play\" : \"pause\"));\n }\n\n function syncSound() {\n sound.setAttribute(\n \"aria-label\",\n video.muted ? options.soundOnLabel : options.soundOffLabel,\n );\n sound.replaceChildren(createIcon(video.muted ? \"soundOff\" : \"soundOn\"));\n }\n\n function togglePlayback() {\n if (video.paused) playVideo(video);\n else video.pause();\n }\n\n video.addEventListener(\"play\", syncPlayback);\n video.addEventListener(\"pause\", syncPlayback);\n video.addEventListener(\"volumechange\", syncSound);\n // Without the video, the cover stays and there is nothing to control.\n video.addEventListener(\"error\", () => {\n toggle.remove();\n sound.remove();\n delete player.dataset.paused;\n });\n\n toggle.addEventListener(\"click\", (event) => {\n event.stopPropagation();\n togglePlayback();\n });\n sound.addEventListener(\"click\", (event) => {\n event.stopPropagation();\n video.muted = !video.muted;\n });\n card.addEventListener(\"click\", () => {\n if (video.isConnected) togglePlayback();\n });\n\n syncPlayback();\n syncSound();\n card.append(toggle, sound);\n}\n\n/**\n * The Story player. It opens as the mini player (see showPlayer); expand\n * switches to the expanded view, a modal dialog, and back. Clicking the\n * backdrop of the expanded view or pressing Escape in it closes the player.\n * The video starts muted (see playPlayer).\n */\nexport function createPlayer(story: StoryMedia, options: PlayerOptions) {\n const player = document.createElement(\"dialog\");\n player.className = \"player\";\n player.setAttribute(\"popover\", \"manual\");\n player.setAttribute(\"aria-label\", options.label);\n\n const frame = document.createElement(\"div\");\n frame.className = \"player-frame\";\n\n const controls = document.createElement(\"div\");\n controls.className = \"player-controls\";\n\n const expand = createButton(\"player-expand\", options.expandLabel, \"expand\");\n const close = createButton(\"player-close\", options.closeLabel, \"close\");\n controls.append(expand, close);\n\n let expanded = false;\n\n // A dialog can't be shown as a popover and as a modal at the same time, so\n // switching modes hides it first, the same way it was shown.\n function hide() {\n if (!expanded && supportsPopover(player)) player.hidePopover();\n else player.close();\n }\n\n expand.addEventListener(\"click\", () => {\n hide();\n expanded = !expanded;\n player.toggleAttribute(\"data-expanded\", expanded);\n player.dataset.switched = \"\";\n expand.setAttribute(\n \"aria-label\",\n expanded ? options.collapseLabel : options.expandLabel,\n );\n expand.replaceChildren(createIcon(expanded ? \"collapse\" : \"expand\"));\n\n if (expanded) player.showModal();\n else showPlayer(player);\n expand.focus();\n });\n\n close.addEventListener(\"click\", options.onClose);\n\n // Clicks on the backdrop of the expanded view reach the dialog itself.\n player.addEventListener(\"click\", (event) => {\n if (expanded && event.target === player) options.onClose();\n });\n\n // Escape on a modal dialog would only hide it: close the player instead.\n player.addEventListener(\"cancel\", (event) => {\n event.preventDefault();\n options.onClose();\n });\n\n const card = createCard(story);\n const video = card.querySelector(\"video\");\n if (video) addVideoControls(player, card, video, options);\n\n frame.append(controls, card);\n player.append(frame);\n return player;\n}\n","import { cardStyles, createCard, playVideo } from \"../components/card\";\nimport {\n createPlayer,\n playerStyles,\n playPlayer,\n showPlayer,\n} from \"../components/player\";\nimport type { StoryMedia } from \"../types\";\n\nexport const TAG_NAME = \"vtex-ads-story\";\n\n// Keeps the module importable where HTMLElement doesn't exist (SSR, Node).\nconst BaseElement: typeof HTMLElement = globalThis.HTMLElement ?? Object;\n\nconst styles = `\n :host {\n display: block;\n font-family: var(--vtex-ads-story-font-family, inherit);\n color: var(--vtex-ads-story-color-text, inherit);\n }\n ${cardStyles}\n ${playerStyles}\n`;\n\n// Temporary: rendered until the element reads Stories from the ad API.\nconst PLACEHOLDER_STORY: StoryMedia = {\n poster_url: \"https://dummyimage.com/1080x1920/e31c57/fff\",\n media_url: \"https://samplelib.com/mp4/sample-10s-vp9.mp4\",\n};\n\n// Temporary: fixed texts until the store can set them by attribute.\nconst LABELS = {\n watch: \"Assistir vídeo\",\n player: \"Vídeo\",\n expand: \"Expandir vídeo\",\n collapse: \"Reduzir vídeo\",\n close: \"Fechar vídeo\",\n play: \"Reproduzir vídeo\",\n pause: \"Pausar vídeo\",\n soundOn: \"Ativar som\",\n soundOff: \"Desativar som\",\n};\n\nexport class VtexAdsStoryElement extends BaseElement {\n private player: HTMLDialogElement | null = null;\n\n private readonly onKeyDown = (event: KeyboardEvent) => {\n if (event.key === \"Escape\") this.closePlayer();\n };\n\n private render() {\n const container = this.shadowRoot?.querySelector(\".root\");\n if (!container) return;\n\n container.replaceChildren(\n createCard(PLACEHOLDER_STORY, {\n label: LABELS.watch,\n soundOnLabel: LABELS.soundOn,\n soundOffLabel: LABELS.soundOff,\n onSelect: () => this.openPlayer(),\n }),\n );\n }\n\n private cardVideo() {\n return this.shadowRoot?.querySelector<HTMLVideoElement>(\n \".root .card video\",\n );\n }\n\n private openPlayer() {\n if (this.player || !this.shadowRoot) return;\n\n this.player = createPlayer(PLACEHOLDER_STORY, {\n label: LABELS.player,\n expandLabel: LABELS.expand,\n collapseLabel: LABELS.collapse,\n closeLabel: LABELS.close,\n playLabel: LABELS.play,\n pauseLabel: LABELS.pause,\n soundOnLabel: LABELS.soundOn,\n soundOffLabel: LABELS.soundOff,\n onClose: () => this.closePlayer(),\n });\n this.shadowRoot.append(this.player);\n showPlayer(this.player);\n // While the player is open, the card behind it stops on its cover.\n this.cardVideo()?.pause();\n playPlayer(this.player);\n document.addEventListener(\"keydown\", this.onKeyDown);\n\n this.player.querySelector<HTMLElement>(\".player-close\")?.focus();\n }\n\n // `removed`: the element is leaving the page, so there is no card to give\n // focus back to or to resume playing (it would play out of sight).\n private closePlayer({ removed = false } = {}) {\n if (!this.player) return;\n\n document.removeEventListener(\"keydown\", this.onKeyDown);\n this.player.remove();\n this.player = null;\n if (removed) return;\n\n const video = this.cardVideo();\n if (video?.autoplay) playVideo(video);\n\n this.shadowRoot?.querySelector<HTMLElement>(\".root .card-open\")?.focus();\n }\n\n connectedCallback() {\n if (!this.shadowRoot) {\n const root = this.attachShadow({ mode: \"open\" });\n root.innerHTML = `<style>${styles}</style><div class=\"root\"></div>`;\n }\n\n this.render();\n }\n\n disconnectedCallback() {\n this.closePlayer({ removed: true });\n }\n}\n\n// Types `document.createElement(\"vtex-ads-story\")` and `querySelector` results\n// as VtexAdsStoryElement in any TypeScript project, no framework needed.\ndeclare global {\n interface HTMLElementTagNameMap {\n \"vtex-ads-story\": VtexAdsStoryElement;\n }\n}\n\nexport function defineVtexAdsStories() {\n if (typeof customElements === \"undefined\") return;\n if (customElements.get(TAG_NAME)) return;\n\n customElements.define(TAG_NAME, VtexAdsStoryElement);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;;;ACAA;;;ACAA;;;ACAA;;;ACAA;;;ACAA;;;ACAA;;;ACaO,IAAM,QAAQ;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAKO,SAAS,WAAW,MAAgB;AACzC,QAAM,WAAW,SAAS,c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AAO,cAA2B,eAAe,MAAtD,mBAAyD;AAAA,EAC3D;AAAA;AAAA;AAAA,EAIQ,YAAY,EAAE,UAAU,MAAM,IAAI,CAAC,GAAG;AAhGhD,QAAAA,KAAA;AAiGI,QAAI,CAAC,KAAK,OAAQ;AAElB,aAAS,oBAAoB,WAAW,KAAK,SAAS;AACtD,SAAK,OAAO,OAAO;AACnB,SAAK,SAAS;AACd,QAAI,QAAS;AAEb,UAAM,QAAQ,KAAK,UAAU;AAC7B,QAAI,+BAAO,SAAU,WAAU,KAAK;AAEpC,WAAAA,MAAA,KAAK,eAAL,gBAAAA,IAAiB,cAA2B,wBAA5C,mBAAiE;AAAA,EACnE;AAAA,EAEA,oBAAoB;AAClB,QAAI,CAAC,KAAK,YAAY;AACpB,YAAM,OAAO,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAC/C,WAAK,YAAY,UAAU,MAAM;AAAA,IACnC;AAEA,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,uBAAuB;AACrB,SAAK,YAAY,EAAE,SAAS,KAAK,CAAC;AAAA,EACpC;AACF;AAUO,SAAS,uBAAuB;AACrC,MAAI,OAAO,mBAAmB,YAAa;AAC3C,MAAI,eAAe,IAAI,QAAQ,EAAG;AAElC,iBAAe,OAAO,UAAU,mBAAmB;AACrD;","names":["_a","_a"]}
@@ -4,6 +4,7 @@ declare class VtexAdsStoryElement extends BaseElement {
4
4
  private player;
5
5
  private readonly onKeyDown;
6
6
  private render;
7
+ private cardVideo;
7
8
  private openPlayer;
8
9
  private closePlayer;
9
10
  connectedCallback(): void;
@@ -4,6 +4,7 @@ declare class VtexAdsStoryElement extends BaseElement {
4
4
  private player;
5
5
  private readonly onKeyDown;
6
6
  private render;
7
+ private cardVideo;
7
8
  private openPlayer;
8
9
  private closePlayer;
9
10
  connectedCallback(): void;