@vtex/ads-stories 0.4.1 → 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.
Files changed (60) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.md +44 -88
  3. package/dist/cdn/vtex-ads-stories.js +1313 -74
  4. package/dist/cdn/vtex-ads-stories.js.map +1 -1
  5. package/dist/cjs/index.cjs +3744 -513
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.d.cts +46 -15
  8. package/dist/cjs/react.cjs.map +1 -1
  9. package/dist/cjs/react.d.cts +51 -5
  10. package/dist/cjs/story-DAiAN9v-.d.cts +260 -0
  11. package/dist/esm/index.d.ts +46 -15
  12. package/dist/esm/index.js +3348 -217
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/react.d.ts +51 -5
  15. package/dist/esm/story-DAiAN9v-.d.ts +260 -0
  16. package/package.json +18 -4
  17. package/src/cdn.ts +7 -3
  18. package/src/commerce-events.ts +93 -0
  19. package/src/components/card/card.ts +79 -14
  20. package/src/components/carousel/carousel-skeleton.ts +113 -0
  21. package/src/components/carousel/carousel.ts +715 -0
  22. package/src/components/carousel/index.ts +22 -0
  23. package/src/components/events/events-controller.ts +222 -0
  24. package/src/components/events/index.ts +6 -0
  25. package/src/components/icons/assets/caret-left-bold.svg +1 -0
  26. package/src/components/icons/assets/caret-right-bold.svg +1 -0
  27. package/src/components/icons/assets/caret-up-bold.svg +1 -0
  28. package/src/components/icons/assets/minus.svg +1 -0
  29. package/src/components/icons/assets/plus.svg +1 -0
  30. package/src/components/icons/assets/trash.svg +1 -0
  31. package/src/components/icons/assets/warning-circle-fill.svg +1 -0
  32. package/src/components/icons/index.ts +14 -0
  33. package/src/components/media/index.ts +8 -1
  34. package/src/components/media/media.styles.ts +113 -0
  35. package/src/components/media/media.ts +124 -23
  36. package/src/components/media/video-controller.ts +127 -6
  37. package/src/components/player/index.ts +6 -2
  38. package/src/components/player/player.ts +824 -50
  39. package/src/components/product/cta.ts +56 -0
  40. package/src/components/product/index.ts +22 -0
  41. package/src/components/product/price.ts +86 -0
  42. package/src/components/product/product.styles.ts +426 -0
  43. package/src/components/product/product.ts +336 -0
  44. package/src/index.ts +17 -1
  45. package/src/locales/en.ts +39 -0
  46. package/src/locales/es.ts +40 -0
  47. package/src/locales/index.ts +36 -0
  48. package/src/locales/pt.ts +39 -0
  49. package/src/locales/types.ts +44 -0
  50. package/src/react.ts +55 -7
  51. package/src/story/index.ts +5 -1
  52. package/src/story/story.ts +679 -49
  53. package/src/tag-name.ts +1 -0
  54. package/src/types.ts +8 -12
  55. package/src/utils/fetch-story.ts +113 -0
  56. package/src/utils/hydrate-products.ts +112 -0
  57. package/src/utils/media-ready.ts +58 -0
  58. package/src/utils/safe-url.ts +36 -0
  59. package/src/utils/send-event.ts +71 -0
  60. package/src/utils/with-timeout.ts +43 -0
@@ -22,16 +22,78 @@ 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
- /** The video could not load: only the cover is shown. */
26
- failed = false;
25
+ private status: "playable" | "failed" | "retrying" = "playable";
27
26
 
28
- constructor(private readonly host: ReactiveControllerHost) {}
27
+ private readyState!: Promise<boolean>;
28
+ private settleReady!: (ready: boolean) => void;
29
29
 
30
- /** For a new <video>, such as each time the player opens. */
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
+ }
51
+
52
+ /** The rendered <video> is on the page and playing. */
53
+ get playing() {
54
+ const video = this.ref.value;
55
+ return !!video && video.isConnected && !video.paused;
56
+ }
57
+
58
+ /**
59
+ * For a new <video>, such as each time the player opens. A retry still
60
+ * loading resolves false.
61
+ */
31
62
  reset() {
63
+ this.cancelRetry();
32
64
  this.paused = true;
33
65
  this.muted = true;
34
- this.failed = false;
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;
35
97
  }
36
98
 
37
99
  play() {
@@ -56,6 +118,47 @@ export class VideoController {
56
118
  else video.pause();
57
119
  }
58
120
 
121
+ mute() {
122
+ const video = this.ref.value;
123
+ if (video) video.muted = true;
124
+ }
125
+
126
+ /**
127
+ * Plays from the start, with the sound on if `withSound`. A new src starts
128
+ * there anyway; the same src (two Stories with one video) would otherwise
129
+ * go on where it was.
130
+ */
131
+ restart({ withSound = false } = {}) {
132
+ const video = this.ref.value;
133
+ if (!video) return;
134
+
135
+ video.currentTime = 0;
136
+ if (withSound) this.playWithSound();
137
+ else playVideo(video);
138
+ }
139
+
140
+ /**
141
+ * Plays with the sound on. A browser that blocks playing with sound (for
142
+ * example, without a recent tap) gets it muted instead: the video keeps
143
+ * playing, and the sound button offers to turn it on. Not if the video was
144
+ * replaced or removed meanwhile (the request then fails for that reason):
145
+ * it would play out of sight.
146
+ */
147
+ playWithSound() {
148
+ const video = this.ref.value;
149
+ if (!video) return;
150
+
151
+ video.muted = false;
152
+ video.play()?.catch((error: unknown) => {
153
+ const blocked =
154
+ error instanceof DOMException && error.name === "NotAllowedError";
155
+ if (!blocked || this.ref.value !== video || !video.isConnected) return;
156
+
157
+ video.muted = true;
158
+ playVideo(video);
159
+ });
160
+ }
161
+
59
162
  toggleMuted() {
60
163
  const video = this.ref.value;
61
164
  if (video) video.muted = !video.muted;
@@ -70,9 +173,27 @@ export class VideoController {
70
173
  this.host.requestUpdate();
71
174
  };
72
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
+
73
184
  /** Listener for the video's error event. */
74
185
  readonly fail = () => {
75
- this.failed = true;
186
+ this.status = "failed";
187
+ this.settleReady(false);
76
188
  this.host.requestUpdate();
77
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
+ }
78
199
  }
@@ -1,2 +1,6 @@
1
- export { PlayerController, playerStyles } from "./player";
2
- export type { PlayerOptions } from "./player";
1
+ export { neighborOffsets, PlayerController, playerStyles } from "./player";
2
+ export type {
3
+ PlayerNeighbor,
4
+ PlayerOptions,
5
+ PlayerRenderOptions,
6
+ } from "./player";