@vtex/ads-stories 0.5.0 → 0.8.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 +33 -0
- package/README.md +29 -297
- package/dist/cdn/vtex-ads-stories.js +438 -113
- package/dist/cdn/vtex-ads-stories.js.map +1 -1
- package/dist/cjs/index.cjs +932 -189
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +42 -196
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/cjs/react.d.cts +13 -3
- package/dist/cjs/story-Ba9J0rbL.d.cts +276 -0
- package/dist/esm/index.d.ts +42 -196
- package/dist/esm/index.js +956 -212
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +13 -3
- package/dist/esm/story-Ba9J0rbL.d.ts +276 -0
- package/package.json +1 -1
- package/src/commerce-events.ts +93 -0
- package/src/components/card/card.ts +16 -3
- package/src/components/carousel/carousel-skeleton.ts +113 -0
- package/src/components/carousel/carousel.ts +53 -8
- package/src/components/carousel/index.ts +7 -0
- package/src/components/events/events-controller.ts +23 -13
- package/src/components/icons/assets/minus.svg +1 -0
- package/src/components/icons/assets/plus.svg +1 -0
- package/src/components/icons/assets/trash.svg +1 -0
- package/src/components/icons/assets/warning-circle-fill.svg +1 -0
- package/src/components/icons/index.ts +8 -0
- package/src/components/media/index.ts +7 -1
- package/src/components/media/media.styles.ts +113 -0
- package/src/components/media/media.ts +108 -11
- package/src/components/media/video-controller.ts +80 -6
- package/src/components/player/player.ts +42 -17
- package/src/components/product/cta.ts +3 -3
- package/src/components/product/index.ts +2 -0
- package/src/components/product/product.styles.ts +63 -6
- package/src/components/product/product.ts +130 -29
- package/src/index.ts +8 -0
- package/src/locales/en.ts +13 -3
- package/src/locales/es.ts +13 -3
- package/src/locales/index.ts +6 -0
- package/src/locales/pt.ts +13 -3
- package/src/locales/types.ts +22 -2
- package/src/react.ts +12 -2
- package/src/story/story.ts +218 -20
- package/src/utils/hydrate-products.ts +30 -6
- package/src/utils/media-ready.ts +58 -0
- package/src/utils/safe-url.ts +36 -0
- package/src/utils/send-event.ts +1 -1
- package/src/utils/http-url.ts +0 -12
|
@@ -10,11 +10,28 @@ import { repeat } from "lit/directives/repeat.js";
|
|
|
10
10
|
import { styleMap } from "lit/directives/style-map.js";
|
|
11
11
|
import { icon } from "../icons";
|
|
12
12
|
|
|
13
|
-
const STAGE_HEIGHT = css`500px`;
|
|
13
|
+
export const STAGE_HEIGHT = css`500px`;
|
|
14
14
|
const SOLO_PADDING = css`24px`;
|
|
15
15
|
|
|
16
|
-
/** Time each Story stays highlighted before the next one, in milliseconds. */
|
|
17
16
|
export const AUTO_ADVANCE_MS = 5000;
|
|
17
|
+
export const MIN_SLIDE_DURATION_MS = 2000;
|
|
18
|
+
export const MAX_SLIDE_DURATION_MS = 120_000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The time each Story stays highlighted: `ms` when it is a number from zero
|
|
22
|
+
* up (zero: no automatic advance), raised to MIN_SLIDE_DURATION_MS when
|
|
23
|
+
* smaller and capped at MAX_SLIDE_DURATION_MS, AUTO_ADVANCE_MS when it is
|
|
24
|
+
* missing, negative or not a number.
|
|
25
|
+
*/
|
|
26
|
+
export const toSlideDuration = (ms?: number) => {
|
|
27
|
+
if (ms === undefined || !Number.isFinite(ms) || ms < 0) {
|
|
28
|
+
return AUTO_ADVANCE_MS;
|
|
29
|
+
}
|
|
30
|
+
if (ms === 0) {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
return Math.min(Math.max(ms, MIN_SLIDE_DURATION_MS), MAX_SLIDE_DURATION_MS);
|
|
34
|
+
};
|
|
18
35
|
|
|
19
36
|
/** How far a pointer must move, in pixels, to count as a swipe. */
|
|
20
37
|
export const SWIPE_DISTANCE = 40;
|
|
@@ -227,6 +244,16 @@ export interface CarouselOptions {
|
|
|
227
244
|
* example, while the player shows the highlighted Story).
|
|
228
245
|
*/
|
|
229
246
|
locked?: () => boolean;
|
|
247
|
+
/**
|
|
248
|
+
* How long each Story stays highlighted before the next one, in
|
|
249
|
+
* milliseconds (see toSlideDuration). Read whenever the host updates.
|
|
250
|
+
*/
|
|
251
|
+
slideDuration?: () => number | undefined;
|
|
252
|
+
/**
|
|
253
|
+
* Whether the person prefers reduced motion: no automatic advance then.
|
|
254
|
+
* Read once each time the host connects.
|
|
255
|
+
*/
|
|
256
|
+
reducedMotion?: () => boolean;
|
|
230
257
|
}
|
|
231
258
|
|
|
232
259
|
/** One rendered card. */
|
|
@@ -281,9 +308,11 @@ function tracker(elements: Map<Element, Slide>, slide: Slide) {
|
|
|
281
308
|
* layout, the arrows, swipes and the automatic advance. Call reset() when
|
|
282
309
|
* the Stories change, and render() from the host's render.
|
|
283
310
|
*
|
|
284
|
-
* The highlight moves on its own every
|
|
285
|
-
*
|
|
286
|
-
* or the host is off the page.
|
|
311
|
+
* The highlight moves on its own every slide duration (AUTO_ADVANCE_MS by
|
|
312
|
+
* default, never with zero), except while the pointer is over the carousel,
|
|
313
|
+
* the keyboard focus is in it, it is locked, or the host is off the page.
|
|
314
|
+
* Never for people who prefer reduced motion: the highlight then moves only
|
|
315
|
+
* when they step it.
|
|
287
316
|
*/
|
|
288
317
|
export class CarouselController implements ReactiveController {
|
|
289
318
|
/**
|
|
@@ -296,10 +325,13 @@ export class CarouselController implements ReactiveController {
|
|
|
296
325
|
private count = 0;
|
|
297
326
|
private narrow = false;
|
|
298
327
|
private media?: MediaQueryList;
|
|
328
|
+
private reducedMotion = false;
|
|
299
329
|
private connected = false;
|
|
300
330
|
private hovered = false;
|
|
301
331
|
private focused = false;
|
|
302
332
|
private timer?: ReturnType<typeof setInterval>;
|
|
333
|
+
/** The interval the running timer was started with. */
|
|
334
|
+
private timerMs = 0;
|
|
303
335
|
private swipe?: { x: number; moved: boolean };
|
|
304
336
|
/** The rendered cards, as their cardRef reported them. */
|
|
305
337
|
private readonly cards = new Map<Element, Slide>();
|
|
@@ -354,6 +386,7 @@ export class CarouselController implements ReactiveController {
|
|
|
354
386
|
this.media = globalThis.matchMedia?.(NARROW_SCREEN.toString());
|
|
355
387
|
this.media?.addEventListener("change", this.onMediaChange);
|
|
356
388
|
this.narrow = this.media?.matches ?? false;
|
|
389
|
+
this.reducedMotion = this.options.reducedMotion?.() ?? false;
|
|
357
390
|
this.syncTimer();
|
|
358
391
|
}
|
|
359
392
|
|
|
@@ -365,8 +398,8 @@ export class CarouselController implements ReactiveController {
|
|
|
365
398
|
this.syncTimer();
|
|
366
399
|
}
|
|
367
400
|
|
|
368
|
-
// Whatever the host changed (the player opening, new Stories
|
|
369
|
-
// stop the automatic advance.
|
|
401
|
+
// Whatever the host changed (the player opening, new Stories, the slide
|
|
402
|
+
// duration) may start, stop or restart the automatic advance.
|
|
370
403
|
hostUpdated() {
|
|
371
404
|
this.syncTimer();
|
|
372
405
|
}
|
|
@@ -405,8 +438,14 @@ export class CarouselController implements ReactiveController {
|
|
|
405
438
|
this.restartTimer();
|
|
406
439
|
}
|
|
407
440
|
|
|
441
|
+
private get slideDuration() {
|
|
442
|
+
return toSlideDuration(this.options.slideDuration?.());
|
|
443
|
+
}
|
|
444
|
+
|
|
408
445
|
private get shouldAdvance() {
|
|
409
446
|
return (
|
|
447
|
+
this.slideDuration > 0 &&
|
|
448
|
+
!this.reducedMotion &&
|
|
410
449
|
this.connected &&
|
|
411
450
|
this.count > 1 &&
|
|
412
451
|
!this.hovered &&
|
|
@@ -416,8 +455,13 @@ export class CarouselController implements ReactiveController {
|
|
|
416
455
|
}
|
|
417
456
|
|
|
418
457
|
private syncTimer() {
|
|
458
|
+
const ms = this.slideDuration;
|
|
459
|
+
// A new duration starts over, with the full new time.
|
|
460
|
+
if (this.timer && this.timerMs !== ms) this.stopTimer();
|
|
461
|
+
|
|
419
462
|
if (this.shouldAdvance && !this.timer) {
|
|
420
|
-
this.
|
|
463
|
+
this.timerMs = ms;
|
|
464
|
+
this.timer = setInterval(() => this.step(1), ms);
|
|
421
465
|
} else if (!this.shouldAdvance && this.timer) {
|
|
422
466
|
this.stopTimer();
|
|
423
467
|
}
|
|
@@ -426,6 +470,7 @@ export class CarouselController implements ReactiveController {
|
|
|
426
470
|
private stopTimer() {
|
|
427
471
|
clearInterval(this.timer);
|
|
428
472
|
this.timer = undefined;
|
|
473
|
+
this.timerMs = 0;
|
|
429
474
|
}
|
|
430
475
|
|
|
431
476
|
private restartTimer() {
|
|
@@ -2,11 +2,18 @@ export {
|
|
|
2
2
|
AUTO_ADVANCE_MS,
|
|
3
3
|
CarouselController,
|
|
4
4
|
carouselStyles,
|
|
5
|
+
MAX_SLIDE_DURATION_MS,
|
|
6
|
+
MIN_SLIDE_DURATION_MS,
|
|
5
7
|
NARROW_SCREEN,
|
|
6
8
|
RENDERED_SIDE,
|
|
7
9
|
SWIPE_DISTANCE,
|
|
10
|
+
toSlideDuration,
|
|
8
11
|
VISIBLE_SIDE,
|
|
9
12
|
} from "./carousel";
|
|
13
|
+
export {
|
|
14
|
+
carouselSkeletonStyles,
|
|
15
|
+
renderCarouselSkeleton,
|
|
16
|
+
} from "./carousel-skeleton";
|
|
10
17
|
export type {
|
|
11
18
|
CarouselLayout,
|
|
12
19
|
CarouselOptions,
|
|
@@ -56,6 +56,7 @@ export class StoryEventsController implements ReactiveController {
|
|
|
56
56
|
};
|
|
57
57
|
|
|
58
58
|
private pendingImpressions: readonly StoryAd[] = [];
|
|
59
|
+
private connected = false;
|
|
59
60
|
|
|
60
61
|
private observer?: IntersectionObserver;
|
|
61
62
|
private readonly watched = new Map<Element, Watched>();
|
|
@@ -68,6 +69,7 @@ export class StoryEventsController implements ReactiveController {
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
hostConnected() {
|
|
72
|
+
this.connected = true;
|
|
71
73
|
if (typeof IntersectionObserver === "function") {
|
|
72
74
|
this.observer = new IntersectionObserver(this.onIntersect, {
|
|
73
75
|
threshold: VIEW_THRESHOLD,
|
|
@@ -78,11 +80,17 @@ export class StoryEventsController implements ReactiveController {
|
|
|
78
80
|
this.onVisibilityChange,
|
|
79
81
|
);
|
|
80
82
|
// Moved on the page: the cards rendered before are still there, but lit
|
|
81
|
-
// hands their refs back only after the controllers connect.
|
|
82
|
-
|
|
83
|
+
// hands their refs back only after the controllers connect. Impressions
|
|
84
|
+
// that came while off the page go out now.
|
|
85
|
+
this.host.updateComplete.then(() => {
|
|
86
|
+
if (!this.connected) return;
|
|
87
|
+
this.sendImpressions();
|
|
88
|
+
this.syncViewTargets();
|
|
89
|
+
});
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
hostDisconnected() {
|
|
93
|
+
this.connected = false;
|
|
86
94
|
globalThis.document?.removeEventListener(
|
|
87
95
|
"visibilitychange",
|
|
88
96
|
this.onVisibilityChange,
|
|
@@ -91,29 +99,31 @@ export class StoryEventsController implements ReactiveController {
|
|
|
91
99
|
this.observer = undefined;
|
|
92
100
|
for (const watched of this.watched.values()) clearTimeout(watched.timer);
|
|
93
101
|
this.watched.clear();
|
|
94
|
-
// Stories that never rendered were never seen.
|
|
95
|
-
this.pendingImpressions = [];
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
hostUpdated() {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.pendingImpressions = [];
|
|
102
|
-
for (const story of stories) this.send("impression", story);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
+
if (!this.connected) return;
|
|
106
|
+
this.sendImpressions();
|
|
105
107
|
this.syncViewTargets();
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
/**
|
|
109
|
-
* The Stories the host
|
|
110
|
-
* render is done,
|
|
111
|
-
* has
|
|
111
|
+
* The Stories the host shows next: their impressions go out once that
|
|
112
|
+
* render is done, on the page, with a session (or after the first render
|
|
113
|
+
* that has both). Replaces Stories passed before and not sent yet.
|
|
112
114
|
*/
|
|
113
115
|
impress(stories: readonly StoryAd[]) {
|
|
114
116
|
this.pendingImpressions = stories;
|
|
115
117
|
}
|
|
116
118
|
|
|
119
|
+
private sendImpressions() {
|
|
120
|
+
const stories = this.pendingImpressions;
|
|
121
|
+
if (!stories.length || !this.options.identity().sessionId) return;
|
|
122
|
+
|
|
123
|
+
this.pendingImpressions = [];
|
|
124
|
+
for (const story of stories) this.send("impression", story);
|
|
125
|
+
}
|
|
126
|
+
|
|
117
127
|
/** A touch on the Story: its click goes out, the first time only. */
|
|
118
128
|
click(story: StoryAd) {
|
|
119
129
|
this.send("click", story);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M224,128a8,8,0,0,1-8,8H40a8,8,0,0,1,0-16H216A8,8,0,0,1,224,128Z"/></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M224,128a8,8,0,0,1-8,8H136v80a8,8,0,0,1-16,0V136H40a8,8,0,0,1,0-16h80V40a8,8,0,0,1,16,0v80h80A8,8,0,0,1,224,128Z"/></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48H176V40a24,24,0,0,0-24-24H104A24,24,0,0,0,80,40v8H40a8,8,0,0,0,0,16h8V208a16,16,0,0,0,16,16H192a16,16,0,0,0,16-16V64h8a8,8,0,0,0,0-16ZM96,40a8,8,0,0,1,8-8h48a8,8,0,0,1,8,8v8H96Zm96,168H64V64H192ZM112,104v64a8,8,0,0,1-16,0V104a8,8,0,0,1,16,0Zm48,0v64a8,8,0,0,1-16,0V104a8,8,0,0,1,16,0Z"/></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm-8,56a8,8,0,0,1,16,0v56a8,8,0,0,1-16,0Zm8,104a12,12,0,1,1,12-12A12,12,0,0,1,128,184Z"/></svg>
|
|
@@ -15,22 +15,30 @@ import close from "./assets/close.svg?raw";
|
|
|
15
15
|
import caretUp from "./assets/caret-up-bold.svg?raw";
|
|
16
16
|
import collapse from "./assets/collapse.svg?raw";
|
|
17
17
|
import expand from "./assets/expand.svg?raw";
|
|
18
|
+
import minus from "./assets/minus.svg?raw";
|
|
18
19
|
import pause from "./assets/pause-fill.svg?raw";
|
|
19
20
|
import play from "./assets/play-fill.svg?raw";
|
|
21
|
+
import plus from "./assets/plus.svg?raw";
|
|
20
22
|
import soundOff from "./assets/speaker-x-fill.svg?raw";
|
|
21
23
|
import soundOn from "./assets/speaker-high-fill.svg?raw";
|
|
24
|
+
import trash from "./assets/trash.svg?raw";
|
|
25
|
+
import warning from "./assets/warning-circle-fill.svg?raw";
|
|
22
26
|
|
|
23
27
|
export const icons = {
|
|
24
28
|
caretUp,
|
|
25
29
|
close,
|
|
26
30
|
collapse,
|
|
27
31
|
expand,
|
|
32
|
+
minus,
|
|
28
33
|
next,
|
|
29
34
|
pause,
|
|
30
35
|
play,
|
|
36
|
+
plus,
|
|
31
37
|
previous,
|
|
32
38
|
soundOff,
|
|
33
39
|
soundOn,
|
|
40
|
+
trash,
|
|
41
|
+
warning,
|
|
34
42
|
};
|
|
35
43
|
|
|
36
44
|
export type IconName = keyof typeof icons;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
hasVideo,
|
|
3
|
+
prefersReducedMotion,
|
|
4
|
+
REDUCED_MOTION,
|
|
5
|
+
renderMedia,
|
|
6
|
+
} from "./media";
|
|
2
7
|
export type { MediaOptions } from "./media";
|
|
8
|
+
export { mediaStyles } from "./media.styles";
|
|
3
9
|
export { playVideo, VideoController } from "./video-controller";
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { css } from "lit";
|
|
2
|
+
|
|
3
|
+
export const mediaStyles = css`
|
|
4
|
+
/* The live region the notice shows in, over the cover. Empty until the
|
|
5
|
+
video can't load or is loading again. Clicks go through to the card. */
|
|
6
|
+
.media-notice {
|
|
7
|
+
position: absolute;
|
|
8
|
+
inset: 0;
|
|
9
|
+
pointer-events: none;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/* The notice: a dimmed layer with the text in the middle. */
|
|
13
|
+
.media-status {
|
|
14
|
+
position: absolute;
|
|
15
|
+
inset: 0;
|
|
16
|
+
display: flex;
|
|
17
|
+
flex-direction: column;
|
|
18
|
+
gap: 12px;
|
|
19
|
+
align-items: center;
|
|
20
|
+
justify-content: center;
|
|
21
|
+
box-sizing: border-box;
|
|
22
|
+
padding: 16px;
|
|
23
|
+
background: rgba(0, 0, 0, 0.45);
|
|
24
|
+
color: #fff;
|
|
25
|
+
text-align: center;
|
|
26
|
+
pointer-events: none;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.media-status > * {
|
|
30
|
+
animation: media-status-in 0.4s ease-out both;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.media-status-text {
|
|
34
|
+
display: flex;
|
|
35
|
+
flex-direction: column;
|
|
36
|
+
gap: 8px;
|
|
37
|
+
align-items: center;
|
|
38
|
+
max-width: 100%;
|
|
39
|
+
font-size: 14px;
|
|
40
|
+
font-weight: 600;
|
|
41
|
+
line-height: 1.3;
|
|
42
|
+
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.media-status-text .icon {
|
|
46
|
+
width: 18px;
|
|
47
|
+
height: 18px;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.media-status[data-retry] .media-status-text .icon {
|
|
51
|
+
width: 24px;
|
|
52
|
+
height: 24px;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.media-retry {
|
|
56
|
+
min-height: 36px;
|
|
57
|
+
padding: 0 16px;
|
|
58
|
+
border: 0;
|
|
59
|
+
border-radius: var(--_button-radius);
|
|
60
|
+
background: rgba(255, 255, 255, 0.15);
|
|
61
|
+
backdrop-filter: blur(4px);
|
|
62
|
+
box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);
|
|
63
|
+
color: #fff;
|
|
64
|
+
font: inherit;
|
|
65
|
+
font-size: 14px;
|
|
66
|
+
font-weight: 600;
|
|
67
|
+
cursor: pointer;
|
|
68
|
+
pointer-events: auto;
|
|
69
|
+
animation-delay: 0.06s;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.media-retry:focus-visible {
|
|
73
|
+
outline: 2px solid #fff;
|
|
74
|
+
outline-offset: 2px;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@media (hover: hover) and (pointer: fine) {
|
|
78
|
+
.media-retry:hover {
|
|
79
|
+
background: rgba(255, 255, 255, 0.25);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* It spins with reduced motion too: a still ring wouldn't read as
|
|
84
|
+
loading. */
|
|
85
|
+
.media-spinner {
|
|
86
|
+
width: 24px;
|
|
87
|
+
height: 24px;
|
|
88
|
+
box-sizing: border-box;
|
|
89
|
+
border: 2px solid rgba(255, 255, 255, 0.35);
|
|
90
|
+
border-top-color: #fff;
|
|
91
|
+
border-radius: 50%;
|
|
92
|
+
animation: media-spin 0.8s linear infinite;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@keyframes media-status-in {
|
|
96
|
+
from {
|
|
97
|
+
opacity: 0;
|
|
98
|
+
transform: translateY(4px);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@keyframes media-spin {
|
|
103
|
+
to {
|
|
104
|
+
transform: rotate(360deg);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@media (prefers-reduced-motion: reduce) {
|
|
109
|
+
.media-status > :not(.media-spinner) {
|
|
110
|
+
animation: none;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { html, nothing } from "lit";
|
|
2
2
|
import { ref } from "lit/directives/ref.js";
|
|
3
|
+
import { pt, type StoryLabels } from "../../locales";
|
|
4
|
+
import { icon } from "../icons";
|
|
3
5
|
import type { StoryMedia } from "../../types";
|
|
4
|
-
import { httpUrl } from "../../utils/
|
|
6
|
+
import { httpUrl } from "../../utils/safe-url";
|
|
5
7
|
import type { VideoController } from "./video-controller";
|
|
6
8
|
|
|
9
|
+
/** Media query for people who asked the system for less motion. */
|
|
10
|
+
export const REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
|
|
11
|
+
|
|
7
12
|
export function prefersReducedMotion() {
|
|
8
|
-
return (
|
|
9
|
-
globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false
|
|
10
|
-
);
|
|
13
|
+
return globalThis.matchMedia?.(REDUCED_MOTION).matches ?? false;
|
|
11
14
|
}
|
|
12
15
|
|
|
13
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Whether renderMedia shows a playable video for this Story right now: not
|
|
18
|
+
* one that failed or is loading again.
|
|
19
|
+
*/
|
|
14
20
|
export function hasVideo(story: StoryMedia, video: VideoController) {
|
|
15
|
-
return !video.failed && httpUrl(story.media_url) !== null;
|
|
21
|
+
return !video.failed && !video.retrying && httpUrl(story.media_url) !== null;
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
export interface MediaOptions {
|
|
@@ -22,20 +28,109 @@ export interface MediaOptions {
|
|
|
22
28
|
loop?: boolean;
|
|
23
29
|
/** Called when a video that doesn't loop reaches its end. */
|
|
24
30
|
onEnded?: () => void;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the video plays on its own (default true; never with reduced
|
|
33
|
+
* motion). Without it, the video loads in full but waits to be played.
|
|
34
|
+
*/
|
|
35
|
+
autoplay?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The texts of the notice over the cover when the video can't load
|
|
38
|
+
* (default Portuguese, as the element's).
|
|
39
|
+
*/
|
|
40
|
+
labels?: StoryLabels;
|
|
41
|
+
/**
|
|
42
|
+
* Whether screen readers announce the notice (default true). Off where
|
|
43
|
+
* several can show at once, as on the cards.
|
|
44
|
+
*/
|
|
45
|
+
announce?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Offers to load a failed video again (see VideoController.retry), with a
|
|
48
|
+
* button in the notice. Without it, the notice only says the video is
|
|
49
|
+
* unavailable.
|
|
50
|
+
*/
|
|
51
|
+
onRetry?: () => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// What the notice says, and the retry button when there is one. The button
|
|
55
|
+
// is inside the card, which plays or pauses on a click: the click stops here.
|
|
56
|
+
function renderFailed(labels: StoryLabels, onRetry?: () => void) {
|
|
57
|
+
const retry = (event: Event) => {
|
|
58
|
+
event.stopPropagation();
|
|
59
|
+
onRetry?.();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return html`
|
|
63
|
+
<div class="media-status" ?data-retry=${!!onRetry}>
|
|
64
|
+
<span class="media-status-text">
|
|
65
|
+
${icon("warning")}
|
|
66
|
+
${onRetry ? labels.videoFailed : labels.videoUnavailable}
|
|
67
|
+
</span>
|
|
68
|
+
${onRetry
|
|
69
|
+
? html`
|
|
70
|
+
<button type="button" class="media-retry" @click=${retry}>
|
|
71
|
+
${labels.retry}
|
|
72
|
+
</button>
|
|
73
|
+
`
|
|
74
|
+
: nothing}
|
|
75
|
+
</div>
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function renderVideoLoading(labels: StoryLabels) {
|
|
80
|
+
return html`
|
|
81
|
+
<div class="media-status">
|
|
82
|
+
<span class="media-status-text">
|
|
83
|
+
<span class="media-spinner" aria-hidden="true"></span>
|
|
84
|
+
${labels.videoLoading}
|
|
85
|
+
</span>
|
|
86
|
+
</div>
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The notice over the cover, if the video failed or is loading again.
|
|
91
|
+
function renderStatus(
|
|
92
|
+
video: VideoController,
|
|
93
|
+
labels: StoryLabels,
|
|
94
|
+
onRetry?: () => void,
|
|
95
|
+
) {
|
|
96
|
+
if (video.failed) return renderFailed(labels, onRetry);
|
|
97
|
+
if (video.retrying) return renderVideoLoading(labels);
|
|
98
|
+
return nothing;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// A live region is announced when its content changes, not when it is
|
|
102
|
+
// added with it: it stays for as long as there is a video, empty until a
|
|
103
|
+
// notice shows.
|
|
104
|
+
function renderNotice(
|
|
105
|
+
video: VideoController,
|
|
106
|
+
{ labels = pt, onRetry, announce = true }: MediaOptions,
|
|
107
|
+
) {
|
|
108
|
+
return html`
|
|
109
|
+
<div
|
|
110
|
+
class="media-notice"
|
|
111
|
+
role=${announce ? "status" : nothing}
|
|
112
|
+
aria-hidden=${announce ? nothing : "true"}
|
|
113
|
+
>
|
|
114
|
+
${renderStatus(video, labels, onRetry)}
|
|
115
|
+
</div>
|
|
116
|
+
`;
|
|
25
117
|
}
|
|
26
118
|
|
|
27
119
|
/**
|
|
28
120
|
* The Story cover and, with a media_url, the video looping muted over it. If
|
|
29
|
-
* the video can't load, it is removed and the cover underneath stays
|
|
30
|
-
*
|
|
121
|
+
* the video can't load, it is removed and the cover underneath stays, with a
|
|
122
|
+
* notice over it (and a retry button with `onRetry`). With withVideo false,
|
|
123
|
+
* only the cover.
|
|
31
124
|
*/
|
|
32
125
|
export function renderMedia(
|
|
33
126
|
story: StoryMedia,
|
|
34
127
|
video: VideoController,
|
|
35
|
-
|
|
128
|
+
options: MediaOptions = {},
|
|
36
129
|
) {
|
|
130
|
+
const { withVideo = true, loop = true, onEnded, autoplay = true } = options;
|
|
37
131
|
const posterUrl = httpUrl(story.poster_url);
|
|
38
132
|
const mediaUrl = withVideo ? httpUrl(story.media_url) : null;
|
|
133
|
+
const preload = autoplay ? "metadata" : "auto";
|
|
39
134
|
|
|
40
135
|
// Muted and inline is what browsers, iOS included, allow to autoplay. The
|
|
41
136
|
// muted attribute and property are both set, since browsers differ on
|
|
@@ -52,19 +147,21 @@ export function renderMedia(
|
|
|
52
147
|
muted
|
|
53
148
|
playsinline
|
|
54
149
|
?loop=${loop}
|
|
55
|
-
preload
|
|
150
|
+
preload=${preload}
|
|
56
151
|
.muted=${true}
|
|
57
|
-
?autoplay=${!prefersReducedMotion()}
|
|
152
|
+
?autoplay=${autoplay && !prefersReducedMotion()}
|
|
58
153
|
poster=${posterUrl ?? nothing}
|
|
59
154
|
src=${mediaUrl}
|
|
60
155
|
${ref(video.ref)}
|
|
61
156
|
@play=${video.sync}
|
|
62
157
|
@pause=${video.sync}
|
|
63
158
|
@volumechange=${video.sync}
|
|
159
|
+
@loadeddata=${video.loaded}
|
|
64
160
|
@error=${video.fail}
|
|
65
161
|
@ended=${onEnded}
|
|
66
162
|
></video>
|
|
67
163
|
`
|
|
68
164
|
: nothing}
|
|
165
|
+
${mediaUrl ? renderNotice(video, options) : nothing}
|
|
69
166
|
`;
|
|
70
167
|
}
|
|
@@ -22,10 +22,32 @@ export class VideoController {
|
|
|
22
22
|
paused = true;
|
|
23
23
|
// The video always starts muted: that is what browsers allow to autoplay.
|
|
24
24
|
muted = true;
|
|
25
|
-
|
|
26
|
-
failed = false;
|
|
25
|
+
private status: "playable" | "failed" | "retrying" = "playable";
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
private readyState!: Promise<boolean>;
|
|
28
|
+
private settleReady!: (ready: boolean) => void;
|
|
29
|
+
|
|
30
|
+
constructor(private readonly host: ReactiveControllerHost) {
|
|
31
|
+
this.waitForReady();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Resolves true once the video's first frame has loaded, or false if it
|
|
36
|
+
* fails. A new one for each new <video> (see reset).
|
|
37
|
+
*/
|
|
38
|
+
get ready(): Promise<boolean> {
|
|
39
|
+
return this.readyState;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The video could not load: the cover shows with a notice over it. */
|
|
43
|
+
get failed() {
|
|
44
|
+
return this.status === "failed";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A failed video is loading again (see retry), until it loads or fails. */
|
|
48
|
+
get retrying() {
|
|
49
|
+
return this.status === "retrying";
|
|
50
|
+
}
|
|
29
51
|
|
|
30
52
|
/** The rendered <video> is on the page and playing. */
|
|
31
53
|
get playing() {
|
|
@@ -33,11 +55,45 @@ export class VideoController {
|
|
|
33
55
|
return !!video && video.isConnected && !video.paused;
|
|
34
56
|
}
|
|
35
57
|
|
|
36
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* For a new <video>, such as each time the player opens. A retry still
|
|
60
|
+
* loading resolves false.
|
|
61
|
+
*/
|
|
37
62
|
reset() {
|
|
63
|
+
this.cancelRetry();
|
|
38
64
|
this.paused = true;
|
|
39
65
|
this.muted = true;
|
|
40
|
-
this.
|
|
66
|
+
this.status = "playable";
|
|
67
|
+
this.waitForReady();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* For the next Story's <video>: neither failed nor loading again (a retry
|
|
72
|
+
* still loading resolves false). Unlike reset, it keeps the playback and
|
|
73
|
+
* sound state.
|
|
74
|
+
*/
|
|
75
|
+
clearFailure() {
|
|
76
|
+
if (this.retrying) {
|
|
77
|
+
this.cancelRetry();
|
|
78
|
+
this.waitForReady();
|
|
79
|
+
}
|
|
80
|
+
this.status = "playable";
|
|
81
|
+
this.host.requestUpdate();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Loads a failed video again: renderMedia renders a new <video> with a
|
|
86
|
+
* notice that it is loading, until it loads or fails. Resolves as `ready`
|
|
87
|
+
* does: false too if the video is replaced first (see reset and
|
|
88
|
+
* clearFailure), and at once for a video that didn't fail.
|
|
89
|
+
*/
|
|
90
|
+
retry() {
|
|
91
|
+
if (this.retrying) return this.ready;
|
|
92
|
+
if (!this.failed) return Promise.resolve(false);
|
|
93
|
+
this.reset();
|
|
94
|
+
this.status = "retrying";
|
|
95
|
+
this.host.requestUpdate();
|
|
96
|
+
return this.ready;
|
|
41
97
|
}
|
|
42
98
|
|
|
43
99
|
play() {
|
|
@@ -117,9 +173,27 @@ export class VideoController {
|
|
|
117
173
|
this.host.requestUpdate();
|
|
118
174
|
};
|
|
119
175
|
|
|
176
|
+
/** Listener for the video's loadeddata event. */
|
|
177
|
+
readonly loaded = () => {
|
|
178
|
+
this.settleReady(true);
|
|
179
|
+
if (!this.retrying) return;
|
|
180
|
+
this.status = "playable";
|
|
181
|
+
this.host.requestUpdate();
|
|
182
|
+
};
|
|
183
|
+
|
|
120
184
|
/** Listener for the video's error event. */
|
|
121
185
|
readonly fail = () => {
|
|
122
|
-
this.
|
|
186
|
+
this.status = "failed";
|
|
187
|
+
this.settleReady(false);
|
|
123
188
|
this.host.requestUpdate();
|
|
124
189
|
};
|
|
190
|
+
|
|
191
|
+
// The video loading again is replaced: its retry resolves false.
|
|
192
|
+
private cancelRetry() {
|
|
193
|
+
if (this.retrying) this.settleReady(false);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private waitForReady() {
|
|
197
|
+
this.readyState = new Promise((settle) => (this.settleReady = settle));
|
|
198
|
+
}
|
|
125
199
|
}
|