@vtex/ads-stories 0.1.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,124 @@
1
+ import { createIcon } from "../icons";
2
+ import type { StoryAd } from "../../types";
3
+
4
+ export const cardStyles = `
5
+ .card {
6
+ position: relative;
7
+ box-sizing: border-box;
8
+ width: calc(500px * 9 / 16);
9
+ height: 500px;
10
+ overflow: hidden;
11
+ border-radius: var(--vtex-ads-story-radius, 20px);
12
+ background: #000;
13
+ }
14
+
15
+ .card[role="button"] {
16
+ cursor: pointer;
17
+ }
18
+
19
+ .poster {
20
+ position: absolute;
21
+ inset: 0;
22
+ width: 100%;
23
+ height: 100%;
24
+ object-fit: cover;
25
+ }
26
+
27
+ .play {
28
+ position: absolute;
29
+ top: 50%;
30
+ left: 50%;
31
+ display: grid;
32
+ width: 66px;
33
+ height: 66px;
34
+ place-items: center;
35
+ border-radius: 50%;
36
+ background:
37
+ linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),
38
+ rgba(255, 255, 255, 0.15);
39
+ backdrop-filter: blur(2px);
40
+ box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);
41
+ transform: translate(-50%, -50%);
42
+ transition:
43
+ opacity 0.3s ease,
44
+ transform 0.3s ease;
45
+ }
46
+
47
+ /* The icon's triangle is 18px tall and already sits slightly right of its
48
+ box, so it looks centered in the circle. */
49
+ .play .icon {
50
+ width: 22px;
51
+ height: 22px;
52
+ color: #fff;
53
+ }
54
+
55
+ /* With a mouse, the play button shows only on hover or keyboard focus.
56
+ Touch screens have no hover, so it stays visible there. */
57
+ @media (hover: hover) and (pointer: fine) {
58
+ .play {
59
+ opacity: 0;
60
+ transform: translate(-50%, -50%) scale(0.9);
61
+ }
62
+
63
+ .card:hover .play,
64
+ .card:focus-visible .play {
65
+ opacity: 1;
66
+ transform: translate(-50%, -50%);
67
+ }
68
+ }
69
+ `;
70
+
71
+ export interface CardOptions {
72
+ /** Accessible name of the card. */
73
+ label: string;
74
+ /** Called on click, Enter or Space. */
75
+ onSelect: () => void;
76
+ }
77
+
78
+ // Only absolute http(s) URLs reach the DOM.
79
+ function isHttpUrl(value: string) {
80
+ try {
81
+ const { protocol } = new URL(value);
82
+ return protocol === "https:" || protocol === "http:";
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * A 9:16 card with the Story cover. With options, the card is a button with
90
+ * a play icon in the middle.
91
+ */
92
+ export function createCard(story: StoryAd, options?: CardOptions) {
93
+ const card = document.createElement("div");
94
+ card.className = "card";
95
+
96
+ if (story.poster_url && isHttpUrl(story.poster_url)) {
97
+ const poster = document.createElement("img");
98
+ poster.className = "poster";
99
+ poster.alt = "";
100
+ poster.src = story.poster_url;
101
+ card.append(poster);
102
+ }
103
+
104
+ if (options) {
105
+ card.setAttribute("role", "button");
106
+ card.setAttribute("aria-label", options.label);
107
+ card.tabIndex = 0;
108
+
109
+ const play = document.createElement("span");
110
+ play.className = "play";
111
+ play.setAttribute("aria-hidden", "true");
112
+ play.append(createIcon("play"));
113
+ card.append(play);
114
+
115
+ card.addEventListener("click", options.onSelect);
116
+ card.addEventListener("keydown", (event) => {
117
+ if (event.key !== "Enter" && event.key !== " ") return;
118
+ event.preventDefault();
119
+ options.onSelect();
120
+ });
121
+ }
122
+
123
+ return card;
124
+ }
@@ -0,0 +1,2 @@
1
+ export { createCard, cardStyles } from "./card";
2
+ export type { CardOptions } from "./card";
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Phosphor Icons
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M205.66,194.34a8,8,0,0,1-11.32,11.32L128,139.31,61.66,205.66a8,8,0,0,1-11.32-11.32L116.69,128,50.34,61.66A8,8,0,0,1,61.66,50.34L128,116.69l66.34-66.35a8,8,0,0,1,11.32,11.32L139.31,128Z"/></svg>
@@ -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 ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * React JSX types for <vtex-ads-story>. Types only: the JS output is empty.
3
+ *
4
+ * Opt in once per project, so non-React projects never need @types/react:
5
+ *
6
+ * import type {} from "@vtex/ads-stories/react";
7
+ *
8
+ * or add "@vtex/ads-stories/react" to `compilerOptions.types` in tsconfig.
9
+ */
10
+ import type { DetailedHTMLProps, HTMLAttributes } from "react";
11
+ import type { VtexAdsStoryElement } from "./story";
12
+
13
+ export type VtexAdsStoryProps = DetailedHTMLProps<
14
+ HTMLAttributes<VtexAdsStoryElement>,
15
+ VtexAdsStoryElement
16
+ >;
17
+
18
+ declare module "react" {
19
+ // eslint-disable-next-line @typescript-eslint/no-namespace
20
+ namespace JSX {
21
+ interface IntrinsicElements {
22
+ "vtex-ads-story": VtexAdsStoryProps;
23
+ }
24
+ }
25
+ }
@@ -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,92 @@ 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;
37
+
38
+ private readonly onKeyDown = (event: KeyboardEvent) => {
39
+ if (event.key === "Escape") this.closePlayer();
40
+ };
20
41
 
21
- get name(): string {
22
- return this.getAttribute("name")?.trim() ?? "";
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;
92
+ disconnectedCallback() {
93
+ this.closePlayer({ restoreFocus: false });
56
94
  }
95
+ }
57
96
 
58
- attributeChangedCallback() {
59
- this.render();
97
+ // Types `document.createElement("vtex-ads-story")` and `querySelector` results
98
+ // as VtexAdsStoryElement in any TypeScript project, no framework needed.
99
+ declare global {
100
+ interface HTMLElementTagNameMap {
101
+ "vtex-ads-story": VtexAdsStoryElement;
60
102
  }
61
103
  }
62
104
 
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
+ }