@vtex/ads-stories 0.5.0 → 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 +27 -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 +909 -186
- 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 +7 -1
- package/dist/cjs/story-DAiAN9v-.d.cts +260 -0
- package/dist/esm/index.d.ts +42 -196
- package/dist/esm/index.js +933 -209
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +7 -1
- package/dist/esm/story-DAiAN9v-.d.ts +260 -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/pt.ts +13 -3
- package/src/locales/types.ts +22 -2
- package/src/react.ts +6 -0
- package/src/story/story.ts +188 -16
- 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
package/dist/esm/react.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DetailedHTMLProps, HTMLAttributes } from 'react';
|
|
2
2
|
import { Channel } from '@vtex/ads-core';
|
|
3
|
-
import { TAG_NAME, VtexAdsStoryCarouselElement } from './
|
|
3
|
+
import { T as TAG_NAME, V as VtexAdsStoryCarouselElement } from './story-DAiAN9v-.js';
|
|
4
4
|
import 'lit';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -51,6 +51,12 @@ interface VtexAdsStoryCarouselAttributes {
|
|
|
51
51
|
* their video, muted. Off by default.
|
|
52
52
|
*/
|
|
53
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;
|
|
54
60
|
}
|
|
55
61
|
type VtexAdsStoryCarouselProps = DetailedHTMLProps<HTMLAttributes<VtexAdsStoryCarouselElement>, VtexAdsStoryCarouselElement> & VtexAdsStoryCarouselAttributes;
|
|
56
62
|
declare module "react" {
|
|
@@ -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
|
@@ -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
|
+
}
|
|
@@ -131,6 +131,11 @@ export interface CardOptions {
|
|
|
131
131
|
* card shows only the cover, with no sound button.
|
|
132
132
|
*/
|
|
133
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;
|
|
134
139
|
/** The texts (the sound button's, shown when the card has a video). */
|
|
135
140
|
labels: StoryLabels;
|
|
136
141
|
/** The Story's product, shown at the bottom of the card. */
|
|
@@ -178,7 +183,8 @@ function renderSoundButton(video: VideoController, options: CardOptions) {
|
|
|
178
183
|
|
|
179
184
|
/**
|
|
180
185
|
* A 9:16 card with the Story cover and, with a media_url, the video looping
|
|
181
|
-
* muted over it (see renderMedia)
|
|
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)
|
|
182
188
|
* covers the card, and a sound button shows while the video plays. With a
|
|
183
189
|
* product, its pill sits at the bottom (see renderProduct); with a link
|
|
184
190
|
* destination, the call to action does (see renderCta). The sound, product
|
|
@@ -198,7 +204,12 @@ export function renderCard(
|
|
|
198
204
|
${ref(options.ref)}
|
|
199
205
|
@click=${{ handleEvent: () => options.onPress?.(), capture: true }}
|
|
200
206
|
>
|
|
201
|
-
${renderMedia(story, video, {
|
|
207
|
+
${renderMedia(story, video, {
|
|
208
|
+
withVideo,
|
|
209
|
+
autoplay: options.autoplay,
|
|
210
|
+
labels: options.labels,
|
|
211
|
+
announce: false,
|
|
212
|
+
})}
|
|
202
213
|
<button
|
|
203
214
|
type="button"
|
|
204
215
|
class="card-open"
|
|
@@ -206,7 +217,9 @@ export function renderCard(
|
|
|
206
217
|
aria-label=${options.label}
|
|
207
218
|
@click=${options.onSelect}
|
|
208
219
|
>
|
|
209
|
-
|
|
220
|
+
${withVideo && video.failed
|
|
221
|
+
? nothing
|
|
222
|
+
: html`<span class="play" aria-hidden="true">${icon("play")}</span>`}
|
|
210
223
|
</button>
|
|
211
224
|
${withVideo && hasVideo(story, video) && !video.paused
|
|
212
225
|
? renderSoundButton(video, options)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { css, html } from "lit";
|
|
2
|
+
import { styleMap } from "lit/directives/style-map.js";
|
|
3
|
+
import { STAGE_HEIGHT, VISIBLE_SIDE } from "./carousel";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* While the covers and product photos load, the carousel is hidden under a
|
|
7
|
+
* skeleton of the same layout, which then fades out. The skeleton's slides
|
|
8
|
+
* have their own class, so they never count as the carousel's.
|
|
9
|
+
*/
|
|
10
|
+
export const carouselSkeletonStyles = css`
|
|
11
|
+
.root {
|
|
12
|
+
position: relative;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.root[data-requesting] {
|
|
16
|
+
height: ${STAGE_HEIGHT};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.root[data-loading] .carousel {
|
|
20
|
+
visibility: hidden;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.skeleton {
|
|
24
|
+
position: absolute;
|
|
25
|
+
inset: 0;
|
|
26
|
+
z-index: 2;
|
|
27
|
+
box-sizing: border-box;
|
|
28
|
+
overflow: hidden;
|
|
29
|
+
pointer-events: none;
|
|
30
|
+
transition:
|
|
31
|
+
opacity 0.3s ease,
|
|
32
|
+
visibility 0s;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.skeleton[data-done] {
|
|
36
|
+
opacity: 0;
|
|
37
|
+
visibility: hidden;
|
|
38
|
+
transition:
|
|
39
|
+
opacity 0.3s ease,
|
|
40
|
+
visibility 0s 0.3s;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.skeleton-slide {
|
|
44
|
+
position: absolute;
|
|
45
|
+
top: 0;
|
|
46
|
+
left: 50%;
|
|
47
|
+
width: calc(${STAGE_HEIGHT} * 9 / 16);
|
|
48
|
+
height: 100%;
|
|
49
|
+
margin-left: calc(${STAGE_HEIGHT} * 9 / -32);
|
|
50
|
+
transform: translateX(calc(var(--place) * (100% + 8px))) scale(0.9);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.skeleton-slide[data-active] {
|
|
54
|
+
transform: none;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.skeleton-card {
|
|
58
|
+
width: 100%;
|
|
59
|
+
height: 100%;
|
|
60
|
+
border-radius: var(--vtex-ads-story-radius, 20px);
|
|
61
|
+
background: #ececec;
|
|
62
|
+
animation: skeleton-pulse 1.2s ease-in-out infinite alternate;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.skeleton[data-done] .skeleton-card {
|
|
66
|
+
animation: none;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@keyframes skeleton-pulse {
|
|
70
|
+
to {
|
|
71
|
+
background-color: #f6f6f6;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@media (prefers-reduced-motion: reduce) {
|
|
76
|
+
.skeleton,
|
|
77
|
+
.skeleton[data-done] {
|
|
78
|
+
transition: none;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.skeleton-card {
|
|
82
|
+
animation: none;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Placeholder for the carousel while it loads.
|
|
89
|
+
* Decorative, so hidden from assistive technology. With `done`, it fades out
|
|
90
|
+
* and stays out of sight.
|
|
91
|
+
*/
|
|
92
|
+
export function renderCarouselSkeleton({ done }: { done: boolean }) {
|
|
93
|
+
const places = Array.from(
|
|
94
|
+
{ length: VISIBLE_SIDE * 2 + 1 },
|
|
95
|
+
(_, i) => i - VISIBLE_SIDE,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return html`
|
|
99
|
+
<div class="skeleton" aria-hidden="true" ?data-done=${done}>
|
|
100
|
+
${places.map(
|
|
101
|
+
(place) => html`
|
|
102
|
+
<div
|
|
103
|
+
class="skeleton-slide"
|
|
104
|
+
?data-active=${place === 0}
|
|
105
|
+
style=${styleMap({ "--place": String(place) })}
|
|
106
|
+
>
|
|
107
|
+
<div class="skeleton-card"></div>
|
|
108
|
+
</div>
|
|
109
|
+
`,
|
|
110
|
+
)}
|
|
111
|
+
</div>
|
|
112
|
+
`;
|
|
113
|
+
}
|
|
@@ -10,11 +10,28 @@ import { repeat } from "lit/directives/repeat.js";
|
|
|
10
10
|
import { styleMap } from "lit/directives/style-map.js";
|
|
11
11
|
import { icon } from "../icons";
|
|
12
12
|
|
|
13
|
-
const STAGE_HEIGHT = css`500px`;
|
|
13
|
+
export const STAGE_HEIGHT = css`500px`;
|
|
14
14
|
const SOLO_PADDING = css`24px`;
|
|
15
15
|
|
|
16
|
-
/** Time each Story stays highlighted before the next one, in milliseconds. */
|
|
17
16
|
export const AUTO_ADVANCE_MS = 5000;
|
|
17
|
+
export const MIN_SLIDE_DURATION_MS = 2000;
|
|
18
|
+
export const MAX_SLIDE_DURATION_MS = 120_000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The time each Story stays highlighted: `ms` when it is a number from zero
|
|
22
|
+
* up (zero: no automatic advance), raised to MIN_SLIDE_DURATION_MS when
|
|
23
|
+
* smaller and capped at MAX_SLIDE_DURATION_MS, AUTO_ADVANCE_MS when it is
|
|
24
|
+
* missing, negative or not a number.
|
|
25
|
+
*/
|
|
26
|
+
export const toSlideDuration = (ms?: number) => {
|
|
27
|
+
if (ms === undefined || !Number.isFinite(ms) || ms < 0) {
|
|
28
|
+
return AUTO_ADVANCE_MS;
|
|
29
|
+
}
|
|
30
|
+
if (ms === 0) {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
return Math.min(Math.max(ms, MIN_SLIDE_DURATION_MS), MAX_SLIDE_DURATION_MS);
|
|
34
|
+
};
|
|
18
35
|
|
|
19
36
|
/** How far a pointer must move, in pixels, to count as a swipe. */
|
|
20
37
|
export const SWIPE_DISTANCE = 40;
|
|
@@ -227,6 +244,16 @@ export interface CarouselOptions {
|
|
|
227
244
|
* example, while the player shows the highlighted Story).
|
|
228
245
|
*/
|
|
229
246
|
locked?: () => boolean;
|
|
247
|
+
/**
|
|
248
|
+
* How long each Story stays highlighted before the next one, in
|
|
249
|
+
* milliseconds (see toSlideDuration). Read whenever the host updates.
|
|
250
|
+
*/
|
|
251
|
+
slideDuration?: () => number | undefined;
|
|
252
|
+
/**
|
|
253
|
+
* Whether the person prefers reduced motion: no automatic advance then.
|
|
254
|
+
* Read once each time the host connects.
|
|
255
|
+
*/
|
|
256
|
+
reducedMotion?: () => boolean;
|
|
230
257
|
}
|
|
231
258
|
|
|
232
259
|
/** One rendered card. */
|
|
@@ -281,9 +308,11 @@ function tracker(elements: Map<Element, Slide>, slide: Slide) {
|
|
|
281
308
|
* layout, the arrows, swipes and the automatic advance. Call reset() when
|
|
282
309
|
* the Stories change, and render() from the host's render.
|
|
283
310
|
*
|
|
284
|
-
* The highlight moves on its own every
|
|
285
|
-
*
|
|
286
|
-
* or the host is off the page.
|
|
311
|
+
* The highlight moves on its own every slide duration (AUTO_ADVANCE_MS by
|
|
312
|
+
* default, never with zero), except while the pointer is over the carousel,
|
|
313
|
+
* the keyboard focus is in it, it is locked, or the host is off the page.
|
|
314
|
+
* Never for people who prefer reduced motion: the highlight then moves only
|
|
315
|
+
* when they step it.
|
|
287
316
|
*/
|
|
288
317
|
export class CarouselController implements ReactiveController {
|
|
289
318
|
/**
|
|
@@ -296,10 +325,13 @@ export class CarouselController implements ReactiveController {
|
|
|
296
325
|
private count = 0;
|
|
297
326
|
private narrow = false;
|
|
298
327
|
private media?: MediaQueryList;
|
|
328
|
+
private reducedMotion = false;
|
|
299
329
|
private connected = false;
|
|
300
330
|
private hovered = false;
|
|
301
331
|
private focused = false;
|
|
302
332
|
private timer?: ReturnType<typeof setInterval>;
|
|
333
|
+
/** The interval the running timer was started with. */
|
|
334
|
+
private timerMs = 0;
|
|
303
335
|
private swipe?: { x: number; moved: boolean };
|
|
304
336
|
/** The rendered cards, as their cardRef reported them. */
|
|
305
337
|
private readonly cards = new Map<Element, Slide>();
|
|
@@ -354,6 +386,7 @@ export class CarouselController implements ReactiveController {
|
|
|
354
386
|
this.media = globalThis.matchMedia?.(NARROW_SCREEN.toString());
|
|
355
387
|
this.media?.addEventListener("change", this.onMediaChange);
|
|
356
388
|
this.narrow = this.media?.matches ?? false;
|
|
389
|
+
this.reducedMotion = this.options.reducedMotion?.() ?? false;
|
|
357
390
|
this.syncTimer();
|
|
358
391
|
}
|
|
359
392
|
|
|
@@ -365,8 +398,8 @@ export class CarouselController implements ReactiveController {
|
|
|
365
398
|
this.syncTimer();
|
|
366
399
|
}
|
|
367
400
|
|
|
368
|
-
// Whatever the host changed (the player opening, new Stories
|
|
369
|
-
// stop the automatic advance.
|
|
401
|
+
// Whatever the host changed (the player opening, new Stories, the slide
|
|
402
|
+
// duration) may start, stop or restart the automatic advance.
|
|
370
403
|
hostUpdated() {
|
|
371
404
|
this.syncTimer();
|
|
372
405
|
}
|
|
@@ -405,8 +438,14 @@ export class CarouselController implements ReactiveController {
|
|
|
405
438
|
this.restartTimer();
|
|
406
439
|
}
|
|
407
440
|
|
|
441
|
+
private get slideDuration() {
|
|
442
|
+
return toSlideDuration(this.options.slideDuration?.());
|
|
443
|
+
}
|
|
444
|
+
|
|
408
445
|
private get shouldAdvance() {
|
|
409
446
|
return (
|
|
447
|
+
this.slideDuration > 0 &&
|
|
448
|
+
!this.reducedMotion &&
|
|
410
449
|
this.connected &&
|
|
411
450
|
this.count > 1 &&
|
|
412
451
|
!this.hovered &&
|
|
@@ -416,8 +455,13 @@ export class CarouselController implements ReactiveController {
|
|
|
416
455
|
}
|
|
417
456
|
|
|
418
457
|
private syncTimer() {
|
|
458
|
+
const ms = this.slideDuration;
|
|
459
|
+
// A new duration starts over, with the full new time.
|
|
460
|
+
if (this.timer && this.timerMs !== ms) this.stopTimer();
|
|
461
|
+
|
|
419
462
|
if (this.shouldAdvance && !this.timer) {
|
|
420
|
-
this.
|
|
463
|
+
this.timerMs = ms;
|
|
464
|
+
this.timer = setInterval(() => this.step(1), ms);
|
|
421
465
|
} else if (!this.shouldAdvance && this.timer) {
|
|
422
466
|
this.stopTimer();
|
|
423
467
|
}
|
|
@@ -426,6 +470,7 @@ export class CarouselController implements ReactiveController {
|
|
|
426
470
|
private stopTimer() {
|
|
427
471
|
clearInterval(this.timer);
|
|
428
472
|
this.timer = undefined;
|
|
473
|
+
this.timerMs = 0;
|
|
429
474
|
}
|
|
430
475
|
|
|
431
476
|
private restartTimer() {
|
|
@@ -2,11 +2,18 @@ export {
|
|
|
2
2
|
AUTO_ADVANCE_MS,
|
|
3
3
|
CarouselController,
|
|
4
4
|
carouselStyles,
|
|
5
|
+
MAX_SLIDE_DURATION_MS,
|
|
6
|
+
MIN_SLIDE_DURATION_MS,
|
|
5
7
|
NARROW_SCREEN,
|
|
6
8
|
RENDERED_SIDE,
|
|
7
9
|
SWIPE_DISTANCE,
|
|
10
|
+
toSlideDuration,
|
|
8
11
|
VISIBLE_SIDE,
|
|
9
12
|
} from "./carousel";
|
|
13
|
+
export {
|
|
14
|
+
carouselSkeletonStyles,
|
|
15
|
+
renderCarouselSkeleton,
|
|
16
|
+
} from "./carousel-skeleton";
|
|
10
17
|
export type {
|
|
11
18
|
CarouselLayout,
|
|
12
19
|
CarouselOptions,
|