@vtex/ads-stories 0.2.0 → 0.3.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.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M152,96V48a8,8,0,0,1,16,0V88h40a8,8,0,0,1,0,16H160A8,8,0,0,1,152,96ZM96,152H48a8,8,0,0,0,0,16H88v40a8,8,0,0,0,16,0V160A8,8,0,0,0,96,152Zm112,0H160a8,8,0,0,0-8,8v48a8,8,0,0,0,16,0V168h40a8,8,0,0,0,0-16ZM96,40a8,8,0,0,0-8,8V88H48a8,8,0,0,0,0,16H96a8,8,0,0,0,8-8V48A8,8,0,0,0,96,40Z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48V88a8,8,0,0,1-16,0V56H168a8,8,0,0,1,0-16h40A8,8,0,0,1,216,48ZM88,200H56V168a8,8,0,0,0-16,0v40a8,8,0,0,0,8,8H88a8,8,0,0,0,0-16Zm120-40a8,8,0,0,0-8,8v32H168a8,8,0,0,0,0,16h40a8,8,0,0,0,8-8V168A8,8,0,0,0,208,160ZM88,40H48a8,8,0,0,0-8,8V88a8,8,0,0,0,16,0V56H88a8,8,0,0,0,0-16Z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M240,128a15.74,15.74,0,0,1-7.6,13.51L88.32,229.65a16,16,0,0,1-16.2.3A15.86,15.86,0,0,1,64,216.13V39.87a15.86,15.86,0,0,1,8.12-13.82,16,16,0,0,1,16.2.3L232.4,114.49A15.74,15.74,0,0,1,240,128Z"/></svg>
@@ -0,0 +1,25 @@
1
+ /*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */
2
+
3
+ // SVG files as downloaded from Phosphor: regular weight, or the weight in the
4
+ // file name. They are bundled as text: `?raw` is handled by Vite in dev and
5
+ // tests, and by the raw plugin in tsup.config.ts.
6
+ import close from "./assets/close.svg?raw";
7
+ import collapse from "./assets/collapse.svg?raw";
8
+ import expand from "./assets/expand.svg?raw";
9
+ import play from "./assets/play-fill.svg?raw";
10
+
11
+ export const icons = { close, collapse, expand, play };
12
+
13
+ export type IconName = keyof typeof icons;
14
+
15
+ /** Renders a bundled icon. It is decorative: name the control around it. */
16
+ export function createIcon(name: IconName) {
17
+ const template = document.createElement("template");
18
+ template.innerHTML = icons[name];
19
+
20
+ const svg = template.content.firstElementChild as SVGSVGElement;
21
+ svg.classList.add("icon");
22
+ svg.setAttribute("aria-hidden", "true");
23
+ svg.setAttribute("focusable", "false");
24
+ return svg;
25
+ }
@@ -0,0 +1,2 @@
1
+ export { createPlayer, playerStyles, showPlayer } from "./player";
2
+ export type { PlayerOptions } from "./player";
@@ -0,0 +1,254 @@
1
+ import { createCard } from "../card";
2
+ import { createIcon, type IconName } from "../icons";
3
+ import type { StoryAd } from "../../types";
4
+
5
+ // The player is a <dialog> shown in the top layer, so no store z-index,
6
+ // overflow or transform can hide or move it.
7
+ //
8
+ // Mini: a manual popover (non-modal, the page stays usable), with the card at
9
+ // the bottom right and the controls above it. Without popover support it is a
10
+ // non-modal dialog, fixed with a high z-index.
11
+ // Expanded: a modal dialog (showModal), so the browser makes the page behind
12
+ // it inert. The card is centered over a dark backdrop, with the controls at
13
+ // the top right of the screen. On narrow screens it fills the screen.
14
+ export const playerStyles = `
15
+ .player {
16
+ position: fixed;
17
+ inset: auto 16px 16px auto;
18
+ z-index: 2147483000;
19
+ box-sizing: border-box;
20
+ width: calc(min(587px, 100vh - 80px) * 9 / 16);
21
+ height: min(587px, 100vh - 80px);
22
+ margin: 0;
23
+ padding: 0;
24
+ overflow: visible;
25
+ border: 0;
26
+ background: transparent;
27
+ color: inherit;
28
+ font: inherit;
29
+ animation: player-in 0.25s ease;
30
+ }
31
+
32
+ /* A <dialog> shown as a popover has no open attribute, which the browser
33
+ hides by default. */
34
+ .player:popover-open {
35
+ display: block;
36
+ }
37
+
38
+ /* Switching modes shows the dialog again; only the first opening fades in. */
39
+ .player[data-switched] {
40
+ animation: none;
41
+ }
42
+
43
+ .player::backdrop {
44
+ background: transparent;
45
+ }
46
+
47
+ .player-frame {
48
+ position: relative;
49
+ width: 100%;
50
+ height: 100%;
51
+ }
52
+
53
+ .player .card {
54
+ width: 100%;
55
+ height: 100%;
56
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
57
+ }
58
+
59
+ /* Above the card: on narrow screens the expanded card can cover them. */
60
+ .player-controls {
61
+ position: absolute;
62
+ right: 0;
63
+ bottom: calc(100% + 8px);
64
+ z-index: 1;
65
+ display: flex;
66
+ gap: 8px;
67
+ }
68
+
69
+ .player-button {
70
+ display: grid;
71
+ width: 36px;
72
+ height: 36px;
73
+ place-items: center;
74
+ padding: 0;
75
+ border: 1px solid rgba(133, 133, 133, 0.3);
76
+ border-radius: 50%;
77
+ background: rgba(0, 0, 0, 0.32);
78
+ backdrop-filter: blur(5px);
79
+ color: #fff;
80
+ cursor: pointer;
81
+ transition: background-color 0.2s;
82
+ }
83
+
84
+ .player-button:hover {
85
+ background: rgba(0, 0, 0, 0.55);
86
+ }
87
+
88
+ .player-button .icon {
89
+ width: 18px;
90
+ height: 18px;
91
+ }
92
+
93
+ .player[data-expanded] {
94
+ inset: 0;
95
+ display: grid;
96
+ width: 100%;
97
+ max-width: none;
98
+ height: 100%;
99
+ max-height: none;
100
+ place-items: center;
101
+ background: rgba(0, 0, 0, 0.85);
102
+ animation: none;
103
+ }
104
+
105
+ .player[data-expanded] .player-frame {
106
+ width: calc(min(78vh, 720px) * 9 / 16);
107
+ height: min(78vh, 720px);
108
+ }
109
+
110
+ .player[data-expanded] .player-controls {
111
+ position: fixed;
112
+ top: 24px;
113
+ right: 24px;
114
+ bottom: auto;
115
+ }
116
+
117
+ .player[data-expanded] .player-button {
118
+ width: 40px;
119
+ height: 40px;
120
+ }
121
+
122
+ @media (max-width: 576px) {
123
+ .player {
124
+ right: 12px;
125
+ width: 220px;
126
+ height: 391px;
127
+ }
128
+
129
+ .player[data-expanded] {
130
+ background: #000;
131
+ }
132
+
133
+ /* The video stays 9:16 and centered; the rest of the screen is black. */
134
+ .player[data-expanded] .player-frame {
135
+ width: min(100vw, 100vh * 9 / 16);
136
+ height: min(100vh, 100vw * 16 / 9);
137
+ }
138
+
139
+ .player[data-expanded] .card {
140
+ border-radius: 0;
141
+ }
142
+
143
+ .player[data-expanded] .player-controls {
144
+ top: 12px;
145
+ right: 12px;
146
+ }
147
+ }
148
+
149
+ @keyframes player-in {
150
+ from {
151
+ opacity: 0;
152
+ transform: translateY(8px);
153
+ }
154
+ }
155
+
156
+ @media (prefers-reduced-motion: reduce) {
157
+ .player {
158
+ animation: none;
159
+ }
160
+ }
161
+ `;
162
+
163
+ export interface PlayerOptions {
164
+ /** Accessible name of the player. */
165
+ label: string;
166
+ /** Accessible names of the buttons. */
167
+ expandLabel: string;
168
+ collapseLabel: string;
169
+ closeLabel: string;
170
+ onClose: () => void;
171
+ }
172
+
173
+ function createButton(className: string, label: string, icon: IconName) {
174
+ const button = document.createElement("button");
175
+ button.type = "button";
176
+ button.className = `player-button ${className}`;
177
+ button.setAttribute("aria-label", label);
178
+ button.append(createIcon(icon));
179
+ return button;
180
+ }
181
+
182
+ function supportsPopover(player: HTMLDialogElement) {
183
+ return typeof player.showPopover === "function";
184
+ }
185
+
186
+ /** Shows the player as the mini player. It must already be in the page. */
187
+ export function showPlayer(player: HTMLDialogElement) {
188
+ if (supportsPopover(player)) player.showPopover();
189
+ else player.show();
190
+ }
191
+
192
+ /**
193
+ * The Story player. It opens as the mini player (see showPlayer); expand
194
+ * switches to the expanded view, a modal dialog, and back. Clicking the
195
+ * backdrop of the expanded view or pressing Escape in it closes the player.
196
+ */
197
+ export function createPlayer(story: StoryAd, options: PlayerOptions) {
198
+ const player = document.createElement("dialog");
199
+ player.className = "player";
200
+ player.setAttribute("popover", "manual");
201
+ player.setAttribute("aria-label", options.label);
202
+
203
+ const frame = document.createElement("div");
204
+ frame.className = "player-frame";
205
+
206
+ const controls = document.createElement("div");
207
+ controls.className = "player-controls";
208
+
209
+ const expand = createButton("player-expand", options.expandLabel, "expand");
210
+ const close = createButton("player-close", options.closeLabel, "close");
211
+ controls.append(expand, close);
212
+
213
+ let expanded = false;
214
+
215
+ // A dialog can't be shown as a popover and as a modal at the same time, so
216
+ // switching modes hides it first, the same way it was shown.
217
+ function hide() {
218
+ if (!expanded && supportsPopover(player)) player.hidePopover();
219
+ else player.close();
220
+ }
221
+
222
+ expand.addEventListener("click", () => {
223
+ hide();
224
+ expanded = !expanded;
225
+ player.toggleAttribute("data-expanded", expanded);
226
+ player.dataset.switched = "";
227
+ expand.setAttribute(
228
+ "aria-label",
229
+ expanded ? options.collapseLabel : options.expandLabel,
230
+ );
231
+ expand.replaceChildren(createIcon(expanded ? "collapse" : "expand"));
232
+
233
+ if (expanded) player.showModal();
234
+ else showPlayer(player);
235
+ expand.focus();
236
+ });
237
+
238
+ close.addEventListener("click", options.onClose);
239
+
240
+ // Clicks on the backdrop of the expanded view reach the dialog itself.
241
+ player.addEventListener("click", (event) => {
242
+ if (expanded && event.target === player) options.onClose();
243
+ });
244
+
245
+ // Escape on a modal dialog would only hide it: close the player instead.
246
+ player.addEventListener("cancel", (event) => {
247
+ event.preventDefault();
248
+ options.onClose();
249
+ });
250
+
251
+ frame.append(controls, createCard(story));
252
+ player.append(frame);
253
+ return player;
254
+ }
package/src/raw.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ // `import text from "./file.svg?raw"` gives the file contents as a string.
2
+ declare module "*?raw" {
3
+ const content: string;
4
+ export default content;
5
+ }
package/src/react.ts CHANGED
@@ -13,10 +13,7 @@ import type { VtexAdsStoryElement } from "./story";
13
13
  export type VtexAdsStoryProps = DetailedHTMLProps<
