@vtex/ads-stories 0.3.0 → 0.4.1

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,14 @@
1
+ // src/story/story.ts
2
+ import { css as css3, html as html4, LitElement } from "lit";
3
+
4
+ // src/components/card/card.ts
5
+ import { css, html as html2, nothing as nothing2 } from "lit";
6
+
7
+ // src/components/icons/index.ts
8
+ import {
9
+ unsafeHTML
10
+ } from "lit/directives/unsafe-html.js";
11
+
1
12
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg
2
13
  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
14
 
@@ -7,23 +18,149 @@ var collapse_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256
7
18
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg
8
19
  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
20
 
21
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/pause-fill.svg
22
+ 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';
23
+
10
24
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg
11
25
  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';
12
26
 
27
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-x-fill.svg
28
+ 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';
29
+
30
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-high-fill.svg
31
+ 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';
32
+
13
33
  // src/components/icons/index.ts
14
- var icons = { close: close_default, collapse: collapse_default, expand: expand_default, play: play_fill_default };
15
- function createIcon(name) {
16
- const template = document.createElement("template");
17
- template.innerHTML = icons[name];
18
- const svg = template.content.firstElementChild;
19
- svg.classList.add("icon");
20
- svg.setAttribute("aria-hidden", "true");
21
- svg.setAttribute("focusable", "false");
22
- return svg;
34
+ var icons = {
35
+ close: close_default,
36
+ collapse: collapse_default,
37
+ expand: expand_default,
38
+ pause: pause_fill_default,
39
+ play: play_fill_default,
40
+ soundOff: speaker_x_fill_default,
41
+ soundOn: speaker_high_fill_default
42
+ };
43
+ var markup = Object.fromEntries(
44
+ Object.entries(icons).map(([name, svg]) => [
45
+ name,
46
+ svg.replace(
47
+ "<svg",
48
+ '<svg class="icon" aria-hidden="true" focusable="false"'
49
+ )
50
+ ])
51
+ );
52
+ function icon(name) {
53
+ return unsafeHTML(markup[name]);
54
+ }
55
+
56
+ // src/components/media/media.ts
57
+ import { html, nothing } from "lit";
58
+ import { ref } from "lit/directives/ref.js";
59
+ function httpUrl(value) {
60
+ if (!value) return null;
61
+ try {
62
+ const { protocol } = new URL(value);
63
+ return protocol === "https:" || protocol === "http:" ? value : null;
64
+ } catch (e) {
65
+ return null;
66
+ }
67
+ }
68
+ function prefersReducedMotion() {
69
+ var _a, _b;
70
+ return (_b = (_a = globalThis.matchMedia) == null ? void 0 : _a.call(globalThis, "(prefers-reduced-motion: reduce)").matches) != null ? _b : false;
71
+ }
72
+ function hasVideo(story, video) {
73
+ return !video.failed && httpUrl(story.media_url) !== null;
74
+ }
75
+ function renderMedia(story, video) {
76
+ const posterUrl = httpUrl(story.poster_url);
77
+ const mediaUrl = httpUrl(story.media_url);
78
+ return html`
79
+ ${posterUrl ? html`<img class="poster" alt="" src=${posterUrl} />` : nothing}
80
+ ${mediaUrl && !video.failed ? html`
81
+ <video
82
+ class="video"
83
+ muted
84
+ playsinline
85
+ loop
86
+ preload="metadata"
87
+ .muted=${true}
88
+ ?autoplay=${!prefersReducedMotion()}
89
+ poster=${posterUrl != null ? posterUrl : nothing}
90
+ src=${mediaUrl}
91
+ ${ref(video.ref)}
92
+ @play=${video.sync}
93
+ @pause=${video.sync}
94
+ @volumechange=${video.sync}
95
+ @error=${video.fail}
96
+ ></video>
97
+ ` : nothing}
98
+ `;
23
99
  }
24
100
 
101
+ // src/components/media/video-controller.ts
102
+ import { createRef } from "lit/directives/ref.js";
103
+ function playVideo(video) {
104
+ var _a;
105
+ (_a = video.play()) == null ? void 0 : _a.catch(() => void 0);
106
+ }
107
+ var VideoController = class {
108
+ constructor(host) {
109
+ this.host = host;
110
+ /** Bound to the <video> by renderMedia. */
111
+ this.ref = createRef();
112
+ this.paused = true;
113
+ // The video always starts muted: that is what browsers allow to autoplay.
114
+ this.muted = true;
115
+ /** The video could not load: only the cover is shown. */
116
+ this.failed = false;
117
+ /** Listener for the video's play, pause and volumechange events. */
118
+ this.sync = () => {
119
+ const video = this.ref.value;
120
+ if (!video) return;
121
+ this.paused = video.paused;
122
+ this.muted = video.muted;
123
+ this.host.requestUpdate();
124
+ };
125
+ /** Listener for the video's error event. */
126
+ this.fail = () => {
127
+ this.failed = true;
128
+ this.host.requestUpdate();
129
+ };
130
+ }
131
+ /** For a new <video>, such as each time the player opens. */
132
+ reset() {
133
+ this.paused = true;
134
+ this.muted = true;
135
+ this.failed = false;
136
+ }
137
+ play() {
138
+ const video = this.ref.value;
139
+ if (video) playVideo(video);
140
+ }
141
+ pause() {
142
+ var _a;
143
+ (_a = this.ref.value) == null ? void 0 : _a.pause();
144
+ }
145
+ /** Plays again if the video plays on its own (see renderMedia). */
146
+ resume() {
147
+ const video = this.ref.value;
148
+ if (video == null ? void 0 : video.autoplay) playVideo(video);
149
+ }
150
+ togglePlayback() {
151
+ const video = this.ref.value;
152
+ if (!video) return;
153
+ if (video.paused) playVideo(video);
154
+ else video.pause();
155
+ }
156
+ toggleMuted() {
157
+ const video = this.ref.value;
158
+ if (video) video.muted = !video.muted;
159
+ }
160
+ };
161
+
25
162
  // src/components/card/card.ts
26
- var cardStyles = `
163
+ var cardStyles = css`
27
164
  .card {
28
165
  position: relative;
29
166
  box-sizing: border-box;
@@ -34,11 +171,29 @@ var cardStyles = `
34
171
  background: #000;
35
172
  }
36
173
 
37
- .card[role="button"] {
174
+ /* The card's button covers it. The focus ring goes on the card, since the
175
+ card's overflow would clip the button's own. */
176
+ .card-open {
177
+ position: absolute;
178
+ inset: 0;
179
+ width: 100%;
180
+ height: 100%;
181
+ padding: 0;
182
+ border: 0;
183
+ background: transparent;
38
184
  cursor: pointer;
39
185
  }
40
186
 
41
- .poster {
187
+ .card-open:focus-visible {
188
+ outline: none;
189
+ }
190
+
191
+ .card:has(> .card-open:focus-visible) {
192
+ outline: auto;
193
+ }
194
+
195
+ .poster,
196
+ .video {
42
197
  position: absolute;
43
198
  inset: 0;
44
199
  width: 100%;
@@ -74,6 +229,29 @@ var cardStyles = `
74
229
  color: #fff;
75
230
  }
76
231
 
232
+ /* Sound on/off, at the top right of the card (as in the prototype). */
233
+ .card-sound {
234
+ position: absolute;
235
+ top: 22px;
236
+ right: 12px;
237
+ z-index: 1;
238
+ display: grid;
239
+ width: 28px;
240
+ height: 28px;
241
+ place-items: center;
242
+ padding: 0;
243
+ border: 0;
244
+ background: transparent;
245
+ color: #fff;
246
+ cursor: pointer;
247
+ filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
248
+ }
249
+
250
+ .card-sound .icon {
251
+ width: 20px;
252
+ height: 20px;
253
+ }
254
+
77
255
  /* With a mouse, the play button shows only on hover or keyboard focus.
78
256
  Touch screens have no hover, so it stays visible there. */
79
257
  @media (hover: hover) and (pointer: fine) {
@@ -83,51 +261,45 @@ var cardStyles = `
83
261
  }
84
262
 
85
263
  .card:hover .play,
86
- .card:focus-visible .play {
264
+ .card-open:focus-visible .play {
87
265
  opacity: 1;
88
266
  transform: translate(-50%, -50%);
89
267
  }
90
268
  }
91
269
  `;
92
- function isHttpUrl(value) {
93
- try {
94
- const { protocol } = new URL(value);
95
- return protocol === "https:" || protocol === "http:";
96
- } catch (e) {
97
- return false;
98
- }
270
+ function renderSoundButton(video, options) {
271
+ return html2`
272
+ <button
273
+ type="button"
274
+ class="card-sound"
275
+ aria-label=${video.muted ? options.soundOnLabel : options.soundOffLabel}
276
+ @click=${() => video.toggleMuted()}
277
+ >
278
+ ${icon(video.muted ? "soundOff" : "soundOn")}
279
+ </button>
280
+ `;
99
281
  }
100
- function createCard(story, options) {
101
- const card = document.createElement("div");
102
- card.className = "card";
103
- if (story.poster_url && isHttpUrl(story.poster_url)) {
104
- const poster = document.createElement("img");
105
- poster.className = "poster";
106
- poster.alt = "";
107
- poster.src = story.poster_url;
108
- card.append(poster);
109
- }
110
- if (options) {
111
- card.setAttribute("role", "button");
112
- card.setAttribute("aria-label", options.label);
113
- card.tabIndex = 0;
114
- const play = document.createElement("span");
115
- play.className = "play";
116
- play.setAttribute("aria-hidden", "true");
117
- play.append(createIcon("play"));
118
- card.append(play);
119
- card.addEventListener("click", options.onSelect);
120
- card.addEventListener("keydown", (event) => {
121
- if (event.key !== "Enter" && event.key !== " ") return;
122
- event.preventDefault();
123
- options.onSelect();
124
- });
125
- }
126
- return card;
282
+ function renderCard(story, video, options) {
283
+ return html2`
284
+ <div class="card">
285
+ ${renderMedia(story, video)}
286
+ <button
287
+ type="button"
288
+ class="card-open"
289
+ aria-label=${options.label}
290
+ @click=${options.onSelect}
291
+ >
292
+ <span class="play" aria-hidden="true">${icon("play")}</span>
293
+ </button>
294
+ ${hasVideo(story, video) && !video.paused ? renderSoundButton(video, options) : nothing2}
295
+ </div>
296
+ `;
127
297
  }
128
298
 
129
299
  // src/components/player/player.ts
130
- var playerStyles = `
300
+ import { css as css2, html as html3, nothing as nothing3 } from "lit";
301
+ import { createRef as createRef2, ref as ref2 } from "lit/directives/ref.js";
302
+ var playerStyles = css2`
131
303
  .player {
132
304
  position: fixed;
133
305
  inset: auto 16px 16px auto;
@@ -172,6 +344,46 @@ var playerStyles = `
172
344
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
173
345
  }
174
346
 
347
+ .player .card:has(.video) {
348
+ cursor: pointer;
349
+ }
350
+
351
+ /* Play/pause reuses the card's glass .play button, as a real button. */
352
+ .player-toggle {
353
+ padding: 0;
354
+ border: 0;
355
+ color: #fff;
356
+ font: inherit;
357
+ cursor: pointer;
358
+ }
359
+
360
+ .player[data-paused] .player-toggle,
361
+ .player-toggle:focus-visible {
362
+ opacity: 1;
363
+ transform: translate(-50%, -50%);
364
+ }
365
+
366
+ /* On touch, tapping the video pauses it; the button shows while paused. */
367
+ @media (hover: none) {
368
+ .player:not([data-paused]) .player-toggle {
369
+ opacity: 0;
370
+ }
371
+ }
372
+
373
+ .player-sound {
374
+ position: absolute;
375
+ top: 12px;
376
+ right: 12px;
377
+ z-index: 1;
378
+ border-color: transparent;
379
+ background: rgba(0, 0, 0, 0.3);
380
+ }
381
+
382
+ .player-sound .icon {
383
+ width: 20px;
384
+ height: 20px;
385
+ }
386
+
175
387
  /* Above the card: on narrow screens the expanded card can cover them. */
176
388
  .player-controls {
177
389
  position: absolute;
@@ -256,8 +468,12 @@ var playerStyles = `
256
468
  border-radius: 0;
257
469
  }
258
470
 
471
+ /* Below the sound button, at the top right of the video. Placed in the
472
+ video frame, not the screen: the frame is centered, so on screens
473
+ taller than 9:16 its top is below the screen's. */
259
474
  .player[data-expanded] .player-controls {
260
- top: 12px;
475
+ position: absolute;
476
+ top: 60px;
261
477
  right: 12px;
262
478
  }
263
479
  }
@@ -275,144 +491,270 @@ var playerStyles = `
275
491
  }
276
492
  }
277
493
  `;
278
- function createButton(className, label, icon) {
279
- const button = document.createElement("button");
280
- button.type = "button";
281
- button.className = `player-button ${className}`;
282
- button.setAttribute("aria-label", label);
283
- button.append(createIcon(icon));
284
- return button;
285
- }
286
- function supportsPopover(player) {
287
- return typeof player.showPopover === "function";
288
- }
289
- function showPlayer(player) {
290
- if (supportsPopover(player)) player.showPopover();
291
- else player.show();
292
- }
293
- function createPlayer(story, options) {
294
- const player = document.createElement("dialog");
295
- player.className = "player";
296
- player.setAttribute("popover", "manual");
297
- player.setAttribute("aria-label", options.label);
298
- const frame = document.createElement("div");
299
- frame.className = "player-frame";
300
- const controls = document.createElement("div");
301
- controls.className = "player-controls";
302
- const expand = createButton("player-expand", options.expandLabel, "expand");
303
- const close = createButton("player-close", options.closeLabel, "close");
304
- controls.append(expand, close);
305
- let expanded = false;
306
- function hide() {
307
- if (!expanded && supportsPopover(player)) player.hidePopover();
308
- else player.close();
309
- }
310
- expand.addEventListener("click", () => {
311
- hide();
312
- expanded = !expanded;
313
- player.toggleAttribute("data-expanded", expanded);
314
- player.dataset.switched = "";
315
- expand.setAttribute(
316
- "aria-label",
317
- expanded ? options.collapseLabel : options.expandLabel
318
- );
319
- expand.replaceChildren(createIcon(expanded ? "collapse" : "expand"));
320
- if (expanded) player.showModal();
321
- else showPlayer(player);
322
- expand.focus();
323
- });
324
- close.addEventListener("click", options.onClose);
325
- player.addEventListener("click", (event) => {
326
- if (expanded && event.target === player) options.onClose();
327
- });
328
- player.addEventListener("cancel", (event) => {
329
- event.preventDefault();
330
- options.onClose();
331
- });
332
- frame.append(controls, createCard(story));
333
- player.append(frame);
334
- return player;
494
+ function supportsPopover(dialog) {
495
+ return typeof dialog.showPopover === "function";
335
496
  }
497
+ var PlayerController = class {
498
+ constructor(host, options) {
499
+ this.host = host;
500
+ this.options = options;
501
+ /** Rendered and, once the host has updated, shown. */
502
+ this.isOpen = false;
503
+ this.expanded = false;
504
+ /** Switched modes at least once: only the first opening fades in. */
505
+ this.switched = false;
506
+ /** The dialog is showing, in the mode `expanded` says. */
507
+ this.shown = false;
508
+ this.dialog = createRef2();
509
+ this.expandButton = createRef2();
510
+ this.closeButton = createRef2();
511
+ this.toggleExpanded = async () => {
512
+ var _a;
513
+ const dialog = this.dialog.value;
514
+ if (!dialog) return;
515
+ this.hide(dialog);
516
+ this.expanded = !this.expanded;
517
+ this.switched = true;
518
+ this.host.requestUpdate();
519
+ await this.host.updateComplete;
520
+ if (!this.isOpen) return;
521
+ this.show(dialog);
522
+ (_a = this.expandButton.value) == null ? void 0 : _a.focus();
523
+ };
524
+ // Escape anywhere on the page, also in the mini player, which is not modal.
525
+ this.onKeyDown = (event) => {
526
+ if (event.key === "Escape") this.options.onClose();
527
+ };
528
+ // Clicks on the backdrop of the expanded view reach the dialog itself.
529
+ this.onDialogClick = (event) => {
530
+ if (this.expanded && event.target === event.currentTarget) {
531
+ this.options.onClose();
532
+ }
533
+ };
534
+ // Escape on a modal dialog would only hide it: close the player instead.
535
+ this.onCancel = (event) => {
536
+ event.preventDefault();
537
+ this.options.onClose();
538
+ };
539
+ host.addController(this);
540
+ this.video = new VideoController(host);
541
+ }
542
+ hostDisconnected() {
543
+ this.close();
544
+ }
545
+ /** Opens the mini player, plays the video and focuses the close button. */
546
+ async open() {
547
+ var _a;
548
+ if (this.isOpen) return;
549
+ document.addEventListener("keydown", this.onKeyDown);
550
+ this.isOpen = true;
551
+ this.expanded = false;
552
+ this.switched = false;
553
+ this.video.reset();
554
+ this.host.requestUpdate();
555
+ await this.host.updateComplete;
556
+ const dialog = this.dialog.value;
557
+ if (!this.isOpen || !dialog) return;
558
+ this.show(dialog);
559
+ this.video.play();
560
+ (_a = this.closeButton.value) == null ? void 0 : _a.focus();
561
+ }
562
+ /** Hides the player now; the host removes it on its next update. */
563
+ close() {
564
+ if (!this.isOpen) return;
565
+ document.removeEventListener("keydown", this.onKeyDown);
566
+ const dialog = this.dialog.value;
567
+ if (dialog) this.hide(dialog);
568
+ this.isOpen = false;
569
+ this.host.requestUpdate();
570
+ }
571
+ // Mini: a manual popover, or a non-modal dialog without popover support.
572
+ // Expanded: a modal dialog.
573
+ // Already showing: a second expand click in the same task resumes after the
574
+ // first one has shown the dialog, and showing it again would throw.
575
+ show(dialog) {
576
+ if (this.shown) return;
577
+ if (this.expanded) dialog.showModal();
578
+ else if (supportsPopover(dialog)) dialog.showPopover();
579
+ else dialog.show();
580
+ this.shown = true;
581
+ }
582
+ // A dialog can't be shown as a popover and as a modal at the same time, so
583
+ // switching modes hides it first, the same way it was shown.
584
+ hide(dialog) {
585
+ if (!this.shown) return;
586
+ if (!this.expanded && supportsPopover(dialog)) dialog.hidePopover();
587
+ else dialog.close();
588
+ this.shown = false;
589
+ }
590
+ /**
591
+ * Play/pause in the middle of the video and sound on/off at its top right.
592
+ * They follow the video's own events, so a blocked autoplay shows as
593
+ * paused.
594
+ */
595
+ renderVideoControls() {
596
+ const { video, options } = this;
597
+ const onToggle = (event) => {
598
+ event.stopPropagation();
599
+ video.togglePlayback();
600
+ };
601
+ const onSound = (event) => {
602
+ event.stopPropagation();
603
+ video.toggleMuted();
604
+ };
605
+ return html3`
606
+ <button
607
+ type="button"
608
+ class="play player-toggle"
609
+ aria-label=${video.paused ? options.playLabel : options.pauseLabel}
610
+ @click=${onToggle}
611
+ >
612
+ ${icon(video.paused ? "play" : "pause")}
613
+ </button>
614
+ <button
615
+ type="button"
616
+ class="player-button player-sound"
617
+ aria-label=${video.muted ? options.soundOnLabel : options.soundOffLabel}
618
+ @click=${onSound}
619
+ >
620
+ ${icon(video.muted ? "soundOff" : "soundOn")}
621
+ </button>
622
+ `;
623
+ }
624
+ /**
625
+ * The player <dialog> while it is open. Clicking the video also plays or
626
+ * pauses it.
627
+ */
628
+ render(story) {
629
+ if (!this.isOpen) return nothing3;
630
+ const { video, options, expanded } = this;
631
+ const withVideo = hasVideo(story, video);
632
+ return html3`
633
+ <dialog
634
+ class="player"
635
+ popover="manual"
636
+ aria-label=${options.label}
637
+ ?data-expanded=${expanded}
638
+ ?data-switched=${this.switched}
639
+ ?data-paused=${withVideo && video.paused}
640
+ ${ref2(this.dialog)}
641
+ @click=${this.onDialogClick}
642
+ @cancel=${this.onCancel}
643
+ >
644
+ <div class="player-frame">
645
+ <div class="player-controls">
646
+ <button
647
+ type="button"
648
+ class="player-button player-expand"
649
+ aria-label=${expanded ? options.collapseLabel : options.expandLabel}
650
+ ${ref2(this.expandButton)}
651
+ @click=${this.toggleExpanded}
652
+ >
653
+ ${icon(expanded ? "collapse" : "expand")}
654
+ </button>
655
+ <button
656
+ type="button"
657
+ class="player-button player-close"
658
+ aria-label=${options.closeLabel}
659
+ ${ref2(this.closeButton)}
660
+ @click=${options.onClose}
661
+ >
662
+ ${icon("close")}
663
+ </button>
664
+ </div>
665
+ <div class="card" @click=${() => video.togglePlayback()}>
666
+ ${renderMedia(story, video)}
667
+ ${withVideo ? this.renderVideoControls() : nothing3}
668
+ </div>
669
+ </div>
670
+ </dialog>
671
+ `;
672
+ }
673
+ };
336
674
 
337
675
  // src/story/story.ts
338
676
  var TAG_NAME = "vtex-ads-story";
339
- var _a;
340
- var BaseElement = (_a = globalThis.HTMLElement) != null ? _a : Object;
341
- var styles = `
342
- :host {
343
- display: block;
344
- font-family: var(--vtex-ads-story-font-family, inherit);
345
- color: var(--vtex-ads-story-color-text, inherit);
346
- }
347
- ${cardStyles}
348
- ${playerStyles}
349
- `;
350
677
  var PLACEHOLDER_STORY = {
351
- ad_id: "placeholder",
352
- poster_url: "https://dummyimage.com/1080x1920/e31c57/fff"
678
+ poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
679
+ media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4"
353
680
  };
354
681
  var LABELS = {
355
682
  watch: "Assistir v\xEDdeo",
356
683
  player: "V\xEDdeo",
357
684
  expand: "Expandir v\xEDdeo",
358
685
  collapse: "Reduzir v\xEDdeo",
359
- close: "Fechar v\xEDdeo"
686
+ close: "Fechar v\xEDdeo",
687
+ play: "Reproduzir v\xEDdeo",
688
+ pause: "Pausar v\xEDdeo",
689
+ soundOn: "Ativar som",
690
+ soundOff: "Desativar som"
360
691
  };
361
- var VtexAdsStoryElement = class extends BaseElement {
692
+ var VtexAdsStoryElement = class extends LitElement {
362
693
  constructor() {
363
694
  super(...arguments);
364
- this.player = null;
365
- this.onKeyDown = (event) => {
366
- if (event.key === "Escape") this.closePlayer();
367
- };
368
- }
369
- render() {
370
- var _a2;
371
- const container = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root");
372
- if (!container) return;
373
- container.replaceChildren(
374
- createCard(PLACEHOLDER_STORY, {
375
- label: LABELS.watch,
376
- onSelect: () => this.openPlayer()
377
- })
378
- );
379
- }
380
- openPlayer() {
381
- var _a2;
382
- if (this.player || !this.shadowRoot) return;
383
- this.player = createPlayer(PLACEHOLDER_STORY, {
695
+ this.cardVideo = new VideoController(this);
696
+ this.player = new PlayerController(this, {
384
697
  label: LABELS.player,
385
698
  expandLabel: LABELS.expand,
386
699
  collapseLabel: LABELS.collapse,
387
700
  closeLabel: LABELS.close,
701
+ playLabel: LABELS.play,
702
+ pauseLabel: LABELS.pause,
703
+ soundOnLabel: LABELS.soundOn,
704
+ soundOffLabel: LABELS.soundOff,
388
705
  onClose: () => this.closePlayer()
389
706
  });
390
- this.shadowRoot.append(this.player);
391
- showPlayer(this.player);
392
- document.addEventListener("keydown", this.onKeyDown);
393
- (_a2 = this.player.querySelector(".player-close")) == null ? void 0 : _a2.focus();
394
- }
395
- closePlayer({ restoreFocus = true } = {}) {
396
- var _a2, _b;
397
- if (!this.player) return;
398
- document.removeEventListener("keydown", this.onKeyDown);
399
- this.player.remove();
400
- this.player = null;
401
- if (restoreFocus) {
402
- (_b = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root .card")) == null ? void 0 : _b.focus();
403
- }
404
707
  }
708
+ // Lit keeps the card's DOM when the element is moved (removed and appended
709
+ // again), but the browser pauses a removed video and doesn't autoplay it
710
+ // again. resume() only plays a video that autoplays (not with reduced
711
+ // motion), and does nothing on the first connection, before the render.
712
+ // Not in VideoController.hostConnected: there, the player's video is still
713
+ // in the DOM until the next update and would play for a moment.
405
714
  connectedCallback() {
406
- if (!this.shadowRoot) {
407
- const root = this.attachShadow({ mode: "open" });
408
- root.innerHTML = `<style>${styles}</style><div class="root"></div>`;
409
- }
410
- this.render();
715
+ super.connectedCallback();
716
+ this.cardVideo.resume();
411
717
  }
412
- disconnectedCallback() {
413
- this.closePlayer({ restoreFocus: false });
718
+ openPlayer() {
719
+ if (this.player.isOpen) return;
720
+ this.cardVideo.pause();
721
+ void this.player.open();
722
+ }
723
+ // The player also closes on its own when the element leaves the page,
724
+ // without this: there is no card to give focus back to or to resume playing
725
+ // (it would play out of sight).
726
+ closePlayer() {
727
+ var _a;
728
+ if (!this.player.isOpen) return;
729
+ this.player.close();
730
+ this.cardVideo.resume();
731
+ (_a = this.renderRoot.querySelector(".root .card-open")) == null ? void 0 : _a.focus();
732
+ }
733
+ render() {
734
+ return html4`
735
+ <div class="root">
736
+ ${renderCard(PLACEHOLDER_STORY, this.cardVideo, {
737
+ label: LABELS.watch,
738
+ soundOnLabel: LABELS.soundOn,
739
+ soundOffLabel: LABELS.soundOff,
740
+ onSelect: () => this.openPlayer()
741
+ })}
742
+ </div>
743
+ ${this.player.render(PLACEHOLDER_STORY)}
744
+ `;
414
745
  }
415
746
  };
747
+ VtexAdsStoryElement.styles = [
748
+ css3`
749
+ :host {
750
+ display: block;
751
+ font-family: var(--vtex-ads-story-font-family, inherit);
752
+ color: var(--vtex-ads-story-color-text, inherit);
753
+ }
754
+ `,
755
+ cardStyles,
756
+ playerStyles
757
+ ];
416
758
  function defineVtexAdsStories() {
417
759
  if (typeof customElements === "undefined") return;
418
760
  if (customElements.get(TAG_NAME)) return;