@vtex/ads-stories 0.4.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/README.md +44 -88
- package/dist/cdn/vtex-ads-stories.js +1313 -74
- package/dist/cdn/vtex-ads-stories.js.map +1 -1
- package/dist/cjs/index.cjs +3744 -513
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +46 -15
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/cjs/react.d.cts +51 -5
- package/dist/cjs/story-DAiAN9v-.d.cts +260 -0
- package/dist/esm/index.d.ts +46 -15
- package/dist/esm/index.js +3348 -217
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +51 -5
- package/dist/esm/story-DAiAN9v-.d.ts +260 -0
- package/package.json +18 -4
- package/src/cdn.ts +7 -3
- package/src/commerce-events.ts +93 -0
- package/src/components/card/card.ts +79 -14
- package/src/components/carousel/carousel-skeleton.ts +113 -0
- package/src/components/carousel/carousel.ts +715 -0
- package/src/components/carousel/index.ts +22 -0
- package/src/components/events/events-controller.ts +222 -0
- package/src/components/events/index.ts +6 -0
- package/src/components/icons/assets/caret-left-bold.svg +1 -0
- package/src/components/icons/assets/caret-right-bold.svg +1 -0
- package/src/components/icons/assets/caret-up-bold.svg +1 -0
- 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 +14 -0
- package/src/components/media/index.ts +8 -1
- package/src/components/media/media.styles.ts +113 -0
- package/src/components/media/media.ts +124 -23
- package/src/components/media/video-controller.ts +127 -6
- package/src/components/player/index.ts +6 -2
- package/src/components/player/player.ts +824 -50
- package/src/components/product/cta.ts +56 -0
- package/src/components/product/index.ts +22 -0
- package/src/components/product/price.ts +86 -0
- package/src/components/product/product.styles.ts +426 -0
- package/src/components/product/product.ts +336 -0
- package/src/index.ts +17 -1
- package/src/locales/en.ts +39 -0
- package/src/locales/es.ts +40 -0
- package/src/locales/index.ts +36 -0
- package/src/locales/pt.ts +39 -0
- package/src/locales/types.ts +44 -0
- package/src/react.ts +55 -7
- package/src/story/index.ts +5 -1
- package/src/story/story.ts +679 -49
- package/src/tag-name.ts +1 -0
- package/src/types.ts +8 -12
- package/src/utils/fetch-story.ts +113 -0
- package/src/utils/hydrate-products.ts +112 -0
- package/src/utils/media-ready.ts +58 -0
- package/src/utils/safe-url.ts +36 -0
- package/src/utils/send-event.ts +71 -0
- package/src/utils/with-timeout.ts +43 -0
package/dist/esm/react.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DetailedHTMLProps, HTMLAttributes } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Channel } from '@vtex/ads-core';
|
|
3
|
+
import { T as TAG_NAME, V as VtexAdsStoryCarouselElement } from './story-DAiAN9v-.js';
|
|
3
4
|
import 'lit';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* React JSX types for <vtex-ads-story>. Types only: the JS output
|
|
7
|
+
* React JSX types for <vtex-ads-story-carousel>. Types only: the JS output
|
|
8
|
+
* is empty.
|
|
7
9
|
*
|
|
8
10
|
* Opt in once per project, so non-React projects never need @types/react:
|
|
9
11
|
*
|
|
@@ -12,13 +14,57 @@ import 'lit';
|
|
|
12
14
|
* or add "@vtex/ads-stories/react" to `compilerOptions.types` in tsconfig.
|
|
13
15
|
*/
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The element's own attributes. The required ones match the README: without
|
|
19
|
+
* them, the element requests nothing and renders nothing.
|
|
20
|
+
*/
|
|
21
|
+
interface VtexAdsStoryCarouselAttributes {
|
|
22
|
+
/** The publisher the ads are requested for. */
|
|
23
|
+
"publisher-id": string;
|
|
24
|
+
/** The store's session, also used to deduplicate events. */
|
|
25
|
+
"session-id": string;
|
|
26
|
+
/** The Story placement configured for the publisher. */
|
|
27
|
+
placement: string;
|
|
28
|
+
"user-id"?: string;
|
|
29
|
+
"account-name"?: string;
|
|
30
|
+
/** `site` or `app`. */
|
|
31
|
+
channel?: Channel;
|
|
32
|
+
/** Time limit for the ad request, in milliseconds (default 2000). */
|
|
33
|
+
timeout?: number;
|
|
34
|
+
/** How many Stories to ask for: the carousel positions (default 8). */
|
|
35
|
+
quantity?: number;
|
|
36
|
+
/** Currency of the prices, ISO 4217 ("BRL"). Default BRL. */
|
|
37
|
+
currency?: string;
|
|
38
|
+
/** The add to cart button's text. Default in the element's language. */
|
|
39
|
+
"label-add-to-cart"?: string;
|
|
40
|
+
/** The view product button's text. Default in the element's language. */
|
|
41
|
+
"label-view-product"?: string;
|
|
42
|
+
/**
|
|
43
|
+
* "none": no product request. To set a strategy, assign the element's
|
|
44
|
+
* `hydration` property. Missing: Intelligent Search with `account-name`.
|
|
45
|
+
*/
|
|
46
|
+
hydration?: "none";
|
|
47
|
+
/** Time limit for the product request, in milliseconds (default 5000). */
|
|
48
|
+
"hydration-timeout"?: number;
|
|
49
|
+
/**
|
|
50
|
+
* With more than one Story, the cards without the highlight also play
|
|
51
|
+
* their video, muted. Off by default.
|
|
52
|
+
*/
|
|
53
|
+
"inactive-autoplay"?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* With more than one Story, how long each one stays highlighted, in
|
|
56
|
+
* milliseconds (default 5000). 0: no automatic advance. Never advances on
|
|
57
|
+
* its own for people who prefer reduced motion.
|
|
58
|
+
*/
|
|
59
|
+
"slide-duration"?: number;
|
|
60
|
+
}
|
|
61
|
+
type VtexAdsStoryCarouselProps = DetailedHTMLProps<HTMLAttributes<VtexAdsStoryCarouselElement>, VtexAdsStoryCarouselElement> & VtexAdsStoryCarouselAttributes;
|
|
16
62
|
declare module "react" {
|
|
17
63
|
namespace JSX {
|
|
18
64
|
interface IntrinsicElements {
|
|
19
|
-
|
|
65
|
+
[TAG_NAME]: VtexAdsStoryCarouselProps;
|
|
20
66
|
}
|
|
21
67
|
}
|
|
22
68
|
}
|
|
23
69
|
|
|
24
|
-
export type {
|
|
70
|
+
export type { VtexAdsStoryCarouselAttributes, VtexAdsStoryCarouselProps };
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import * as lit from 'lit';
|
|
2
|
+
import { LitElement, PropertyValues, nothing } from 'lit';
|
|
3
|
+
import { ProductFetcher, Offer, ProductPrice, Channel } from '@vtex/ads-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Where the element gets its products' details: a fetcher and a matcher, as
|
|
7
|
+
* in `@vtex/ads-core`, and a reader for the price of a fetched product.
|
|
8
|
+
*/
|
|
9
|
+
interface StoryHydration<TProduct extends object = object> {
|
|
10
|
+
fetcher: ProductFetcher<TProduct>;
|
|
11
|
+
matcher(product: TProduct, offer: Offer): boolean;
|
|
12
|
+
price(product: TProduct, offer: Offer): ProductPrice | undefined;
|
|
13
|
+
/** Whether the product can be bought. Without it, it isn't checked. */
|
|
14
|
+
available?(product: TProduct, offer: Offer): boolean | undefined;
|
|
15
|
+
}
|
|
16
|
+
interface StoryProductDetails {
|
|
17
|
+
price?: ProductPrice;
|
|
18
|
+
/** Missing when the strategy doesn't say. */
|
|
19
|
+
available?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare const TAG_NAME = "vtex-ads-story-carousel";
|
|
23
|
+
|
|
24
|
+
declare class VtexAdsStoryCarouselElement extends LitElement {
|
|
25
|
+
static readonly properties: {
|
|
26
|
+
publisherId: {
|
|
27
|
+
attribute: string;
|
|
28
|
+
};
|
|
29
|
+
sessionId: {
|
|
30
|
+
attribute: string;
|
|
31
|
+
};
|
|
32
|
+
placement: {};
|
|
33
|
+
userId: {
|
|
34
|
+
attribute: string;
|
|
35
|
+
};
|
|
36
|
+
accountName: {
|
|
37
|
+
attribute: string;
|
|
38
|
+
};
|
|
39
|
+
channel: {
|
|
40
|
+
converter: {
|
|
41
|
+
fromAttribute: (value: string | null) => Channel | undefined;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
timeout: {
|
|
45
|
+
type: NumberConstructor;
|
|
46
|
+
};
|
|
47
|
+
quantity: {
|
|
48
|
+
type: NumberConstructor;
|
|
49
|
+
};
|
|
50
|
+
currency: {};
|
|
51
|
+
labelAddToCart: {
|
|
52
|
+
attribute: string;
|
|
53
|
+
};
|
|
54
|
+
labelViewProduct: {
|
|
55
|
+
attribute: string;
|
|
56
|
+
};
|
|
57
|
+
hydration: {
|
|
58
|
+
converter: {
|
|
59
|
+
fromAttribute: (value: string | null) => "none" | undefined;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
hydrationTimeout: {
|
|
63
|
+
type: NumberConstructor;
|
|
64
|
+
attribute: string;
|
|
65
|
+
};
|
|
66
|
+
slideDuration: {
|
|
67
|
+
type: NumberConstructor;
|
|
68
|
+
attribute: string;
|
|
69
|
+
};
|
|
70
|
+
inactiveAutoplay: {
|
|
71
|
+
type: BooleanConstructor;
|
|
72
|
+
attribute: string;
|
|
73
|
+
converter: {
|
|
74
|
+
fromAttribute: (value: string | null) => boolean;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
stories: {
|
|
78
|
+
state: boolean;
|
|
79
|
+
};
|
|
80
|
+
products: {
|
|
81
|
+
state: boolean;
|
|
82
|
+
};
|
|
83
|
+
requesting: {
|
|
84
|
+
state: boolean;
|
|
85
|
+
};
|
|
86
|
+
mediaLoading: {
|
|
87
|
+
state: boolean;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
/** Required. The publisher the ads are requested for. */
|
|
91
|
+
publisherId?: string;
|
|
92
|
+
/** Required. The store's session, also used to deduplicate events. */
|
|
93
|
+
sessionId?: string;
|
|
94
|
+
/** Required. The Story placement configured for the publisher. */
|
|
95
|
+
placement?: string;
|
|
96
|
+
userId?: string;
|
|
97
|
+
accountName?: string;
|
|
98
|
+
/** `site` or `app`. Any other attribute value is ignored. */
|
|
99
|
+
channel?: Channel;
|
|
100
|
+
/**
|
|
101
|
+
* Time limit for the ad request, in milliseconds (default 2000). Without a
|
|
102
|
+
* Story by then, the element renders nothing. A change applies to the next
|
|
103
|
+
* request; it doesn't request again.
|
|
104
|
+
*/
|
|
105
|
+
timeout?: number;
|
|
106
|
+
/**
|
|
107
|
+
* How many Stories to ask for: the carousel positions (default 8). Missing,
|
|
108
|
+
* below 1 or not a number: the default.
|
|
109
|
+
*/
|
|
110
|
+
quantity?: number;
|
|
111
|
+
/**
|
|
112
|
+
* Currency of the prices, as an ISO 4217 code ("BRL", "ARS"). Missing or
|
|
113
|
+
* invalid: BRL.
|
|
114
|
+
*/
|
|
115
|
+
currency?: string;
|
|
116
|
+
/**
|
|
117
|
+
* The add to cart button's text. Missing or blank: the default in the
|
|
118
|
+
* element's language ("Adicionar ao carrinho").
|
|
119
|
+
*/
|
|
120
|
+
labelAddToCart?: string;
|
|
121
|
+
/**
|
|
122
|
+
* The view product button's text. Missing or blank: the default in the
|
|
123
|
+
* element's language ("Ver produto").
|
|
124
|
+
*/
|
|
125
|
+
labelViewProduct?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Where the products' details (for now, their prices) come from. A
|
|
128
|
+
* strategy (fetcher, matcher and price reader) is set as a property; the
|
|
129
|
+
* attribute takes only "none", for no product request. Missing:
|
|
130
|
+
* Intelligent Search when `account-name` is set, no product request
|
|
131
|
+
* otherwise.
|
|
132
|
+
*/
|
|
133
|
+
hydration?: StoryHydration | "none";
|
|
134
|
+
/**
|
|
135
|
+
* Time limit for the product request, in milliseconds (default 5000).
|
|
136
|
+
* Without the prices by then, the cards show the ad server's.
|
|
137
|
+
*/
|
|
138
|
+
hydrationTimeout?: number;
|
|
139
|
+
/**
|
|
140
|
+
* With more than one Story, whether the cards without the highlight also
|
|
141
|
+
* play their video, muted. Off by default: they show only their cover, and
|
|
142
|
+
* only the highlighted card plays.
|
|
143
|
+
*/
|
|
144
|
+
inactiveAutoplay: boolean;
|
|
145
|
+
/**
|
|
146
|
+
* With more than one Story, how long each one stays highlighted before the
|
|
147
|
+
* next, in milliseconds (default 5000). Zero: the highlight moves only by
|
|
148
|
+
* hand. A change starts the time over; it doesn't request again.
|
|
149
|
+
*/
|
|
150
|
+
slideDuration?: number;
|
|
151
|
+
/** The Stories shown: none while loading, on error or with none. */
|
|
152
|
+
private stories;
|
|
153
|
+
/**
|
|
154
|
+
* The fetched products, by Story `ad_id`, or `"loading"` while the product
|
|
155
|
+
* request runs. Undefined without a product request.
|
|
156
|
+
*/
|
|
157
|
+
private products?;
|
|
158
|
+
/**
|
|
159
|
+
* The ad request is pending (and has what it needs to go out): a skeleton
|
|
160
|
+
* stands in for the carousel until the Stories arrive.
|
|
161
|
+
*/
|
|
162
|
+
private requesting;
|
|
163
|
+
/**
|
|
164
|
+
* The first Story's media is still loading: a skeleton covers the cards
|
|
165
|
+
* until its cover or its video is ready (or MEDIA_TIMEOUT passes), so the
|
|
166
|
+
* first card doesn't show up piece by piece. Meanwhile, the cards load
|
|
167
|
+
* underneath, their videos without playing, and no event goes out.
|
|
168
|
+
*/
|
|
169
|
+
private mediaLoading;
|
|
170
|
+
/**
|
|
171
|
+
* Cancels the wait for the media in progress: when other Stories replace
|
|
172
|
+
* its Stories, or the element leaves the page.
|
|
173
|
+
*/
|
|
174
|
+
private mediaWait?;
|
|
175
|
+
/**
|
|
176
|
+
* Cancels the request in progress. Kept, already aborted, when leaving the
|
|
177
|
+
* page cancelled it, so connecting again requests once more.
|
|
178
|
+
*/
|
|
179
|
+
private request?;
|
|
180
|
+
static readonly styles: lit.CSSResult[];
|
|
181
|
+
/**
|
|
182
|
+
* How many of each SKU, from each seller (see cartKey), were added since
|
|
183
|
+
* the player opened, for its stepper. It doesn't read the store's cart: it
|
|
184
|
+
* counts the add and remove events sent, and starts over each time the
|
|
185
|
+
* player opens, as the cart may have changed meanwhile.
|
|
186
|
+
*/
|
|
187
|
+
private readonly cartQuantities;
|
|
188
|
+
/**
|
|
189
|
+
* One per rendered card, by its key (see Slide): a Story can show in more
|
|
190
|
+
* than one card, each with its own video.
|
|
191
|
+
*/
|
|
192
|
+
private readonly cardVideos;
|
|
193
|
+
/** The sound was on when the highlight moved: the new card turns it on. */
|
|
194
|
+
private soundPending;
|
|
195
|
+
/**
|
|
196
|
+
* What to do once the next render is on screen, when the new highlighted
|
|
197
|
+
* card's video exists (see leaveHighlight and onLayoutChange). A step
|
|
198
|
+
* taken before then replaces it.
|
|
199
|
+
*/
|
|
200
|
+
private afterRender?;
|
|
201
|
+
private readonly carousel;
|
|
202
|
+
private readonly player;
|
|
203
|
+
private readonly events;
|
|
204
|
+
constructor();
|
|
205
|
+
connectedCallback(): void;
|
|
206
|
+
disconnectedCallback(): void;
|
|
207
|
+
protected willUpdate(changed: PropertyValues<this>): void;
|
|
208
|
+
protected updated(): void;
|
|
209
|
+
private get activeHydration();
|
|
210
|
+
/**
|
|
211
|
+
* Requests the Story and shows it, then hydrates its products. Until the
|
|
212
|
+
* Story arrives, and when there is none (see fetchStories), the element
|
|
213
|
+
* renders nothing.
|
|
214
|
+
*/
|
|
215
|
+
private loadStory;
|
|
216
|
+
private showStories;
|
|
217
|
+
/**
|
|
218
|
+
* Shows the cards once the first Story's cover or video is ready: whichever
|
|
219
|
+
* loads first, or once both failed. Only then are the Stories impressed
|
|
220
|
+
* and do their videos play, from the start.
|
|
221
|
+
*/
|
|
222
|
+
private waitForFirstMedia;
|
|
223
|
+
/**
|
|
224
|
+
* Where each Story is being watched: the cards on screen (not the hidden
|
|
225
|
+
* ones kept for the slide in) and the open player's video, while their
|
|
226
|
+
* video plays. A card showing only its cover doesn't count. Behind the
|
|
227
|
+
* expanded player, a modal over the page, the cards are covered.
|
|
228
|
+
*/
|
|
229
|
+
private viewTargets;
|
|
230
|
+
private videoFor;
|
|
231
|
+
private get activeVideo();
|
|
232
|
+
private leaveHighlight;
|
|
233
|
+
private stepInPlayer;
|
|
234
|
+
private openPlayer;
|
|
235
|
+
private closePlayer;
|
|
236
|
+
private formatter?;
|
|
237
|
+
private get locale();
|
|
238
|
+
private labelSet?;
|
|
239
|
+
private get labels();
|
|
240
|
+
private get formatPrice();
|
|
241
|
+
private productOf;
|
|
242
|
+
private readonly renderSlide;
|
|
243
|
+
protected render(): typeof nothing | lit.TemplateResult<1>;
|
|
244
|
+
private renderPlayer;
|
|
245
|
+
private cartActionsFor;
|
|
246
|
+
/**
|
|
247
|
+
* Adds one of `item` to the stepper's count, or removes one, and tells the
|
|
248
|
+
* store, which changes its cart: the element has none. When add to cart
|
|
249
|
+
* and the stepper swap places, the focus moves to the one now shown.
|
|
250
|
+
*/
|
|
251
|
+
private changeCart;
|
|
252
|
+
}
|
|
253
|
+
declare global {
|
|
254
|
+
interface HTMLElementTagNameMap {
|
|
255
|
+
[TAG_NAME]: VtexAdsStoryCarouselElement;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
declare function defineVtexAdsStoryCarousel(): void;
|
|
259
|
+
|
|
260
|
+
export { type StoryHydration as S, TAG_NAME as T, VtexAdsStoryCarouselElement as V, type StoryProductDetails as a, defineVtexAdsStoryCarousel as d };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vtex/ads-stories",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "VTEX Ads Stories web component",
|
|
6
6
|
"main": "dist/cjs/index.cjs",
|
|
7
7
|
"module": "dist/esm/index.js",
|
|
@@ -60,21 +60,35 @@
|
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
+
"@storybook/web-components-vite": "10.6.5",
|
|
63
64
|
"@types/node": "^22.15.21",
|
|
64
65
|
"@types/react": "^19.1.2",
|
|
66
|
+
"msw": "2.15.0",
|
|
67
|
+
"msw-storybook-addon": "3.0.3",
|
|
68
|
+
"storybook": "10.6.5",
|
|
65
69
|
"tsup": "^8.5.0",
|
|
66
70
|
"vite": "^5.0.0"
|
|
67
71
|
},
|
|
68
72
|
"dependencies": {
|
|
73
|
+
"@vtex/ads-core": "^0.6.0",
|
|
69
74
|
"lit": "^3.3.3"
|
|
70
75
|
},
|
|
76
|
+
"msw": {
|
|
77
|
+
"workerDirectory": [
|
|
78
|
+
".storybook/public"
|
|
79
|
+
]
|
|
80
|
+
},
|
|
71
81
|
"scripts": {
|
|
82
|
+
"prebuild": "pnpm --filter @vtex/ads-core run build",
|
|
72
83
|
"build": "tsup",
|
|
73
84
|
"build:watch": "tsup --watch",
|
|
74
85
|
"clean": "rimraf dist",
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
86
|
+
"predev": "pnpm run prebuild",
|
|
87
|
+
"dev": "storybook dev -p 6006",
|
|
88
|
+
"preview:cdn": "pnpm build && STORIES_SOURCE=cdn storybook dev -p 6006",
|
|
89
|
+
"prebuild-storybook": "pnpm run prebuild",
|
|
90
|
+
"build-storybook": "storybook build",
|
|
91
|
+
"check-types": "tsc --noEmit && tsc -p stories",
|
|
78
92
|
"test": "vitest run",
|
|
79
93
|
"test:watch": "vitest --coverage.enabled=false"
|
|
80
94
|
}
|
package/src/cdn.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineVtexAdsStoryCarousel } from "./story";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
defineVtexAdsStoryCarousel();
|
|
4
4
|
|
|
5
|
-
export {
|
|
5
|
+
export {
|
|
6
|
+
VtexAdsStoryCarouselElement,
|
|
7
|
+
defineVtexAdsStoryCarousel,
|
|
8
|
+
TAG_NAME,
|
|
9
|
+
} from "./story";
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commerce events the element hands to the store: it has no cart of its
|
|
3
|
+
* own. They bubble and cross shadow roots, so the store can listen on the
|
|
4
|
+
* element, the document or any ancestor.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Add to cart, or the stepper's plus, was clicked in the player. No default
|
|
9
|
+
* action.
|
|
10
|
+
*/
|
|
11
|
+
export const ADD_TO_CART_EVENT = "vtex-ads-story:add-to-cart";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The stepper's minus, or its trash with one left, was clicked in the
|
|
15
|
+
* player. No default action.
|
|
16
|
+
*/
|
|
17
|
+
export const REMOVE_FROM_CART_EVENT = "vtex-ads-story:remove-from-cart";
|
|
18
|
+
|
|
19
|
+
interface StoryCartEventDetail {
|
|
20
|
+
/** The Story product's SKU, from the ad API (`product.product_sku`). */
|
|
21
|
+
sku: string;
|
|
22
|
+
/**
|
|
23
|
+
* The product's seller, from the ad API (`product.seller_id`). Missing
|
|
24
|
+
* when the ad API doesn't send one.
|
|
25
|
+
*/
|
|
26
|
+
seller?: string;
|
|
27
|
+
/** How many to add (or, removing, to remove). Always 1 for now. */
|
|
28
|
+
quantity: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The detail of the add to cart event: `quantity` is how many to add. */
|
|
32
|
+
export type StoryAddToCartDetail = StoryCartEventDetail;
|
|
33
|
+
|
|
34
|
+
/** The detail of remove from cart: `quantity` is how many to remove. */
|
|
35
|
+
export type StoryRemoveFromCartDetail = StoryCartEventDetail;
|
|
36
|
+
|
|
37
|
+
/** The product an event is about: its SKU and, when known, its seller. */
|
|
38
|
+
export type StoryCartItem = Pick<StoryCartEventDetail, "sku" | "seller">;
|
|
39
|
+
|
|
40
|
+
export type StoryAddToCartEvent = CustomEvent<StoryAddToCartDetail>;
|
|
41
|
+
export type StoryRemoveFromCartEvent = CustomEvent<StoryRemoveFromCartDetail>;
|
|
42
|
+
|
|
43
|
+
function cartEvent(
|
|
44
|
+
type: typeof ADD_TO_CART_EVENT | typeof REMOVE_FROM_CART_EVENT,
|
|
45
|
+
{ sku, seller }: StoryCartItem,
|
|
46
|
+
quantity: number,
|
|
47
|
+
): CustomEvent<StoryCartEventDetail> {
|
|
48
|
+
return new CustomEvent<StoryCartEventDetail>(type, {
|
|
49
|
+
detail: { sku, ...(seller && { seller }), quantity },
|
|
50
|
+
bubbles: true,
|
|
51
|
+
composed: true,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The add to cart event for `item`, bubbling and composed, to dispatch from
|
|
57
|
+
* the element. `quantity` defaults to 1.
|
|
58
|
+
*/
|
|
59
|
+
export function addToCartEvent(
|
|
60
|
+
item: StoryCartItem,
|
|
61
|
+
quantity = 1,
|
|
62
|
+
): StoryAddToCartEvent {
|
|
63
|
+
return cartEvent(ADD_TO_CART_EVENT, item, quantity);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The remove from cart event for `item`, bubbling and composed, to dispatch
|
|
68
|
+
* from the element. `quantity` (how many to remove) defaults to 1.
|
|
69
|
+
*/
|
|
70
|
+
export function removeFromCartEvent(
|
|
71
|
+
item: StoryCartItem,
|
|
72
|
+
quantity = 1,
|
|
73
|
+
): StoryRemoveFromCartEvent {
|
|
74
|
+
return cartEvent(REMOVE_FROM_CART_EVENT, item, quantity);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Types `addEventListener("vtex-ads-story:add-to-cart", …)` and
|
|
78
|
+
// "vtex-ads-story:remove-from-cart" on any element, the document and the
|
|
79
|
+
// window.
|
|
80
|
+
declare global {
|
|
81
|
+
interface HTMLElementEventMap {
|
|
82
|
+
[ADD_TO_CART_EVENT]: StoryAddToCartEvent;
|
|
83
|
+
[REMOVE_FROM_CART_EVENT]: StoryRemoveFromCartEvent;
|
|
84
|
+
}
|
|
85
|
+
interface DocumentEventMap {
|
|
86
|
+
[ADD_TO_CART_EVENT]: StoryAddToCartEvent;
|
|
87
|
+
[REMOVE_FROM_CART_EVENT]: StoryRemoveFromCartEvent;
|
|
88
|
+
}
|
|
89
|
+
interface WindowEventMap {
|
|
90
|
+
[ADD_TO_CART_EVENT]: StoryAddToCartEvent;
|
|
91
|
+
[REMOVE_FROM_CART_EVENT]: StoryRemoveFromCartEvent;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
import { css, html, nothing } from "lit";
|
|
2
|
+
import { ref, type RefOrCallback } from "lit/directives/ref.js";
|
|
3
|
+
import type { StoryLabels } from "../../locales";
|
|
2
4
|
import { icon } from "../icons";
|
|
3
5
|
import { hasVideo, renderMedia, type VideoController } from "../media";
|
|
6
|
+
import {
|
|
7
|
+
renderCta,
|
|
8
|
+
renderProduct,
|
|
9
|
+
type CardCta,
|
|
10
|
+
type CardProduct,
|
|
11
|
+
} from "../product";
|
|
4
12
|
import type { StoryMedia } from "../../types";
|
|
5
13
|
|
|
6
14
|
export const cardStyles = css`
|
|
15
|
+
/* 9:16 at the height its container gives it (--card-height, default
|
|
16
|
+
500px). */
|
|
7
17
|
.card {
|
|
8
18
|
position: relative;
|
|
9
19
|
box-sizing: border-box;
|
|
10
|
-
width: calc(500px * 9 / 16);
|
|
11
|
-
height: 500px;
|
|
20
|
+
width: calc(var(--card-height, 500px) * 9 / 16);
|
|
21
|
+
height: var(--card-height, 500px);
|
|
12
22
|
overflow: hidden;
|
|
13
23
|
border-radius: var(--vtex-ads-story-radius, 20px);
|
|
14
24
|
background: #000;
|
|
@@ -116,9 +126,38 @@ export interface CardOptions {
|
|
|
116
126
|
label: string;
|
|
117
127
|
/** Called on click, Enter or Space. */
|
|
118
128
|
onSelect: () => void;
|
|
119
|
-
/**
|
|
120
|
-
|
|
121
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Whether the video plays over the cover (default true). Without it, the
|
|
131
|
+
* card shows only the cover, with no sound button.
|
|
132
|
+
*/
|
|
133
|
+
withVideo?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Whether the video plays on its own (default true). Without it, the
|
|
136
|
+
* video loads but waits (see renderMedia).
|
|
137
|
+
*/
|
|
138
|
+
autoplay?: boolean;
|
|
139
|
+
/** The texts (the sound button's, shown when the card has a video). */
|
|
140
|
+
labels: StoryLabels;
|
|
141
|
+
/** The Story's product, shown at the bottom of the card. */
|
|
142
|
+
product?: CardProduct;
|
|
143
|
+
/** Called by a click on the product (default onSelect). */
|
|
144
|
+
onProductSelect?: () => void;
|
|
145
|
+
/** The Story's call to action, for a link destination (no product). */
|
|
146
|
+
cta?: CardCta;
|
|
147
|
+
/**
|
|
148
|
+
* Called by a click on the call to action instead of following the link
|
|
149
|
+
* (a card without the highlight). Without it, the link opens.
|
|
150
|
+
*/
|
|
151
|
+
onCtaSelect?: () => void;
|
|
152
|
+
/**
|
|
153
|
+
* Called by any click on the card, its product, call to action and sound
|
|
154
|
+
* button included, before what that click does.
|
|
155
|
+
*/
|
|
156
|
+
onPress?: () => void;
|
|
157
|
+
/** Receives the card's element (see Slide's cardRef). */
|
|
158
|
+
ref?: RefOrCallback;
|
|
159
|
+
/** Receives the button that takes the focus (see Slide's focusRef). */
|
|
160
|
+
openRef?: RefOrCallback;
|
|
122
161
|
}
|
|
123
162
|
|
|
124
163
|
/**
|
|
@@ -132,7 +171,9 @@ function renderSoundButton(video: VideoController, options: CardOptions) {
|
|
|
132
171
|
<button
|
|
133
172
|
type="button"
|
|
134
173
|
class="card-sound"
|
|
135
|
-
aria-label=${video.muted
|
|
174
|
+
aria-label=${video.muted
|
|
175
|
+
? options.labels.soundOn
|
|
176
|
+
: options.labels.soundOff}
|
|
136
177
|
@click=${() => video.toggleMuted()}
|
|
137
178
|
>
|
|
138
179
|
${icon(video.muted ? "soundOff" : "soundOn")}
|
|
@@ -142,30 +183,54 @@ function renderSoundButton(video: VideoController, options: CardOptions) {
|
|
|
142
183
|
|
|
143
184
|
/**
|
|
144
185
|
* A 9:16 card with the Story cover and, with a media_url, the video looping
|
|
145
|
-
* muted over it (see renderMedia)
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
186
|
+
* muted over it (see renderMedia), or a notice that it is unavailable if it
|
|
187
|
+
* can't load. A button with a play icon in the middle (not over the notice)
|
|
188
|
+
* covers the card, and a sound button shows while the video plays. With a
|
|
189
|
+
* product, its pill sits at the bottom (see renderProduct); with a link
|
|
190
|
+
* destination, the call to action does (see renderCta). The sound, product
|
|
191
|
+
* and call to action are beside the card's button, not inside it: a button
|
|
192
|
+
* can't contain another.
|
|
149
193
|
*/
|
|
150
194
|
export function renderCard(
|
|
151
195
|
story: StoryMedia,
|
|
152
196
|
video: VideoController,
|
|
153
197
|
options: CardOptions,
|
|
154
198
|
) {
|
|
199
|
+
const withVideo = options.withVideo ?? true;
|
|
200
|
+
|
|
155
201
|
return html`
|
|
156
|
-
<div
|
|
157
|
-
|
|
202
|
+
<div
|
|
203
|
+
class="card"
|
|
204
|
+
${ref(options.ref)}
|
|
205
|
+
@click=${{ handleEvent: () => options.onPress?.(), capture: true }}
|
|
206
|
+
>
|
|
207
|
+
${renderMedia(story, video, {
|
|
208
|
+
withVideo,
|
|
209
|
+
autoplay: options.autoplay,
|
|
210
|
+
labels: options.labels,
|
|
211
|
+
announce: false,
|
|
212
|
+
})}
|
|
158
213
|
<button
|
|
159
214
|
type="button"
|
|
160
215
|
class="card-open"
|
|
216
|
+
${ref(options.openRef)}
|
|
161
217
|
aria-label=${options.label}
|
|
162
218
|
@click=${options.onSelect}
|
|
163
219
|
>
|
|
164
|
-
|
|
220
|
+
${withVideo && video.failed
|
|
221
|
+
? nothing
|
|
222
|
+
: html`<span class="play" aria-hidden="true">${icon("play")}</span>`}
|
|
165
223
|
</button>
|
|
166
|
-
${hasVideo(story, video) && !video.paused
|
|
224
|
+
${withVideo && hasVideo(story, video) && !video.paused
|
|
167
225
|
? renderSoundButton(video, options)
|
|
168
226
|
: nothing}
|
|
227
|
+
${options.product
|
|
228
|
+
? renderProduct(
|
|
229
|
+
options.product,
|
|
230
|
+
options.onProductSelect ?? options.onSelect,
|
|
231
|
+
)
|
|
232
|
+
: nothing}
|
|
233
|
+
${options.cta ? renderCta(options.cta, options.onCtaSelect) : nothing}
|
|
169
234
|
</div>
|
|
170
235
|
`;
|
|
171
236
|
}
|