@vtex/ads-stories 0.4.0 → 0.5.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 (51) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +260 -28
  3. package/dist/cdn/vtex-ads-stories.js +1073 -20
  4. package/dist/cdn/vtex-ads-stories.js.map +1 -1
  5. package/dist/cjs/index.cjs +4392 -315
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.d.cts +198 -12
  8. package/dist/cjs/react.cjs.map +1 -1
  9. package/dist/cjs/react.d.cts +46 -5
  10. package/dist/esm/index.d.ts +198 -12
  11. package/dist/esm/index.js +2819 -291
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/react.d.ts +46 -5
  14. package/package.json +21 -4
  15. package/src/cdn.ts +7 -3
  16. package/src/components/card/card.ts +101 -111
  17. package/src/components/card/index.ts +1 -1
  18. package/src/components/carousel/carousel.ts +670 -0
  19. package/src/components/carousel/index.ts +15 -0
  20. package/src/components/events/events-controller.ts +212 -0
  21. package/src/components/events/index.ts +6 -0
  22. package/src/components/icons/assets/caret-left-bold.svg +1 -0
  23. package/src/components/icons/assets/caret-right-bold.svg +1 -0
  24. package/src/components/icons/assets/caret-up-bold.svg +1 -0
  25. package/src/components/icons/index.ts +30 -9
  26. package/src/components/media/index.ts +3 -0
  27. package/src/components/media/media.ts +70 -0
  28. package/src/components/media/video-controller.ts +125 -0
  29. package/src/components/player/index.ts +6 -2
  30. package/src/components/player/player.ts +946 -134
  31. package/src/components/product/cta.ts +56 -0
  32. package/src/components/product/index.ts +20 -0
  33. package/src/components/product/price.ts +86 -0
  34. package/src/components/product/product.styles.ts +369 -0
  35. package/src/components/product/product.ts +235 -0
  36. package/src/index.ts +9 -1
  37. package/src/locales/en.ts +29 -0
  38. package/src/locales/es.ts +30 -0
  39. package/src/locales/index.ts +36 -0
  40. package/src/locales/pt.ts +29 -0
  41. package/src/locales/types.ts +24 -0
  42. package/src/react.ts +49 -7
  43. package/src/story/index.ts +5 -1
  44. package/src/story/story.ts +528 -93
  45. package/src/tag-name.ts +1 -0
  46. package/src/types.ts +8 -12
  47. package/src/utils/fetch-story.ts +113 -0
  48. package/src/utils/http-url.ts +12 -0
  49. package/src/utils/hydrate-products.ts +88 -0
  50. package/src/utils/send-event.ts +71 -0
  51. package/src/utils/with-timeout.ts +43 -0
