@vtex/ads-stories 0.5.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +29 -297
- package/dist/cdn/vtex-ads-stories.js +438 -113
- package/dist/cdn/vtex-ads-stories.js.map +1 -1
- package/dist/cjs/index.cjs +932 -189
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +42 -196
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/cjs/react.d.cts +13 -3
- package/dist/cjs/story-Ba9J0rbL.d.cts +276 -0
- package/dist/esm/index.d.ts +42 -196
- package/dist/esm/index.js +956 -212
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +13 -3
- package/dist/esm/story-Ba9J0rbL.d.ts +276 -0
- package/package.json +1 -1
- package/src/commerce-events.ts +93 -0
- package/src/components/card/card.ts +16 -3
- package/src/components/carousel/carousel-skeleton.ts +113 -0
- package/src/components/carousel/carousel.ts +53 -8
- package/src/components/carousel/index.ts +7 -0
- package/src/components/events/events-controller.ts +23 -13
- package/src/components/icons/assets/minus.svg +1 -0
- package/src/components/icons/assets/plus.svg +1 -0
- package/src/components/icons/assets/trash.svg +1 -0
- package/src/components/icons/assets/warning-circle-fill.svg +1 -0
- package/src/components/icons/index.ts +8 -0
- package/src/components/media/index.ts +7 -1
- package/src/components/media/media.styles.ts +113 -0
- package/src/components/media/media.ts +108 -11
- package/src/components/media/video-controller.ts +80 -6
- package/src/components/player/player.ts +42 -17
- package/src/components/product/cta.ts +3 -3
- package/src/components/product/index.ts +2 -0
- package/src/components/product/product.styles.ts +63 -6
- package/src/components/product/product.ts +130 -29
- package/src/index.ts +8 -0
- package/src/locales/en.ts +13 -3
- package/src/locales/es.ts +13 -3
- package/src/locales/index.ts +6 -0
- package/src/locales/pt.ts +13 -3
- package/src/locales/types.ts +22 -2
- package/src/react.ts +12 -2
- package/src/story/story.ts +218 -20
- package/src/utils/hydrate-products.ts +30 -6
- package/src/utils/media-ready.ts +58 -0
- package/src/utils/safe-url.ts +36 -0
- package/src/utils/send-event.ts +1 -1
- package/src/utils/http-url.ts +0 -12
|
@@ -11,12 +11,13 @@ import {
|
|
|
11
11
|
renderMedia,
|
|
12
12
|
VideoController,
|
|
13
13
|
} from "../media";
|
|
14
|
-
import { httpUrl } from "../../utils/
|
|
14
|
+
import { httpUrl } from "../../utils/safe-url";
|
|
15
15
|
import {
|
|
16
16
|
renderCta,
|
|
17
17
|
renderExpandableProduct,
|
|
18
18
|
type CardCta,
|
|
19
19
|
type CardProduct,
|
|
20
|
+
type ProductCartActions,
|
|
20
21
|
} from "../product";
|
|
21
22
|
import type { StoryMedia } from "../../types";
|
|
22
23
|
|
|
@@ -249,7 +250,7 @@ export const playerStyles = css`
|
|
|
249
250
|
padding: 0;
|
|
250
251
|
overflow: hidden;
|
|
251
252
|
border: 0;
|
|
252
|
-
border-radius: 16px;
|
|
253
|
+
border-radius: var(--vtex-ads-story-radius, 16px);
|
|
253
254
|
background: #111;
|
|
254
255
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
|
|
255
256
|
cursor: pointer;
|
|
@@ -455,6 +456,11 @@ export interface PlayerRenderOptions {
|
|
|
455
456
|
product?: CardProduct;
|
|
456
457
|
/** The Story's call to action, for a link destination (no product). */
|
|
457
458
|
cta?: CardCta;
|
|
459
|
+
/**
|
|
460
|
+
* What the product's panel does with the cart: add to cart and plus keep
|
|
461
|
+
* the player open; minus is a trash with one left.
|
|
462
|
+
*/
|
|
463
|
+
productActions?: ProductCartActions;
|
|
458
464
|
/**
|
|
459
465
|
* Called by any click on the video's card (the video, its play and sound
|
|
460
466
|
* buttons, the product and the call to action), before what that click
|
|
@@ -465,7 +471,10 @@ export interface PlayerRenderOptions {
|
|
|
465
471
|
}
|
|
466
472
|
|
|
467
473
|
// What shows at the bottom of the video: one or the other.
|
|
468
|
-
type StoryDestination = Pick<
|
|
474
|
+
type StoryDestination = Pick<
|
|
475
|
+
PlayerRenderOptions,
|
|
476
|
+
"product" | "cta" | "labels" | "productActions"
|
|
477
|
+
>;
|
|
469
478
|
|
|
470
479
|
/**
|
|
471
480
|
* Offsets of the Stories shown beside the expanded video, as in the
|
|
@@ -628,7 +637,7 @@ function supportsPopover(dialog: HTMLDialogElement) {
|
|
|
628
637
|
/**
|
|
629
638
|
* The Story player, rendered by its host with render(). open() shows it as
|
|
630
639
|
* the mini player; expand switches to the expanded view, a modal dialog, and
|
|
631
|
-
* back. The video
|
|
640
|
+
* back. The video plays with the sound on when the player opens. It closes on
|
|
632
641
|
* its own when the host leaves the page.
|
|
633
642
|
*/
|
|
634
643
|
export class PlayerController implements ReactiveController {
|
|
@@ -692,7 +701,8 @@ export class PlayerController implements ReactiveController {
|
|
|
692
701
|
}
|
|
693
702
|
|
|
694
703
|
/**
|
|
695
|
-
* Opens the mini player, plays the video
|
|
704
|
+
* Opens the mini player, plays the video with the sound on (muted if the
|
|
705
|
+
* browser blocks it) and focuses the close button.
|
|
696
706
|
* With `productOpen`, the product panel shows open (the shopper chose the
|
|
697
707
|
* product).
|
|
698
708
|
*/
|
|
@@ -715,7 +725,7 @@ export class PlayerController implements ReactiveController {
|
|
|
715
725
|
if (!this.isOpen || !dialog) return;
|
|
716
726
|
|
|
717
727
|
this.show(dialog);
|
|
718
|
-
this.video.
|
|
728
|
+
this.video.playWithSound();
|
|
719
729
|
this.closeButton.value?.focus();
|
|
720
730
|
}
|
|
721
731
|
|
|
@@ -743,7 +753,7 @@ export class PlayerController implements ReactiveController {
|
|
|
743
753
|
// Read before the render: a Story without a video, or a video that
|
|
744
754
|
// failed, replaces the <video> with a new one, muted.
|
|
745
755
|
const withSound = !this.video.muted;
|
|
746
|
-
this.video.
|
|
756
|
+
this.video.clearFailure();
|
|
747
757
|
// Another Story, another product: its panel starts closed.
|
|
748
758
|
this.productOpen = false;
|
|
749
759
|
this.host.requestUpdate();
|
|
@@ -911,6 +921,18 @@ export class PlayerController implements ReactiveController {
|
|
|
911
921
|
this.host.requestUpdate();
|
|
912
922
|
};
|
|
913
923
|
|
|
924
|
+
/**
|
|
925
|
+
* Loads the failed video again and, once it loads, plays it with the sound
|
|
926
|
+
* on (muted if the browser blocks it). Not if the player closed or stepped
|
|
927
|
+
* meanwhile, even back to the same Story: the retry then resolves false.
|
|
928
|
+
*/
|
|
929
|
+
private readonly retryVideo = () => {
|
|
930
|
+
this.dialog.value?.focus();
|
|
931
|
+
void this.video.retry().then((loaded) => {
|
|
932
|
+
if (loaded) this.video.playWithSound();
|
|
933
|
+
});
|
|
934
|
+
};
|
|
935
|
+
|
|
914
936
|
/**
|
|
915
937
|
* A click on the video: closes the product panel when it is open, plays or
|
|
916
938
|
* pauses the video otherwise.
|
|
@@ -925,14 +947,20 @@ export class PlayerController implements ReactiveController {
|
|
|
925
947
|
* its actions, or the call to action of a link destination. Clicks on them
|
|
926
948
|
* don't reach the video.
|
|
927
949
|
*/
|
|
928
|
-
private renderDestination({
|
|
950
|
+
private renderDestination({
|
|
951
|
+
product,
|
|
952
|
+
cta,
|
|
953
|
+
labels,
|
|
954
|
+
productActions,
|
|
955
|
+
}: StoryDestination) {
|
|
929
956
|
let content;
|
|
930
957
|
if (product) {
|
|
931
958
|
content = renderExpandableProduct(product, {
|
|
959
|
+
...productActions,
|
|
960
|
+
labels,
|
|
932
961
|
open: this.productOpen,
|
|
933
962
|
onToggle: this.toggleProduct,
|
|
934
963
|
panelId: "player-product-panel",
|
|
935
|
-
labels,
|
|
936
964
|
});
|
|
937
965
|
} else if (cta) {
|
|
938
966
|
content = renderCta(cta);
|
|
@@ -1092,6 +1120,8 @@ export class PlayerController implements ReactiveController {
|
|
|
1092
1120
|
${renderMedia(story, video, {
|
|
1093
1121
|
loop: !advances,
|
|
1094
1122
|
onEnded: advances ? () => options.onStep?.(1) : undefined,
|
|
1123
|
+
labels: destination.labels,
|
|
1124
|
+
onRetry: this.retryVideo,
|
|
1095
1125
|
})}
|
|
1096
1126
|
${hasVideo(story, video)
|
|
1097
1127
|
? this.renderVideoControls(destination.labels)
|
|
@@ -1129,14 +1159,13 @@ export class PlayerController implements ReactiveController {
|
|
|
1129
1159
|
render(
|
|
1130
1160
|
story: StoryMedia,
|
|
1131
1161
|
{
|
|
1132
|
-
labels,
|
|
1133
1162
|
navigation = false,
|
|
1134
1163
|
neighbors = [],
|
|
1135
|
-
product,
|
|
1136
|
-
cta,
|
|
1137
1164
|
onPress,
|
|
1165
|
+
...destination
|
|
1138
1166
|
}: PlayerRenderOptions,
|
|
1139
1167
|
) {
|
|
1168
|
+
const { labels } = destination;
|
|
1140
1169
|
if (!this.isOpen) return nothing;
|
|
1141
1170
|
|
|
1142
1171
|
const { video, expanded } = this;
|
|
@@ -1177,11 +1206,7 @@ export class PlayerController implements ReactiveController {
|
|
|
1177
1206
|
}}
|
|
1178
1207
|
>
|
|
1179
1208
|
${navigation ? this.renderNavigation(labels) : nothing}
|
|
1180
|
-
${this.renderVideo(story, navigation && expanded,
|
|
1181
|
-
product,
|
|
1182
|
-
cta,
|
|
1183
|
-
labels,
|
|
1184
|
-
})}
|
|
1209
|
+
${this.renderVideo(story, navigation && expanded, destination)}
|
|
1185
1210
|
<span class="player-dim" aria-hidden="true" ${ref(this.dim)}></span>
|
|
1186
1211
|
</div>
|
|
1187
1212
|
</div>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { html } from "lit";
|
|
2
2
|
import { icon } from "../icons";
|
|
3
|
-
import {
|
|
3
|
+
import { destinationUrl } from "../../utils/safe-url";
|
|
4
4
|
import type { StoryAd } from "../../types";
|
|
5
5
|
|
|
6
6
|
/** The call to action of a Story whose destination is a link. */
|
|
@@ -14,7 +14,7 @@ export interface CardCta {
|
|
|
14
14
|
/**
|
|
15
15
|
* The call to action of a Story without a product (its destination is a
|
|
16
16
|
* link), with the text set in the campaign. Undefined when the Story has a
|
|
17
|
-
* product, no text or no http(s) destination.
|
|
17
|
+
* product, no text or no http(s) or same-store path destination.
|
|
18
18
|
*/
|
|
19
19
|
export function toCardCta(
|
|
20
20
|
story: Pick<StoryAd, "product" | "destination_url" | "call_to_action">,
|
|
@@ -22,7 +22,7 @@ export function toCardCta(
|
|
|
22
22
|
if (story.product) return undefined;
|
|
23
23
|
|
|
24
24
|
const label = story.call_to_action?.trim();
|
|
25
|
-
const url =
|
|
25
|
+
const url = destinationUrl(story.destination_url);
|
|
26
26
|
|
|
27
27
|
return label && url ? { label, url } : undefined;
|
|
28
28
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export {
|
|
2
|
+
focusCartAction,
|
|
2
3
|
renderExpandableProduct,
|
|
3
4
|
renderProduct,
|
|
4
5
|
toCardProduct,
|
|
@@ -10,6 +11,7 @@ export type {
|
|
|
10
11
|
CardProduct,
|
|
11
12
|
ExpandableProductOptions,
|
|
12
13
|
FetchedProduct,
|
|
14
|
+
ProductCartActions,
|
|
13
15
|
} from "./product";
|
|
14
16
|
export {
|
|
15
17
|
DEFAULT_CURRENCY,
|
|
@@ -14,9 +14,9 @@ export const productStyles = css`
|
|
|
14
14
|
gap: 8px;
|
|
15
15
|
align-items: center;
|
|
16
16
|
box-sizing: border-box;
|
|
17
|
-
padding:
|
|
17
|
+
padding: var(--_pill-inset);
|
|
18
18
|
border: 0;
|
|
19
|
-
border-radius:
|
|
19
|
+
border-radius: var(--_pill-radius);
|
|
20
20
|
background: rgba(255, 255, 255, 0.15);
|
|
21
21
|
backdrop-filter: blur(5px);
|
|
22
22
|
color: #fff;
|
|
@@ -111,7 +111,7 @@ export const productStyles = css`
|
|
|
111
111
|
align-items: center;
|
|
112
112
|
box-sizing: border-box;
|
|
113
113
|
width: 100%;
|
|
114
|
-
padding:
|
|
114
|
+
padding: var(--_pill-inset);
|
|
115
115
|
border: 0;
|
|
116
116
|
border-radius: inherit;
|
|
117
117
|
background: transparent;
|
|
@@ -174,7 +174,7 @@ export const productStyles = css`
|
|
|
174
174
|
height: 48px;
|
|
175
175
|
padding: 0 16px;
|
|
176
176
|
border: 1px solid var(--vtex-ads-story-color-accent, #1b75a8);
|
|
177
|
-
border-radius:
|
|
177
|
+
border-radius: var(--_button-radius);
|
|
178
178
|
font: inherit;
|
|
179
179
|
font-size: 14px;
|
|
180
180
|
font-weight: 700;
|
|
@@ -201,6 +201,62 @@ export const productStyles = css`
|
|
|
201
201
|
);
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
/* In the cart: the add to cart button's pill, with minus and plus at its
|
|
205
|
+
ends and the quantity between them. */
|
|
206
|
+
.product-stepper {
|
|
207
|
+
justify-content: space-between;
|
|
208
|
+
padding: 0;
|
|
209
|
+
background: var(--vtex-ads-story-color-accent, #1b75a8);
|
|
210
|
+
color: var(--vtex-ads-story-color-accent-contrast, #fff);
|
|
211
|
+
cursor: auto;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.product-stepper-button {
|
|
215
|
+
display: grid;
|
|
216
|
+
flex: none;
|
|
217
|
+
place-items: center;
|
|
218
|
+
width: 46px;
|
|
219
|
+
height: 46px;
|
|
220
|
+
padding: 0;
|
|
221
|
+
border: 0;
|
|
222
|
+
border-radius: var(--_button-radius);
|
|
223
|
+
background: transparent;
|
|
224
|
+
color: inherit;
|
|
225
|
+
cursor: pointer;
|
|
226
|
+
transition: background-color 0.2s ease;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
.product-stepper-button .icon {
|
|
230
|
+
width: 18px;
|
|
231
|
+
height: 18px;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
.product-stepper-button:hover {
|
|
235
|
+
background: rgba(0, 0, 0, 0.15);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
.product-stepper-button:focus-visible {
|
|
239
|
+
outline: 2px solid var(--vtex-ads-story-color-accent-contrast, #fff);
|
|
240
|
+
outline-offset: -4px;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.product-quantity {
|
|
244
|
+
flex: 1;
|
|
245
|
+
min-width: 0;
|
|
246
|
+
font-size: 16px;
|
|
247
|
+
text-align: center;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.product-cart-status {
|
|
251
|
+
position: absolute;
|
|
252
|
+
width: 1px;
|
|
253
|
+
height: 1px;
|
|
254
|
+
margin: -1px;
|
|
255
|
+
overflow: hidden;
|
|
256
|
+
clip-path: inset(50%);
|
|
257
|
+
white-space: nowrap;
|
|
258
|
+
}
|
|
259
|
+
|
|
204
260
|
/* Sold out: add to cart, disabled and grayed. */
|
|
205
261
|
.product-unavailable {
|
|
206
262
|
border-color: transparent;
|
|
@@ -290,11 +346,12 @@ export const productStyles = css`
|
|
|
290
346
|
}
|
|
291
347
|
}
|
|
292
348
|
|
|
349
|
+
/* Its corners follow the pill's, one inset in from them: 8px by default. */
|
|
293
350
|
.product-image {
|
|
294
351
|
flex: none;
|
|
295
352
|
width: 60px;
|
|
296
353
|
height: 60px;
|
|
297
|
-
border-radius:
|
|
354
|
+
border-radius: max(0px, var(--_pill-radius) - var(--_pill-inset));
|
|
298
355
|
background: #fff;
|
|
299
356
|
object-fit: contain;
|
|
300
357
|
}
|
|
@@ -358,7 +415,7 @@ export const productStyles = css`
|
|
|
358
415
|
.product-add-to-cart-loading {
|
|
359
416
|
display: block;
|
|
360
417
|
height: 48px;
|
|
361
|
-
border-radius:
|
|
418
|
+
border-radius: var(--_button-radius);
|
|
362
419
|
}
|
|
363
420
|
|
|
364
421
|
@media (prefers-reduced-motion: reduce) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { html, nothing } from "lit";
|
|
2
2
|
import type { StoryLabels } from "../../locales";
|
|
3
3
|
import { icon } from "../icons";
|
|
4
|
-
import { httpUrl } from "../../utils/
|
|
4
|
+
import { destinationUrl, httpUrl } from "../../utils/safe-url";
|
|
5
5
|
import type { StoryAd } from "../../types";
|
|
6
6
|
import type { StoryProductDetails } from "../../utils/hydrate-products";
|
|
7
|
-
import { readProductPrice } from "./price";
|
|
7
|
+
import { readProductPrice, type ProductPrice } from "./price";
|
|
8
8
|
|
|
9
9
|
/** What the card shows of the Story's product, with the prices formatted. */
|
|
10
10
|
export interface CardProduct {
|
|
@@ -15,6 +15,10 @@ export interface CardProduct {
|
|
|
15
15
|
listPrice?: string;
|
|
16
16
|
/** The product page, opened by view product link button. */
|
|
17
17
|
url?: string;
|
|
18
|
+
/** The SKU added to the cart. Without one, there is no add to cart. */
|
|
19
|
+
sku?: string;
|
|
20
|
+
/** The product's seller, sent with the SKU when known. */
|
|
21
|
+
seller?: string;
|
|
18
22
|
/** Accessible name of the product button: name and prices. */
|
|
19
23
|
label: string;
|
|
20
24
|
/**
|
|
@@ -23,8 +27,9 @@ export interface CardProduct {
|
|
|
23
27
|
*/
|
|
24
28
|
isLoading?: boolean;
|
|
25
29
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
30
|
+
* Set when the fetched product can't be bought: add to cart becomes a
|
|
31
|
+
* disabled button, and without a price this text ("Esgotado") shows in its
|
|
32
|
+
* place.
|
|
28
33
|
*/
|
|
29
34
|
soldOut?: string;
|
|
30
35
|
}
|
|
@@ -42,7 +47,9 @@ export type FetchedProduct = StoryProductDetails | "loading" | undefined;
|
|
|
42
47
|
*
|
|
43
48
|
* The price is the fetched one when there is one; while the product loads,
|
|
44
49
|
* none; and otherwise the ad server's, from `product_metadata`. A fetched
|
|
45
|
-
* product that can't be bought shows `labels.soldOut`
|
|
50
|
+
* product that can't be bought still shows its price, or `labels.soldOut`
|
|
51
|
+
* when it has none. Its price of 0 is no price: Intelligent Search gives one
|
|
52
|
+
* to a SKU out of stock.
|
|
46
53
|
*/
|
|
47
54
|
export function toCardProduct(
|
|
48
55
|
story: Pick<StoryAd, "product" | "destination_url">,
|
|
@@ -57,21 +64,19 @@ export function toCardProduct(
|
|
|
57
64
|
const base = {
|
|
58
65
|
name,
|
|
59
66
|
imageUrl: httpUrl(product.image_url) ?? undefined,
|
|
60
|
-
url:
|
|
67
|
+
url: destinationUrl(story.destination_url) ?? undefined,
|
|
68
|
+
sku: product.product_sku?.trim() || undefined,
|
|
69
|
+
seller: product.seller_id?.trim() || undefined,
|
|
61
70
|
};
|
|
62
71
|
|
|
63
72
|
if (fetched === "loading") {
|
|
64
73
|
return { ...base, label: labels.product(name), isLoading: true };
|
|
65
74
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const amounts = fetched?.price ?? readProductPrice(product.product_metadata);
|
|
75
|
+
const soldOut = fetched?.available === false ? labels.soldOut : undefined;
|
|
76
|
+
const shown = (amounts: ProductPrice | undefined) =>
|
|
77
|
+
soldOut && amounts?.price === 0 ? undefined : amounts;
|
|
78
|
+
const amounts =
|
|
79
|
+
shown(fetched?.price) ?? shown(readProductPrice(product.product_metadata));
|
|
75
80
|
const price = amounts && format(amounts.price);
|
|
76
81
|
const listPrice =
|
|
77
82
|
amounts?.listPrice === undefined ? undefined : format(amounts.listPrice);
|
|
@@ -80,7 +85,8 @@ export function toCardProduct(
|
|
|
80
85
|
...base,
|
|
81
86
|
price,
|
|
82
87
|
listPrice,
|
|
83
|
-
|
|
88
|
+
soldOut,
|
|
89
|
+
label: labels.product(name, price, listPrice, soldOut),
|
|
84
90
|
};
|
|
85
91
|
}
|
|
86
92
|
|
|
@@ -101,14 +107,15 @@ const addToCartSkeleton = html`
|
|
|
101
107
|
`;
|
|
102
108
|
|
|
103
109
|
// The price and, on sale, the "from" price struck through beside it. While
|
|
104
|
-
// the product loads, its placeholder; sold out, the sold out
|
|
105
|
-
// price, nothing.
|
|
110
|
+
// the product loads, its placeholder; sold out without a price, the sold out
|
|
111
|
+
// text; otherwise without a price, nothing.
|
|
106
112
|
function renderPrice({ isLoading, soldOut, price, listPrice }: CardProduct) {
|
|
107
113
|
if (isLoading) return priceSkeleton;
|
|
108
|
-
if (
|
|
109
|
-
return
|
|
114
|
+
if (!price) {
|
|
115
|
+
return soldOut
|
|
116
|
+
? html`<span class="product-price product-sold-out">${soldOut}</span>`
|
|
117
|
+
: nothing;
|
|
110
118
|
}
|
|
111
|
-
if (!price) return nothing;
|
|
112
119
|
|
|
113
120
|
return html`
|
|
114
121
|
<span class="product-price">
|
|
@@ -152,7 +159,20 @@ export function renderProduct(product: CardProduct, onSelect: () => void) {
|
|
|
152
159
|
`;
|
|
153
160
|
}
|
|
154
161
|
|
|
155
|
-
|
|
162
|
+
/** What the panel does with the cart. Without `onAddToCart`, no add to cart. */
|
|
163
|
+
export interface ProductCartActions {
|
|
164
|
+
/**
|
|
165
|
+
* How many of the product were added from the element. Above 0, a stepper
|
|
166
|
+
* shows in place of add to cart.
|
|
167
|
+
*/
|
|
168
|
+
cartQuantity?: number;
|
|
169
|
+
/** Called by add to cart and the stepper's plus. */
|
|
170
|
+
onAddToCart?: () => void;
|
|
171
|
+
/** Called by the stepper's minus, a trash with one left. */
|
|
172
|
+
onRemoveFromCart?: () => void;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface ExpandableProductOptions extends ProductCartActions {
|
|
156
176
|
/** Whether the panel with the actions shows. */
|
|
157
177
|
open: boolean;
|
|
158
178
|
/** Called by the pill's button, to open or close the panel. */
|
|
@@ -163,12 +183,71 @@ export interface ExpandableProductOptions {
|
|
|
163
183
|
labels: StoryLabels;
|
|
164
184
|
}
|
|
165
185
|
|
|
166
|
-
|
|
167
|
-
|
|
186
|
+
const STEPPER_ADD_CLASS = "product-stepper-add";
|
|
187
|
+
const ADD_TO_CART_CLASS = "product-add-to-cart";
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Moves the focus to the control that takes the place of the one clicked
|
|
191
|
+
* when add to cart and the stepper swap: the stepper's plus (`"stepper"`) or
|
|
192
|
+
* add to cart (`"add"`). Does nothing if the panel isn't in `root`.
|
|
193
|
+
*/
|
|
194
|
+
export function focusCartAction(root: ParentNode, action: "add" | "stepper") {
|
|
195
|
+
const className =
|
|
196
|
+
action === "stepper" ? STEPPER_ADD_CLASS : ADD_TO_CART_CLASS;
|
|
197
|
+
root.querySelector<HTMLElement>(`.${className}`)?.focus();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// In the cart: minus (a trash with one left), the quantity and plus, in the
|
|
201
|
+
// add to cart button's place.
|
|
202
|
+
function renderStepper(
|
|
203
|
+
quantity: number,
|
|
204
|
+
{
|
|
205
|
+
labels,
|
|
206
|
+
onAddToCart,
|
|
207
|
+
onRemoveFromCart,
|
|
208
|
+
}: Pick<
|
|
209
|
+
ExpandableProductOptions,
|
|
210
|
+
"labels" | "onAddToCart" | "onRemoveFromCart"
|
|
211
|
+
>,
|
|
212
|
+
) {
|
|
213
|
+
const last = quantity === 1;
|
|
214
|
+
|
|
215
|
+
return html`
|
|
216
|
+
<div
|
|
217
|
+
class="product-action product-stepper"
|
|
218
|
+
role="group"
|
|
219
|
+
aria-label=${labels.cartQuantity}
|
|
220
|
+
>
|
|
221
|
+
<button
|
|
222
|
+
type="button"
|
|
223
|
+
class="product-stepper-button product-stepper-remove"
|
|
224
|
+
aria-label=${last ? labels.removeFromCart : labels.decreaseQuantity}
|
|
225
|
+
@click=${onRemoveFromCart}
|
|
226
|
+
>
|
|
227
|
+
${icon(last ? "trash" : "minus")}
|
|
228
|
+
</button>
|
|
229
|
+
<span class="product-quantity">${quantity}</span>
|
|
230
|
+
<button
|
|
231
|
+
type="button"
|
|
232
|
+
class="product-stepper-button ${STEPPER_ADD_CLASS}"
|
|
233
|
+
aria-label=${labels.increaseQuantity}
|
|
234
|
+
@click=${onAddToCart}
|
|
235
|
+
>
|
|
236
|
+
${icon("plus")}
|
|
237
|
+
</button>
|
|
238
|
+
</div>
|
|
239
|
+
`;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Add to cart: its placeholder while the product loads, a disabled button
|
|
243
|
+
// when sold out, and the stepper once in the cart. Nothing without a SKU or
|
|
244
|
+
// an `onAddToCart` to call.
|
|
168
245
|
function renderAddToCart(
|
|
169
|
-
{ isLoading, soldOut }: CardProduct,
|
|
170
|
-
|
|
246
|
+
{ isLoading, soldOut, sku }: CardProduct,
|
|
247
|
+
options: ExpandableProductOptions,
|
|
171
248
|
) {
|
|
249
|
+
const { labels, onAddToCart, cartQuantity = 0 } = options;
|
|
250
|
+
if (!sku || !onAddToCart) return nothing;
|
|
172
251
|
if (isLoading) return addToCartSkeleton;
|
|
173
252
|
if (soldOut) {
|
|
174
253
|
return html`
|
|
@@ -177,13 +256,33 @@ function renderAddToCart(
|
|
|
177
256
|
</button>
|
|
178
257
|
`;
|
|
179
258
|
}
|
|
259
|
+
if (cartQuantity > 0) return renderStepper(cartQuantity, options);
|
|
180
260
|
return html`
|
|
181
|
-
<button
|
|
261
|
+
<button
|
|
262
|
+
type="button"
|
|
263
|
+
class="product-action ${ADD_TO_CART_CLASS}"
|
|
264
|
+
@click=${onAddToCart}
|
|
265
|
+
>
|
|
182
266
|
${labels.addToCart}
|
|
183
267
|
</button>
|
|
184
268
|
`;
|
|
185
269
|
}
|
|
186
270
|
|
|
271
|
+
// Announces the quantity in the cart. It stays in the panel while the stepper
|
|
272
|
+
// comes and goes, so the first value is announced too.
|
|
273
|
+
function renderCartStatus(
|
|
274
|
+
{ sku }: CardProduct,
|
|
275
|
+
{ labels, cartQuantity = 0, onAddToCart }: ExpandableProductOptions,
|
|
276
|
+
) {
|
|
277
|
+
if (!sku || !onAddToCart) return nothing;
|
|
278
|
+
|
|
279
|
+
return html`
|
|
280
|
+
<span class="product-cart-status" role="status">
|
|
281
|
+
${cartQuantity > 0 ? `${labels.cartQuantity}: ${cartQuantity}` : ""}
|
|
282
|
+
</span>
|
|
283
|
+
`;
|
|
284
|
+
}
|
|
285
|
+
|
|
187
286
|
/**
|
|
188
287
|
* The product pill in the player. The pill is a button that opens a panel
|
|
189
288
|
* below it, as in the prototype, with "Adicionar ao carrinho" (a placeholder
|
|
@@ -191,8 +290,9 @@ function renderAddToCart(
|
|
|
191
290
|
* and "Ver produto" (the product page, in a new tab; only with a URL). Open,
|
|
192
291
|
* the pill turns white.
|
|
193
292
|
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
293
|
+
* Add to cart calls `onAddToCart` and leaves the panel open. With
|
|
294
|
+
* `cartQuantity` above 0, a stepper takes its place: minus (a trash with one
|
|
295
|
+
* left) calls `onRemoveFromCart`, plus `onAddToCart`.
|
|
196
296
|
*/
|
|
197
297
|
export function renderExpandableProduct(
|
|
198
298
|
product: CardProduct,
|
|
@@ -215,6 +315,7 @@ export function renderExpandableProduct(
|
|
|
215
315
|
</button>
|
|
216
316
|
<div id=${options.panelId} class="product-panel" ?inert=${!open}>
|
|
217
317
|
<div class="product-actions">
|
|
318
|
+
${renderCartStatus(product, options)}
|
|
218
319
|
${renderAddToCart(product, options)}
|
|
219
320
|
${product.url
|
|
220
321
|
? html`
|
package/src/index.ts
CHANGED
|
@@ -7,3 +7,11 @@ export type {
|
|
|
7
7
|
StoryHydration,
|
|
8
8
|
StoryProductDetails,
|
|
9
9
|
} from "./utils/hydrate-products";
|
|
10
|
+
export {
|
|
11
|
+
ADD_TO_CART_EVENT,
|
|
12
|
+
REMOVE_FROM_CART_EVENT,
|
|
13
|
+
type StoryAddToCartDetail,
|
|
14
|
+
type StoryAddToCartEvent,
|
|
15
|
+
type StoryRemoveFromCartDetail,
|
|
16
|
+
type StoryRemoveFromCartEvent,
|
|
17
|
+
} from "./commerce-events";
|
package/src/locales/en.ts
CHANGED
|
@@ -17,12 +17,22 @@ export const en: StoryLabels = {
|
|
|
17
17
|
soundOn: "Turn sound on",
|
|
18
18
|
soundOff: "Turn sound off",
|
|
19
19
|
addToCart: "Add to cart",
|
|
20
|
+
cartQuantity: "Quantity in cart",
|
|
21
|
+
decreaseQuantity: "Decrease quantity",
|
|
22
|
+
removeFromCart: "Remove from cart",
|
|
23
|
+
increaseQuantity: "Increase quantity",
|
|
20
24
|
unavailable: "Product unavailable",
|
|
21
25
|
soldOut: "Sold out",
|
|
22
26
|
viewProduct: "View product",
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
videoUnavailable: "Video unavailable",
|
|
28
|
+
videoFailed: "Couldn't load the video",
|
|
29
|
+
retry: "Try again",
|
|
30
|
+
videoLoading: "Loading video",
|
|
31
|
+
product: (name, price, listPrice, soldOut) => {
|
|
32
|
+
let text = name;
|
|
33
|
+
if (price && listPrice) text = `${name}, was ${listPrice}, now ${price}`;
|
|
34
|
+
else if (price) text = `${name}, ${price}`;
|
|
35
|
+
return soldOut ? `${text}, ${soldOut}` : text;
|
|
26
36
|
},
|
|
27
37
|
position: (label, index, count) =>
|
|
28
38
|
count > 1 ? `${label} ${index + 1} of ${count}` : label,
|
package/src/locales/es.ts
CHANGED
|
@@ -17,13 +17,23 @@ export const es: StoryLabels = {
|
|
|
17
17
|
soundOn: "Activar sonido",
|
|
18
18
|
soundOff: "Desactivar sonido",
|
|
19
19
|
addToCart: "Agregar al carrito",
|
|
20
|
+
cartQuantity: "Cantidad en el carrito",
|
|
21
|
+
decreaseQuantity: "Disminuir cantidad",
|
|
22
|
+
removeFromCart: "Quitar del carrito",
|
|
23
|
+
increaseQuantity: "Aumentar cantidad",
|
|
20
24
|
unavailable: "Producto no disponible",
|
|
21
25
|
soldOut: "Agotado",
|
|
22
26
|
viewProduct: "Ver producto",
|
|
23
|
-
|
|
27
|
+
videoUnavailable: "Video no disponible",
|
|
28
|
+
videoFailed: "No se pudo cargar el video",
|
|
29
|
+
retry: "Intentar de nuevo",
|
|
30
|
+
videoLoading: "Cargando video",
|
|
31
|
+
product: (name, price, listPrice, soldOut) => {
|
|
32
|
+
let text = name;
|
|
24
33
|
if (price && listPrice)
|
|
25
|
-
|
|
26
|
-
|
|
34
|
+
text = `${name}, antes ${listPrice}, ahora ${price}`;
|
|
35
|
+
else if (price) text = `${name}, ${price}`;
|
|
36
|
+
return soldOut ? `${text}, ${soldOut}` : text;
|
|
27
37
|
},
|
|
28
38
|
position: (label, index, count) =>
|
|
29
39
|
count > 1 ? `${label} ${index + 1} de ${count}` : label,
|
package/src/locales/index.ts
CHANGED
|
@@ -11,6 +11,8 @@ export { pt } from "./pt";
|
|
|
11
11
|
export interface StoryLabelOverrides {
|
|
12
12
|
addToCart?: string;
|
|
13
13
|
viewProduct?: string;
|
|
14
|
+
unavailable?: string;
|
|
15
|
+
soldOut?: string;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
const BY_LANGUAGE = new Map<string, StoryLabels>([
|
|
@@ -27,10 +29,14 @@ export function labelsFor(
|
|
|
27
29
|
const labels = BY_LANGUAGE.get(language) ?? pt;
|
|
28
30
|
const addToCart = overrides.addToCart?.trim();
|
|
29
31
|
const viewProduct = overrides.viewProduct?.trim();
|
|
32
|
+
const unavailable = overrides.unavailable?.trim();
|
|
33
|
+
const soldOut = overrides.soldOut?.trim();
|
|
30
34
|
|
|
31
35
|
return {
|
|
32
36
|
...labels,
|
|
33
37
|
...(addToCart && { addToCart }),
|
|
34
38
|
...(viewProduct && { viewProduct }),
|
|
39
|
+
...(unavailable && { unavailable }),
|
|
40
|
+
...(soldOut && { soldOut }),
|
|
35
41
|
};
|
|
36
42
|
}
|