@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.
- package/CHANGELOG.md +67 -0
- package/README.md +260 -28
- package/dist/cdn/vtex-ads-stories.js +1073 -20
- package/dist/cdn/vtex-ads-stories.js.map +1 -1
- package/dist/cjs/index.cjs +4392 -315
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +198 -12
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/cjs/react.d.cts +46 -5
- package/dist/esm/index.d.ts +198 -12
- package/dist/esm/index.js +2819 -291
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react.d.ts +46 -5
- package/package.json +21 -4
- package/src/cdn.ts +7 -3
- package/src/components/card/card.ts +101 -111
- package/src/components/card/index.ts +1 -1
- package/src/components/carousel/carousel.ts +670 -0
- package/src/components/carousel/index.ts +15 -0
- package/src/components/events/events-controller.ts +212 -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/index.ts +30 -9
- package/src/components/media/index.ts +3 -0
- package/src/components/media/media.ts +70 -0
- package/src/components/media/video-controller.ts +125 -0
- package/src/components/player/index.ts +6 -2
- package/src/components/player/player.ts +946 -134
- package/src/components/product/cta.ts +56 -0
- package/src/components/product/index.ts +20 -0
- package/src/components/product/price.ts +86 -0
- package/src/components/product/product.styles.ts +369 -0
- package/src/components/product/product.ts +235 -0
- package/src/index.ts +9 -1
- package/src/locales/en.ts +29 -0
- package/src/locales/es.ts +30 -0
- package/src/locales/index.ts +36 -0
- package/src/locales/pt.ts +29 -0
- package/src/locales/types.ts +24 -0
- package/src/react.ts +49 -7
- package/src/story/index.ts +5 -1
- package/src/story/story.ts +528 -93
- package/src/tag-name.ts +1 -0
- package/src/types.ts +8 -12
- package/src/utils/fetch-story.ts +113 -0
- package/src/utils/http-url.ts +12 -0
- package/src/utils/hydrate-products.ts +88 -0
- package/src/utils/send-event.ts +71 -0
- package/src/utils/with-timeout.ts +43 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
import {
|
|
2
|
+
css,
|
|
3
|
+
html,
|
|
4
|
+
type ReactiveController,
|
|
5
|
+
type ReactiveControllerHost,
|
|
6
|
+
type TemplateResult,
|
|
7
|
+
} from "lit";
|
|
8
|
+
import type { StoryLabels } from "../../locales";
|
|
9
|
+
import { repeat } from "lit/directives/repeat.js";
|
|
10
|
+
import { styleMap } from "lit/directives/style-map.js";
|
|
11
|
+
import { icon } from "../icons";
|
|
12
|
+
|
|
13
|
+
const STAGE_HEIGHT = css`500px`;
|
|
14
|
+
const SOLO_PADDING = css`24px`;
|
|
15
|
+
|
|
16
|
+
/** Time each Story stays highlighted before the next one, in milliseconds. */
|
|
17
|
+
export const AUTO_ADVANCE_MS = 5000;
|
|
18
|
+
|
|
19
|
+
/** How far a pointer must move, in pixels, to count as a swipe. */
|
|
20
|
+
export const SWIPE_DISTANCE = 40;
|
|
21
|
+
|
|
22
|
+
/** Screens this narrow have no room for two cards side by side. */
|
|
23
|
+
export const NARROW_SCREEN = css`(max-width: 576px)`;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Rolling layout: cards on each side of the highlighted one. Up to
|
|
27
|
+
* VISIBLE_SIDE show on each side; RENDERED_SIDE are kept in the DOM, the
|
|
28
|
+
* farther ones hidden, so a card slides in from its place instead of
|
|
29
|
+
* appearing.
|
|
30
|
+
*/
|
|
31
|
+
export const VISIBLE_SIDE = 2;
|
|
32
|
+
export const RENDERED_SIDE = VISIBLE_SIDE + 3;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* How the cards are laid out:
|
|
36
|
+
* - solo: one Story, the card centered over the background.
|
|
37
|
+
* - pair: two Stories side by side; moving only changes the highlight.
|
|
38
|
+
* - rolling: the highlighted card in the middle, the others on each side,
|
|
39
|
+
* all moving one place per step. Three or more Stories, or two on narrow
|
|
40
|
+
* screens.
|
|
41
|
+
*/
|
|
42
|
+
export type CarouselLayout = "solo" | "pair" | "rolling";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The carousel stage: a band as tall as the card, as wide as the element's
|
|
46
|
+
* parent. The store sets the width on its own container.
|
|
47
|
+
*
|
|
48
|
+
* Solo: the card sits centered over a solid background with room around it:
|
|
49
|
+
* the card shrinks to fit the padding and gets a soft shadow. On narrow
|
|
50
|
+
* screens (576px or less) there is no room for it: the card shows alone at
|
|
51
|
+
* full height.
|
|
52
|
+
*
|
|
53
|
+
* Pair and rolling: the highlighted card is full size, the others smaller
|
|
54
|
+
* (0.9) and dimmed.
|
|
55
|
+
*/
|
|
56
|
+
export const carouselStyles = css`
|
|
57
|
+
.carousel {
|
|
58
|
+
position: relative;
|
|
59
|
+
box-sizing: border-box;
|
|
60
|
+
display: grid;
|
|
61
|
+
height: ${STAGE_HEIGHT};
|
|
62
|
+
place-items: center;
|
|
63
|
+
--card-height: ${STAGE_HEIGHT};
|
|
64
|
+
user-select: none;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.carousel[data-layout="solo"] {
|
|
68
|
+
padding: ${SOLO_PADDING};
|
|
69
|
+
border-radius: var(--vtex-ads-story-radius, 20px);
|
|
70
|
+
background: var(--vtex-ads-story-color-surface, #fafafa);
|
|
71
|
+
--card-height: calc(${STAGE_HEIGHT} - 2 * ${SOLO_PADDING});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.carousel[data-layout="solo"] .card {
|
|
75
|
+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.track {
|
|
79
|
+
position: absolute;
|
|
80
|
+
inset: 0;
|
|
81
|
+
overflow: hidden;
|
|
82
|
+
touch-action: pan-y;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Each slide starts centered, then moves by its own transform. */
|
|
86
|
+
.slide {
|
|
87
|
+
--ease: cubic-bezier(0.25, 1, 0.5, 1);
|
|
88
|
+
--duration: var(--slide-duration, 0.4s);
|
|
89
|
+
position: absolute;
|
|
90
|
+
top: 0;
|
|
91
|
+
left: 50%;
|
|
92
|
+
width: calc(${STAGE_HEIGHT} * 9 / 16);
|
|
93
|
+
height: 100%;
|
|
94
|
+
margin-left: calc(${STAGE_HEIGHT} * 9 / -32);
|
|
95
|
+
opacity: 0.5;
|
|
96
|
+
transition:
|
|
97
|
+
transform var(--duration) var(--ease),
|
|
98
|
+
opacity var(--duration) var(--ease),
|
|
99
|
+
visibility 0s;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.slide[data-active] {
|
|
103
|
+
opacity: 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/* Pair: --side is -1 for the first card, 1 for the second. The highlighted
|
|
107
|
+
card sits 47.5% of its width off center, the other (scaled to 0.9)
|
|
108
|
+
52.5%, so the gap between them stays 8px. */
|
|
109
|
+
.carousel[data-layout="pair"] .slide {
|
|
110
|
+
--side: 1;
|
|
111
|
+
--offset: 52.5%;
|
|
112
|
+
--scale: 0.9;
|
|
113
|
+
transform: translateX(calc(var(--side) * (var(--offset) + 4px)))
|
|
114
|
+
scale(var(--scale));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.carousel[data-layout="pair"] .slide[data-index="0"] {
|
|
118
|
+
--side: -1;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.carousel[data-layout="pair"] .slide[data-active] {
|
|
122
|
+
--offset: 47.5%;
|
|
123
|
+
--scale: 1;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/* Rolling: --place is the slide's distance from the highlighted one, in
|
|
127
|
+
cards, 8px apart. A hidden slide fades out, then stops being visible. */
|
|
128
|
+
.carousel[data-layout="rolling"] .slide {
|
|
129
|
+
transform: translateX(calc(var(--place) * (100% + 8px))) scale(0.9);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.carousel[data-layout="rolling"] .slide[data-active] {
|
|
133
|
+
transform: translateX(0) scale(1);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.slide[data-hidden] {
|
|
137
|
+
opacity: 0;
|
|
138
|
+
visibility: hidden;
|
|
139
|
+
transition:
|
|
140
|
+
transform var(--duration) var(--ease),
|
|
141
|
+
opacity var(--duration) var(--ease),
|
|
142
|
+
visibility 0s var(--duration);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/* Only the highlighted card opens the player, so only it shows play. Its
|
|
146
|
+
sound is the only one to turn on: the others play muted, if at all. */
|
|
147
|
+
.slide:not([data-active]) .play,
|
|
148
|
+
.slide:not([data-active]) .card-sound {
|
|
149
|
+
display: none;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.arrow {
|
|
153
|
+
position: absolute;
|
|
154
|
+
top: 50%;
|
|
155
|
+
z-index: 2;
|
|
156
|
+
display: grid;
|
|
157
|
+
width: 32px;
|
|
158
|
+
height: 32px;
|
|
159
|
+
place-items: center;
|
|
160
|
+
padding: 0;
|
|
161
|
+
border: 1px solid rgba(133, 133, 133, 0.3);
|
|
162
|
+
border-radius: 50%;
|
|
163
|
+
background: rgba(0, 0, 0, 0.3);
|
|
164
|
+
backdrop-filter: blur(5px);
|
|
165
|
+
color: #fff;
|
|
166
|
+
cursor: pointer;
|
|
167
|
+
transform: translateY(-50%);
|
|
168
|
+
transition: background-color 0.2s;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.arrow:hover {
|
|
172
|
+
background: #262626;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.arrow:disabled {
|
|
176
|
+
cursor: default;
|
|
177
|
+
opacity: 0.5;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.arrow .icon {
|
|
181
|
+
width: 16px;
|
|
182
|
+
height: 16px;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.arrow-previous {
|
|
186
|
+
left: 3px;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
.arrow-next {
|
|
190
|
+
right: 3px;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@media ${NARROW_SCREEN} {
|
|
194
|
+
.carousel[data-layout="solo"] {
|
|
195
|
+
padding: 0;
|
|
196
|
+
background: none;
|
|
197
|
+
--card-height: ${STAGE_HEIGHT};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.carousel[data-layout="solo"] .card {
|
|
201
|
+
box-shadow: none;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@media (prefers-reduced-motion: reduce) {
|
|
206
|
+
.slide,
|
|
207
|
+
.slide[data-hidden] {
|
|
208
|
+
transition: none;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
`;
|
|
212
|
+
|
|
213
|
+
export interface CarouselOptions {
|
|
214
|
+
/**
|
|
215
|
+
* Called when the highlight moves, before the host renders it, with the
|
|
216
|
+
* keys (see Slide) of the slide that loses it and the one that gets it.
|
|
217
|
+
* The host's updateComplete then resolves once the step is rendered.
|
|
218
|
+
*/
|
|
219
|
+
onStep?: (from: number, to: number) => void;
|
|
220
|
+
/**
|
|
221
|
+
* Called when the layout changes (the screen got narrow or wide): every
|
|
222
|
+
* slide is new, with new keys.
|
|
223
|
+
*/
|
|
224
|
+
onLayoutChange?: () => void;
|
|
225
|
+
/**
|
|
226
|
+
* While true, the highlight doesn't move, by hand or on its own (for
|
|
227
|
+
* example, while the player shows the highlighted Story).
|
|
228
|
+
*/
|
|
229
|
+
locked?: () => boolean;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** One rendered card. */
|
|
233
|
+
export interface Slide {
|
|
234
|
+
/**
|
|
235
|
+
* Stays with the card while it moves, so state kept per card (such as its
|
|
236
|
+
* video) follows it. Unique among the rendered slides of a layout; the
|
|
237
|
+
* same Story can show in more than one slide.
|
|
238
|
+
*/
|
|
239
|
+
key: number;
|
|
240
|
+
/** The Story's index in the list. */
|
|
241
|
+
index: number;
|
|
242
|
+
/** Places from the highlighted card: negative before it, positive after. */
|
|
243
|
+
offset: number;
|
|
244
|
+
/** Has the highlight. */
|
|
245
|
+
active: boolean;
|
|
246
|
+
/** Kept in the DOM out of sight, so it can slide in. */
|
|
247
|
+
hidden: boolean;
|
|
248
|
+
/**
|
|
249
|
+
* Pass to the card's element (renderCard's ref): the carousel then knows
|
|
250
|
+
* where each Story's card is (see visibleCards).
|
|
251
|
+
*/
|
|
252
|
+
cardRef: (element?: Element) => void;
|
|
253
|
+
/**
|
|
254
|
+
* Pass to the card's element that takes the keyboard focus (renderCard's
|
|
255
|
+
* openRef; see focusActive).
|
|
256
|
+
*/
|
|
257
|
+
focusRef: (element?: Element) => void;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Renders the card of one Story. */
|
|
261
|
+
export type RenderSlide<T> = (item: T, slide: Slide) => TemplateResult;
|
|
262
|
+
|
|
263
|
+
const wrap = (value: number, count: number) =>
|
|
264
|
+
((value % count) + count) % count;
|
|
265
|
+
|
|
266
|
+
// A ref callback that keeps `elements` up to date. Each render passes new
|
|
267
|
+
// callbacks: lit calls the last render's with undefined, then the new one
|
|
268
|
+
// with the element, so an element dropped by the rolling layout or the host
|
|
269
|
+
// leaving the page leaves `elements`.
|
|
270
|
+
function tracker(elements: Map<Element, Slide>, slide: Slide) {
|
|
271
|
+
let last: Element | undefined;
|
|
272
|
+
return (element?: Element) => {
|
|
273
|
+
if (last) elements.delete(last);
|
|
274
|
+
last = element;
|
|
275
|
+
if (element) elements.set(element, slide);
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The carousel around the Story cards: which Story has the highlight, the
|
|
281
|
+
* layout, the arrows, swipes and the automatic advance. Call reset() when
|
|
282
|
+
* the Stories change, and render() from the host's render.
|
|
283
|
+
*
|
|
284
|
+
* The highlight moves on its own every AUTO_ADVANCE_MS, except while the
|
|
285
|
+
* pointer is over the carousel, the keyboard focus is in it, it is locked,
|
|
286
|
+
* or the host is off the page.
|
|
287
|
+
*/
|
|
288
|
+
export class CarouselController implements ReactiveController {
|
|
289
|
+
/**
|
|
290
|
+
* Steps taken since the Stories loaded, back and forth, not wrapped: the
|
|
291
|
+
* rolling layout keys its slides by it.
|
|
292
|
+
*/
|
|
293
|
+
private position = 0;
|
|
294
|
+
/** Stories moved by the last step: longer jumps animate a bit slower. */
|
|
295
|
+
private distance = 1;
|
|
296
|
+
private count = 0;
|
|
297
|
+
private narrow = false;
|
|
298
|
+
private media?: MediaQueryList;
|
|
299
|
+
private connected = false;
|
|
300
|
+
private hovered = false;
|
|
301
|
+
private focused = false;
|
|
302
|
+
private timer?: ReturnType<typeof setInterval>;
|
|
303
|
+
private swipe?: { x: number; moved: boolean };
|
|
304
|
+
/** The rendered cards, as their cardRef reported them. */
|
|
305
|
+
private readonly cards = new Map<Element, Slide>();
|
|
306
|
+
/** The rendered cards' focus targets, as their focusRef reported them. */
|
|
307
|
+
private readonly focusTargets = new Map<Element, Slide>();
|
|
308
|
+
|
|
309
|
+
constructor(
|
|
310
|
+
private readonly host: ReactiveControllerHost,
|
|
311
|
+
private readonly options: CarouselOptions,
|
|
312
|
+
) {
|
|
313
|
+
host.addController(this);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Index of the highlighted Story. */
|
|
317
|
+
get active() {
|
|
318
|
+
return this.count ? wrap(this.position, this.count) : 0;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
get layout(): CarouselLayout {
|
|
322
|
+
if (this.count < 2) return "solo";
|
|
323
|
+
return this.count === 2 && !this.narrow ? "pair" : "rolling";
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Key (see Slide) of the highlighted slide. */
|
|
327
|
+
get activeKey() {
|
|
328
|
+
if (this.layout === "rolling") return this.position;
|
|
329
|
+
return this.active;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The cards on screen, with their slide: not the hidden ones kept for the
|
|
334
|
+
* slide in.
|
|
335
|
+
*/
|
|
336
|
+
*visibleCards(): Generator<readonly [card: Element, slide: Slide]> {
|
|
337
|
+
for (const [card, slide] of this.cards) {
|
|
338
|
+
if (!slide.hidden) yield [card, slide];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Moves the keyboard focus to the highlighted card, once rendered. */
|
|
343
|
+
focusActive() {
|
|
344
|
+
for (const [target, slide] of this.focusTargets) {
|
|
345
|
+
if (slide.active && target instanceof HTMLElement) {
|
|
346
|
+
target.focus();
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
hostConnected() {
|
|
353
|
+
this.connected = true;
|
|
354
|
+
this.media = globalThis.matchMedia?.(NARROW_SCREEN.toString());
|
|
355
|
+
this.media?.addEventListener("change", this.onMediaChange);
|
|
356
|
+
this.narrow = this.media?.matches ?? false;
|
|
357
|
+
this.syncTimer();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
hostDisconnected() {
|
|
361
|
+
this.connected = false;
|
|
362
|
+
this.media?.removeEventListener("change", this.onMediaChange);
|
|
363
|
+
this.media = undefined;
|
|
364
|
+
this.endSwipe();
|
|
365
|
+
this.syncTimer();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Whatever the host changed (the player opening, new Stories) may start or
|
|
369
|
+
// stop the automatic advance.
|
|
370
|
+
hostUpdated() {
|
|
371
|
+
this.syncTimer();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** For a new list of Stories: the first one gets the highlight. */
|
|
375
|
+
reset(count: number) {
|
|
376
|
+
this.count = count;
|
|
377
|
+
this.position = 0;
|
|
378
|
+
this.distance = 1;
|
|
379
|
+
// The carousel may leave the page now (the element renders nothing while
|
|
380
|
+
// it loads). Removing the element with the pointer or focus doesn't fire
|
|
381
|
+
// pointerleave or focusout, and the new one would never advance.
|
|
382
|
+
this.hovered = false;
|
|
383
|
+
this.focused = false;
|
|
384
|
+
this.restartTimer();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Moves the highlight by `delta` Stories, wrapping around the ends. While
|
|
389
|
+
* locked, only with `evenIfLocked` (for example, the player stepping to
|
|
390
|
+
* the next Story it shows).
|
|
391
|
+
*/
|
|
392
|
+
step(delta: number, { evenIfLocked = false } = {}) {
|
|
393
|
+
if (this.count < 2 || delta === 0) return;
|
|
394
|
+
if (this.options.locked?.() && !evenIfLocked) return;
|
|
395
|
+
|
|
396
|
+
const from = this.activeKey;
|
|
397
|
+
this.position += delta;
|
|
398
|
+
this.distance = Math.abs(delta);
|
|
399
|
+
|
|
400
|
+
// The update is requested first, so onStep can wait for the render that
|
|
401
|
+
// shows the step (the host's updateComplete).
|
|
402
|
+
this.host.requestUpdate();
|
|
403
|
+
this.options.onStep?.(from, this.activeKey);
|
|
404
|
+
// A step by hand gives the new Story its full time.
|
|
405
|
+
this.restartTimer();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private get shouldAdvance() {
|
|
409
|
+
return (
|
|
410
|
+
this.connected &&
|
|
411
|
+
this.count > 1 &&
|
|
412
|
+
!this.hovered &&
|
|
413
|
+
!this.focused &&
|
|
414
|
+
!this.options.locked?.()
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private syncTimer() {
|
|
419
|
+
if (this.shouldAdvance && !this.timer) {
|
|
420
|
+
this.timer = setInterval(() => this.step(1), AUTO_ADVANCE_MS);
|
|
421
|
+
} else if (!this.shouldAdvance && this.timer) {
|
|
422
|
+
this.stopTimer();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private stopTimer() {
|
|
427
|
+
clearInterval(this.timer);
|
|
428
|
+
this.timer = undefined;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private restartTimer() {
|
|
432
|
+
this.stopTimer();
|
|
433
|
+
this.syncTimer();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private readonly onMediaChange = (event: MediaQueryListEvent) => {
|
|
437
|
+
const layout = this.layout;
|
|
438
|
+
this.narrow = event.matches;
|
|
439
|
+
if (this.layout === layout) return;
|
|
440
|
+
|
|
441
|
+
this.options.onLayoutChange?.();
|
|
442
|
+
this.host.requestUpdate();
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
private readonly onPointerEnter = () => {
|
|
446
|
+
this.hovered = true;
|
|
447
|
+
this.syncTimer();
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
private readonly onPointerLeave = () => {
|
|
451
|
+
this.hovered = false;
|
|
452
|
+
this.syncTimer();
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// Only keyboard focus stops the advance: a click also focuses the button
|
|
456
|
+
// it hits, and the advance would then stay stopped after the pointer left.
|
|
457
|
+
private readonly onFocusIn = (event: FocusEvent) => {
|
|
458
|
+
this.focused = (event.target as Element).matches(":focus-visible");
|
|
459
|
+
this.syncTimer();
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
private readonly onFocusOut = (event: FocusEvent) => {
|
|
463
|
+
const carousel = event.currentTarget as Element;
|
|
464
|
+
const next = event.relatedTarget as Node | null;
|
|
465
|
+
if (!next || !carousel.contains(next)) this.focused = false;
|
|
466
|
+
this.syncTimer();
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// The release is listened for on the window: a mouse drag can end over an
|
|
470
|
+
// arrow or off the carousel, where the track never gets the pointerup.
|
|
471
|
+
private readonly onPointerDown = (event: PointerEvent) => {
|
|
472
|
+
this.endSwipe();
|
|
473
|
+
this.swipe = { x: event.clientX, moved: false };
|
|
474
|
+
globalThis.addEventListener("pointerup", this.onPointerUp);
|
|
475
|
+
globalThis.addEventListener("pointercancel", this.onPointerCancel);
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
private readonly onPointerUp = (event: PointerEvent) => {
|
|
479
|
+
const { swipe } = this;
|
|
480
|
+
this.removeSwipeListeners();
|
|
481
|
+
if (!swipe) return;
|
|
482
|
+
|
|
483
|
+
const distance = event.clientX - swipe.x;
|
|
484
|
+
if (Math.abs(distance) > SWIPE_DISTANCE) {
|
|
485
|
+
swipe.moved = true;
|
|
486
|
+
this.step(distance < 0 ? 1 : -1);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
private readonly onPointerCancel = () => this.endSwipe();
|
|
491
|
+
|
|
492
|
+
private endSwipe() {
|
|
493
|
+
this.removeSwipeListeners();
|
|
494
|
+
this.swipe = undefined;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private removeSwipeListeners() {
|
|
498
|
+
globalThis.removeEventListener("pointerup", this.onPointerUp);
|
|
499
|
+
globalThis.removeEventListener("pointercancel", this.onPointerCancel);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// A swipe ends with a click on the card under the pointer: it must not
|
|
503
|
+
// open the player or select that card.
|
|
504
|
+
private readonly onClickCapture = {
|
|
505
|
+
handleEvent: (event: Event) => {
|
|
506
|
+
if (this.swipe?.moved) {
|
|
507
|
+
event.stopPropagation();
|
|
508
|
+
event.preventDefault();
|
|
509
|
+
}
|
|
510
|
+
this.swipe = undefined;
|
|
511
|
+
},
|
|
512
|
+
capture: true,
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
private slide(fields: Omit<Slide, "cardRef" | "focusRef">): Slide {
|
|
516
|
+
const slide: Slide = {
|
|
517
|
+
...fields,
|
|
518
|
+
cardRef: (element) => track(element),
|
|
519
|
+
focusRef: (element) => trackFocus(element),
|
|
520
|
+
};
|
|
521
|
+
const track = tracker(this.cards, slide);
|
|
522
|
+
const trackFocus = tracker(this.focusTargets, slide);
|
|
523
|
+
return slide;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Pair: both Stories, each keyed by its index. */
|
|
527
|
+
private pairSlides(): Slide[] {
|
|
528
|
+
const { active } = this;
|
|
529
|
+
return [0, 1].map((index) =>
|
|
530
|
+
this.slide({
|
|
531
|
+
key: index,
|
|
532
|
+
index,
|
|
533
|
+
offset: index - active,
|
|
534
|
+
active: index === active,
|
|
535
|
+
hidden: false,
|
|
536
|
+
}),
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Rolling: the slides around the highlighted one, keyed by their position.
|
|
542
|
+
* Up to VISIBLE_SIDE show on each side, fewer when there aren't enough
|
|
543
|
+
* Stories to fill both sides without repeating: half on each side, the
|
|
544
|
+
* extra one after it. With four, the Story opposite the highlighted one
|
|
545
|
+
* shows at both ends; with two (narrow screens), the other one shows on
|
|
546
|
+
* both sides.
|
|
547
|
+
*/
|
|
548
|
+
private rollingSlides(): Slide[] {
|
|
549
|
+
const { count } = this;
|
|
550
|
+
const even = count === 2 || count === 4;
|
|
551
|
+
const before = even ? count / 2 : Math.floor((count - 1) / 2);
|
|
552
|
+
const after = even ? count / 2 : count - 1 - before;
|
|
553
|
+
|
|
554
|
+
const slides: Slide[] = [];
|
|
555
|
+
for (let offset = -RENDERED_SIDE; offset <= RENDERED_SIDE; offset += 1) {
|
|
556
|
+
slides.push(
|
|
557
|
+
this.slide({
|
|
558
|
+
key: this.position + offset,
|
|
559
|
+
index: wrap(this.position + offset, count),
|
|
560
|
+
offset,
|
|
561
|
+
active: offset === 0,
|
|
562
|
+
hidden:
|
|
563
|
+
Math.abs(offset) > VISIBLE_SIDE ||
|
|
564
|
+
offset < -before ||
|
|
565
|
+
offset > after,
|
|
566
|
+
}),
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
return slides;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
private renderSlides<T>(items: readonly T[], renderSlide: RenderSlide<T>) {
|
|
573
|
+
const { layout } = this;
|
|
574
|
+
const slides = layout === "pair" ? this.pairSlides() : this.rollingSlides();
|
|
575
|
+
|
|
576
|
+
// The layout is part of the key: the same key in another layout is a new
|
|
577
|
+
// card, with a new video and controller (see onLayoutChange).
|
|
578
|
+
return repeat(
|
|
579
|
+
slides,
|
|
580
|
+
(slide) => `${layout}:${slide.key}`,
|
|
581
|
+
(slide) => {
|
|
582
|
+
const { index } = slide;
|
|
583
|
+
return html`
|
|
584
|
+
<div
|
|
585
|
+
class="slide"
|
|
586
|
+
data-index=${index}
|
|
587
|
+
?data-active=${slide.active}
|
|
588
|
+
?data-hidden=${slide.hidden}
|
|
589
|
+
?inert=${slide.hidden}
|
|
590
|
+
style=${styleMap({ "--place": String(slide.offset) })}
|
|
591
|
+
>
|
|
592
|
+
${renderSlide(items[index], slide)}
|
|
593
|
+
</div>
|
|
594
|
+
`;
|
|
595
|
+
},
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* One item: the card centered over the background. More: the cards in the
|
|
601
|
+
* pair or rolling layout, with the arrows.
|
|
602
|
+
*/
|
|
603
|
+
render<T>(
|
|
604
|
+
items: readonly T[],
|
|
605
|
+
renderSlide: RenderSlide<T>,
|
|
606
|
+
labels: StoryLabels,
|
|
607
|
+
) {
|
|
608
|
+
if (this.layout === "solo") {
|
|
609
|
+
return html`
|
|
610
|
+
<div class="carousel" data-layout="solo">
|
|
611
|
+
${renderSlide(
|
|
612
|
+
items[0],
|
|
613
|
+
this.slide({
|
|
614
|
+
key: 0,
|
|
615
|
+
index: 0,
|
|
616
|
+
offset: 0,
|
|
617
|
+
active: true,
|
|
618
|
+
hidden: false,
|
|
619
|
+
}),
|
|
620
|
+
)}
|
|
621
|
+
</div>
|
|
622
|
+
`;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const locked = this.options.locked?.() ?? false;
|
|
626
|
+
|
|
627
|
+
return html`
|
|
628
|
+
<div
|
|
629
|
+
class="carousel"
|
|
630
|
+
data-layout=${this.layout}
|
|
631
|
+
role="region"
|
|
632
|
+
aria-roledescription=${labels.carouselRole}
|
|
633
|
+
aria-label=${labels.carousel}
|
|
634
|
+
@pointerenter=${this.onPointerEnter}
|
|
635
|
+
@pointerleave=${this.onPointerLeave}
|
|
636
|
+
@focusin=${this.onFocusIn}
|
|
637
|
+
@focusout=${this.onFocusOut}
|
|
638
|
+
>
|
|
639
|
+
<div
|
|
640
|
+
class="track"
|
|
641
|
+
style=${styleMap({
|
|
642
|
+
"--slide-duration": this.distance > 1 ? "0.52s" : "0.4s",
|
|
643
|
+
})}
|
|
644
|
+
@pointerdown=${this.onPointerDown}
|
|
645
|
+
@click=${this.onClickCapture}
|
|
646
|
+
>
|
|
647
|
+
${this.renderSlides(items, renderSlide)}
|
|
648
|
+
</div>
|
|
649
|
+
<button
|
|
650
|
+
type="button"
|
|
651
|
+
class="arrow arrow-previous"
|
|
652
|
+
aria-label=${labels.previous}
|
|
653
|
+
?disabled=${locked}
|
|
654
|
+
@click=${() => this.step(-1)}
|
|
655
|
+
>
|
|
656
|
+
${icon("previous")}
|
|
657
|
+
</button>
|
|
658
|
+
<button
|
|
659
|
+
type="button"
|
|
660
|
+
class="arrow arrow-next"
|
|
661
|
+
aria-label=${labels.next}
|
|
662
|
+
?disabled=${locked}
|
|
663
|
+
@click=${() => this.step(1)}
|
|
664
|
+
>
|
|
665
|
+
${icon("next")}
|
|
666
|
+
</button>
|
|
667
|
+
</div>
|
|
668
|
+
`;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export {
|
|
2
|
+
AUTO_ADVANCE_MS,
|
|
3
|
+
CarouselController,
|
|
4
|
+
carouselStyles,
|
|
5
|
+
NARROW_SCREEN,
|
|
6
|
+
RENDERED_SIDE,
|
|
7
|
+
SWIPE_DISTANCE,
|
|
8
|
+
VISIBLE_SIDE,
|
|
9
|
+
} from "./carousel";
|
|
10
|
+
export type {
|
|
11
|
+
CarouselLayout,
|
|
12
|
+
CarouselOptions,
|
|
13
|
+
RenderSlide,
|
|
14
|
+
Slide,
|
|
15
|
+
} from "./carousel";
|