@@ -0,0 +1,56 @@
1
+ import { html } from "lit";
2
+ import { icon } from "../icons";
3
+ import { httpUrl } from "../../utils/http-url";
4
+ import type { StoryAd } from "../../types";
5
+
6
+ /** The call to action of a Story whose destination is a link. */
7
+ export interface CardCta {
8
+ /** The button's text, from the campaign ("Saiba mais"). */
9
+ label: string;
10
+ /** The advertiser's link. */
11
+ url: string;
12
+ }
13
+
14
+ /**
15
+ * The call to action of a Story without a product (its destination is a
16
+ * link), with the text set in the campaign. Undefined when the Story has a
17
+ * product, no text or no http(s) destination.
18
+ */
19
+ export function toCardCta(
20
+ story: Pick<StoryAd, "product" | "destination_url" | "call_to_action">,
21
+ ): CardCta | undefined {
22
+ if (story.product) return undefined;
23
+
24
+ const label = story.call_to_action?.trim();
25
+ const url = httpUrl(story.destination_url);
26
+
27
+ return label && url ? { label, url } : undefined;
28
+ }
29
+
30
+ /**
31
+ * The call to action at the bottom of the card or the video: a glass pill
32
+ * with the text and a caret, a link to the destination in a new tab. With
33
+ * `onSelect`, a click calls it instead of following the link (a card
34
+ * without the highlight takes it first).
35
+ */
36
+ export function renderCta(cta: CardCta, onSelect?: () => void) {
37
+ const onClick = onSelect
38
+ ? (event: Event) => {
39
+ event.preventDefault();
40
+ onSelect();
41
+ }
42
+ : undefined;
43
+
44
+ return html`
45
+ <a
46
+ class="card-cta"
47
+ href=${cta.url}
48
+ target="_blank"
49
+ rel="noopener noreferrer"
50
+ @click=${onClick}
51
+ >
52
+ <span class="cta-label">${cta.label}</span>
53
+ ${icon("next")}
54
+ </a>
55
+ `;
56
+ }
@@ -0,0 +1,20 @@
1
+ export {
2
+ renderExpandableProduct,
3
+ renderProduct,
4
+ toCardProduct,
5
+ } from "./product";
6
+ export { renderCta, toCardCta } from "./cta";
7
+ export type { CardCta } from "./cta";
8
+ export { productStyles } from "./product.styles";
9
+ export type {
10
+ CardProduct,
11
+ ExpandableProductOptions,
12
+ FetchedProduct,
13
+ } from "./product";
14
+ export {
15
+ DEFAULT_CURRENCY,
16
+ DEFAULT_LOCALE,
17
+ priceFormatter,
18
+ readProductPrice,
19
+ } from "./price";
20
+ export type { ProductPrice } from "./price";
@@ -0,0 +1,86 @@
1
+ /** Locale used when the element has none and the page has no `lang`. */
2
+ export const DEFAULT_LOCALE = "pt-BR";
3
+
4
+ /** Currency used when the element has none or an invalid one. */
5
+ export const DEFAULT_CURRENCY = "BRL";
6
+
7
+ /**
8
+ * A product's price, as plain numbers. `listPrice` is the "from" price, only
9
+ * present when it is above `price` (the product is on sale).
10
+ */
11
+ export interface ProductPrice {
12
+ price: number;
13
+ listPrice?: number;
14
+ }
15
+
16
+ // A positive amount, from a number or a numeric string ("129.9").
17
+ function toAmount(value: unknown) {
18
+ let amount = Number.NaN;
19
+ if (typeof value === "number") amount = value;
20
+ else if (typeof value === "string" && value.trim() !== "") {
21
+ amount = Number(value);
22
+ }
23
+ return Number.isFinite(amount) && amount > 0 ? amount : undefined;
24
+ }
25
+
26
+ /**
27
+ * Reads the price from a Story product's `product_metadata`, with the names
28
+ * the ad platform uses for prices: `price` (regular) and `promotional_price`
29
+ * (sale, 0 or missing when there is none). The ad API doesn't send a price
30
+ * yet, so this is undefined for now unless the catalog indexed one.
31
+ */
32
+ export function readProductPrice(
33
+ metadata: Record<string, unknown> | undefined,
34
+ ): ProductPrice | undefined {
35
+ const regular = toAmount(metadata?.price);
36
+ const promotional = toAmount(metadata?.promotional_price);
37
+
38
+ if (
39
+ promotional !== undefined &&
40
+ (regular === undefined || promotional < regular)
41
+ ) {
42
+ return regular === undefined
43
+ ? { price: promotional }
44
+ : { price: promotional, listPrice: regular };
45
+ }
46
+ return regular === undefined ? undefined : { price: regular };
47
+ }
48
+
49
+ // A language the browser can format numbers in.
50
+ function isLocale(locale: string) {
51
+ try {
52
+ return Intl.NumberFormat.supportedLocalesOf(locale).length > 0;
53
+ } catch {
54
+ return false;
55
+ }
56
+ }
57
+
58
+ // Intl.supportedValuesOf is newer than the TypeScript lib this package
59
+ // targets, and missing in older browsers.
60
+ const intl = Intl as typeof Intl & {
61
+ supportedValuesOf?: (key: "currency") => string[];
62
+ };
63
+
64
+ // A currency the browser knows. Without the list (older browsers), any
65
+ // three-letter code.
66
+ function isCurrency(currency: string) {
67
+ if (!/^[a-z]{3}$/i.test(currency)) return false;
68
+ const known = intl.supportedValuesOf?.("currency");
69
+ return known ? known.includes(currency.toUpperCase()) : true;
70
+ }
71
+
72
+ /**
73
+ * Formats amounts as the given currency in the given locale ("R$ 129,90").
74
+ * An empty or unknown locale falls back to DEFAULT_LOCALE, and an empty or
75
+ * unknown currency (not an ISO 4217 code) to DEFAULT_CURRENCY.
76
+ */
77
+ export function priceFormatter(locale?: string, currency?: string) {
78
+ const format = new Intl.NumberFormat(
79
+ locale && isLocale(locale) ? locale : DEFAULT_LOCALE,
80
+ {
81
+ style: "currency",
82
+ currency: currency && isCurrency(currency) ? currency : DEFAULT_CURRENCY,
83
+ },
84
+ );
85
+ return (amount: number) => format.format(amount);
86
+ }
@@ -0,0 +1,369 @@
1
+ import { css } from "lit";
2
+
3
+ export const productStyles = css`
4
+ /* A glass pill at the bottom of the card, above its button: the product or
5
+ the call to action. */
6
+ .card-product,
7
+ .card-cta {
8
+ position: absolute;
9
+ right: 12px;
10
+ bottom: 12px;
11
+ left: 12px;
12
+ z-index: 1;
13
+ display: flex;
14
+ gap: 8px;
15
+ align-items: center;
16
+ box-sizing: border-box;
17
+ padding: 8px;
18
+ border: 0;
19
+ border-radius: 16px;
20
+ background: rgba(255, 255, 255, 0.15);
21
+ backdrop-filter: blur(5px);
22
+ color: #fff;
23
+ font: inherit;
24
+ text-align: left;
25
+ isolation: isolate;
26
+ transition: transform 0.2s ease;
27
+ }
28
+
29
+ /* The glass's tint and inner light, darker on hover. */
30
+ .card-product::before,
31
+ .card-cta::before {
32
+ position: absolute;
33
+ inset: 0;
34
+ z-index: -1;
35
+ border-radius: inherit;
36
+ background: rgba(0, 0, 0, 0.15);
37
+ box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);
38
+ content: "";
39
+ transition: background-color 0.2s ease;
40
+ }
41
+
42
+ button.card-product,
43
+ .card-cta {
44
+ cursor: pointer;
45
+ }
46
+
47
+ .card-product:focus-visible,
48
+ .card-cta:focus-visible {
49
+ outline: 2px solid #fff;
50
+ outline-offset: 2px;
51
+ }
52
+
53
+ @media (hover: hover) and (pointer: fine) {
54
+ .card-product:hover,
55
+ .card-cta:hover {
56
+ transform: translateY(-2px);
57
+ }
58
+
59
+ .card-product:hover::before,
60
+ .card-cta:hover::before {
61
+ background: rgba(0, 0, 0, 0.32);
62
+ }
63
+ }
64
+
65
+ /* A Story with a link: the pill is the call to action, its text centered
66
+ with a caret after it. */
67
+ .card-cta {
68
+ justify-content: center;
69
+ gap: 6px;
70
+ height: 48px;
71
+ padding: 0 16px;
72
+ font-size: 14px;
73
+ font-weight: 700;
74
+ line-height: 1;
75
+ text-decoration: none;
76
+ }
77
+
78
+ .cta-label {
79
+ min-width: 0;
80
+ overflow: hidden;
81
+ text-overflow: ellipsis;
82
+ white-space: nowrap;
83
+ }
84
+
85
+ .card-cta .icon {
86
+ flex: none;
87
+ width: 14px;
88
+ height: 14px;
89
+ }
90
+
91
+ /* In the player: the pill is a toggle, with the actions panel below. */
92
+ .product-expandable {
93
+ display: block;
94
+ padding: 0;
95
+ transition:
96
+ background-color 0.3s ease,
97
+ color 0.3s ease,
98
+ box-shadow 0.3s ease,
99
+ transform 0.2s ease;
100
+ }
101
+
102
+ .product-expandable::before {
103
+ transition:
104
+ opacity 0.3s ease,
105
+ background-color 0.2s ease;
106
+ }
107
+
108
+ .product-summary {
109
+ display: flex;
110
+ gap: 8px;
111
+ align-items: center;
112
+ box-sizing: border-box;
113
+ width: 100%;
114
+ padding: 8px;
115
+ border: 0;
116
+ border-radius: inherit;
117
+ background: transparent;
118
+ color: inherit;
119
+ font: inherit;
120
+ text-align: left;
121
+ cursor: pointer;
122
+ transition: padding 0.3s ease;
123
+ }
124
+
125
+ .product-summary:focus-visible {
126
+ outline: 2px solid currentColor;
127
+ outline-offset: 2px;
128
+ }
129
+
130
+ /* The caret points up while closed and down while open. */
131
+ .product-toggle {
132
+ display: grid;
133
+ flex: none;
134
+ margin-right: 4px;
135
+ opacity: 0.7;
136
+ transition:
137
+ transform 0.3s ease,
138
+ opacity 0.2s ease;
139
+ }
140
+
141
+ .product-toggle .icon {
142
+ width: 14px;
143
+ height: 14px;
144
+ }
145
+
146
+ .product-summary:hover .product-toggle {
147
+ opacity: 1;
148
+ }
149
+
150
+ /* Closed: 0 rows tall, so it grows smoothly when it opens. */
151
+ .product-panel {
152
+ display: grid;
153
+ grid-template-rows: 0fr;
154
+ transition: grid-template-rows 0.3s ease;
155
+ }
156
+
157
+ .product-actions {
158
+ display: grid;
159
+ gap: 8px;
160
+ min-height: 0;
161
+ overflow: hidden;
162
+ padding: 0 12px;
163
+ opacity: 0;
164
+ transition:
165
+ padding 0.3s ease,
166
+ opacity 0.2s ease;
167
+ }
168
+
169
+ .product-action {
170
+ display: flex;
171
+ align-items: center;
172
+ justify-content: center;
173
+ box-sizing: border-box;
174
+ height: 48px;
175
+ padding: 0 16px;
176
+ border: 1px solid var(--vtex-ads-story-color-accent, #1b75a8);
177
+ border-radius: 50px;
178
+ font: inherit;
179
+ font-size: 14px;
180
+ font-weight: 700;
181
+ text-decoration: none;
182
+ cursor: pointer;
183
+ transition: background-color 0.2s ease;
184
+ }
185
+
186
+ .product-action:focus-visible {
187
+ outline: 2px solid var(--vtex-ads-story-color-accent, #1b75a8);
188
+ outline-offset: 2px;
189
+ }
190
+
191
+ .product-add-to-cart {
192
+ background: var(--vtex-ads-story-color-accent, #1b75a8);
193
+ color: var(--vtex-ads-story-color-accent-contrast, #fff);
194
+ }
195
+
196
+ .product-add-to-cart:hover {
197
+ background: color-mix(
198
+ in srgb,
199
+ var(--vtex-ads-story-color-accent, #1b75a8) 85%,
200
+ #000
201
+ );
202
+ }
203
+
204
+ /* Sold out: add to cart, disabled and grayed. */
205
+ .product-unavailable {
206
+ border-color: transparent;
207
+ background: rgba(0, 0, 0, 0.06);
208
+ color: var(--vtex-ads-story-color-muted, #6b6b6b);
209
+ cursor: not-allowed;
210
+ }
211
+
212
+ .product-view {
213
+ background: var(--vtex-ads-story-color-surface, #fff);
214
+ color: var(--vtex-ads-story-color-accent, #1b75a8);
215
+ }
216
+
217
+ /* A light tint of the accent. */
218
+ .product-view:hover {
219
+ background: color-mix(
220
+ in srgb,
221
+ var(--vtex-ads-story-color-accent, #1b75a8) 8%,
222
+ var(--vtex-ads-story-color-surface, #fff)
223
+ );
224
+ }
225
+
226
+ /* Open: a solid card (white by default), without the glass. */
227
+ .product-expandable[data-open] {
228
+ background: var(--vtex-ads-story-color-surface, #fff);
229
+ color: var(--vtex-ads-story-color-text, #242424);
230
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
231
+ }
232
+
233
+ .product-expandable[data-open]::before {
234
+ opacity: 0;
235
+ }
236
+
237
+ .product-expandable[data-open] .product-price {
238
+ color: inherit;
239
+ }
240
+
241
+ .product-expandable[data-open] .product-list-price {
242
+ color: var(--vtex-ads-story-color-muted, #6b6b6b);
243
+ opacity: 1;
244
+ }
245
+
246
+ .product-expandable[data-open] .product-summary {
247
+ padding: 12px;
248
+ }
249
+
250
+ .product-expandable[data-open] .product-toggle {
251
+ transform: rotate(180deg);
252
+ }
253
+
254
+ .product-expandable[data-open] .product-panel {
255
+ grid-template-rows: 1fr;
256
+ }
257
+
258
+ .product-expandable[data-open] .product-actions {
259
+ padding: 0 12px 12px;
260
+ opacity: 1;
261
+ transition:
262
+ padding 0.3s ease,
263
+ opacity 0.25s ease 0.1s;
264
+ }
265
+
266
+ /* Open, it stays in place on hover. */
267
+ @media (hover: hover) and (pointer: fine) {
268
+ .product-expandable[data-open]:hover {
269
+ transform: none;
270
+ }
271
+ }
272
+
273
+ @media (prefers-reduced-motion: reduce) {
274
+ .card-product,
275
+ .card-product:hover,
276
+ .card-cta,
277
+ .card-cta:hover {
278
+ transform: none;
279
+ transition: none;
280
+ }
281
+
282
+ .card-product::before,
283
+ .card-cta::before,
284
+ .product-summary,
285
+ .product-toggle,
286
+ .product-panel,
287
+ .product-actions,
288
+ .product-expandable[data-open] .product-actions {
289
+ transition: none;
290
+ }
291
+ }
292
+
293
+ .product-image {
294
+ flex: none;
295
+ width: 60px;
296
+ height: 60px;
297
+ border-radius: 8px;
298
+ background: #fff;
299
+ object-fit: contain;
300
+ }
301
+
302
+ .product-info {
303
+ display: grid;
304
+ flex: 1;
305
+ gap: 4px;
306
+ min-width: 0;
307
+ font-size: 12px;
308
+ font-weight: 700;
309
+ line-height: 16px;
310
+ }
311
+
312
+ .product-name {
313
+ display: -webkit-box;
314
+ overflow: hidden;
315
+ -webkit-box-orient: vertical;
316
+ -webkit-line-clamp: 2;
317
+ overflow-wrap: anywhere;
318
+ }
319
+
320
+ .product-price {
321
+ display: flex;
322
+ flex-wrap: wrap;
323
+ gap: 0 6px;
324
+ }
325
+
326
+ .product-list-price {
327
+ font-weight: 400;
328
+ opacity: 0.5;
329
+ }
330
+
331
+ /* In place of the price, for a product that can't be bought. */
332
+ .product-sold-out {
333
+ font-weight: 700;
334
+ opacity: 0.75;
335
+ }
336
+
337
+ /* Placeholders while the product loads, pulsing gently. */
338
+ .product-loading {
339
+ background: currentColor;
340
+ opacity: 0.25;
341
+ animation: product-loading-pulse 1.2s ease-in-out infinite alternate;
342
+ }
343
+
344
+ @keyframes product-loading-pulse {
345
+ to {
346
+ opacity: 0.1;
347
+ }
348
+ }
349
+
350
+ /* The price's: a line its height (the info's line height). */
351
+ .product-price-loading {
352
+ width: 64px;
353
+ height: 16px;
354
+ border-radius: 4px;
355
+ }
356
+
357
+ /* The add to cart button's: its size and shape. */
358
+ .product-add-to-cart-loading {
359
+ display: block;
360
+ height: 48px;
361
+ border-radius: 50px;
362
+ }
363
+
364
+ @media (prefers-reduced-motion: reduce) {
365
+ .product-loading {
366
+ animation: none;
367
+ }
368
+ }
369
+ `;