@vtex/ads-stories 0.5.0 → 0.7.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 +27 -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 +909 -186
- 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 +7 -1
- package/dist/cjs/story-DAiAN9v-.d.cts +260 -0
- package/dist/esm/index.d.ts +42 -196
- package/dist/esm/index.js +933 -209
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +7 -1
- package/dist/esm/story-DAiAN9v-.d.ts +260 -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/pt.ts +13 -3
- package/src/locales/types.ts +22 -2
- package/src/react.ts +6 -0
- package/src/story/story.ts +188 -16
- 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
|
@@ -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
|
}
|
|
@@ -11,12 +11,13 @@ import {
|
|
|
11
11
|
renderMedia,
|
|
12
12
|
VideoController,
|
|
13
13
|
} from "../media";
|
|
14
|
-
import { httpUrl } from "../../utils/
|
|
14
|
+
import { httpUrl } from "../../utils/safe-url";
|
|
15
15
|
import {
|
|
16
16
|
renderCta,
|
|
17
17
|
renderExpandableProduct,
|
|
18
18
|
type CardCta,
|
|
19
19
|
type CardProduct,
|
|
20
|
+
type ProductCartActions,
|
|
20
21
|
} from "../product";
|
|
21
22
|
import type { StoryMedia } from "../../types";
|
|
22
23
|
|
|
@@ -249,7 +250,7 @@ export const playerStyles = css`
|
|
|
249
250
|
padding: 0;
|
|
250
251
|
overflow: hidden;
|
|
251
252
|
border: 0;
|
|
252
|
-
border-radius: 16px;
|
|
253
|
+
border-radius: var(--vtex-ads-story-radius, 16px);
|
|
253
254
|
background: #111;
|
|
254
255
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
|
|
255
256
|
cursor: pointer;
|
|
@@ -455,6 +456,11 @@ export interface PlayerRenderOptions {
|
|
|
455
456
|
product?: CardProduct;
|
|
456
457
|
/** The Story's call to action, for a link destination (no product). */
|
|
457
458
|
cta?: CardCta;
|
|
459
|
+
/**
|
|
460
|
+
* What the product's panel does with the cart: add to cart and plus keep
|
|
461
|
+
* the player open; minus is a trash with one left.
|
|
462
|
+
*/
|
|
463
|
+
productActions?: ProductCartActions;
|
|
458
464
|
/**
|
|
459
465
|
* Called by any click on the video's card (the video, its play and sound
|
|
460
466
|
* buttons, the product and the call to action), before what that click
|
|
@@ -465,7 +471,10 @@ export interface PlayerRenderOptions {
|
|
|
465
471
|
}
|
|
466
472
|
|
|
467
473
|
// What shows at the bottom of the video: one or the other.
|
|
468
|
-
type StoryDestination = Pick<
|
|
474
|
+
type StoryDestination = Pick<
|
|
475
|
+
PlayerRenderOptions,
|
|
476
|
+
"product" | "cta" | "labels" | "productActions"
|
|
477
|
+
>;
|
|
469
478
|
|
|
470
479
|
/**
|
|
471
480
|
* Offsets of the Stories shown beside the expanded video, as in the
|
|
@@ -628,7 +637,7 @@ function supportsPopover(dialog: HTMLDialogElement) {
|
|
|
628
637
|
/**
|
|
629
638
|
* The Story player, rendered by its host with render(). open() shows it as
|
|
630
639
|
* the mini player; expand switches to the expanded view, a modal dialog, and
|
|
631
|
-
* back. The video
|
|
640
|
+
* back. The video plays with the sound on when the player opens. It closes on
|
|
632
641
|
* its own when the host leaves the page.
|
|
633
642
|
*/
|
|
634
643
|
export class PlayerController implements ReactiveController {
|
|
@@ -692,7 +701,8 @@ export class PlayerController implements ReactiveController {
|
|
|
692
701
|
}
|
|
693
702
|
|
|
694
703
|
/**
|
|
695
|
-
* Opens the mini player, plays the video
|
|
704
|
+
* Opens the mini player, plays the video with the sound on (muted if the
|
|
705
|
+
* browser blocks it) and focuses the close button.
|
|
696
706
|
* With `productOpen`, the product panel shows open (the shopper chose the
|
|
697
707
|
* product).
|
|
698
708
|
*/
|
|
@@ -715,7 +725,7 @@ export class PlayerController implements ReactiveController {
|
|
|
715
725
|
if (!this.isOpen || !dialog) return;
|
|
716
726
|
|
|
717
727
|
this.show(dialog);
|
|
718
|
-
this.video.
|
|
728
|
+
this.video.playWithSound();
|
|
719
729
|
this.closeButton.value?.focus();
|
|
720
730
|
}
|
|
721
731
|
|
|
@@ -743,7 +753,7 @@ export class PlayerController implements ReactiveController {
|
|
|
743
753
|
// Read before the render: a Story without a video, or a video that
|
|
744
754
|
// failed, replaces the <video> with a new one, muted.
|
|
745
755
|
const withSound = !this.video.muted;
|
|
746
|
-
this.video.
|
|
756
|
+
this.video.clearFailure();
|
|
747
757
|
// Another Story, another product: its panel starts closed.
|
|
748
758
|
this.productOpen = false;
|
|
749
759
|
this.host.requestUpdate();
|
|
@@ -911,6 +921,18 @@ export class PlayerController implements ReactiveController {
|
|
|
911
921
|
this.host.requestUpdate();
|
|
912
922
|
};
|
|
913
923
|
|
|
924
|
+
/**
|
|
925
|
+
* Loads the failed video again and, once it loads, plays it with the sound
|
|
926
|
+
* on (muted if the browser blocks it). Not if the player closed or stepped
|
|
927
|
+
* meanwhile, even back to the same Story: the retry then resolves false.
|
|
928
|
+
*/
|
|
929
|
+
private readonly retryVideo = () => {
|
|
930
|
+
this.dialog.value?.focus();
|
|
931
|
+
void this.video.retry().then((loaded) => {
|
|
932
|
+
if (loaded) this.video.playWithSound();
|
|
933
|
+
});
|
|
934
|
+
};
|
|
935
|
+
|
|
914
936
|
/**
|
|
915
937
|
* A click on the video: closes the product panel when it is open, plays or
|
|
916
938
|
* pauses the video otherwise.
|
|
@@ -925,14 +947,20 @@ export class PlayerController implements ReactiveController {
|
|
|
925
947
|
* its actions, or the call to action of a link destination. Clicks on them
|
|
926
948
|
* don't reach the video.
|
|
927
949
|
*/
|
|
928
|
-
private renderDestination({
|
|
950
|
+
private renderDestination({
|
|
951
|
+
product,
|
|
952
|
+
cta,
|
|
953
|
+
labels,
|
|
954
|
+
productActions,
|
|
955
|
+
}: StoryDestination) {
|
|
929
956
|
let content;
|
|
930
957
|
if (product) {
|
|
931
958
|
content = renderExpandableProduct(product, {
|
|
959
|
+
...productActions,
|
|
960
|
+
labels,
|
|
932
961
|
open: this.productOpen,
|
|
933
962
|
onToggle: this.toggleProduct,
|
|
934
963
|
panelId: "player-product-panel",
|
|
935
|
-
labels,
|
|
936
964
|
});
|
|
937
965
|
} else if (cta) {
|
|
938
966
|
content = renderCta(cta);
|
|
@@ -1092,6 +1120,8 @@ export class PlayerController implements ReactiveController {
|
|
|
1092
1120
|
${renderMedia(story, video, {
|
|
1093
1121
|
loop: !advances,
|
|
1094
1122
|
onEnded: advances ? () => options.onStep?.(1) : undefined,
|
|
1123
|
+
labels: destination.labels,
|
|
1124
|
+
onRetry: this.retryVideo,
|
|
1095
1125
|
})}
|
|
1096
1126
|
${hasVideo(story, video)
|
|
1097
1127
|
? this.renderVideoControls(destination.labels)
|
|
@@ -1129,14 +1159,13 @@ export class PlayerController implements ReactiveController {
|
|
|
1129
1159
|
render(
|
|
1130
1160
|
story: StoryMedia,
|
|
1131
1161
|
{
|
|
1132
|
-
labels,
|
|
1133
1162
|
navigation = false,
|
|
1134
1163
|
neighbors = [],
|
|
1135
|
-
product,
|
|
1136
|
-
cta,
|
|
1137
1164
|
onPress,
|
|
1165
|
+
...destination
|
|
1138
1166
|
}: PlayerRenderOptions,
|
|
1139
1167
|
) {
|
|
1168
|
+
const { labels } = destination;
|
|
1140
1169
|
if (!this.isOpen) return nothing;
|
|
1141
1170
|
|
|
1142
1171
|
const { video, expanded } = this;
|
|
@@ -1177,11 +1206,7 @@ export class PlayerController implements ReactiveController {
|
|
|
1177
1206
|
}}
|
|
1178
1207
|
>
|
|
1179
1208
|
${navigation ? this.renderNavigation(labels) : nothing}
|
|
1180
|
-
${this.renderVideo(story, navigation && expanded,
|
|
1181
|
-
product,
|
|
1182
|
-
cta,
|
|
1183
|
-
labels,
|
|
1184
|
-
})}
|
|
1209
|
+
${this.renderVideo(story, navigation && expanded, destination)}
|
|
1185
1210
|
<span class="player-dim" aria-hidden="true" ${ref(this.dim)}></span>
|
|
1186
1211
|
</div>
|
|
1187
1212
|
</div>
|