@vtex/ads-stories 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +12 -4
- package/dist/cdn/vtex-ads-stories.js +309 -2
- package/dist/cdn/vtex-ads-stories.js.map +1 -1
- package/dist/cjs/index.cjs +612 -25
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +6 -5
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/cjs/react.d.cts +1 -4
- package/dist/esm/index.d.ts +6 -5
- package/dist/esm/index.js +612 -25
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +1 -4
- package/package.json +2 -1
- package/src/components/card/card.ts +233 -0
- package/src/components/card/index.ts +2 -0
- package/src/components/icons/LICENSE +21 -0
- package/src/components/icons/assets/close.svg +1 -0
- package/src/components/icons/assets/collapse.svg +1 -0
- package/src/components/icons/assets/expand.svg +1 -0
- package/src/components/icons/assets/pause-fill.svg +1 -0
- package/src/components/icons/assets/play-fill.svg +1 -0
- package/src/components/icons/assets/speaker-high-fill.svg +1 -0
- package/src/components/icons/assets/speaker-x-fill.svg +1 -0
- package/src/components/icons/index.ts +36 -0
- package/src/components/player/index.ts +2 -0
- package/src/components/player/player.ts +379 -0
- package/src/raw.d.ts +5 -0
- package/src/react.ts +1 -4
- package/src/story/story.ts +89 -27
- package/src/types.ts +14 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { createCard, playVideo } from "../card";
|
|
2
|
+
import { createIcon, type IconName } from "../icons";
|
|
3
|
+
import type { StoryMedia } from "../../types";
|
|
4
|
+
|
|
5
|
+
// The player is a <dialog> shown in the top layer, so no store z-index,
|
|
6
|
+
// overflow or transform can hide or move it.
|
|
7
|
+
//
|
|
8
|
+
// Mini: a manual popover (non-modal, the page stays usable), with the card at
|
|
9
|
+
// the bottom right and the controls above it. Without popover support it is a
|
|
10
|
+
// non-modal dialog, fixed with a high z-index.
|
|
11
|
+
// Expanded: a modal dialog (showModal), so the browser makes the page behind
|
|
12
|
+
// it inert. The card is centered over a dark backdrop, with the controls at
|
|
13
|
+
// the top right of the screen. On narrow screens it fills the screen.
|
|
14
|
+
export const playerStyles = `
|
|
15
|
+
.player {
|
|
16
|
+
position: fixed;
|
|
17
|
+
inset: auto 16px 16px auto;
|
|
18
|
+
z-index: 2147483000;
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
width: calc(min(587px, 100vh - 80px) * 9 / 16);
|
|
21
|
+
height: min(587px, 100vh - 80px);
|
|
22
|
+
margin: 0;
|
|
23
|
+
padding: 0;
|
|
24
|
+
overflow: visible;
|
|
25
|
+
border: 0;
|
|
26
|
+
background: transparent;
|
|
27
|
+
color: inherit;
|
|
28
|
+
font: inherit;
|
|
29
|
+
animation: player-in 0.25s ease;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* A <dialog> shown as a popover has no open attribute, which the browser
|
|
33
|
+
hides by default. */
|
|
34
|
+
.player:popover-open {
|
|
35
|
+
display: block;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* Switching modes shows the dialog again; only the first opening fades in. */
|
|
39
|
+
.player[data-switched] {
|
|
40
|
+
animation: none;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.player::backdrop {
|
|
44
|
+
background: transparent;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.player-frame {
|
|
48
|
+
position: relative;
|
|
49
|
+
width: 100%;
|
|
50
|
+
height: 100%;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.player .card {
|
|
54
|
+
width: 100%;
|
|
55
|
+
height: 100%;
|
|
56
|
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
|
|
57
|
+
}
|
|
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
|
+
|
|
99
|
+
/* Above the card: on narrow screens the expanded card can cover them. */
|
|
100
|
+
.player-controls {
|
|
101
|
+
position: absolute;
|
|
102
|
+
right: 0;
|
|
103
|
+
bottom: calc(100% + 8px);
|
|
104
|
+
z-index: 1;
|
|
105
|
+
display: flex;
|
|
106
|
+
gap: 8px;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.player-button {
|
|
110
|
+
display: grid;
|
|
111
|
+
width: 36px;
|
|
112
|
+
height: 36px;
|
|
113
|
+
place-items: center;
|
|
114
|
+
padding: 0;
|
|
115
|
+
border: 1px solid rgba(133, 133, 133, 0.3);
|
|
116
|
+
border-radius: 50%;
|
|
117
|
+
background: rgba(0, 0, 0, 0.32);
|
|
118
|
+
backdrop-filter: blur(5px);
|
|
119
|
+
color: #fff;
|
|
120
|
+
cursor: pointer;
|
|
121
|
+
transition: background-color 0.2s;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.player-button:hover {
|
|
125
|
+
background: rgba(0, 0, 0, 0.55);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.player-button .icon {
|
|
129
|
+
width: 18px;
|
|
130
|
+
height: 18px;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.player[data-expanded] {
|
|
134
|
+
inset: 0;
|
|
135
|
+
display: grid;
|
|
136
|
+
width: 100%;
|
|
137
|
+
max-width: none;
|
|
138
|
+
height: 100%;
|
|
139
|
+
max-height: none;
|
|
140
|
+
place-items: center;
|
|
141
|
+
background: rgba(0, 0, 0, 0.85);
|
|
142
|
+
animation: none;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.player[data-expanded] .player-frame {
|
|
146
|
+
width: calc(min(78vh, 720px) * 9 / 16);
|
|
147
|
+
height: min(78vh, 720px);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.player[data-expanded] .player-controls {
|
|
151
|
+
position: fixed;
|
|
152
|
+
top: 24px;
|
|
153
|
+
right: 24px;
|
|
154
|
+
bottom: auto;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.player[data-expanded] .player-button {
|
|
158
|
+
width: 40px;
|
|
159
|
+
height: 40px;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@media (max-width: 576px) {
|
|
163
|
+
.player {
|
|
164
|
+
right: 12px;
|
|
165
|
+
width: 220px;
|
|
166
|
+
height: 391px;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.player[data-expanded] {
|
|
170
|
+
background: #000;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* The video stays 9:16 and centered; the rest of the screen is black. */
|
|
174
|
+
.player[data-expanded] .player-frame {
|
|
175
|
+
width: min(100vw, 100vh * 9 / 16);
|
|
176
|
+
height: min(100vh, 100vw * 16 / 9);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.player[data-expanded] .card {
|
|
180
|
+
border-radius: 0;
|
|
181
|
+
}
|
|
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. */
|
|
186
|
+
.player[data-expanded] .player-controls {
|
|
187
|
+
position: absolute;
|
|
188
|
+
top: 60px;
|
|
189
|
+
right: 12px;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@keyframes player-in {
|
|
194
|
+
from {
|
|
195
|
+
opacity: 0;
|
|
196
|
+
transform: translateY(8px);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@media (prefers-reduced-motion: reduce) {
|
|
201
|
+
.player {
|
|
202
|
+
animation: none;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
`;
|
|
206
|
+
|
|
207
|
+
export interface PlayerOptions {
|
|
208
|
+
/** Accessible name of the player. */
|
|
209
|
+
label: string;
|
|
210
|
+
/** Accessible names of the buttons. */
|
|
211
|
+
expandLabel: string;
|
|
212
|
+
collapseLabel: string;
|
|
213
|
+
closeLabel: string;
|
|
214
|
+
playLabel: string;
|
|
215
|
+
pauseLabel: string;
|
|
216
|
+
soundOnLabel: string;
|
|
217
|
+
soundOffLabel: string;
|
|
218
|
+
onClose: () => void;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function createButton(className: string, label: string, icon: IconName) {
|
|
222
|
+
const button = document.createElement("button");
|
|
223
|
+
button.type = "button";
|
|
224
|
+
button.className = `player-button ${className}`;
|
|
225
|
+
button.setAttribute("aria-label", label);
|
|
226
|
+
button.append(createIcon(icon));
|
|
227
|
+
return button;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function supportsPopover(player: HTMLDialogElement) {
|
|
231
|
+
return typeof player.showPopover === "function";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Shows the player as the mini player. It must already be in the page. */
|
|
235
|
+
export function showPlayer(player: HTMLDialogElement) {
|
|
236
|
+
if (supportsPopover(player)) player.showPopover();
|
|
237
|
+
else player.show();
|
|
238
|
+
}
|
|
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
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The Story player. It opens as the mini player (see showPlayer); expand
|
|
314
|
+
* switches to the expanded view, a modal dialog, and back. Clicking the
|
|
315
|
+
* backdrop of the expanded view or pressing Escape in it closes the player.
|
|
316
|
+
* The video starts muted (see playPlayer).
|
|
317
|
+
*/
|
|
318
|
+
export function createPlayer(story: StoryMedia, options: PlayerOptions) {
|
|
319
|
+
const player = document.createElement("dialog");
|
|
320
|
+
player.className = "player";
|
|
321
|
+
player.setAttribute("popover", "manual");
|
|
322
|
+
player.setAttribute("aria-label", options.label);
|
|
323
|
+
|
|
324
|
+
const frame = document.createElement("div");
|
|
325
|
+
frame.className = "player-frame";
|
|
326
|
+
|
|
327
|
+
const controls = document.createElement("div");
|
|
328
|
+
controls.className = "player-controls";
|
|
329
|
+
|
|
330
|
+
const expand = createButton("player-expand", options.expandLabel, "expand");
|
|
331
|
+
const close = createButton("player-close", options.closeLabel, "close");
|
|
332
|
+
controls.append(expand, close);
|
|
333
|
+
|
|
334
|
+
let expanded = false;
|
|
335
|
+
|
|
336
|
+
// A dialog can't be shown as a popover and as a modal at the same time, so
|
|
337
|
+
// switching modes hides it first, the same way it was shown.
|
|
338
|
+
function hide() {
|
|
339
|
+
if (!expanded && supportsPopover(player)) player.hidePopover();
|
|
340
|
+
else player.close();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
expand.addEventListener("click", () => {
|
|
344
|
+
hide();
|
|
345
|
+
expanded = !expanded;
|
|
346
|
+
player.toggleAttribute("data-expanded", expanded);
|
|
347
|
+
player.dataset.switched = "";
|
|
348
|
+
expand.setAttribute(
|
|
349
|
+
"aria-label",
|
|
350
|
+
expanded ? options.collapseLabel : options.expandLabel,
|
|
351
|
+
);
|
|
352
|
+
expand.replaceChildren(createIcon(expanded ? "collapse" : "expand"));
|
|
353
|
+
|
|
354
|
+
if (expanded) player.showModal();
|
|
355
|
+
else showPlayer(player);
|
|
356
|
+
expand.focus();
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
close.addEventListener("click", options.onClose);
|
|
360
|
+
|
|
361
|
+
// Clicks on the backdrop of the expanded view reach the dialog itself.
|
|
362
|
+
player.addEventListener("click", (event) => {
|
|
363
|
+
if (expanded && event.target === player) options.onClose();
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// Escape on a modal dialog would only hide it: close the player instead.
|
|
367
|
+
player.addEventListener("cancel", (event) => {
|
|
368
|
+
event.preventDefault();
|
|
369
|
+
options.onClose();
|
|
370
|
+
});
|
|
371
|
+
|
|
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);
|
|
377
|
+
player.append(frame);
|
|
378
|
+
return player;
|
|
379
|
+
}
|
package/src/raw.d.ts
ADDED
package/src/react.ts
CHANGED
|
@@ -13,10 +13,7 @@ import type { VtexAdsStoryElement } from "./story";
|
|
|
13
13
|
export type VtexAdsStoryProps = DetailedHTMLProps<
|
|
14
14
|
HTMLAttributes<VtexAdsStoryElement>,
|
|
15
15
|
VtexAdsStoryElement
|
|
16
|
-
|
|
17
|
-
/** Name shown in the greeting; falls back to "world" when empty. */
|
|
18
|
-
name?: string;
|
|
19
|
-
};
|
|
16
|
+
>;
|
|
20
17
|
|
|
21
18
|
declare module "react" {
|
|
22
19
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
package/src/story/story.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
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";
|
|
9
|
+
|
|
1
10
|
export const TAG_NAME = "vtex-ads-story";
|
|
2
11
|
|
|
3
12
|
// Keeps the module importable where HTMLElement doesn't exist (SSR, Node).
|
|
@@ -9,54 +18,107 @@ const styles = `
|
|
|
9
18
|
font-family: var(--vtex-ads-story-font-family, inherit);
|
|
10
19
|
color: var(--vtex-ads-story-color-text, inherit);
|
|
11
20
|
}
|
|
21
|
+
${cardStyles}
|
|
22
|
+
${playerStyles}
|
|
12
23
|
`;
|
|
13
24
|
|
|
14
|
-
|
|
25
|
+
// Temporary: rendered until the element reads Stories from the ad API.
|
|
26
|
+
const PLACEHOLDER_STORY: StoryMedia = {
|
|
27
|
+
poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
|
|
28
|
+
media_url: "https://samplelib.com/mp4/sample-10s-vp9.mp4",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Temporary: fixed texts until the store can set them by attribute.
|
|
32
|
+
const LABELS = {
|
|
33
|
+
watch: "Assistir vídeo",
|
|
34
|
+
player: "Vídeo",
|
|
35
|
+
expand: "Expandir vídeo",
|
|
36
|
+
collapse: "Reduzir vídeo",
|
|
37
|
+
close: "Fechar vídeo",
|
|
38
|
+
play: "Reproduzir vídeo",
|
|
39
|
+
pause: "Pausar vídeo",
|
|
40
|
+
soundOn: "Ativar som",
|
|
41
|
+
soundOff: "Desativar som",
|
|
42
|
+
};
|
|
15
43
|
|
|
16
44
|
export class VtexAdsStoryElement extends BaseElement {
|
|
17
|
-
|
|
18
|
-
|
|
45
|
+
private player: HTMLDialogElement | null = null;
|
|
46
|
+
|
|
47
|
+
private readonly onKeyDown = (event: KeyboardEvent) => {
|
|
48
|
+
if (event.key === "Escape") this.closePlayer();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
private render() {
|
|
52
|
+
const container = this.shadowRoot?.querySelector(".root");
|
|
53
|
+
if (!container) return;
|
|
54
|
+
|
|
55
|
+
container.replaceChildren(
|
|
56
|
+
createCard(PLACEHOLDER_STORY, {
|
|
57
|
+
label: LABELS.watch,
|
|
58
|
+
soundOnLabel: LABELS.soundOn,
|
|
59
|
+
soundOffLabel: LABELS.soundOff,
|
|
60
|
+
onSelect: () => this.openPlayer(),
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
19
63
|
}
|
|
20
64
|
|
|
21
|
-
|
|
22
|
-
return this.
|
|
65
|
+
private cardVideo() {
|
|
66
|
+
return this.shadowRoot?.querySelector<HTMLVideoElement>(
|
|
67
|
+
".root .card video",
|
|
68
|
+
);
|
|
23
69
|
}
|
|
24
70
|
|
|
25
|
-
|
|
26
|
-
this.
|
|
71
|
+
private openPlayer() {
|
|
72
|
+
if (this.player || !this.shadowRoot) return;
|
|
73
|
+
|
|
74
|
+
this.player = createPlayer(PLACEHOLDER_STORY, {
|
|
75
|
+
label: LABELS.player,
|
|
76
|
+
expandLabel: LABELS.expand,
|
|
77
|
+
collapseLabel: LABELS.collapse,
|
|
78
|
+
closeLabel: LABELS.close,
|
|
79
|
+
playLabel: LABELS.play,
|
|
80
|
+
pauseLabel: LABELS.pause,
|
|
81
|
+
soundOnLabel: LABELS.soundOn,
|
|
82
|
+
soundOffLabel: LABELS.soundOff,
|
|
83
|
+
onClose: () => this.closePlayer(),
|
|
84
|
+
});
|
|
85
|
+
this.shadowRoot.append(this.player);
|
|
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);
|
|
90
|
+
document.addEventListener("keydown", this.onKeyDown);
|
|
91
|
+
|
|
92
|
+
this.player.querySelector<HTMLElement>(".player-close")?.focus();
|
|
27
93
|
}
|
|
28
94
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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 } = {}) {
|
|
98
|
+
if (!this.player) return;
|
|
99
|
+
|
|
100
|
+
document.removeEventListener("keydown", this.onKeyDown);
|
|
101
|
+
this.player.remove();
|
|
102
|
+
this.player = null;
|
|
103
|
+
if (removed) return;
|
|
32
104
|
|
|
33
|
-
|
|
34
|
-
|
|
105
|
+
const video = this.cardVideo();
|
|
106
|
+
if (video?.autoplay) playVideo(video);
|
|
107
|
+
|
|
108
|
+
this.shadowRoot?.querySelector<HTMLElement>(".root .card-open")?.focus();
|
|
35
109
|
}
|
|
36
110
|
|
|
37
111
|
connectedCallback() {
|
|
38
112
|
if (!this.shadowRoot) {
|
|
39
113
|
const root = this.attachShadow({ mode: "open" });
|
|
40
|
-
root.innerHTML = `<style>${styles}</style><div
|
|
114
|
+
root.innerHTML = `<style>${styles}</style><div class="root"></div>`;
|
|
41
115
|
}
|
|
42
116
|
|
|
43
|
-
this.upgradeProperty("name");
|
|
44
117
|
this.render();
|
|
45
118
|
}
|
|
46
119
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// Remove it and set it again so it goes through the setter.
|
|
50
|
-
private upgradeProperty(prop: "name") {
|
|
51
|
-
if (!Object.prototype.hasOwnProperty.call(this, prop)) return;
|
|
52
|
-
|
|
53
|
-
const value = this[prop];
|
|
54
|
-
Reflect.deleteProperty(this, prop);
|
|
55
|
-
this[prop] = value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
attributeChangedCallback() {
|
|
59
|
-
this.render();
|
|
120
|
+
disconnectedCallback() {
|
|
121
|
+
this.closePlayer({ removed: true });
|
|
60
122
|
}
|
|
61
123
|
}
|
|
62
124
|
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One Story from the ad API response. Field names follow the API; only the
|
|
3
|
+
* fields the component reads so far are listed.
|
|
4
|
+
*/
|
|
5
|
+
export interface StoryAd {
|
|
6
|
+
ad_id: string;
|
|
7
|
+
/** Cover image shown before the video plays. */
|
|
8
|
+
poster_url?: string;
|
|
9
|
+
/** The 9:16 MP4 the card and the player play. */
|
|
10
|
+
media_url?: string;
|
|
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">;
|