@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.
@@ -1,5 +1,5 @@
1
1
  import { createIcon } from "../icons";
2
- import type { StoryAd } from "../../types";
2
+ import type { StoryMedia } from "../../types";
3
3
 
4
4
  export const cardStyles = `
5
5
  .card {
@@ -12,11 +12,29 @@ export const cardStyles = `
12
12
  background: #000;
13
13
  }
14
14
 
15
- .card[role="button"] {
15
+ /* The card's button covers it. The focus ring goes on the card, since the
16
+ card's overflow would clip the button's own. */
17
+ .card-open {
18
+ position: absolute;
19
+ inset: 0;
20
+ width: 100%;
21
+ height: 100%;
22
+ padding: 0;
23
+ border: 0;
24
+ background: transparent;
16
25
  cursor: pointer;
17
26
  }
18
27
 
19
- .poster {
28
+ .card-open:focus-visible {
29
+ outline: none;
30
+ }
31
+
32
+ .card:has(> .card-open:focus-visible) {
33
+ outline: auto;
34
+ }
35
+
36
+ .poster,
37
+ .video {
20
38
  position: absolute;
21
39
  inset: 0;
22
40
  width: 100%;
@@ -52,6 +70,29 @@ export const cardStyles = `
52
70
  color: #fff;
53
71
  }
54
72
 
73
+ /* Sound on/off, at the top right of the card (as in the prototype). */
74
+ .card-sound {
75
+ position: absolute;
76
+ top: 22px;
77
+ right: 12px;
78
+ z-index: 1;
79
+ display: grid;
80
+ width: 28px;
81
+ height: 28px;
82
+ place-items: center;
83
+ padding: 0;
84
+ border: 0;
85
+ background: transparent;
86
+ color: #fff;
87
+ cursor: pointer;
88
+ filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
89
+ }
90
+
91
+ .card-sound .icon {
92
+ width: 20px;
93
+ height: 20px;
94
+ }
95
+
55
96
  /* With a mouse, the play button shows only on hover or keyboard focus.
56
97
  Touch screens have no hover, so it stays visible there. */
57
98
  @media (hover: hover) and (pointer: fine) {
@@ -61,7 +102,7 @@ export const cardStyles = `
61
102
  }
62
103
 
63
104
  .card:hover .play,
64
- .card:focus-visible .play {
105
+ .card-open:focus-visible .play {
65
106
  opacity: 1;
66
107
  transform: translate(-50%, -50%);
67
108
  }
@@ -73,51 +114,119 @@ export interface CardOptions {
73
114
  label: string;
74
115
  /** Called on click, Enter or Space. */
75
116
  onSelect: () => void;
117
+ /** Accessible names of the sound button, shown when the card has a video. */
118
+ soundOnLabel: string;
119
+ soundOffLabel: string;
76
120
  }
77
121
 
78
- // Only absolute http(s) URLs reach the DOM.
79
- function isHttpUrl(value: string) {
122
+ // Only absolute http(s) URLs reach the DOM. Relative and protocol-relative
123
+ // ones ("/a.jpg", "//host/a.jpg") don't parse without a base, so they're out.
124
+ function httpUrl(value: string | undefined) {
125
+ if (!value) return null;
80
126
  try {
81
127
  const { protocol } = new URL(value);
82
- return protocol === "https:" || protocol === "http:";
128
+ return protocol === "https:" || protocol === "http:" ? value : null;
83
129
  } catch {
84
- return false;
130
+ return null;
85
131
  }
86
132
  }
87
133
 
134
+ function prefersReducedMotion() {
135
+ return (
136
+ globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false
137
+ );
138
+ }
139
+
88
140
  /**
89
- * A 9:16 card with the Story cover. With options, the card is a button with
90
- * a play icon in the middle.
141
+ * Plays a video. A blocked play is ignored: the cover or the paused state
142
+ * stays.
91
143
  */
92
- export function createCard(story: StoryAd, options?: CardOptions) {
144
+ export function playVideo(video: HTMLVideoElement) {
145
+ video.play()?.catch(() => undefined);
146
+ }
147
+
148
+ function createVideo(src: string, poster: string | null) {
149
+ const video = document.createElement("video");
150
+ video.className = "video";
151
+ // Muted and inline is what browsers, iOS included, allow to autoplay.
152
+ video.muted = true;
153
+ video.setAttribute("muted", "");
154
+ video.setAttribute("playsinline", "");
155
+ video.loop = true;
156
+ video.preload = "metadata";
157
+ // No autoplay for people who asked for reduced motion.
158
+ video.autoplay = !prefersReducedMotion();
159
+ if (poster) video.poster = poster;
160
+ video.src = src;
161
+
162
+ // If the video can't load, the cover underneath stays.
163
+ video.addEventListener("error", () => video.remove());
164
+ return video;
165
+ }
166
+
167
+ /** Sound on/off for the card's video, which starts muted. */
168
+ function createSoundButton(video: HTMLVideoElement, options: CardOptions) {
169
+ const button = document.createElement("button");
170
+ button.type = "button";
171
+ button.className = "card-sound";
172
+
173
+ function sync() {
174
+ button.setAttribute(
175
+ "aria-label",
176
+ video.muted ? options.soundOnLabel : options.soundOffLabel,
177
+ );
178
+ button.replaceChildren(createIcon(video.muted ? "soundOff" : "soundOn"));
179
+ }
180
+
181
+ video.addEventListener("volumechange", sync);
182
+ // Without the video, the cover stays and there is no sound to control.
183
+ video.addEventListener("error", () => button.remove());
184
+ button.addEventListener("click", () => {
185
+ video.muted = !video.muted;
186
+ });
187
+
188
+ sync();
189
+ return button;
190
+ }
191
+
192
+ /**
193
+ * A 9:16 card with the Story cover and, with a media_url, the video looping
194
+ * muted over it. With options, a button with a play icon in the middle covers
195
+ * the card, and a card with a video gets a sound button. The sound button is
196
+ * beside the card's button, not inside it: a button can't contain another.
197
+ */
198
+ export function createCard(story: StoryMedia, options?: CardOptions) {
93
199
  const card = document.createElement("div");
94
200
  card.className = "card";
95
201
 
96
- if (story.poster_url && isHttpUrl(story.poster_url)) {
202
+ const posterUrl = httpUrl(story.poster_url);
203
+ if (posterUrl) {
97
204
  const poster = document.createElement("img");
98
205
  poster.className = "poster";
99
206
  poster.alt = "";
100
- poster.src = story.poster_url;
207
+ poster.src = posterUrl;
101
208
  card.append(poster);
102
209
  }
103
210
 
211
+ const mediaUrl = httpUrl(story.media_url);
212
+ const video = mediaUrl ? createVideo(mediaUrl, posterUrl) : null;
213
+ if (video) card.append(video);
214
+
104
215
  if (options) {
105
- card.setAttribute("role", "button");
106
- card.setAttribute("aria-label", options.label);
107
- card.tabIndex = 0;
216
+ const open = document.createElement("button");
217
+ open.type = "button";
218
+ open.className = "card-open";
219
+ open.setAttribute("aria-label", options.label);
108
220
 
109
221
  const play = document.createElement("span");
110
222
  play.className = "play";
111
223
  play.setAttribute("aria-hidden", "true");
112
224
  play.append(createIcon("play"));
113
- card.append(play);
114
-
115
- card.addEventListener("click", options.onSelect);
116
- card.addEventListener("keydown", (event) => {
117
- if (event.key !== "Enter" && event.key !== " ") return;
118
- event.preventDefault();
119
- options.onSelect();
120
- });
225
+ open.append(play);
226
+ open.addEventListener("click", options.onSelect);
227
+
228
+ card.append(open);
229
+ if (video) card.append(createSoundButton(video, options));
121
230
  }
122
231
 
123
232
  return card;
@@ -1,2 +1,2 @@
1
- export { createCard, cardStyles } from "./card";
1
+ export { createCard, cardStyles, playVideo } from "./card";
2
2
  export type { CardOptions } from "./card";
@@ -0,0 +1 @@
1
+ <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>
@@ -0,0 +1 @@
1
+ <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>
@@ -0,0 +1 @@
1
+ <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>
@@ -6,9 +6,20 @@
6
6
  import close from "./assets/close.svg?raw";
7
7
  import collapse from "./assets/collapse.svg?raw";
8
8
  import expand from "./assets/expand.svg?raw";
9
+ import pause from "./assets/pause-fill.svg?raw";
9
10
  import play from "./assets/play-fill.svg?raw";
11
+ import soundOff from "./assets/speaker-x-fill.svg?raw";
12
+ import soundOn from "./assets/speaker-high-fill.svg?raw";
10
13
 
11
- export const icons = { close, collapse, expand, play };
14
+ export const icons = {
15
+ close,
16
+ collapse,
17
+ expand,
18
+ pause,
19
+ play,
20
+ soundOff,
21
+ soundOn,
22
+ };
12
23
 
13
24
  export type IconName = keyof typeof icons;
14
25
 
@@ -1,2 +1,2 @@
1
- export { createPlayer, playerStyles, showPlayer } from "./player";
1
+ export { createPlayer, playerStyles, playPlayer, showPlayer } from "./player";
2
2
  export type { PlayerOptions } from "./player";
@@ -1,6 +1,6 @@
1
- import { createCard } from "../card";
1
+ import { createCard, playVideo } from "../card";
2
2
  import { createIcon, type IconName } from "../icons";
3
- import type { StoryAd } from "../../types";
3
+ import type { StoryMedia } from "../../types";
4
4
 
5
5
  // The player is a <dialog> shown in the top layer, so no store z-index,
6
6
  // overflow or transform can hide or move it.
@@ -56,6 +56,46 @@ export const playerStyles = `
56
56
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
57
57
  }
58
58
 
59
+ .player .card:has(.video) {
60
+ cursor: pointer;
61
+ }
62
+
63
+ /* Play/pause reuses the card's glass .play button, as a real button. */
64
+ .player-toggle {
65
+ padding: 0;
66
+ border: 0;
67
+ color: #fff;
68
+ font: inherit;
69
+ cursor: pointer;
70
+ }
71
+
72
+ .player[data-paused] .player-toggle,
73
+ .player-toggle:focus-visible {
74
+ opacity: 1;
75
+ transform: translate(-50%, -50%);
76
+ }
77
+
78
+ /* On touch, tapping the video pauses it; the button shows while paused. */
79
+ @media (hover: none) {
80
+ .player:not([data-paused]) .player-toggle {
81
+ opacity: 0;
82
+ }
83
+ }
84
+
85
+ .player-sound {
86
+ position: absolute;
87
+ top: 12px;
88
+ right: 12px;
89
+ z-index: 1;
90
+ border-color: transparent;
91
+ background: rgba(0, 0, 0, 0.3);
92
+ }
93
+
94
+ .player-sound .icon {
95
+ width: 20px;
96
+ height: 20px;
97
+ }
98
+
59
99
  /* Above the card: on narrow screens the expanded card can cover them. */
60
100
  .player-controls {
61
101
  position: absolute;
@@ -140,8 +180,12 @@ export const playerStyles = `
140
180
  border-radius: 0;
141
181
  }
142
182
 
183
+ /* Below the sound button, at the top right of the video. Placed in the
184
+ video frame, not the screen: the frame is centered, so on screens
185
+ taller than 9:16 its top is below the screen's. */
143
186
  .player[data-expanded] .player-controls {
144
- top: 12px;
187
+ position: absolute;
188
+ top: 60px;
145
189
  right: 12px;
146
190
  }
147
191
  }
@@ -167,6 +211,10 @@ export interface PlayerOptions {
167
211
  expandLabel: string;
168
212
  collapseLabel: string;
169
213
  closeLabel: string;
214
+ playLabel: string;
215
+ pauseLabel: string;
216
+ soundOnLabel: string;
217
+ soundOffLabel: string;
170
218
  onClose: () => void;
171
219
  }
172
220
 
@@ -189,12 +237,85 @@ export function showPlayer(player: HTMLDialogElement) {
189
237
  else player.show();
190
238
  }
191
239
 
240
+ /** Plays the player's video, if it has one. */
241
+ export function playPlayer(player: HTMLDialogElement) {
242
+ const video = player.querySelector("video");
243
+ if (video) playVideo(video);
244
+ }
245
+
246
+ /**
247
+ * Play/pause in the middle of the video and sound on/off at its top right.
248
+ * Clicking the video also plays or pauses it. The buttons follow the video's
249
+ * own events, so a blocked autoplay shows as paused.
250
+ */
251
+ function addVideoControls(
252
+ player: HTMLDialogElement,
253
+ card: HTMLElement,
254
+ video: HTMLVideoElement,
255
+ options: PlayerOptions,
256
+ ) {
257
+ // Not a .player-button: it keeps the card's larger glass look.
258
+ const toggle = document.createElement("button");
259
+ toggle.type = "button";
260
+ toggle.className = "play player-toggle";
261
+ const sound = createButton("player-sound", "", "soundOff");
262
+
263
+ function syncPlayback() {
264
+ player.toggleAttribute("data-paused", video.paused);
265
+ toggle.setAttribute(
266
+ "aria-label",
267
+ video.paused ? options.playLabel : options.pauseLabel,
268
+ );
269
+ toggle.replaceChildren(createIcon(video.paused ? "play" : "pause"));
270
+ }
271
+
272
+ function syncSound() {
273
+ sound.setAttribute(
274
+ "aria-label",
275
+ video.muted ? options.soundOnLabel : options.soundOffLabel,
276
+ );
277
+ sound.replaceChildren(createIcon(video.muted ? "soundOff" : "soundOn"));
278
+ }
279
+
280
+ function togglePlayback() {
281
+ if (video.paused) playVideo(video);
282
+ else video.pause();
283
+ }
284
+
285
+ video.addEventListener("play", syncPlayback);
286
+ video.addEventListener("pause", syncPlayback);
287
+ video.addEventListener("volumechange", syncSound);
288
+ // Without the video, the cover stays and there is nothing to control.
289
+ video.addEventListener("error", () => {
290
+ toggle.remove();
291
+ sound.remove();
292
+ delete player.dataset.paused;
293
+ });
294
+
295
+ toggle.addEventListener("click", (event) => {
296
+ event.stopPropagation();
297
+ togglePlayback();
298
+ });
299
+ sound.addEventListener("click", (event) => {
300
+ event.stopPropagation();
301
+ video.muted = !video.muted;
302
+ });
303
+ card.addEventListener("click", () => {
304
+ if (video.isConnected) togglePlayback();
305
+ });
306
+
307
+ syncPlayback();
308
+ syncSound();
309
+ card.append(toggle, sound);
310
+ }
311
+
192
312
  /**
193
313
  * The Story player. It opens as the mini player (see showPlayer); expand
194
314
  * switches to the expanded view, a modal dialog, and back. Clicking the
195
315
  * backdrop of the expanded view or pressing Escape in it closes the player.
316
+ * The video starts muted (see playPlayer).
196
317
  */
197
- export function createPlayer(story: StoryAd, options: PlayerOptions) {
318
+ export function createPlayer(story: StoryMedia, options: PlayerOptions) {
198
319
  const player = document.createElement("dialog");
199
320
  player.className = "player";
200
321
  player.setAttribute("popover", "manual");
@@ -248,7 +369,11 @@ export function createPlayer(story: StoryAd, options: PlayerOptions) {
248
369
  options.onClose();
249
370
  });
250
371
 
251
- frame.append(controls, createCard(story));
372
+ const card = createCard(story);
373
+ const video = card.querySelector("video");
374
+ if (video) addVideoControls(player, card, video, options);
375
+
376
+ frame.append(controls, card);
252
377
  player.append(frame);
253
378
  return player;
254
379
  }
@@ -1,6 +1,11 @@
1
- import { cardStyles, createCard } from "../components/card";
2
- import { createPlayer, playerStyles, showPlayer } from "../components/player";
3
- import type { StoryAd } from "../types";
1
+ import { cardStyles, createCard, playVideo } from "../components/card";
2
+ import {
3
+ createPlayer,
4
+ playerStyles,
5
+ playPlayer,
6
+ showPlayer,
7
+ } from "../components/player";
8
+ import type { StoryMedia } from "../types";
4
9
 
5
10
  export const TAG_NAME = "vtex-ads-story";
6
11
 
@@ -18,9 +23,9 @@ const styles = `
18
23
  `;
19
24
 
20
25
  // Temporary: rendered until the element reads Stories from the ad API.
21
- const PLACEHOLDER_STORY: StoryAd = {
22
- ad_id: "placeholder",
26
+ const PLACEHOLDER_STORY: StoryMedia = {
23
27
  poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
28
+ media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4",
24
29
  };
25
30
 
26
31
  // Temporary: fixed texts until the store can set them by attribute.
@@ -30,6 +35,10 @@ const LABELS = {
30
35
  expand: "Expandir vídeo",
31
36
  collapse: "Reduzir vídeo",
32
37
  close: "Fechar vídeo",
38
+ play: "Reproduzir vídeo",
39
+ pause: "Pausar vídeo",
40
+ soundOn: "Ativar som",
41
+ soundOff: "Desativar som",
33
42
  };
34
43
 
35
44
  export class VtexAdsStoryElement extends BaseElement {
@@ -46,11 +55,19 @@ export class VtexAdsStoryElement extends BaseElement {
46
55
  container.replaceChildren(
47
56
  createCard(PLACEHOLDER_STORY, {
48
57
  label: LABELS.watch,
58
+ soundOnLabel: LABELS.soundOn,
59
+ soundOffLabel: LABELS.soundOff,
49
60
  onSelect: () => this.openPlayer(),
50
61
  }),
51
62
  );
52
63
  }
53
64
 
65
+ private cardVideo() {
66
+ return this.shadowRoot?.querySelector<HTMLVideoElement>(
67
+ ".root .card video",
68
+ );
69
+ }
70
+
54
71
  private openPlayer() {
55
72
  if (this.player || !this.shadowRoot) return;
56
73
 
@@ -59,25 +76,36 @@ export class VtexAdsStoryElement extends BaseElement {
59
76
  expandLabel: LABELS.expand,
60
77
  collapseLabel: LABELS.collapse,
61
78
  closeLabel: LABELS.close,
79
+ playLabel: LABELS.play,
80
+ pauseLabel: LABELS.pause,
81
+ soundOnLabel: LABELS.soundOn,
82
+ soundOffLabel: LABELS.soundOff,
62
83
  onClose: () => this.closePlayer(),
63
84
  });
64
85
  this.shadowRoot.append(this.player);
65
86
  showPlayer(this.player);
87
+ // While the player is open, the card behind it stops on its cover.
88
+ this.cardVideo()?.pause();
89
+ playPlayer(this.player);
66
90
  document.addEventListener("keydown", this.onKeyDown);
67
91
 
68
92
  this.player.querySelector<HTMLElement>(".player-close")?.focus();
69
93
  }
70
94
 
71
- private closePlayer({ restoreFocus = true } = {}) {
95
+ // `removed`: the element is leaving the page, so there is no card to give
96
+ // focus back to or to resume playing (it would play out of sight).
97
+ private closePlayer({ removed = false } = {}) {
72
98
  if (!this.player) return;
73
99
 
74
100
  document.removeEventListener("keydown", this.onKeyDown);
75
101
  this.player.remove();
76
102
  this.player = null;
103
+ if (removed) return;
77
104
 
78
- if (restoreFocus) {
79
- this.shadowRoot?.querySelector<HTMLElement>(".root .card")?.focus();
80
- }
105
+ const video = this.cardVideo();
106
+ if (video?.autoplay) playVideo(video);
107
+
108
+ this.shadowRoot?.querySelector<HTMLElement>(".root .card-open")?.focus();
81
109
  }
82
110
 
83
111
  connectedCallback() {
@@ -90,7 +118,7 @@ export class VtexAdsStoryElement extends BaseElement {
90
118
  }
91
119
 
92
120
  disconnectedCallback() {
93
- this.closePlayer({ restoreFocus: false });
121
+ this.closePlayer({ removed: true });
94
122
  }
95
123
  }
96
124
 
package/src/types.ts CHANGED
@@ -6,4 +6,9 @@ export interface StoryAd {
6
6
  ad_id: string;
7
7
  /** Cover image shown before the video plays. */
8
8
  poster_url?: string;
9
+ /** The 9:16 MP4 the card and the player play. */
10
+ media_url?: string;
9
11
  }
12
+
13
+ /** What the card and the player show: the cover and the video. */
14
+ export type StoryMedia = Pick<StoryAd, "poster_url" | "media_url">;