@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.
Files changed (49) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +29 -297
  3. package/dist/cdn/vtex-ads-stories.js +438 -113
  4. package/dist/cdn/vtex-ads-stories.js.map +1 -1
  5. package/dist/cjs/index.cjs +932 -189
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.d.cts +42 -196
  8. package/dist/cjs/react.cjs.map +1 -1
  9. package/dist/cjs/react.d.cts +13 -3
  10. package/dist/cjs/story-Ba9J0rbL.d.cts +276 -0
  11. package/dist/esm/index.d.ts +42 -196
  12. package/dist/esm/index.js +956 -212
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/react.d.ts +13 -3
  15. package/dist/esm/story-Ba9J0rbL.d.ts +276 -0
  16. package/package.json +1 -1
  17. package/src/commerce-events.ts +93 -0
  18. package/src/components/card/card.ts +16 -3
  19. package/src/components/carousel/carousel-skeleton.ts +113 -0
  20. package/src/components/carousel/carousel.ts +53 -8
  21. package/src/components/carousel/index.ts +7 -0
  22. package/src/components/events/events-controller.ts +23 -13
  23. package/src/components/icons/assets/minus.svg +1 -0
  24. package/src/components/icons/assets/plus.svg +1 -0
  25. package/src/components/icons/assets/trash.svg +1 -0
  26. package/src/components/icons/assets/warning-circle-fill.svg +1 -0
  27. package/src/components/icons/index.ts +8 -0
  28. package/src/components/media/index.ts +7 -1
  29. package/src/components/media/media.styles.ts +113 -0
  30. package/src/components/media/media.ts +108 -11
  31. package/src/components/media/video-controller.ts +80 -6
  32. package/src/components/player/player.ts +42 -17
  33. package/src/components/product/cta.ts +3 -3
  34. package/src/components/product/index.ts +2 -0
  35. package/src/components/product/product.styles.ts +63 -6
  36. package/src/components/product/product.ts +130 -29
  37. package/src/index.ts +8 -0
  38. package/src/locales/en.ts +13 -3
  39. package/src/locales/es.ts +13 -3
  40. package/src/locales/index.ts +6 -0
  41. package/src/locales/pt.ts +13 -3
  42. package/src/locales/types.ts +22 -2
  43. package/src/react.ts +12 -2
  44. package/src/story/story.ts +218 -20
  45. package/src/utils/hydrate-products.ts +30 -6
  46. package/src/utils/media-ready.ts +58 -0
  47. package/src/utils/safe-url.ts +36 -0
  48. package/src/utils/send-event.ts +1 -1
  49. package/src/utils/http-url.ts +0 -12
package/dist/esm/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/story/story.ts
2
- import { css as css5, html as html7, LitElement, nothing as nothing5 } from "lit";
2
+ import { css as css7, html as html8, LitElement, nothing as nothing5 } from "lit";
3
3
 
4
4
  // src/components/card/card.ts
5
- import { css as css2, html as html4, nothing as nothing3 } from "lit";
5
+ import { css as css3, html as html4, nothing as nothing3 } from "lit";
6
6
  import { ref as ref2 } from "lit/directives/ref.js";
7
7
 
8
8
  // src/components/icons/index.ts
@@ -28,30 +28,46 @@ var collapse_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256
28
28
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg
29
29
  var expand_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48V88a8,8,0,0,1-16,0V56H168a8,8,0,0,1,0-16h40A8,8,0,0,1,216,48ZM88,200H56V168a8,8,0,0,0-16,0v40a8,8,0,0,0,8,8H88a8,8,0,0,0,0-16Zm120-40a8,8,0,0,0-8,8v32H168a8,8,0,0,0,0,16h40a8,8,0,0,0,8-8V168A8,8,0,0,0,208,160ZM88,40H48a8,8,0,0,0-8,8V88a8,8,0,0,0,16,0V56H88a8,8,0,0,0,0-16Z"/></svg>\n';
30
30
 
31
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/minus.svg
32
+ var minus_default = '<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>';
33
+
31
34
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/pause-fill.svg
32
35
  var pause_fill_default = '<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>\n';
33
36
 
34
37
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg
35
38
  var play_fill_default = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M240,128a15.74,15.74,0,0,1-7.6,13.51L88.32,229.65a16,16,0,0,1-16.2.3A15.86,15.86,0,0,1,64,216.13V39.87a15.86,15.86,0,0,1,8.12-13.82,16,16,0,0,1,16.2.3L232.4,114.49A15.74,15.74,0,0,1,240,128Z"/></svg>\n';
36
39
 
40
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/plus.svg
41
+ var plus_default = '<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>';
42
+
37
43
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-x-fill.svg
38
44
  var speaker_x_fill_default = '<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>\n';
39
45
 
40
46
  // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-high-fill.svg
41
47
  var speaker_high_fill_default = '<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>\n';
42
48
 
49
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/trash.svg
50
+ var trash_default = '<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>';
51
+
52
+ // raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/warning-circle-fill.svg
53
+ var warning_circle_fill_default = '<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>\n';
54
+
43
55
  // src/components/icons/index.ts
44
56
  var icons = {
45
57
  caretUp: caret_up_bold_default,
46
58
  close: close_default,
47
59
  collapse: collapse_default,
48
60
  expand: expand_default,
61
+ minus: minus_default,
49
62
  next: caret_right_bold_default,
50
63
  pause: pause_fill_default,
51
64
  play: play_fill_default,
65
+ plus: plus_default,
52
66
  previous: caret_left_bold_default,
53
67
  soundOff: speaker_x_fill_default,
54
- soundOn: speaker_high_fill_default
68
+ soundOn: speaker_high_fill_default,
69
+ trash: trash_default,
70
+ warning: warning_circle_fill_default
55
71
  };
