@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
package/dist/esm/react.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { DetailedHTMLProps, HTMLAttributes } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Channel } from '@vtex/ads-core';
|
|
3
|
+
import { TAG_NAME, VtexAdsStoryCarouselElement } from './index.js';
|
|
4
|
+
import 'lit';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
|
-
* 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.
|
|
6
9
|
*
|
|
7
10
|
* Opt in once per project, so non-React projects never need @types/react:
|
|
8
11
|
*
|
|
@@ -11,13 +14,51 @@ import { VtexAdsStoryElement } from './index.js';
|
|
|
11
14
|
* or add "@vtex/ads-stories/react" to `compilerOptions.types` in tsconfig.
|
|
12
15
|
*/
|
|
13
16
|
|
|
14
|
-
|
|
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
|
+
type VtexAdsStoryCarouselProps = DetailedHTMLProps<HTMLAttributes<VtexAdsStoryCarouselElement>, VtexAdsStoryCarouselElement> & VtexAdsStoryCarouselAttributes;
|
|
15
56
|
declare module "react" {
|
|
16
57
|
namespace JSX {
|
|
17
58
|
interface IntrinsicElements {
|
|
18
|
-
|
|
59
|
+
[TAG_NAME]: VtexAdsStoryCarouselProps;
|
|
19
60
|
}
|
|
20
61
|
}
|
|
21
62
|
}
|
|
22
63
|
|
|
23
|
-
export type {
|
|
64
|
+
export type { VtexAdsStoryCarouselAttributes, VtexAdsStoryCarouselProps };
|
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.5.0",
|
|
5
5
|
"description": "VTEX Ads Stories web component",
|
|
6
6
|
"main": "dist/cjs/index.cjs",
|
|
7
7
|
"module": "dist/esm/index.js",
|
|
@@ -60,18 +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
|
},
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"@vtex/ads-core": "^0.6.0",
|
|
74
|
+
"lit": "^3.3.3"
|
|
75
|
+
},
|
|
76
|
+
"msw": {
|
|
77
|
+
"workerDirectory": [
|
|
78
|
+
".storybook/public"
|
|
79
|
+
]
|
|
80
|
+
},
|
|
68
81
|
"scripts": {
|
|
82
|
+
"prebuild": "pnpm --filter @vtex/ads-core run build",
|
|
69
83
|
"build": "tsup",
|
|
70
84
|
"build:watch": "tsup --watch",
|
|
71
85
|
"clean": "rimraf dist",
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
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",
|
|
75
92
|
"test": "vitest run",
|
|
76
93
|
"test:watch": "vitest --coverage.enabled=false"
|
|
77
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";
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { css, html, nothing } from "lit";
|
|
2
|
+
import { ref, type RefOrCallback } from "lit/directives/ref.js";
|
|
3
|
+
import type { StoryLabels } from "../../locales";
|
|
4
|
+
import { icon } from "../icons";
|
|
5
|
+
import { hasVideo, renderMedia, type VideoController } from "../media";
|
|
6
|
+
import {
|
|
7
|
+
renderCta,
|
|
8
|
+
renderProduct,
|
|
9
|
+
type CardCta,
|
|
10
|
+
type CardProduct,
|
|
11
|
+
} from "../product";
|
|
2
12
|
import type { StoryMedia } from "../../types";
|
|
3
13
|
|
|
4
|
-
export const cardStyles = `
|
|
14
|
+
export const cardStyles = css`
|
|
15
|
+
/* 9:16 at the height its container gives it (--card-height, default
|
|
16
|
+
500px). */
|
|
5
17
|
.card {
|
|
6
18
|
position: relative;
|
|
7
19
|
box-sizing: border-box;
|
|
8
|
-
width: calc(500px * 9 / 16);
|
|
9
|
-
height: 500px;
|
|
20
|
+
width: calc(var(--card-height, 500px) * 9 / 16);
|
|
21
|
+
height: var(--card-height, 500px);
|
|
10
22
|
overflow: hidden;
|
|
11
23
|
border-radius: var(--vtex-ads-story-radius, 20px);
|
|
12
24
|
background: #000;
|
|
@@ -114,120 +126,98 @@ export interface CardOptions {
|
|
|
114
126
|
label: string;
|
|
115
127
|
/** Called on click, Enter or Space. */
|
|
116
128
|
onSelect: () => void;
|
|
117
|
-
/**
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
+
/** The texts (the sound button's, shown when the card has a video). */
|
|
135
|
+
labels: StoryLabels;
|
|
136
|
+
/** The Story's product, shown at the bottom of the card. */
|
|
137
|
+
product?: CardProduct;
|
|
138
|
+
/** Called by a click on the product (default onSelect). */
|
|
139
|
+
onProductSelect?: () => void;
|
|
140
|
+
/** The Story's call to action, for a link destination (no product). */
|
|
141
|
+
cta?: CardCta;
|
|
142
|
+
/**
|
|
143
|
+
* Called by a click on the call to action instead of following the link
|
|
144
|
+
* (a card without the highlight). Without it, the link opens.
|
|
145
|
+
*/
|
|
146
|
+
onCtaSelect?: () => void;
|
|
147
|
+
/**
|
|
148
|
+
* Called by any click on the card, its product, call to action and sound
|
|
149
|
+
* button included, before what that click does.
|
|
150
|
+
*/
|
|
151
|
+
onPress?: () => void;
|
|
152
|
+
/** Receives the card's element (see Slide's cardRef). */
|
|
153
|
+
ref?: RefOrCallback;
|
|
154
|
+
/** Receives the button that takes the focus (see Slide's focusRef). */
|
|
155
|
+
openRef?: RefOrCallback;
|
|
138
156
|
}
|
|
139
157
|
|
|
140
158
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
159
|
+
* Sound on/off for the card's video, which starts muted. Only shown while the
|
|
160
|
+
* video plays: a video that never plays (reduced motion, or autoplay blocked
|
|
161
|
+
* as in iOS Low Power Mode) has no sound to turn on, and the card paused
|
|
162
|
+
* behind the open player needs none.
|
|
143
163
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
// No autoplay for people who asked for reduced motion.
|
|
158
|
-
video.autoplay = !prefersReducedMotion();
|
|
159
|
-
if (poster) video.poster = poster;
|
|
160
|
-
video.src = src;
|
|
161
|
-
|
|
162
|
-
// If the video can't load, the cover underneath stays.
|
|
163
|
-
video.addEventListener("error", () => video.remove());
|
|
164
|
-
return video;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** Sound on/off for the card's video, which starts muted. */
|
|
168
|
-
function createSoundButton(video: HTMLVideoElement, options: CardOptions) {
|
|
169
|
-
const button = document.createElement("button");
|
|
170
|
-
button.type = "button";
|
|
171
|
-
button.className = "card-sound";
|
|
172
|
-
|
|
173
|
-
function sync() {
|
|
174
|
-
button.setAttribute(
|
|
175
|
-
"aria-label",
|
|
176
|
-
video.muted ? options.soundOnLabel : options.soundOffLabel,
|
|
177
|
-
);
|
|
178
|
-
button.replaceChildren(createIcon(video.muted ? "soundOff" : "soundOn"));
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
video.addEventListener("volumechange", sync);
|
|
182
|
-
// Without the video, the cover stays and there is no sound to control.
|
|
183
|
-
video.addEventListener("error", () => button.remove());
|
|
184
|
-
button.addEventListener("click", () => {
|
|
185
|
-
video.muted = !video.muted;
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
sync();
|
|
189
|
-
return button;
|
|
164
|
+
function renderSoundButton(video: VideoController, options: CardOptions) {
|
|
165
|
+
return html`
|
|
166
|
+
<button
|
|
167
|
+
type="button"
|
|
168
|
+
class="card-sound"
|
|
169
|
+
aria-label=${video.muted
|
|
170
|
+
? options.labels.soundOn
|
|
171
|
+
: options.labels.soundOff}
|
|
172
|
+
@click=${() => video.toggleMuted()}
|
|
173
|
+
>
|
|
174
|
+
${icon(video.muted ? "soundOff" : "soundOn")}
|
|
175
|
+
</button>
|
|
176
|
+
`;
|
|
190
177
|
}
|
|
191
178
|
|
|
192
179
|
/**
|
|
193
180
|
* A 9:16 card with the Story cover and, with a media_url, the video looping
|
|
194
|
-
* muted over it
|
|
195
|
-
* the card, and a
|
|
196
|
-
*
|
|
181
|
+
* muted over it (see renderMedia). A button with a play icon in the middle
|
|
182
|
+
* covers the card, and a sound button shows while the video plays. With a
|
|
183
|
+
* product, its pill sits at the bottom (see renderProduct); with a link
|
|
184
|
+
* destination, the call to action does (see renderCta). The sound, product
|
|
185
|
+
* and call to action are beside the card's button, not inside it: a button
|
|
186
|
+
* can't contain another.
|
|
197
187
|
*/
|
|
198
|
-
export function
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
188
|
+
export function renderCard(
|
|
189
|
+
story: StoryMedia,
|
|
190
|
+
video: VideoController,
|
|
191
|
+
options: CardOptions,
|
|
192
|
+
) {
|
|
193
|
+
const withVideo = options.withVideo ?? true;
|
|
194
|
+
|
|
195
|
+
return html`
|
|
196
|
+
<div
|
|
197
|
+
class="card"
|
|
198
|
+
${ref(options.ref)}
|
|
199
|
+
@click=${{ handleEvent: () => options.onPress?.(), capture: true }}
|
|
200
|
+
>
|
|
201
|
+
${renderMedia(story, video, { withVideo })}
|
|
202
|
+
<button
|
|
203
|
+
type="button"
|
|
204
|
+
class="card-open"
|
|
205
|
+
${ref(options.openRef)}
|
|
206
|
+
aria-label=${options.label}
|
|
207
|
+
@click=${options.onSelect}
|
|
208
|
+
>
|
|
209
|
+
<span class="play" aria-hidden="true">${icon("play")}</span>
|
|
210
|
+
</button>
|
|
211
|
+
${withVideo && hasVideo(story, video) && !video.paused
|
|
212
|
+
? renderSoundButton(video, options)
|
|
213
|
+
: nothing}
|
|
214
|
+
${options.product
|
|
215
|
+
? renderProduct(
|
|
216
|
+
options.product,
|
|
217
|
+
options.onProductSelect ?? options.onSelect,
|
|
218
|
+
)
|
|
219
|
+
: nothing}
|
|
220
|
+
${options.cta ? renderCta(options.cta, options.onCtaSelect) : nothing}
|
|
221
|
+
</div>
|
|
222
|
+
`;
|
|
233
223
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { cardStyles, renderCard } from "./card";
|
|
2
2
|
export type { CardOptions } from "./card";
|