@vtex/ads-stories 0.4.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
 
@@ -29,18 +40,127 @@ var icons = {
29
40
  soundOff: speaker_x_fill_default,
30
41
  soundOn: speaker_high_fill_default
31
42
  };
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;
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
+ `;
40
99
  }
41
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
+
42
162
  // src/components/card/card.ts
43
- var cardStyles = `
163
+ var cardStyles = css`
44
164
  .card {
45
165
  position: relative;
46
166
  box-sizing: border-box;
@@ -147,89 +267,39 @@ var cardStyles = `
147
267
  }
148
268
  }
149
269
  `;
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
- }
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
+ `;
158
281
  }
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;
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
+ `;
229
297
  }
230
298
 
231
299
  // src/components/player/player.ts
232
- 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`
233
303
  .player {
234
304
  position: fixed;
235
305
  inset: auto 16px 16px auto;
@@ -421,132 +491,189 @@ var playerStyles = `
421
491
  }
422
492
  }
423
493
  `;
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";
494
+ function supportsPopover(dialog) {
495
+ return typeof dialog.showPopover === "function";
434
496
  }
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();
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);
466
541
  }
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
- }
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
+ };
536
674
 
537
675
  // src/story/story.ts
538
676
  var TAG_NAME = "vtex-ads-story";
539
- var _a;
540
- var BaseElement = (_a = globalThis.HTMLElement) != null ? _a : Object;
541
- var styles = `
542
- :host {
543
- display: block;
544
- font-family: var(--vtex-ads-story-font-family, inherit);
545
- color: var(--vtex-ads-story-color-text, inherit);
546
- }
547
- ${cardStyles}
548
- ${playerStyles}
549
- `;
550
677
  var PLACEHOLDER_STORY = {
551
678
  poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
552
679
  media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4"
@@ -562,37 +689,11 @@ var LABELS = {
562
689
  soundOn: "Ativar som",
563
690
  soundOff: "Desativar som"
564
691
  };
565
- var VtexAdsStoryElement = class extends BaseElement {
692
+ var VtexAdsStoryElement = class extends LitElement {
566
693
  constructor() {
567
694
  super(...arguments);
568
- this.player = null;
569
- this.onKeyDown = (event) => {
570
- if (event.key === "Escape") this.closePlayer();
571
- };
572
- }
573
- render() {
574
- var _a2;
575
- const container = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector(".root");
576
- if (!container) return;
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, {
695
+ this.cardVideo = new VideoController(this);
696
+ this.player = new PlayerController(this, {
596
697
  label: LABELS.player,
597
698
  expandLabel: LABELS.expand,
598
699
  collapseLabel: LABELS.collapse,
@@ -603,37 +704,57 @@ var VtexAdsStoryElement = class extends BaseElement {
603
704
  soundOffLabel: LABELS.soundOff,
604
705
  onClose: () => this.closePlayer()
605
706
  });
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();
625
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.
626
714
  connectedCallback() {
627
- if (!this.shadowRoot) {
628
- const root = this.attachShadow({ mode: "open" });
629
- root.innerHTML = `<style>${styles}</style><div class="root"></div>`;
630
- }
631
- this.render();
715
+ super.connectedCallback();
716
+ this.cardVideo.resume();
632
717
  }
633
- disconnectedCallback() {
634
- this.closePlayer({ removed: true });
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
+ `;
635
745
  }
636
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
+ ];
637
758
  function defineVtexAdsStories() {
638
759
  if (typeof customElements === "undefined") return;
639
760
  if (customElements.get(TAG_NAME)) return;