56
72
  var markup = Object.fromEntries(
57
73
  Object.entries(icons).map(([name, svg]) => [
@@ -70,7 +86,142 @@ function icon(name) {
70
86
  import { html, nothing } from "lit";
71
87
  import { ref } from "lit/directives/ref.js";
72
88
 
73
- // src/utils/http-url.ts
89
+ // src/locales/en.ts
90
+ var en = {
91
+ watch: "Watch video",
92
+ show: "Show video",
93
+ carousel: "Videos",
94
+ carouselRole: "carousel",
95
+ previous: "Previous video",
96
+ next: "Next video",
97
+ player: "Video",
98
+ expand: "Expand video",
99
+ collapse: "Collapse video",
100
+ close: "Close video",
101
+ play: "Play video",
102
+ pause: "Pause video",
103
+ soundOn: "Turn sound on",
104
+ soundOff: "Turn sound off",
105
+ addToCart: "Add to cart",
106
+ cartQuantity: "Quantity in cart",
107
+ decreaseQuantity: "Decrease quantity",
108
+ removeFromCart: "Remove from cart",
109
+ increaseQuantity: "Increase quantity",
110
+ unavailable: "Product unavailable",
111
+ soldOut: "Sold out",
112
+ viewProduct: "View product",
113
+ videoUnavailable: "Video unavailable",
114
+ videoFailed: "Couldn't load the video",
115
+ retry: "Try again",
116
+ videoLoading: "Loading video",
117
+ product: (name, price, listPrice, soldOut) => {
118
+ let text = name;
119
+ if (price && listPrice) text = `${name}, was ${listPrice}, now ${price}`;
120
+ else if (price) text = `${name}, ${price}`;
121
+ return soldOut ? `${text}, ${soldOut}` : text;
122
+ },
123
+ position: (label, index, count) => count > 1 ? `${label} ${index + 1} of ${count}` : label
124
+ };
125
+
126
+ // src/locales/es.ts
127
+ var es = {
128
+ watch: "Ver video",
129
+ show: "Mostrar video",
130
+ carousel: "Videos",
131
+ carouselRole: "carrusel",
132
+ previous: "Video anterior",
133
+ next: "Video siguiente",
134
+ player: "Video",
135
+ expand: "Ampliar video",
136
+ collapse: "Reducir video",
137
+ close: "Cerrar video",
138
+ play: "Reproducir video",
139
+ pause: "Pausar video",
140
+ soundOn: "Activar sonido",
141
+ soundOff: "Desactivar sonido",
142
+ addToCart: "Agregar al carrito",
143
+ cartQuantity: "Cantidad en el carrito",
144
+ decreaseQuantity: "Disminuir cantidad",
145
+ removeFromCart: "Quitar del carrito",
146
+ increaseQuantity: "Aumentar cantidad",
147
+ unavailable: "Producto no disponible",
148
+ soldOut: "Agotado",
149
+ viewProduct: "Ver producto",
150
+ videoUnavailable: "Video no disponible",
151
+ videoFailed: "No se pudo cargar el video",
152
+ retry: "Intentar de nuevo",
153
+ videoLoading: "Cargando video",
154
+ product: (name, price, listPrice, soldOut) => {
155
+ let text = name;
156
+ if (price && listPrice)
157
+ text = `${name}, antes ${listPrice}, ahora ${price}`;
158
+ else if (price) text = `${name}, ${price}`;
159
+ return soldOut ? `${text}, ${soldOut}` : text;
160
+ },
161
+ position: (label, index, count) => count > 1 ? `${label} ${index + 1} de ${count}` : label
162
+ };
163
+
164
+ // src/locales/pt.ts
165
+ var pt = {
166
+ watch: "Assistir v\xEDdeo",
167
+ show: "Mostrar v\xEDdeo",
168
+ carousel: "V\xEDdeos",
169
+ carouselRole: "carrossel",
170
+ previous: "V\xEDdeo anterior",
171
+ next: "Pr\xF3ximo v\xEDdeo",
172
+ player: "V\xEDdeo",
173
+ expand: "Expandir v\xEDdeo",
174
+ collapse: "Reduzir v\xEDdeo",
175
+ close: "Fechar v\xEDdeo",
176
+ play: "Reproduzir v\xEDdeo",
177
+ pause: "Pausar v\xEDdeo",
178
+ soundOn: "Ativar som",
179
+ soundOff: "Desativar som",
180
+ addToCart: "Adicionar ao carrinho",
181
+ cartQuantity: "Quantidade no carrinho",
182
+ decreaseQuantity: "Diminuir quantidade",
183
+ removeFromCart: "Remover do carrinho",
184
+ increaseQuantity: "Aumentar quantidade",
185
+ unavailable: "Produto indispon\xEDvel",
186
+ soldOut: "Esgotado",
187
+ viewProduct: "Ver produto",
188
+ videoUnavailable: "V\xEDdeo indispon\xEDvel",
189
+ videoFailed: "N\xE3o foi poss\xEDvel carregar o v\xEDdeo",
190
+ retry: "Tentar de novo",
191
+ videoLoading: "Carregando v\xEDdeo",
192
+ product: (name, price, listPrice, soldOut) => {
193
+ let text = name;
194
+ if (price && listPrice) text = `${name}, de ${listPrice} por ${price}`;
195
+ else if (price) text = `${name}, ${price}`;
196
+ return soldOut ? `${text}, ${soldOut}` : text;
197
+ },
198
+ position: (label, index, count) => count > 1 ? `${label} ${index + 1} de ${count}` : label
199
+ };
200
+
201
+ // src/locales/index.ts
202
+ var BY_LANGUAGE = /* @__PURE__ */ new Map([
203
+ ["pt", pt],
204
+ ["es", es],
205
+ ["en", en]
206
+ ]);
207
+ function labelsFor(locale, overrides = {}) {
208
+ var _a, _b, _c, _d, _e;
209
+ const language = locale.split("-")[0].toLowerCase();
210
+ const labels = (_a = BY_LANGUAGE.get(language)) != null ? _a : pt;
211
+ const addToCart = (_b = overrides.addToCart) == null ? void 0 : _b.trim();
212
+ const viewProduct = (_c = overrides.viewProduct) == null ? void 0 : _c.trim();
213
+ const unavailable = (_d = overrides.unavailable) == null ? void 0 : _d.trim();
214
+ const soldOut = (_e = overrides.soldOut) == null ? void 0 : _e.trim();
215
+ return {
216
+ ...labels,
217
+ ...addToCart && { addToCart },
218
+ ...viewProduct && { viewProduct },
219
+ ...unavailable && { unavailable },
220
+ ...soldOut && { soldOut }
221
+ };
222
+ }
223
+
224
+ // src/utils/safe-url.ts
74
225
  function httpUrl(value) {
75
226
  if (!value) return null;
76
227
  try {
@@ -80,18 +231,82 @@ function httpUrl(value) {
80
231
  return null;
81
232
  }
82
233
  }
234
+ var STORE_BASE = "https://store.invalid";
235
+ function storePath(value) {
236
+ try {
237
+ const url = new URL(value, STORE_BASE);
238
+ if (url.origin !== STORE_BASE || url.pathname.startsWith("//")) {
239
+ return null;
240
+ }
241
+ return `${url.pathname}${url.search}${url.hash}`;
242
+ } catch (e) {
243
+ return null;
244
+ }
245
+ }
246
+ function destinationUrl(value) {
247
+ if (!value) return null;
248
+ return value.startsWith("/") ? storePath(value) : httpUrl(value);
249
+ }
83
250
 
84
251
  // src/components/media/media.ts
252
+ var REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
85
253
  function prefersReducedMotion() {
86
254
  var _a, _b;
87
- return (_b = (_a = globalThis.matchMedia) == null ? void 0 : _a.call(globalThis, "(prefers-reduced-motion: reduce)").matches) != null ? _b : false;
255
+ return (_b = (_a = globalThis.matchMedia) == null ? void 0 : _a.call(globalThis, REDUCED_MOTION).matches) != null ? _b : false;
88
256
  }
89
257
  function hasVideo(story, video) {
90
- return !video.failed && httpUrl(story.media_url) !== null;
258
+ return !video.failed && !video.retrying && httpUrl(story.media_url) !== null;
259
+ }
260
+ function renderFailed(labels, onRetry) {
261
+ const retry = (event) => {
262
+ event.stopPropagation();
263
+ onRetry == null ? void 0 : onRetry();
264
+ };
265
+ return html`
266
+ <div class="media-status" ?data-retry=${!!onRetry}>
267
+ <span class="media-status-text">
268
+ ${icon("warning")}
269
+ ${onRetry ? labels.videoFailed : labels.videoUnavailable}
270
+ </span>
271
+ ${onRetry ? html`
272
+ <button type="button" class="media-retry" @click=${retry}>
273
+ ${labels.retry}
274
+ </button>
275
+ ` : nothing}
276
+ </div>
277
+ `;
278
+ }
279
+ function renderVideoLoading(labels) {
280
+ return html`
281
+ <div class="media-status">
282
+ <span class="media-status-text">
283
+ <span class="media-spinner" aria-hidden="true"></span>
284
+ ${labels.videoLoading}
285
+ </span>
286
+ </div>
287
+ `;
91
288
  }
92
- function renderMedia(story, video, { withVideo = true, loop = true, onEnded } = {}) {
289
+ function renderStatus(video, labels, onRetry) {
290
+ if (video.failed) return renderFailed(labels, onRetry);
291
+ if (video.retrying) return renderVideoLoading(labels);
292
+ return nothing;
293
+ }
294
+ function renderNotice(video, { labels = pt, onRetry, announce = true }) {
295
+ return html`
296
+ <div
297
+ class="media-notice"
298
+ role=${announce ? "status" : nothing}
299
+ aria-hidden=${announce ? nothing : "true"}
300
+ >
301
+ ${renderStatus(video, labels, onRetry)}
302
+ </div>
303
+ `;
304
+ }
305
+ function renderMedia(story, video, options = {}) {
306
+ const { withVideo = true, loop = true, onEnded, autoplay = true } = options;
93
307
  const posterUrl = httpUrl(story.poster_url);
94
308
  const mediaUrl = withVideo ? httpUrl(story.media_url) : null;
309
+ const preload = autoplay ? "metadata" : "auto";
95
310
  return html`
96
311
  ${posterUrl ? html`<img class="poster" alt="" src=${posterUrl} />` : nothing}
97
312
  ${mediaUrl && !video.failed ? html`
@@ -100,22 +315,138 @@ function renderMedia(story, video, { withVideo = true, loop = true, onEnded } =
100
315
  muted
101
316
  playsinline
102
317
  ?loop=${loop}
103
- preload="metadata"
318
+ preload=${preload}
104
319
  .muted=${true}
105
- ?autoplay=${!prefersReducedMotion()}
320
+ ?autoplay=${autoplay && !prefersReducedMotion()}
106
321
  poster=${posterUrl != null ? posterUrl : nothing}
107
322
  src=${mediaUrl}
108
323
  ${ref(video.ref)}
109
324
  @play=${video.sync}
110
325
  @pause=${video.sync}
111
326
  @volumechange=${video.sync}
327
+ @loadeddata=${video.loaded}
112
328
  @error=${video.fail}
113
329
  @ended=${onEnded}
114
330
  ></video>
115
331
  ` : nothing}
332
+ ${mediaUrl ? renderNotice(video, options) : nothing}
116
333
  `;
117
334
  }
118
335
 
336
+ // src/components/media/media.styles.ts
337
+ import { css } from "lit";
338
+ var mediaStyles = css`
339
+ /* The live region the notice shows in, over the cover. Empty until the
340
+ video can't load or is loading again. Clicks go through to the card. */
341
+ .media-notice {
342
+ position: absolute;
343
+ inset: 0;
344
+ pointer-events: none;
345
+ }
346
+
347
+ /* The notice: a dimmed layer with the text in the middle. */
348
+ .media-status {
349
+ position: absolute;
350
+ inset: 0;
351
+ display: flex;
352
+ flex-direction: column;
353
+ gap: 12px;
354
+ align-items: center;
355
+ justify-content: center;
356
+ box-sizing: border-box;
357
+ padding: 16px;
358
+ background: rgba(0, 0, 0, 0.45);
359
+ color: #fff;
360
+ text-align: center;
361
+ pointer-events: none;
362
+ }
363
+
364
+ .media-status > * {
365
+ animation: media-status-in 0.4s ease-out both;
366
+ }
367
+
368
+ .media-status-text {
369
+ display: flex;
370
+ flex-direction: column;
371
+ gap: 8px;
372
+ align-items: center;
373
+ max-width: 100%;
374
+ font-size: 14px;
375
+ font-weight: 600;
376
+ line-height: 1.3;
377
+ text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
378
+ }
379
+
380
+ .media-status-text .icon {
381
+ width: 18px;
382
+ height: 18px;
383
+ }
384
+
385
+ .media-status[data-retry] .media-status-text .icon {
386
+ width: 24px;
387
+ height: 24px;
388
+ }
389
+
390
+ .media-retry {
391
+ min-height: 36px;
392
+ padding: 0 16px;
393
+ border: 0;
394
+ border-radius: var(--_button-radius);
395
+ background: rgba(255, 255, 255, 0.15);
396
+ backdrop-filter: blur(4px);
397
+ box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);
398
+ color: #fff;
399
+ font: inherit;
400
+ font-size: 14px;
401
+ font-weight: 600;
402
+ cursor: pointer;
403
+ pointer-events: auto;
404
+ animation-delay: 0.06s;
405
+ }
406
+
407
+ .media-retry:focus-visible {
408
+ outline: 2px solid #fff;
409
+ outline-offset: 2px;
410
+ }
411
+
412
+ @media (hover: hover) and (pointer: fine) {
413
+ .media-retry:hover {
414
+ background: rgba(255, 255, 255, 0.25);
415
+ }
416
+ }
417
+
418
+ /* It spins with reduced motion too: a still ring wouldn't read as
419
+ loading. */
420
+ .media-spinner {
421
+ width: 24px;
422
+ height: 24px;
423
+ box-sizing: border-box;
424
+ border: 2px solid rgba(255, 255, 255, 0.35);
425
+ border-top-color: #fff;
426
+ border-radius: 50%;
427
+ animation: media-spin 0.8s linear infinite;
428
+ }
429
+
430
+ @keyframes media-status-in {
431
+ from {
432
+ opacity: 0;
433
+ transform: translateY(4px);
434
+ }
435
+ }
436
+
437
+ @keyframes media-spin {
438
+ to {
439
+ transform: rotate(360deg);
440
+ }
441
+ }
442
+
443
+ @media (prefers-reduced-motion: reduce) {
444
+ .media-status > :not(.media-spinner) {
445
+ animation: none;
446
+ }
447
+ }
448
+ `;
449
+
119
450
  // src/components/media/video-controller.ts
120
451
  import { createRef } from "lit/directives/ref.js";
121
452
  function playVideo(video) {
@@ -130,8 +461,7 @@ var VideoController = class {
130
461
  this.paused = true;
131
462
  // The video always starts muted: that is what browsers allow to autoplay.
132
463
  this.muted = true;
133
- /** The video could not load: only the cover is shown. */
134
- this.failed = false;
464
+ this.status = "playable";
135
465
  /** Listener for the video's play, pause and volumechange events. */
136
466
  this.sync = () => {
137
467
  const video = this.ref.value;
@@ -140,22 +470,78 @@ var VideoController = class {
140
470
  this.muted = video.muted;
141
471
  this.host.requestUpdate();
142
472
  };
473
+ /** Listener for the video's loadeddata event. */
474
+ this.loaded = () => {
475
+ this.settleReady(true);
476
+ if (!this.retrying) return;
477
+ this.status = "playable";
478
+ this.host.requestUpdate();
479
+ };
143
480
  /** Listener for the video's error event. */
144
481
  this.fail = () => {
145
- this.failed = true;
482
+ this.status = "failed";
483
+ this.settleReady(false);
146
484
  this.host.requestUpdate();
147
485
  };
486
+ this.waitForReady();
487
+ }
488
+ /**
489
+ * Resolves true once the video's first frame has loaded, or false if it
490
+ * fails. A new one for each new <video> (see reset).
491
+ */
492
+ get ready() {
493
+ return this.readyState;
494
+ }
495
+ /** The video could not load: the cover shows with a notice over it. */
496
+ get failed() {
497
+ return this.status === "failed";
498
+ }
499
+ /** A failed video is loading again (see retry), until it loads or fails. */
500
+ get retrying() {
501
+ return this.status === "retrying";
148
502
  }
149
503
  /** The rendered <video> is on the page and playing. */
150
504
  get playing() {
151
505
  const video = this.ref.value;
152
506
  return !!video && video.isConnected && !video.paused;
153
507
  }
154
- /** For a new <video>, such as each time the player opens. */
508
+ /**
509
+ * For a new <video>, such as each time the player opens. A retry still
510
+ * loading resolves false.
511
+ */
155
512
  reset() {
513
+ this.cancelRetry();
156
514
  this.paused = true;
157
515
  this.muted = true;
158
- this.failed = false;
516
+ this.status = "playable";
517
+ this.waitForReady();
518
+ }
519
+ /**
520
+ * For the next Story's <video>: neither failed nor loading again (a retry
521
+ * still loading resolves false). Unlike reset, it keeps the playback and
522
+ * sound state.
523
+ */
524
+ clearFailure() {
525
+ if (this.retrying) {
526
+ this.cancelRetry();
527
+ this.waitForReady();
528
+ }
529
+ this.status = "playable";
530
+ this.host.requestUpdate();
531
+ }
532
+ /**
533
+ * Loads a failed video again: renderMedia renders a new <video> with a
534
+ * notice that it is loading, until it loads or fails. Resolves as `ready`
535
+ * does: false too if the video is replaced first (see reset and
536
+ * clearFailure), and at once for a video that didn't fail.
537
+ */
538
+ retry() {
539
+ if (this.retrying) return this.ready;
540
+ if (!this.failed) return Promise.resolve(false);
541
+ this.reset();
542
+ this.status = "retrying";
543
+ this.host.requestUpdate();
544
+ return this.ready;
159
545
  }
160
546
  play() {
161
547
  const video = this.ref.value;
@@ -215,6 +601,13 @@ var VideoController = class {
215
601
  const video = this.ref.value;
216
602
  if (video) video.muted = !video.muted;
217
603
  }
604
+ // The video loading again is replaced: its retry resolves false.
605
+ cancelRetry() {
606
+ if (this.retrying) this.settleReady(false);
607
+ }
608
+ waitForReady() {
609
+ this.readyState = new Promise((settle) => this.settleReady = settle);
610
+ }
218
611
  };
219
612
 
220
613
  // src/components/product/product.ts
@@ -266,33 +659,31 @@ function priceFormatter(locale, currency) {
266
659
 
267
660
  // src/components/product/product.ts
268
661
  function toCardProduct(story, format, labels, fetched) {
269
- var _a, _b, _c, _d;
662
+ var _a, _b, _c, _d, _e, _f;
270
663
  const product = story.product;
271
664
  const name = (_a = product == null ? void 0 : product.product_name) == null ? void 0 : _a.trim();
272
665
  if (!product || !name) return void 0;
273
666
  const base = {
274
667
  name,
275
668
  imageUrl: (_b = httpUrl(product.image_url)) != null ? _b : void 0,
276
- url: (_c = httpUrl(story.destination_url)) != null ? _c : void 0
669
+ url: (_c = destinationUrl(story.destination_url)) != null ? _c : void 0,
670
+ sku: ((_d = product.product_sku) == null ? void 0 : _d.trim()) || void 0,
671
+ seller: ((_e = product.seller_id) == null ? void 0 : _e.trim()) || void 0
277
672
  };
278
673
  if (fetched === "loading") {
279
674
  return { ...base, label: labels.product(name), isLoading: true };
280
675
  }
281
- if ((fetched == null ? void 0 : fetched.available) === false) {
282
- return {
283
- ...base,
284
- label: labels.product(name, labels.soldOut),
285
- soldOut: labels.soldOut
286
- };
287
- }
288
- const amounts = (_d = fetched == null ? void 0 : fetched.price) != null ? _d : readProductPrice(product.product_metadata);
676
+ const soldOut = (fetched == null ? void 0 : fetched.available) === false ? labels.soldOut : void 0;
677
+ const shown = (amounts2) => soldOut && (amounts2 == null ? void 0 : amounts2.price) === 0 ? void 0 : amounts2;
678
+ const amounts = (_f = shown(fetched == null ? void 0 : fetched.price)) != null ? _f : shown(readProductPrice(product.product_metadata));
289
679
  const price = amounts && format(amounts.price);
290
680
  const listPrice = (amounts == null ? void 0 : amounts.listPrice) === void 0 ? void 0 : format(amounts.listPrice);
291
681
  return {
292
682
  ...base,
293
683
  price,
294
684
  listPrice,
295
- label: labels.product(name, price, listPrice)
685
+ soldOut,
686
+ label: labels.product(name, price, listPrice, soldOut)
296
687
  };
297
688
  }
298
689
  var priceSkeleton = html2`
@@ -309,10 +700,9 @@ var addToCartSkeleton = html2`
309
700
  `;
310
701
  function renderPrice({ isLoading, soldOut, price, listPrice }) {
311
702
  if (isLoading) return priceSkeleton;
312
- if (soldOut) {
313
- return html2`<span class="product-price product-sold-out">${soldOut}</span>`;
703
+ if (!price) {
704
+ return soldOut ? html2`<span class="product-price product-sold-out">${soldOut}</span>` : nothing2;
314
705
  }
315
- if (!price) return nothing2;
316
706
  return html2`
317
707
  <span class="product-price">
318
708
  <span>${price}</span>
@@ -341,7 +731,48 @@ function renderProduct(product, onSelect) {
341
731
  </button>
342
732
  `;
343
733
  }
344
- function renderAddToCart({ isLoading, soldOut }, { labels }) {
734
+ var STEPPER_ADD_CLASS = "product-stepper-add";
735
+ var ADD_TO_CART_CLASS = "product-add-to-cart";
736
+ function focusCartAction(root, action) {
737
+ var _a;
738
+ const className = action === "stepper" ? STEPPER_ADD_CLASS : ADD_TO_CART_CLASS;
739
+ (_a = root.querySelector(`.${className}`)) == null ? void 0 : _a.focus();
740
+ }
741
+ function renderStepper(quantity, {
742
+ labels,
743
+ onAddToCart,
744
+ onRemoveFromCart
745
+ }) {
746
+ const last = quantity === 1;
747
+ return html2`
748
+ <div
749
+ class="product-action product-stepper"
750
+ role="group"
751
+ aria-label=${labels.cartQuantity}
752
+ >
753
+ <button
754
+ type="button"
755
+ class="product-stepper-button product-stepper-remove"
756
+ aria-label=${last ? labels.removeFromCart : labels.decreaseQuantity}
757
+ @click=${onRemoveFromCart}
758
+ >
759
+ ${icon(last ? "trash" : "minus")}
760
+ </button>
761
+ <span class="product-quantity">${quantity}</span>
762
+ <button
763
+ type="button"
764
+ class="product-stepper-button ${STEPPER_ADD_CLASS}"
765
+ aria-label=${labels.increaseQuantity}
766
+ @click=${onAddToCart}
767
+ >
768
+ ${icon("plus")}
769
+ </button>
770
+ </div>
771
+ `;
772
+ }
773
+ function renderAddToCart({ isLoading, soldOut, sku }, options) {
774
+ const { labels, onAddToCart, cartQuantity = 0 } = options;
775
+ if (!sku || !onAddToCart) return nothing2;
345
776
  if (isLoading) return addToCartSkeleton;
346
777
  if (soldOut) {
347
778
  return html2`
@@ -350,12 +781,25 @@ function renderAddToCart({ isLoading, soldOut }, { labels }) {
350
781
  </button>
351
782
  `;
352
783
  }
784
+ if (cartQuantity > 0) return renderStepper(cartQuantity, options);
353
785
  return html2`
354
- <button type="button" class="product-action product-add-to-cart">
786
+ <button
787
+ type="button"
788
+ class="product-action ${ADD_TO_CART_CLASS}"
789
+ @click=${onAddToCart}
790
+ >
355
791
  ${labels.addToCart}
356
792
  </button>
357
793
  `;
358
794
  }
795
+ function renderCartStatus({ sku }, { labels, cartQuantity = 0, onAddToCart }) {
796
+ if (!sku || !onAddToCart) return nothing2;
797
+ return html2`
798
+ <span class="product-cart-status" role="status">
799
+ ${cartQuantity > 0 ? `${labels.cartQuantity}: ${cartQuantity}` : ""}
800
+ </span>
801
+ `;
802
+ }
359
803
  function renderExpandableProduct(product, options) {
360
804
  const { open } = options;
361
805
  return html2`
@@ -373,6 +817,7 @@ function renderExpandableProduct(product, options) {
373
817
  </button>
374
818
  <div id=${options.panelId} class="product-panel" ?inert=${!open}>
375
819
  <div class="product-actions">
820
+ ${renderCartStatus(product, options)}
376
821
  ${renderAddToCart(product, options)}
377
822
  ${product.url ? html2`
378
823
  <a
@@ -396,7 +841,7 @@ function toCardCta(story) {
396
841
  var _a;
397
842
  if (story.product) return void 0;
398
843
  const label = (_a = story.call_to_action) == null ? void 0 : _a.trim();
399
- const url = httpUrl(story.destination_url);
844
+ const url = destinationUrl(story.destination_url);
400
845
  return label && url ? { label, url } : void 0;
401
846
  }
402
847
  function renderCta(cta, onSelect) {
@@ -419,8 +864,8 @@ function renderCta(cta, onSelect) {
419
864
  }
420
865
 
421
866
  // src/components/product/product.styles.ts
422
- import { css } from "lit";
423
- var productStyles = css`
867
+ import { css as css2 } from "lit";
868
+ var productStyles = css2`
424
869
  /* A glass pill at the bottom of the card, above its button: the product or
425
870
  the call to action. */
426
871
  .card-product,
@@ -434,9 +879,9 @@ var productStyles = css`
434
879
  gap: 8px;
435
880
  align-items: center;
436
881
  box-sizing: border-box;
437
- padding: 8px;
882
+ padding: var(--_pill-inset);
438
883
  border: 0;
439
- border-radius: 16px;
884
+ border-radius: var(--_pill-radius);
440
885
  background: rgba(255, 255, 255, 0.15);
441
886
  backdrop-filter: blur(5px);
442
887
  color: #fff;
@@ -531,7 +976,7 @@ var productStyles = css`
531
976
  align-items: center;
532
977
  box-sizing: border-box;
533
978
  width: 100%;
534
- padding: 8px;
979
+ padding: var(--_pill-inset);
535
980
  border: 0;
536
981
  border-radius: inherit;
537
982
  background: transparent;
@@ -594,7 +1039,7 @@ var productStyles = css`
594
1039
  height: 48px;
595
1040
  padding: 0 16px;
596
1041
  border: 1px solid var(--vtex-ads-story-color-accent, #1b75a8);
597
- border-radius: 50px;
1042
+ border-radius: var(--_button-radius);
598
1043
  font: inherit;
599
1044
  font-size: 14px;
600
1045
  font-weight: 700;
@@ -621,6 +1066,62 @@ var productStyles = css`
621
1066
  );
622
1067
  }
623
1068
 
1069
+ /* In the cart: the add to cart button's pill, with minus and plus at its
1070
+ ends and the quantity between them. */
1071
+ .product-stepper {
1072
+ justify-content: space-between;
1073
+ padding: 0;
1074
+ background: var(--vtex-ads-story-color-accent, #1b75a8);
1075
+ color: var(--vtex-ads-story-color-accent-contrast, #fff);
1076
+ cursor: auto;
1077
+ }
1078
+
1079
+ .product-stepper-button {
1080
+ display: grid;
1081
+ flex: none;
1082
+ place-items: center;
1083
+ width: 46px;
1084
+ height: 46px;
1085
+ padding: 0;
1086
+ border: 0;
1087
+ border-radius: var(--_button-radius);
1088
+ background: transparent;
1089
+ color: inherit;
1090
+ cursor: pointer;
1091
+ transition: background-color 0.2s ease;
1092
+ }
1093
+
1094
+ .product-stepper-button .icon {
1095
+ width: 18px;
1096
+ height: 18px;
1097
+ }
1098
+
1099
+ .product-stepper-button:hover {
1100
+ background: rgba(0, 0, 0, 0.15);
1101
+ }
1102
+
1103
+ .product-stepper-button:focus-visible {
1104
+ outline: 2px solid var(--vtex-ads-story-color-accent-contrast, #fff);
1105
+ outline-offset: -4px;
1106
+ }
1107
+
1108
+ .product-quantity {
1109
+ flex: 1;
1110
+ min-width: 0;
1111
+ font-size: 16px;
1112
+ text-align: center;
1113
+ }
1114
+
1115
+ .product-cart-status {
1116
+ position: absolute;
1117
+ width: 1px;
1118
+ height: 1px;
1119
+ margin: -1px;
1120
+ overflow: hidden;
1121
+ clip-path: inset(50%);
1122
+ white-space: nowrap;
1123
+ }
1124
+
624
1125
  /* Sold out: add to cart, disabled and grayed. */
625
1126
  .product-unavailable {
626
1127
  border-color: transparent;
@@ -710,11 +1211,12 @@ var productStyles = css`
710
1211
  }
711
1212
  }
712
1213
 
1214
+ /* Its corners follow the pill's, one inset in from them: 8px by default. */
713
1215
  .product-image {
714
1216
  flex: none;
715
1217
  width: 60px;
716
1218
  height: 60px;
717
- border-radius: 8px;
1219
+ border-radius: max(0px, var(--_pill-radius) - var(--_pill-inset));
718
1220
  background: #fff;
719
1221
  object-fit: contain;
720
1222
  }
@@ -778,7 +1280,7 @@ var productStyles = css`
778
1280
  .product-add-to-cart-loading {
779
1281
  display: block;
780
1282
  height: 48px;
781
- border-radius: 50px;
1283
+ border-radius: var(--_button-radius);
782
1284
  }
783
1285
 
784
1286
  @media (prefers-reduced-motion: reduce) {
@@ -789,7 +1291,7 @@ var productStyles = css`
789
1291
  `;
790
1292
 
791
1293
  // src/components/card/card.ts
792
- var cardStyles = css2`
1294
+ var cardStyles = css3`
793
1295
  /* 9:16 at the height its container gives it (--card-height, default
794
1296
  500px). */
795
1297
  .card {
@@ -922,7 +1424,12 @@ function renderCard(story, video, options) {
922
1424
  return (_a2 = options.onPress) == null ? void 0 : _a2.call(options);
923
1425
  }, capture: true }}
924
1426
  >
925
- ${renderMedia(story, video, { withVideo })}
1427
+ ${renderMedia(story, video, {
1428
+ withVideo,
1429
+ autoplay: options.autoplay,
1430
+ labels: options.labels,
1431
+ announce: false
1432
+ })}
926
1433
  <button
927
1434
  type="button"
928
1435
  class="card-open"
@@ -930,7 +1437,7 @@ function renderCard(story, video, options) {
930
1437
  aria-label=${options.label}
931
1438
  @click=${options.onSelect}
932
1439
  >
933
- <span class="play" aria-hidden="true">${icon("play")}</span>
1440
+ ${withVideo && video.failed ? nothing3 : html4`<span class="play" aria-hidden="true">${icon("play")}</span>`}
934
1441
  </button>
935
1442
  ${withVideo && hasVideo(story, video) && !video.paused ? renderSoundButton(video, options) : nothing3}
936
1443
  ${options.product ? renderProduct(
@@ -944,19 +1451,30 @@ function renderCard(story, video, options) {
944
1451
 
945
1452
  // src/components/carousel/carousel.ts
946
1453
  import {
947
- css as css3,
1454
+ css as css4,
948
1455
  html as html5
949
1456
  } from "lit";
950
1457
  import { repeat } from "lit/directives/repeat.js";
951
1458
  import { styleMap } from "lit/directives/style-map.js";
952
- var STAGE_HEIGHT = css3`500px`;
953
- var SOLO_PADDING = css3`24px`;
1459
+ var STAGE_HEIGHT = css4`500px`;
1460
+ var SOLO_PADDING = css4`24px`;
954
1461
  var AUTO_ADVANCE_MS = 5e3;
1462
+ var MIN_SLIDE_DURATION_MS = 2e3;
1463
+ var MAX_SLIDE_DURATION_MS = 12e4;
1464
+ var toSlideDuration = (ms) => {
1465
+ if (ms === void 0 || !Number.isFinite(ms) || ms < 0) {
1466
+ return AUTO_ADVANCE_MS;
1467
+ }
1468
+ if (ms === 0) {
1469
+ return 0;
1470
+ }
1471
+ return Math.min(Math.max(ms, MIN_SLIDE_DURATION_MS), MAX_SLIDE_DURATION_MS);
1472
+ };
955
1473
  var SWIPE_DISTANCE = 40;
956
- var NARROW_SCREEN = css3`(max-width: 576px)`;
1474
+ var NARROW_SCREEN = css4`(max-width: 576px)`;
957
1475
  var VISIBLE_SIDE = 2;
958
1476
  var RENDERED_SIDE = VISIBLE_SIDE + 3;
959
- var carouselStyles = css3`
1477
+ var carouselStyles = css4`
960
1478
  .carousel {
961
1479
  position: relative;
962
1480
  box-sizing: border-box;
@@ -1134,9 +1652,12 @@ var CarouselController = class {
1134
1652
  this.distance = 1;
1135
1653
  this.count = 0;
1136
1654
  this.narrow = false;
1655
+ this.reducedMotion = false;
1137
1656
  this.connected = false;
1138
1657
  this.hovered = false;
1139
1658
  this.focused = false;
1659
+ /** The interval the running timer was started with. */
1660
+ this.timerMs = 0;
1140
1661
  /** The rendered cards, as their cardRef reported them. */
1141
1662
  this.cards = /* @__PURE__ */ new Map();
1142
1663
  /** The rendered cards' focus targets, as their focusRef reported them. */
@@ -1235,11 +1756,12 @@ var CarouselController = class {
1235
1756
  }
1236
1757
  }
1237
1758
  hostConnected() {
1238
- var _a, _b, _c, _d;
1759
+ var _a, _b, _c, _d, _e, _f, _g;
1239
1760
  this.connected = true;
1240
1761
  this.media = (_a = globalThis.matchMedia) == null ? void 0 : _a.call(globalThis, NARROW_SCREEN.toString());
1241
1762
  (_b = this.media) == null ? void 0 : _b.addEventListener("change", this.onMediaChange);
1242
1763
  this.narrow = (_d = (_c = this.media) == null ? void 0 : _c.matches) != null ? _d : false;
1764
+ this.reducedMotion = (_g = (_f = (_e = this.options).reducedMotion) == null ? void 0 : _f.call(_e)) != null ? _g : false;
1243
1765
  this.syncTimer();
1244
1766
  }
1245
1767
  hostDisconnected() {
@@ -1250,8 +1772,8 @@ var CarouselController = class {
1250
1772
  this.endSwipe();
1251
1773
  this.syncTimer();
1252
1774
  }
1253
- // Whatever the host changed (the player opening, new Stories) may start or
1254
- // stop the automatic advance.
1775
+ // Whatever the host changed (the player opening, new Stories, the slide
1776
+ // duration) may start, stop or restart the automatic advance.
1255
1777
  hostUpdated() {
1256
1778
  this.syncTimer();
1257
1779
  }
@@ -1280,13 +1802,20 @@ var CarouselController = class {
1280
1802
  (_d = (_c = this.options).onStep) == null ? void 0 : _d.call(_c, from, this.activeKey);
1281
1803
  this.restartTimer();
1282
1804
  }
1805
+ get slideDuration() {
1806
+ var _a, _b;
1807
+ return toSlideDuration((_b = (_a = this.options).slideDuration) == null ? void 0 : _b.call(_a));
1808
+ }
1283
1809
  get shouldAdvance() {
1284
1810
  var _a, _b;
1285
- return this.connected && this.count > 1 && !this.hovered && !this.focused && !((_b = (_a = this.options).locked) == null ? void 0 : _b.call(_a));
1811
+ return this.slideDuration > 0 && !this.reducedMotion && this.connected && this.count > 1 && !this.hovered && !this.focused && !((_b = (_a = this.options).locked) == null ? void 0 : _b.call(_a));
1286
1812
  }
1287
1813
  syncTimer() {
1814
+ const ms = this.slideDuration;
1815
+ if (this.timer && this.timerMs !== ms) this.stopTimer();
1288
1816
  if (this.shouldAdvance && !this.timer) {
1289
- this.timer = setInterval(() => this.step(1), AUTO_ADVANCE_MS);
1817
+ this.timerMs = ms;
1818
+ this.timer = setInterval(() => this.step(1), ms);
1290
1819
  } else if (!this.shouldAdvance && this.timer) {
1291
1820
  this.stopTimer();
1292
1821
  }
@@ -1294,6 +1823,7 @@ var CarouselController = class {
1294
1823
  stopTimer() {
1295
1824
  clearInterval(this.timer);
1296
1825
  this.timer = void 0;
1826
+ this.timerMs = 0;
1297
1827
  }
1298
1828
  restartTimer() {
1299
1829
  this.stopTimer();
@@ -1448,6 +1978,107 @@ var CarouselController = class {
1448
1978
  }
1449
1979
  };
1450
1980
 
1981
+ // src/components/carousel/carousel-skeleton.ts
1982
+ import { css as css5, html as html6 } from "lit";
1983
+ import { styleMap as styleMap2 } from "lit/directives/style-map.js";
1984
+ var carouselSkeletonStyles = css5`
1985
+ .root {
1986
+ position: relative;
1987
+ }
1988
+
1989
+ .root[data-requesting] {
1990
+ height: ${STAGE_HEIGHT};
1991
+ }
1992
+
1993
+ .root[data-loading] .carousel {
1994
+ visibility: hidden;
1995
+ }
1996
+
1997
+ .skeleton {
1998
+ position: absolute;
1999
+ inset: 0;
2000
+ z-index: 2;
2001
+ box-sizing: border-box;
2002
+ overflow: hidden;
2003
+ pointer-events: none;
2004
+ transition:
2005
+ opacity 0.3s ease,
2006
+ visibility 0s;
2007
+ }
2008
+
2009
+ .skeleton[data-done] {
2010
+ opacity: 0;
2011
+ visibility: hidden;
2012
+ transition:
2013
+ opacity 0.3s ease,
2014
+ visibility 0s 0.3s;
2015
+ }
2016
+
2017
+ .skeleton-slide {
2018
+ position: absolute;
2019
+ top: 0;
2020
+ left: 50%;
2021
+ width: calc(${STAGE_HEIGHT} * 9 / 16);
2022
+ height: 100%;
2023
+ margin-left: calc(${STAGE_HEIGHT} * 9 / -32);
2024
+ transform: translateX(calc(var(--place) * (100% + 8px))) scale(0.9);
2025
+ }
2026
+
2027
+ .skeleton-slide[data-active] {
2028
+ transform: none;
2029
+ }
2030
+
2031
+ .skeleton-card {
2032
+ width: 100%;
2033
+ height: 100%;
2034
+ border-radius: var(--vtex-ads-story-radius, 20px);
2035
+ background: #ececec;
2036
+ animation: skeleton-pulse 1.2s ease-in-out infinite alternate;
2037
+ }
2038
+
2039
+ .skeleton[data-done] .skeleton-card {
2040
+ animation: none;
2041
+ }
2042
+
2043
+ @keyframes skeleton-pulse {
2044
+ to {
2045
+ background-color: #f6f6f6;
2046
+ }
2047
+ }
2048
+
2049
+ @media (prefers-reduced-motion: reduce) {
2050
+ .skeleton,
2051
+ .skeleton[data-done] {
2052
+ transition: none;
2053
+ }
2054
+
2055
+ .skeleton-card {
2056
+ animation: none;
2057
+ }
2058
+ }
2059
+ `;
2060
+ function renderCarouselSkeleton({ done }) {
2061
+ const places = Array.from(
2062
+ { length: VISIBLE_SIDE * 2 + 1 },
2063
+ (_, i) => i - VISIBLE_SIDE
2064
+ );
2065
+ return html6`
2066
+ <div class="skeleton" aria-hidden="true" ?data-done=${done}>
2067
+ ${places.map(
2068
+ (place) => html6`
2069
+ <div
2070
+ class="skeleton-slide"
2071
+ ?data-active=${place === 0}
2072
+ style=${styleMap2({ "--place": String(place) })}
2073
+ >
2074
+ <div class="skeleton-card"></div>
2075
+ </div>
2076
+ `
2077
+ )}
2078
+ </div>
2079
+ `;
2080
+ }
2081
+
1451
2082
  // src/tag-name.ts
1452
2083
  var TAG_NAME = "vtex-ads-story-carousel";
1453
2084
 
@@ -1503,6 +2134,7 @@ var StoryEventsController = class {
1503
2134
  click: /* @__PURE__ */ new Set()
1504
2135
  };
1505
2136
  this.pendingImpressions = [];
2137
+ this.connected = false;
1506
2138
  this.watched = /* @__PURE__ */ new Map();
1507
2139
  this.onIntersect = (entries) => {
1508
2140
  for (const entry of entries) {
@@ -1525,6 +2157,7 @@ var StoryEventsController = class {
1525
2157
  }
1526
2158
  hostConnected() {
1527
2159
  var _a;
2160
+ this.connected = true;
1528
2161
  if (typeof IntersectionObserver === "function") {
1529
2162
  this.observer = new IntersectionObserver(this.onIntersect, {
1530
2163
  threshold: VIEW_THRESHOLD
@@ -1534,10 +2167,15 @@ var StoryEventsController = class {
1534
2167
  "visibilitychange",
1535
2168
  this.onVisibilityChange
1536
2169
  );
1537
- this.host.updateComplete.then(() => this.syncViewTargets());
2170
+ this.host.updateComplete.then(() => {
2171
+ if (!this.connected) return;
2172
+ this.sendImpressions();
2173
+ this.syncViewTargets();
2174
+ });
1538
2175
  }
1539
2176
  hostDisconnected() {
1540
2177
  var _a, _b;
2178
+ this.connected = false;
1541
2179
  (_a = globalThis.document) == null ? void 0 : _a.removeEventListener(
1542
2180
  "visibilitychange",
1543
2181
  this.onVisibilityChange
@@ -1546,24 +2184,26 @@ var StoryEventsController = class {
1546
2184
  this.observer = void 0;
1547
2185
  for (const watched of this.watched.values()) clearTimeout(watched.timer);
1548
2186
  this.watched.clear();
1549
- this.pendingImpressions = [];
1550
2187
  }
1551
2188
  hostUpdated() {
1552
- const stories = this.pendingImpressions;
1553
- if (stories.length && this.options.identity().sessionId) {
1554
- this.pendingImpressions = [];
1555
- for (const story of stories) this.send("impression", story);
1556
- }
2189
+ if (!this.connected) return;
2190
+ this.sendImpressions();
1557
2191
  this.syncViewTargets();
1558
2192
  }
1559
2193
  /**
1560
- * The Stories the host renders next: their impressions go out once that
1561
- * render is done, or, without a session yet, after the first render that
1562
- * has one. Replaces Stories passed before and not sent yet.
2194
+ * The Stories the host shows next: their impressions go out once that
2195
+ * render is done, on the page, with a session (or after the first render
2196
+ * that has both). Replaces Stories passed before and not sent yet.
1563
2197
  */
1564
2198
  impress(stories) {
1565
2199
  this.pendingImpressions = stories;
1566
2200
  }
2201
+ sendImpressions() {
2202
+ const stories = this.pendingImpressions;
2203
+ if (!stories.length || !this.options.identity().sessionId) return;
2204
+ this.pendingImpressions = [];
2205
+ for (const story of stories) this.send("impression", story);
2206
+ }
1567
2207
  /** A touch on the Story: its click goes out, the first time only. */
1568
2208
  click(story) {
1569
2209
  this.send("click", story);
@@ -1630,13 +2270,13 @@ var StoryEventsController = class {
1630
2270
  };
1631
2271
 
1632
2272
  // src/components/player/player.ts
1633
- import { css as css4, html as html6, nothing as nothing4 } from "lit";
2273
+ import { css as css6, html as html7, nothing as nothing4 } from "lit";
1634
2274
  import { createRef as createRef2, ref as ref3 } from "lit/directives/ref.js";
1635
2275
  import { repeat as repeat2 } from "lit/directives/repeat.js";
1636
- import { styleMap as styleMap2 } from "lit/directives/style-map.js";
2276
+ import { styleMap as styleMap3 } from "lit/directives/style-map.js";
1637
2277
  var NEAR_GAP = 72;
1638
2278
  var FAR_GAP = 48;
1639
- var playerStyles = css4`
2279
+ var playerStyles = css6`
1640
2280
  .player {
1641
2281
  position: fixed;
1642
2282
  inset: auto 16px 16px auto;
@@ -1849,7 +2489,7 @@ var playerStyles = css4`
1849
2489
  padding: 0;
1850
2490
  overflow: hidden;
1851
2491
  border: 0;
1852
- border-radius: 16px;
2492
+ border-radius: var(--vtex-ads-story-radius, 16px);
1853
2493
  background: #111;
1854
2494
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
1855
2495
  cursor: pointer;
@@ -2177,6 +2817,18 @@ var PlayerController = class {
2177
2817
  this.productOpen = !this.productOpen;
2178
2818
  this.host.requestUpdate();
2179
2819
  };
2820
+ /**
2821
+ * Loads the failed video again and, once it loads, plays it with the sound
2822
+ * on (muted if the browser blocks it). Not if the player closed or stepped
2823
+ * meanwhile, even back to the same Story: the retry then resolves false.
2824
+ */
2825
+ this.retryVideo = () => {
2826
+ var _a;
2827
+ (_a = this.dialog.value) == null ? void 0 : _a.focus();
2828
+ void this.video.retry().then((loaded) => {
2829
+ if (loaded) this.video.playWithSound();
2830
+ });
2831
+ };
2180
2832
  /**
2181
2833
  * A click on the video: closes the product panel when it is open, plays or
2182
2834
  * pauses the video otherwise.
@@ -2200,7 +2852,8 @@ var PlayerController = class {
2200
2852
  this.close();
2201
2853
  }
2202
2854
  /**
2203
- * Opens the mini player, plays the video and focuses the close button.
2855
+ * Opens the mini player, plays the video with the sound on (muted if the
2856
+ * browser blocks it) and focuses the close button.
2204
2857
  * With `productOpen`, the product panel shows open (the shopper chose the
2205
2858
  * product).
2206
2859
  */
@@ -2220,7 +2873,7 @@ var PlayerController = class {
2220
2873
  const dialog = this.dialog.value;
2221
2874
  if (!this.isOpen || !dialog) return;
2222
2875
  this.show(dialog);
2223
- this.video.play();
2876
+ this.video.playWithSound();
2224
2877
  (_a = this.closeButton.value) == null ? void 0 : _a.focus();
2225
2878
  }
2226
2879
  /** Hides the player now; the host removes it on its next update. */
@@ -2242,7 +2895,7 @@ var PlayerController = class {
2242
2895
  async showNext(delta = 0) {
2243
2896
  if (!this.isOpen) return;
2244
2897
  const withSound = !this.video.muted;
2245
- this.video.failed = false;
2898
+ this.video.clearFailure();
2246
2899
  this.productOpen = false;
2247
2900
  this.host.requestUpdate();
2248
2901
  await this.host.updateComplete;
@@ -2344,21 +2997,27 @@ var PlayerController = class {
2344
2997
  * its actions, or the call to action of a link destination. Clicks on them
2345
2998
  * don't reach the video.
2346
2999
  */
2347
- renderDestination({ product, cta, labels }) {
3000
+ renderDestination({
3001
+ product,
3002
+ cta,
3003
+ labels,
3004
+ productActions
3005
+ }) {
2348
3006
  let content;
2349
3007
  if (product) {
2350
3008
  content = renderExpandableProduct(product, {
3009
+ ...productActions,
3010
+ labels,
2351
3011
  open: this.productOpen,
2352
3012
  onToggle: this.toggleProduct,
2353
- panelId: "player-product-panel",
2354
- labels
3013
+ panelId: "player-product-panel"
2355
3014
  });
2356
3015
  } else if (cta) {
2357
3016
  content = renderCta(cta);
2358
3017
  } else {
2359
3018
  return nothing4;
2360
3019
  }
2361
- return html6`
3020
+ return html7`
2362
3021
  <div
2363
3022
  class="player-destination"
2364
3023
  @click=${(event) => event.stopPropagation()}
@@ -2377,7 +3036,7 @@ var PlayerController = class {
2377
3036
  event.stopPropagation();
2378
3037
  video.toggleMuted();
2379
3038
  };
2380
- return html6`
3039
+ return html7`
2381
3040
  <button
2382
3041
  type="button"
2383
3042
  class="play player-toggle"
@@ -2406,7 +3065,7 @@ var PlayerController = class {
2406
3065
  var _a;
2407
3066
  return (_a = options.onStep) == null ? void 0 : _a.call(options, delta);
2408
3067
  };
2409
- return html6`
3068
+ return html7`
2410
3069
  <div class="player-controls player-nav">
2411
3070
  <button
2412
3071
  type="button"
@@ -2436,12 +3095,12 @@ var PlayerController = class {
2436
3095
  ({ offset, story, label }) => {
2437
3096
  const poster = httpUrl(story.poster_url);
2438
3097
  const side = Math.sign(offset);
2439
- return html6`
3098
+ return html7`
2440
3099
  <button
2441
3100
  type="button"
2442
3101
  class="player-neighbor"
2443
3102
  aria-label=${label != null ? label : labels.show}
2444
- style=${styleMap2({
3103
+ style=${styleMap3({
2445
3104
  "--side": String(side),
2446
3105
  "--beyond": String(offset - side)
2447
3106
  })}
@@ -2451,7 +3110,7 @@ var PlayerController = class {
2451
3110
  return (_a = options.onStep) == null ? void 0 : _a.call(options, offset);
2452
3111
  }}
2453
3112
  >
2454
- ${poster ? html6`<img alt="" src=${poster} />` : nothing4}
3113
+ ${poster ? html7`<img alt="" src=${poster} />` : nothing4}
2455
3114
  </button>
2456
3115
  `;
2457
3116
  }
@@ -2460,7 +3119,7 @@ var PlayerController = class {
2460
3119
  /** Expand or collapse, and close: above the video, or at the top right. */
2461
3120
  renderWindowControls(labels) {
2462
3121
  const { options, expanded } = this;
2463
- return html6`
3122
+ return html7`
2464
3123
  <div class="player-controls">
2465
3124
  <button
2466
3125
  type="button"
@@ -2490,14 +3149,16 @@ var PlayerController = class {
2490
3149
  */
2491
3150
  renderVideo(story, advances, destination) {
2492
3151
  const { video, options } = this;
2493
- return html6`
3152
+ return html7`
2494
3153
  <div class="card" ${ref3(this.stageCardRef)} @click=${this.onVideoClick}>
2495
3154
  ${renderMedia(story, video, {
2496
3155
  loop: !advances,
2497
3156
  onEnded: advances ? () => {
2498
3157
  var _a;
2499
3158
  return (_a = options.onStep) == null ? void 0 : _a.call(options, 1);
2500
- } : void 0
3159
+ } : void 0,
3160
+ labels: destination.labels,
3161
+ onRetry: this.retryVideo
2501
3162
  })}
2502
3163
  ${hasVideo(story, video) ? this.renderVideoControls(destination.labels) : nothing4}
2503
3164
  ${this.renderDestination(destination)}
@@ -2528,26 +3189,25 @@ var PlayerController = class {
2528
3189
  * The expanded view also shows the `neighbors` beside it.
2529
3190
  */
2530
3191
  render(story, {
2531
- labels,
2532
3192
  navigation = false,
2533
3193
  neighbors = [],
2534
- product,
2535
- cta,
2536
- onPress
3194
+ onPress,
3195
+ ...destination
2537
3196
  }) {
3197
+ const { labels } = destination;
2538
3198
  if (!this.isOpen) return nothing4;
2539
3199
  const { video, expanded } = this;
2540
3200
  const shownNeighbors = this.track(story, neighbors);
2541
3201
  const pair = this.pairDirection !== 0;
2542
3202
  this.navigation = navigation;
2543
- return html6`
3203
+ return html7`
2544
3204
  <dialog
2545
3205
  class="player"
2546
3206
  popover="manual"
2547
3207
  aria-label=${labels.player}
2548
3208
  ?data-expanded=${expanded}
2549
3209
  ?data-pair=${pair}
2550
- style=${styleMap2({
3210
+ style=${styleMap3({
2551
3211
  "--pair-dir": pair ? String(this.pairDirection) : void 0
2552
3212
  })}
2553
3213
  ?data-switched=${this.switched}
@@ -2573,11 +3233,7 @@ var PlayerController = class {
2573
3233
  }}
2574
3234
  >
2575
3235
  ${navigation ? this.renderNavigation(labels) : nothing4}
2576
- ${this.renderVideo(story, navigation && expanded, {
2577
- product,
2578
- cta,
2579
- labels
2580
- })}
3236
+ ${this.renderVideo(story, navigation && expanded, destination)}
2581
3237
  <span class="player-dim" aria-hidden="true" ${ref3(this.dim)}></span>
2582
3238
  </div>
2583
3239
  </div>
@@ -2628,6 +3284,12 @@ var intelligentSearchHydration = {
2628
3284
  price: intelligentSearchPrice,
2629
3285
  available: intelligentSearchAvailability
2630
3286
  };
3287
+ function toShownPrice(value) {
3288
+ const isAmount = (amount) => typeof amount === "number" && Number.isFinite(amount) && amount >= 0;
3289
+ if (!isAmount(value == null ? void 0 : value.price)) return void 0;
3290
+ const { price, listPrice } = value;
3291
+ return isAmount(listPrice) && listPrice > price ? { price, listPrice } : { price };
3292
+ }
2631
3293
  async function hydrateStoryProducts(placement, stories, hydration, identity, { signal, timeout } = {}) {
2632
3294
  var _a, _b;
2633
3295
  const products = /* @__PURE__ */ new Map();
@@ -2642,12 +3304,19 @@ async function hydrateStoryProducts(placement, stories, hydration, identity, { s
2642
3304
  );
2643
3305
  for (const { story, product, offer } of (_a = hydrated.stories[placement]) != null ? _a : []) {
2644
3306
  if (!product || !offer) continue;
2645
- const price = hydration.price(product, offer);
2646
- const available = (_b = hydration.available) == null ? void 0 : _b.call(hydration, product, offer);
2647
- products.set(story.ad_id, {
2648
- ...price && { price },
2649
- ...available !== void 0 && { available }
2650
- });
3307
+ try {
3308
+ const price = toShownPrice(hydration.price(product, offer));
3309
+ const available = (_b = hydration.available) == null ? void 0 : _b.call(hydration, product, offer);
3310
+ products.set(story.ad_id, {
3311
+ ...price && { price },
3312
+ ...available !== void 0 && { available }
3313
+ });
3314
+ } catch (error) {
3315
+ console.error(
3316
+ `[${TAG_NAME}] Product details failed for ad ${story.ad_id}`,
3317
+ error
3318
+ );
3319
+ }
2651
3320
  }
2652
3321
  } catch (error) {
2653
3322
  if (!limit.signal.aborted) {
@@ -2710,105 +3379,57 @@ async function fetchStories({
2710
3379
  }
2711
3380
  }
2712
3381
 
2713
- // src/locales/en.ts
2714
- var en = {
2715
- watch: "Watch video",
2716
- show: "Show video",
2717
- carousel: "Videos",
2718
- carouselRole: "carousel",
2719
- previous: "Previous video",
2720
- next: "Next video",
2721
- player: "Video",
2722
- expand: "Expand video",
2723
- collapse: "Collapse video",
2724
- close: "Close video",
2725
- play: "Play video",
2726
- pause: "Pause video",
2727
- soundOn: "Turn sound on",
2728
- soundOff: "Turn sound off",
2729
- addToCart: "Add to cart",
2730
- unavailable: "Product unavailable",
2731
- soldOut: "Sold out",
2732
- viewProduct: "View product",
2733
- product: (name, price, listPrice) => {
2734
- if (price && listPrice) return `${name}, was ${listPrice}, now ${price}`;
2735
- return price ? `${name}, ${price}` : name;
2736
- },
2737
- position: (label, index, count) => count > 1 ? `${label} ${index + 1} of ${count}` : label
2738
- };
2739
-
2740
- // src/locales/es.ts
2741
- var es = {
2742
- watch: "Ver video",
2743
- show: "Mostrar video",
2744
- carousel: "Videos",
2745
- carouselRole: "carrusel",
2746
- previous: "Video anterior",
2747
- next: "Video siguiente",
2748
- player: "Video",
2749
- expand: "Ampliar video",
2750
- collapse: "Reducir video",
2751
- close: "Cerrar video",
2752
- play: "Reproducir video",
2753
- pause: "Pausar video",
2754
- soundOn: "Activar sonido",
2755
- soundOff: "Desactivar sonido",
2756
- addToCart: "Agregar al carrito",
2757
- unavailable: "Producto no disponible",
2758
- soldOut: "Agotado",
2759
- viewProduct: "Ver producto",
2760
- product: (name, price, listPrice) => {
2761
- if (price && listPrice)
2762
- return `${name}, antes ${listPrice}, ahora ${price}`;
2763
- return price ? `${name}, ${price}` : name;
2764
- },
2765
- position: (label, index, count) => count > 1 ? `${label} ${index + 1} de ${count}` : label
2766
- };
2767
-
2768
- // src/locales/pt.ts
2769
- var pt = {
2770
- watch: "Assistir v\xEDdeo",
2771
- show: "Mostrar v\xEDdeo",
2772
- carousel: "V\xEDdeos",
2773
- carouselRole: "carrossel",
2774
- previous: "V\xEDdeo anterior",
2775
- next: "Pr\xF3ximo v\xEDdeo",
2776
- player: "V\xEDdeo",
2777
- expand: "Expandir v\xEDdeo",
2778
- collapse: "Reduzir v\xEDdeo",
2779
- close: "Fechar v\xEDdeo",
2780
- play: "Reproduzir v\xEDdeo",
2781
- pause: "Pausar v\xEDdeo",
2782
- soundOn: "Ativar som",
2783
- soundOff: "Desativar som",
2784
- addToCart: "Adicionar ao carrinho",
2785
- unavailable: "Produto indispon\xEDvel",
2786
- soldOut: "Esgotado",
2787
- viewProduct: "Ver produto",
2788
- product: (name, price, listPrice) => {
2789
- if (price && listPrice) return `${name}, de ${listPrice} por ${price}`;
2790
- return price ? `${name}, ${price}` : name;
2791
- },
2792
- position: (label, index, count) => count > 1 ? `${label} ${index + 1} de ${count}` : label
2793
- };
3382
+ // src/utils/media-ready.ts
3383
+ var MEDIA_TIMEOUT = 3e3;
3384
+ function decodeImage(url) {
3385
+ if (typeof Image === "undefined") return Promise.resolve(true);
3386
+ const image = new Image();
3387
+ image.src = url;
3388
+ if (!image.decode) return Promise.resolve(true);
3389
+ return image.decode().then(
3390
+ () => true,
3391
+ () => false
3392
+ );
3393
+ }
3394
+ function firstReady(checks, signal) {
3395
+ if (!checks.length) return Promise.resolve();
3396
+ const limit = withTimeout(void 0, MEDIA_TIMEOUT, signal);
3397
+ return new Promise((resolve) => {
3398
+ const done = () => {
3399
+ limit.clear();
3400
+ resolve();
3401
+ };
3402
+ if (limit.signal.aborted) return done();
3403
+ limit.signal.addEventListener("abort", done, { once: true });
3404
+ let failed = 0;
3405
+ for (const check of checks) {
3406
+ check.then(
3407
+ (ready) => {
3408
+ if (ready || ++failed === checks.length) done();
3409
+ },
3410
+ () => {
3411
+ if (++failed === checks.length) done();
3412
+ }
3413
+ );
3414
+ }
3415
+ });
3416
+ }
2794
3417
 
2795
- // src/locales/index.ts
2796
- var BY_LANGUAGE = /* @__PURE__ */ new Map([
2797
- ["pt", pt],
2798
- ["es", es],
2799
- ["en", en]
2800
- ]);
2801
- function labelsFor(locale, overrides = {}) {
2802
- var _a, _b, _c;
2803
- const language = locale.split("-")[0].toLowerCase();
2804
- const labels = (_a = BY_LANGUAGE.get(language)) != null ? _a : pt;
2805
- const addToCart = (_b = overrides.addToCart) == null ? void 0 : _b.trim();
2806
- const viewProduct = (_c = overrides.viewProduct) == null ? void 0 : _c.trim();
2807
- return {
2808
- ...labels,
2809
- ...addToCart && { addToCart },
2810
- ...viewProduct && { viewProduct }
2811
- };
3418
+ // src/commerce-events.ts
3419
+ var ADD_TO_CART_EVENT = "vtex-ads-story:add-to-cart";
3420
+ var REMOVE_FROM_CART_EVENT = "vtex-ads-story:remove-from-cart";
3421
+ function cartEvent(type, { sku, seller }, quantity) {
3422
+ return new CustomEvent(type, {
3423
+ detail: { sku, ...seller && { seller }, quantity },
3424
+ bubbles: true,
3425
+ composed: true
3426
+ });
3427
+ }
3428
+ function addToCartEvent(item, quantity = 1) {
3429
+ return cartEvent(ADD_TO_CART_EVENT, item, quantity);
3430
+ }
3431
+ function removeFromCartEvent(item, quantity = 1) {
3432
+ return cartEvent(REMOVE_FROM_CART_EVENT, item, quantity);
2812
3433
  }
2813
3434
 
2814
3435
  // src/story/story.ts
@@ -2833,9 +3454,17 @@ var channelConverter = {
2833
3454
  var hydrationConverter = {
2834
3455
  fromAttribute: (value) => value === "none" ? "none" : void 0
2835
3456
  };
3457
+ var cartKey = ({ sku, seller }) => JSON.stringify([sku, seller != null ? seller : ""]);
2836
3458
  var VtexAdsStoryCarouselElement = class extends LitElement {
2837
3459
  constructor() {
2838
3460
  super();
3461
+ /**
3462
+ * How many of each SKU, from each seller (see cartKey), were added since
3463
+ * the player opened, for its stepper. It doesn't read the store's cart: it
3464
+ * counts the add and remove events sent, and starts over each time the
3465
+ * player opens, as the cart may have changed meanwhile.
3466
+ */
3467
+ this.cartQuantities = /* @__PURE__ */ new Map();
2839
3468
  /**
2840
3469
  * One per rendered card, by its key (see Slide): a Story can show in more
2841
3470
  * than one card, each with its own video.
@@ -2857,7 +3486,9 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2857
3486
  }
2858
3487
  },
2859
3488
  // The player shows the highlighted Story: the highlight stays on it.
2860
- locked: () => this.player.isOpen
3489
+ locked: () => this.player.isOpen || this.mediaLoading,
3490
+ slideDuration: () => this.slideDuration,
3491
+ reducedMotion: prefersReducedMotion
2861
3492
  });
2862
3493
  this.player = new PlayerController(this, {
2863
3494
  onStep: (delta) => this.stepInPlayer(delta),
@@ -2879,6 +3510,7 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2879
3510
  ),
2880
3511
  labels: this.labels,
2881
3512
  withVideo: active || this.inactiveAutoplay && !hidden,
3513
+ autoplay: !this.mediaLoading,
2882
3514
  product: this.productOf(story),
2883
3515
  onSelect: active ? () => this.openPlayer() : () => this.carousel.step(offset),
2884
3516
  // On a card without the highlight, the product moves it, as the card.
@@ -2893,6 +3525,8 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2893
3525
  });
2894
3526
  this.inactiveAutoplay = false;
2895
3527
  this.stories = [];
3528
+ this.requesting = false;
3529
+ this.mediaLoading = false;
2896
3530
  }
2897
3531
  // Lit keeps the card's DOM when the element is moved (removed and appended
2898
3532
  // again), but the browser pauses a removed video and doesn't autoplay it
@@ -2908,9 +3542,10 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2908
3542
  }
2909
3543
  // A request still pending when the element leaves the page is cancelled.
2910
3544
  disconnectedCallback() {
2911
- var _a;
3545
+ var _a, _b;
2912
3546
  super.disconnectedCallback();
2913
3547
  (_a = this.request) == null ? void 0 : _a.abort();
3548
+ (_b = this.mediaWait) == null ? void 0 : _b.abort();
2914
3549
  }
2915
3550
  willUpdate(changed) {
2916
3551
  if (REQUEST_PROPERTIES.some((name) => changed.has(name))) {
@@ -2939,16 +3574,21 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2939
3574
  const request = new AbortController();
2940
3575
  this.request = request;
2941
3576
  this.player.close();
3577
+ this.requesting = false;
2942
3578
  this.showStories([]);
2943
3579
  if (!this.isConnected) {
2944
3580
  request.abort();
2945
3581
  return;
2946
3582
  }
3583
+ this.requesting = Boolean(
3584
+ this.publisherId && this.sessionId && this.placement
3585
+ );
2947
3586
  const stories = await fetchStories(this, {
2948
3587
  signal: request.signal,
2949
3588
  timeout: this.timeout
2950
3589
  });
2951
3590
  if (request.signal.aborted) return;
3591
+ this.requesting = false;
2952
3592
  const hydration = this.activeHydration;
2953
3593
  if (!hydration || !stories.some((story) => story.product)) {
2954
3594
  this.request = void 0;
@@ -2982,7 +3622,34 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2982
3622
  this.afterRender = void 0;
2983
3623
  this.carousel.reset(stories.length);
2984
3624
  this.stories = stories;
3625
+ void this.waitForFirstMedia(stories);
3626
+ }
3627
+ /**
3628
+ * Shows the cards once the first Story's cover or video is ready: whichever
3629
+ * loads first, or once both failed. Only then are the Stories impressed
3630
+ * and do their videos play, from the start.
3631
+ */
3632
+ async waitForFirstMedia(stories) {
3633
+ var _a, _b, _c;
3634
+ (_a = this.mediaWait) == null ? void 0 : _a.abort();
3635
+ const wait = new AbortController();
3636
+ this.mediaWait = wait;
3637
+ const cover = httpUrl((_b = stories[0]) == null ? void 0 : _b.poster_url);
3638
+ const video = httpUrl((_c = stories[0]) == null ? void 0 : _c.media_url);
3639
+ const checks = [];
3640
+ if (cover) checks.push(decodeImage(cover));
3641
+ if (video) checks.push(this.videoFor(this.carousel.activeKey).ready);
3642
+ this.mediaLoading = checks.length > 0;
3643
+ await firstReady(checks, wait.signal);
3644
+ if (this.mediaWait !== wait) return;
3645
+ this.mediaWait = void 0;
3646
+ this.mediaLoading = false;
2985
3647
  this.events.impress(stories);
3648
+ this.afterRender = () => {
3649
+ if (!this.isConnected) return;
3650
+ for (const cardVideo of this.cardVideos.values()) cardVideo.resume();
3651
+ };
3652
+ this.requestUpdate();
2986
3653
  }
2987
3654
  /**
2988
3655
  * Where each Story is being watched: the cards on screen (not the hidden
@@ -2993,6 +3660,7 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
2993
3660
  *viewTargets() {
2994
3661
  var _a;
2995
3662
  const { stories } = this;
3663
+ if (this.mediaLoading) return;
2996
3664
  if (!this.player.isExpanded) {
2997
3665
  for (const [card, { key, index }] of this.carousel.visibleCards()) {
2998
3666
  const story = stories[index];
@@ -3050,6 +3718,7 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
3050
3718
  openPlayer({ productOpen = false } = {}) {
3051
3719
  var _a;
3052
3720
  if (this.player.isOpen) return;
3721
+ this.cartQuantities.clear();
3053
3722
  (_a = this.activeVideo) == null ? void 0 : _a.pause();
3054
3723
  void this.player.open({ productOpen });
3055
3724
  }
@@ -3072,14 +3741,28 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
3072
3741
  }
3073
3742
  get labels() {
3074
3743
  var _a;
3075
- const { locale, labelAddToCart = "", labelViewProduct = "" } = this;
3076
- const key = JSON.stringify([locale, labelAddToCart, labelViewProduct]);
3744
+ const {
3745
+ locale,
3746
+ labelAddToCart = "",
3747
+ labelViewProduct = "",
3748
+ labelUnavailable = "",
3749
+ labelSoldOut = ""
3750
+ } = this;
3751
+ const key = JSON.stringify([
3752
+ locale,
3753
+ labelAddToCart,
3754
+ labelViewProduct,
3755
+ labelUnavailable,
3756
+ labelSoldOut
3757
+ ]);
3077
3758
  if (((_a = this.labelSet) == null ? void 0 : _a.key) !== key) {
3078
3759
  this.labelSet = {
3079
3760
  key,
3080
3761
  labels: labelsFor(locale, {
3081
3762
  addToCart: labelAddToCart,
3082
- viewProduct: labelViewProduct
3763
+ viewProduct: labelViewProduct,
3764
+ unavailable: labelUnavailable,
3765
+ soldOut: labelSoldOut
3083
3766
  })
3084
3767
  };
3085
3768
  }
@@ -3103,7 +3786,23 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
3103
3786
  const { stories } = this;
3104
3787
  const index = this.carousel.active;
3105
3788
  const active = stories[index];
3106
- if (!active) return nothing5;
3789
+ if (!active && !this.requesting) return nothing5;
3790
+ const loading = this.requesting || this.mediaLoading;
3791
+ return html8`
3792
+ <div
3793
+ class="root"
3794
+ ?data-loading=${loading}
3795
+ ?data-requesting=${this.requesting}
3796
+ >
3797
+ ${active ? this.carousel.render(stories, this.renderSlide, this.labels) : nothing5}
3798
+ ${renderCarouselSkeleton({ done: !loading })}
3799
+ </div>
3800
+ ${active ? this.renderPlayer(active) : nothing5}
3801
+ `;
3802
+ }
3803
+ renderPlayer(active) {
3804
+ const { stories } = this;
3805
+ const index = this.carousel.active;
3107
3806
  const neighbors = neighborOffsets(stories.length, index).map((offset) => {
3108
3807
  const neighbor = (index + offset + stories.length) % stories.length;
3109
3808
  return {
@@ -3112,19 +3811,52 @@ var VtexAdsStoryCarouselElement = class extends LitElement {
3112
3811
  label: this.labels.position(this.labels.show, neighbor, stories.length)
3113
3812
  };
3114
3813
  });
3115
- return html7`
3116
- <div class="root">
3117
- ${this.carousel.render(stories, this.renderSlide, this.labels)}
3118
- </div>
3119
- ${this.player.render(active, {
3814
+ const product = this.productOf(active);
3815
+ return this.player.render(active, {
3120
3816
  labels: this.labels,
3121
3817
  navigation: stories.length > 1,
3122
3818
  neighbors,
3123
- product: this.productOf(active),
3819
+ product,
3124
3820
  cta: toCardCta(active),
3821
+ productActions: this.cartActionsFor(product),
3125
3822
  onPress: () => this.events.click(active)
3126
- })}
3127
- `;
3823
+ });
3824
+ }
3825
+ cartActionsFor(product) {
3826
+ const sku = product == null ? void 0 : product.sku;
3827
+ if (!sku) return void 0;
3828
+ const item = { sku, seller: product.seller };
3829
+ return {
3830
+ cartQuantity: this.cartQuantities.get(cartKey(item)),
3831
+ onAddToCart: () => this.changeCart(item, 1),
3832
+ onRemoveFromCart: () => this.changeCart(item, -1)
3833
+ };
3834
+ }
3835
+ /**
3836
+ * Adds one of `item` to the stepper's count, or removes one, and tells the
3837
+ * store, which changes its cart: the element has none. When add to cart
3838
+ * and the stepper swap places, the focus moves to the one now shown.
3839
+ */
3840
+ changeCart(item, delta) {
3841
+ var _a;
3842
+ const key = cartKey(item);
3843
+ const before = (_a = this.cartQuantities.get(key)) != null ? _a : 0;
3844
+ const after = Math.max(0, before + delta);
3845
+ if (after === before) return;
3846
+ if (after > 0) this.cartQuantities.set(key, after);
3847
+ else this.cartQuantities.delete(key);
3848
+ this.requestUpdate();
3849
+ this.dispatchEvent(
3850
+ delta > 0 ? addToCartEvent(item) : removeFromCartEvent(item)
3851
+ );
3852
+ let swapped;
3853
+ if (before === 0) swapped = "stepper";
3854
+ else if (after === 0) swapped = "add";
3855
+ if (!swapped) return;
3856
+ const action = swapped;
3857
+ this.updateComplete.then(
3858
+ () => this.shadowRoot && focusCartAction(this.shadowRoot, action)
3859
+ );
3128
3860
  }
3129
3861
  };
3130
3862
  VtexAdsStoryCarouselElement.properties = {
@@ -3139,27 +3871,37 @@ VtexAdsStoryCarouselElement.properties = {
3139
3871
  currency: {},
3140
3872
  labelAddToCart: { attribute: "label-add-to-cart" },
3141
3873
  labelViewProduct: { attribute: "label-view-product" },
3874
+ labelUnavailable: { attribute: "label-unavailable" },
3875
+ labelSoldOut: { attribute: "label-sold-out" },
3142
3876
  hydration: { converter: hydrationConverter },
3143
3877
  hydrationTimeout: { type: Number, attribute: "hydration-timeout" },
3878
+ slideDuration: { type: Number, attribute: "slide-duration" },
3144
3879
  inactiveAutoplay: {
3145
3880
  type: Boolean,
3146
3881
  attribute: "inactive-autoplay",
3147
3882
  converter: booleanConverter
3148
3883
  },
3149
3884
  stories: { state: true },
3150
- products: { state: true }
3885
+ products: { state: true },
3886
+ requesting: { state: true },
3887
+ mediaLoading: { state: true }
3151
3888
  };
3152
3889
  VtexAdsStoryCarouselElement.styles = [
3153
- css5`
3890
+ css7`
3154
3891
  :host {
3155
3892
  display: block;
3156
3893
  font-family: var(--vtex-ads-story-font-family, inherit);
3157
3894
  color: var(--vtex-ads-story-color-text, inherit);
3895
+ --_pill-radius: var(--vtex-ads-story-radius, 16px);
3896
+ --_pill-inset: 8px;
3897
+ --_button-radius: var(--vtex-ads-story-radius, 999px);
3158
3898
  }
3159
3899
  `,
3160
3900
  cardStyles,
3901
+ mediaStyles,
3161
3902
  productStyles,
3162
3903
  carouselStyles,
3904
+ carouselSkeletonStyles,
3163
3905
  playerStyles
3164
3906
  ];
3165
3907
  function defineVtexAdsStoryCarousel() {
@@ -3168,6 +3910,8 @@ function defineVtexAdsStoryCarousel() {
3168
3910
  customElements.define(TAG_NAME, VtexAdsStoryCarouselElement);
3169
3911
  }
3170
3912
  export {
3913
+ ADD_TO_CART_EVENT,
3914
+ REMOVE_FROM_CART_EVENT,
3171
3915
  TAG_NAME,
3172
3916
  VtexAdsStoryCarouselElement,
3173
3917
  defineVtexAdsStoryCarousel