14
14
  HTMLAttributes<VtexAdsStoryElement>,
15
15
  VtexAdsStoryElement
16
- > & {
17
- /** Name shown in the greeting; falls back to "world" when empty. */
18
- name?: string;
19
- };
16
+ >;
20
17
 
21
18
  declare module "react" {
22
19
  // eslint-disable-next-line @typescript-eslint/no-namespace
@@ -1,3 +1,7 @@
1
+ import { cardStyles, createCard } from "../components/card";
2
+ import { createPlayer, playerStyles, showPlayer } from "../components/player";
3
+ import type { StoryAd } from "../types";
4
+
1
5
  export const TAG_NAME = "vtex-ads-story";
2
6
 
3
7
  // Keeps the module importable where HTMLElement doesn't exist (SSR, Node).
@@ -9,54 +13,84 @@ const styles = `
9
13
  font-family: var(--vtex-ads-story-font-family, inherit);
10
14
  color: var(--vtex-ads-story-color-text, inherit);
11
15
  }
16
+ ${cardStyles}
17
+ ${playerStyles}
12
18
  `;
13
19
 
14
- const DEFAULT_NAME = "world";
20
+ // Temporary: rendered until the element reads Stories from the ad API.
21
+ const PLACEHOLDER_STORY: StoryAd = {
22
+ ad_id: "placeholder",
23
+ poster_url: "https://dummyimage.com/1080x1920/e31c57/fff",
24
+ };
25
+
26
+ // Temporary: fixed texts until the store can set them by attribute.
27
+ const LABELS = {
28
+ watch: "Assistir vídeo",
29
+ player: "Vídeo",
30
+ expand: "Expandir vídeo",
31
+ collapse: "Reduzir vídeo",
32
+ close: "Fechar vídeo",
33
+ };
15
34
 
16
35
  export class VtexAdsStoryElement extends BaseElement {
17
- static get observedAttributes() {
18
- return ["name"];
19
- }
36
+ private player: HTMLDialogElement | null = null;
20
37
 
21
- get name(): string {
22
- return this.getAttribute("name")?.trim() ?? "";
38
+ private readonly onKeyDown = (event: KeyboardEvent) => {
39
+ if (event.key === "Escape") this.closePlayer();
40
+ };
41
+
42
+ private render() {
43
+ const container = this.shadowRoot?.querySelector(".root");
44
+ if (!container) return;
45
+
46
+ container.replaceChildren(
47
+ createCard(PLACEHOLDER_STORY, {
48
+ label: LABELS.watch,
49
+ onSelect: () => this.openPlayer(),
50
+ }),
51
+ );
23
52
  }
24
53
 
25
- set name(value: string) {
26
- this.setAttribute("name", value);
54
+ private openPlayer() {
55
+ if (this.player || !this.shadowRoot) return;
56
+
57
+ this.player = createPlayer(PLACEHOLDER_STORY, {
58
+ label: LABELS.player,
59
+ expandLabel: LABELS.expand,
60
+ collapseLabel: LABELS.collapse,
61
+ closeLabel: LABELS.close,
62
+ onClose: () => this.closePlayer(),
63
+ });
64
+ this.shadowRoot.append(this.player);
65
+ showPlayer(this.player);
66
+ document.addEventListener("keydown", this.onKeyDown);
67
+
68
+ this.player.querySelector<HTMLElement>(".player-close")?.focus();
27
69
  }
28
70
 
29
- private render() {
30
- const container = this.shadowRoot?.querySelector("[part='root']");
31
- if (!container) return;
71
+ private closePlayer({ restoreFocus = true } = {}) {
72
+ if (!this.player) return;
73
+
74
+ document.removeEventListener("keydown", this.onKeyDown);
75
+ this.player.remove();
76
+ this.player = null;
32
77
 
33
- // textContent keeps the name as plain text (no HTML injection).
34
- container.textContent = `Hello ${this.name || DEFAULT_NAME}!`;
78
+ if (restoreFocus) {
79
+ this.shadowRoot?.querySelector<HTMLElement>(".root .card")?.focus();
80
+ }
35
81
  }
36
82
 
37
83
  connectedCallback() {
38
84
  if (!this.shadowRoot) {
39
85
  const root = this.attachShadow({ mode: "open" });
40
- root.innerHTML = `<style>${styles}</style><div part="root"></div>`;
86
+ root.innerHTML = `<style>${styles}</style><div class="root"></div>`;
41
87
  }
42
88
 
43
- this.upgradeProperty("name");
44
89
  this.render();
45
90
  }
46
91
 
47
- // A property set before the element is defined (e.g. while the CDN script
48
- // loads) becomes an own property that shadows the class getter/setter.
49
- // Remove it and set it again so it goes through the setter.
50
- private upgradeProperty(prop: "name") {
51
- if (!Object.prototype.hasOwnProperty.call(this, prop)) return;
52
-
53
- const value = this[prop];
54
- Reflect.deleteProperty(this, prop);
55
- this[prop] = value;
56
- }
57
-
58
- attributeChangedCallback() {
59
- this.render();
92
+ disconnectedCallback() {
93
+ this.closePlayer({ restoreFocus: false });
60
94
  }
61
95
  }
62
96
 
package/src/types.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * One Story from the ad API response. Field names follow the API; only the
3
+ * fields the component reads so far are listed.
4
+ */
5
+ export interface StoryAd {
6
+ ad_id: string;
7
+ /** Cover image shown before the video plays. */
8
+ poster_url?: string;
9
+ }