@vtex/ads-stories 0.3.0 → 0.4.1

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/collapse.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg","../../src/components/icons/index.ts","../../src/components/card/card.ts","../../src/components/player/player.ts","../../src/story/story.ts"],"sourcesContent":["<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>\n","<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>\n","<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>\n","<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>\n","/*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */\n\n// SVG files as downloaded from Phosphor: regular weight, or the weight in the\n// file name. They are bundled as text: `?raw` is handled by Vite in dev and\n// tests, and by the raw plugin in tsup.config.ts.\nimport close from \"./assets/close.svg?raw\";\nimport collapse from \"./assets/collapse.svg?raw\";\nimport expand from \"./assets/expand.svg?raw\";\nimport play from \"./assets/play-fill.svg?raw\";\n\nexport const icons = { close, collapse, expand, play };\n\nexport type IconName = keyof typeof icons;\n\n/** Renders a bundled icon. It is decorative: name the control around it. */\nexport function createIcon(name: IconName) {\n const template = document.createElement(\"template\");\n template.innerHTML = icons[name];\n\n const svg = template.content.firstElementChild as SVGSVGElement;\n svg.classList.add(\"icon\");\n svg.setAttribute(\"aria-hidden\", \"true\");\n svg.setAttribute(\"focusable\", \"false\");\n return svg;\n}\n","import { createIcon } from \"../icons\";\nimport type { StoryAd } from \"../../types\";\n\nexport const cardStyles = `\n .card {\n position: relative;\n box-sizing: border-box;\n width: calc(500px * 9 / 16);\n height: 500px;\n overflow: hidden;\n border-radius: var(--vtex-ads-story-radius, 20px);\n background: #000;\n }\n\n .card[role=\"button\"] {\n cursor: pointer;\n }\n\n .poster {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n }\n\n .play {\n position: absolute;\n top: 50%;\n left: 50%;\n display: grid;\n width: 66px;\n height: 66px;\n place-items: center;\n border-radius: 50%;\n background:\n linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),\n rgba(255, 255, 255, 0.15);\n backdrop-filter: blur(2px);\n box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);\n transform: translate(-50%, -50%);\n transition:\n opacity 0.3s ease,\n transform 0.3s ease;\n }\n\n /* The icon's triangle is 18px tall and already sits slightly right of its\n box, so it looks centered in the circle. */\n .play .icon {\n width: 22px;\n height: 22px;\n color: #fff;\n }\n\n /* With a mouse, the play button shows only on hover or keyboard focus.\n Touch screens have no hover, so it stays visible there. */\n @media (hover: hover) and (pointer: fine) {\n .play {\n opacity: 0;\n transform: translate(-50%, -50%) scale(0.9);\n }\n\n .card:hover .play,\n .card:focus-visible .play {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n }\n`;\n\nexport interface CardOptions {\n /** Accessible name of the card. */\n label: string;\n /** Called on click, Enter or Space. */\n onSelect: () => void;\n}\n\n// Only absolute http(s) URLs reach the DOM.\nfunction isHttpUrl(value: string) {\n try {\n const { protocol } = new URL(value);\n return protocol === \"https:\" || protocol === \"http:\";\n } catch {\n return false;\n }\n}\n\n/**\n * A 9:16 card with the Story cover. With options, the card is a button with\n * a play icon in the middle.\n */\nexport function createCard(story: StoryAd, options?: CardOptions) {\n const card = document.createElement(\"div\");\n card.className = \"card\";\n\n if (story.poster_url && isHttpUrl(story.poster_url)) {\n const poster = document.createElement(\"img\");\n poster.className = \"poster\";\n poster.alt = \"\";\n poster.src = story.poster_url;\n card.append(poster);\n }\n\n if (options) {\n card.setAttribute(\"role\", \"button\");\n card.setAttribute(\"aria-label\", options.label);\n card.tabIndex = 0;\n\n const play = document.createElement(\"span\");\n play.className = \"play\";\n play.setAttribute(\"aria-hidden\", \"true\");\n play.append(createIcon(\"play\"));\n card.append(play);\n\n card.addEventListener(\"click\", options.onSelect);\n card.addEventListener(\"keydown\", (event) => {\n if (event.key !== \"Enter\" && event.key !== \" \") return;\n event.preventDefault();\n options.onSelect();\n });\n }\n\n return card;\n}\n","import { createCard } from \"../card\";\nimport { createIcon, type IconName } from \"../icons\";\nimport type { StoryAd } from \"../../types\";\n\n// The player is a <dialog> shown in the top layer, so no store z-index,\n// overflow or transform can hide or move it.\n//\n// Mini: a manual popover (non-modal, the page stays usable), with the card at\n// the bottom right and the controls above it. Without popover support it is a\n// non-modal dialog, fixed with a high z-index.\n// Expanded: a modal dialog (showModal), so the browser makes the page behind\n// it inert. The card is centered over a dark backdrop, with the controls at\n// the top right of the screen. On narrow screens it fills the screen.\nexport const playerStyles = `\n .player {\n position: fixed;\n inset: auto 16px 16px auto;\n z-index: 2147483000;\n box-sizing: border-box;\n width: calc(min(587px, 100vh - 80px) * 9 / 16);\n height: min(587px, 100vh - 80px);\n margin: 0;\n padding: 0;\n overflow: visible;\n border: 0;\n background: transparent;\n color: inherit;\n font: inherit;\n animation: player-in 0.25s ease;\n }\n\n /* A <dialog> shown as a popover has no open attribute, which the browser\n hides by default. */\n .player:popover-open {\n display: block;\n }\n\n /* Switching modes shows the dialog again; only the first opening fades in. */\n .player[data-switched] {\n animation: none;\n }\n\n .player::backdrop {\n background: transparent;\n }\n\n .player-frame {\n position: relative;\n width: 100%;\n height: 100%;\n }\n\n .player .card {\n width: 100%;\n height: 100%;\n box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);\n }\n\n /* Above the card: on narrow screens the expanded card can cover them. */\n .player-controls {\n position: absolute;\n right: 0;\n bottom: calc(100% + 8px);\n z-index: 1;\n display: flex;\n gap: 8px;\n }\n\n .player-button {\n display: grid;\n width: 36px;\n height: 36px;\n place-items: center;\n padding: 0;\n border: 1px solid rgba(133, 133, 133, 0.3);\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.32);\n backdrop-filter: blur(5px);\n color: #fff;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .player-button:hover {\n background: rgba(0, 0, 0, 0.55);\n }\n\n .player-button .icon {\n width: 18px;\n height: 18px;\n }\n\n .player[data-expanded] {\n inset: 0;\n display: grid;\n width: 100%;\n max-width: none;\n height: 100%;\n max-height: none;\n place-items: center;\n background: rgba(0, 0, 0, 0.85);\n animation: none;\n }\n\n .player[data-expanded] .player-frame {\n width: calc(min(78vh, 720px) * 9 / 16);\n height: min(78vh, 720px);\n }\n\n .player[data-expanded] .player-controls {\n position: fixed;\n top: 24px;\n right: 24px;\n bottom: auto;\n }\n\n .player[data-expanded] .player-button {\n width: 40px;\n height: 40px;\n }\n\n @media (max-width: 576px) {\n .player {\n right: 12px;\n width: 220px;\n height: 391px;\n }\n\n .player[data-expanded] {\n background: #000;\n }\n\n /* The video stays 9:16 and centered; the rest of the screen is black. */\n .player[data-expanded] .player-frame {\n width: min(100vw, 100vh * 9 / 16);\n height: min(100vh, 100vw * 16 / 9);\n }\n\n .player[data-expanded] .card {\n border-radius: 0;\n }\n\n .player[data-expanded] .player-controls {\n top: 12px;\n right: 12px;\n }\n }\n\n @keyframes player-in {\n from {\n opacity: 0;\n transform: translateY(8px);\n }\n }\n\n @media (prefers-reduced-motion: reduce) {\n .player {\n animation: none;\n }\n }\n`;\n\nexport interface PlayerOptions {\n /** Accessible name of the player. */\n label: string;\n /** Accessible names of the buttons. */\n expandLabel: string;\n collapseLabel: string;\n closeLabel: string;\n onClose: () => void;\n}\n\nfunction createButton(className: string, label: string, icon: IconName) {\n const button = document.createElement(\"button\");\n button.type = \"button\";\n button.className = `player-button ${className}`;\n button.setAttribute(\"aria-label\", label);\n button.append(createIcon(icon));\n return button;\n}\n\nfunction supportsPopover(player: HTMLDialogElement) {\n return typeof player.showPopover === \"function\";\n}\n\n/** Shows the player as the mini player. It must already be in the page. */\nexport function showPlayer(player: HTMLDialogElement) {\n if (supportsPopover(player)) player.showPopover();\n else player.show();\n}\n\n/**\n * The Story player. It opens as the mini player (see showPlayer); expand\n * switches to the expanded view, a modal dialog, and back. Clicking the\n * backdrop of the expanded view or pressing Escape in it closes the player.\n */\nexport function createPlayer(story: StoryAd, options: PlayerOptions) {\n const player = document.createElement(\"dialog\");\n player.className = \"player\";\n player.setAttribute(\"popover\", \"manual\");\n player.setAttribute(\"aria-label\", options.label);\n\n const frame = document.createElement(\"div\");\n frame.className = \"player-frame\";\n\n const controls = document.createElement(\"div\");\n controls.className = \"player-controls\";\n\n const expand = createButton(\"player-expand\", options.expandLabel, \"expand\");\n const close = createButton(\"player-close\", options.closeLabel, \"close\");\n controls.append(expand, close);\n\n let expanded = false;\n\n // A dialog can't be shown as a popover and as a modal at the same time, so\n // switching modes hides it first, the same way it was shown.\n function hide() {\n if (!expanded && supportsPopover(player)) player.hidePopover();\n else player.close();\n }\n\n expand.addEventListener(\"click\", () => {\n hide();\n expanded = !expanded;\n player.toggleAttribute(\"data-expanded\", expanded);\n player.dataset.switched = \"\";\n expand.setAttribute(\n \"aria-label\",\n expanded ? options.collapseLabel : options.expandLabel,\n );\n expand.replaceChildren(createIcon(expanded ? \"collapse\" : \"expand\"));\n\n if (expanded) player.showModal();\n else showPlayer(player);\n expand.focus();\n });\n\n close.addEventListener(\"click\", options.onClose);\n\n // Clicks on the backdrop of the expanded view reach the dialog itself.\n player.addEventListener(\"click\", (event) => {\n if (expanded && event.target === player) options.onClose();\n });\n\n // Escape on a modal dialog would only hide it: close the player instead.\n player.addEventListener(\"cancel\", (event) => {\n event.preventDefault();\n options.onClose();\n });\n\n frame.append(controls, createCard(story));\n player.append(frame);\n return player;\n}\n","import { cardStyles, createCard } from \"../components/card\";\nimport { createPlayer, playerStyles, showPlayer } from \"../components/player\";\nimport type { StoryAd } from \"../types\";\n\nexport const TAG_NAME = \"vtex-ads-story\";\n\n// Keeps the module importable where HTMLElement doesn't exist (SSR, Node).\nconst BaseElement: typeof HTMLElement = globalThis.HTMLElement ?? Object;\n\nconst styles = `\n :host {\n display: block;\n font-family: var(--vtex-ads-story-font-family, inherit);\n color: var(--vtex-ads-story-color-text, inherit);\n }\n ${cardStyles}\n ${playerStyles}\n`;\n\n// Temporary: rendered until the element reads Stories from the ad API.\nconst PLACEHOLDER_STORY: StoryAd = {\n ad_id: \"placeholder\",\n poster_url: \"https://dummyimage.com/1080x1920/e31c57/fff\",\n};\n\n// Temporary: fixed texts until the store can set them by attribute.\nconst LABELS = {\n watch: \"Assistir vídeo\",\n player: \"Vídeo\",\n expand: \"Expandir vídeo\",\n collapse: \"Reduzir vídeo\",\n close: \"Fechar vídeo\",\n};\n\nexport class VtexAdsStoryElement extends BaseElement {\n private player: HTMLDialogElement | null = null;\n\n private readonly onKeyDown = (event: KeyboardEvent) => {\n if (event.key === \"Escape\") this.closePlayer();\n };\n\n private render() {\n const container = this.shadowRoot?.querySelector(\".root\");\n if (!container) return;\n\n container.replaceChildren(\n createCard(PLACEHOLDER_STORY, {\n label: LABELS.watch,\n onSelect: () => this.openPlayer(),\n }),\n );\n }\n\n private openPlayer() {\n if (this.player || !this.shadowRoot) return;\n\n this.player = createPlayer(PLACEHOLDER_STORY, {\n label: LABELS.player,\n expandLabel: LABELS.expand,\n collapseLabel: LABELS.collapse,\n closeLabel: LABELS.close,\n onClose: () => this.closePlayer(),\n });\n this.shadowRoot.append(this.player);\n showPlayer(this.player);\n document.addEventListener(\"keydown\", this.onKeyDown);\n\n this.player.querySelector<HTMLElement>(\".player-close\")?.focus();\n }\n\n private closePlayer({ restoreFocus = true } = {}) {\n if (!this.player) return;\n\n document.removeEventListener(\"keydown\", this.onKeyDown);\n this.player.remove();\n this.player = null;\n\n if (restoreFocus) {\n this.shadowRoot?.querySelector<HTMLElement>(\".root .card\")?.focus();\n }\n }\n\n connectedCallback() {\n if (!this.shadowRoot) {\n const root = this.attachShadow({ mode: \"open\" });\n root.innerHTML = `<style>${styles}</style><div class=\"root\"></div>`;\n }\n\n this.render();\n }\n\n disconnectedCallback() {\n this.closePlayer({ restoreFocus: false });\n }\n}\n\n// Types `document.createElement(\"vtex-ads-story\")` and `querySelector` results\n// as VtexAdsStoryElement in any TypeScript project, no framework needed.\ndeclare global {\n interface HTMLElementTagNameMap {\n \"vtex-ads-story\": VtexAdsStoryElement;\n }\n}\n\nexport function defineVtexAdsStories() {\n if (typeof customElements === \"undefined\") return;\n if (customElements.get(TAG_NAME)) return;\n\n customElements.define(TAG_NAME, VtexAdsStoryElement);\n}\n"],"mappings":";AAAA;;;ACAA;;;ACAA;;;ACAA;;;ACUO,IAAM,QAAQ,EAAE,sBAAO,4BAAU,wBAAQ,wBAAK;AAK9C,SAAS,WAAW,MAAgB;AACzC,QAAM,WAAW,SAAS,cAAc,UAAU;AAClD,WAAS,YAAY,MAAM,IAAI;AAE/B,QAAM,MAAM,SAAS,QAAQ;AAC7B,MAAI,UAAU,IAAI,MAAM;AACxB,MAAI,aAAa,eAAe,MAAM;AACtC,MAAI,aAAa,aAAa,OAAO;AACrC,SAAO;AACT;;;ACrBO,IAAM,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2E1B,SAAS,UAAU,OAAe;AAChC,MAAI;AACF,UAAM,EAAE,SAAS,IAAI,IAAI,IAAI,KAAK;AAClC,WAAO,aAAa,YAAY,aAAa;AAAA,EAC/C,SAAQ;AACN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,WAAW,OAAgB,SAAuB;AAChE,QAAM,OAAO,SAAS,cAAc,KAAK;AACzC,OAAK,YAAY;AAEjB,MAAI,MAAM,cAAc,UAAU,M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,MAAI,eAAe,IAAI,QAAQ,EAAG;AAElC,iBAAe,OAAO,UAAU,mBAAmB;AACrD;","names":["_a"]}
1
+ {"version":3,"sources":["../../src/story/story.ts","../../src/components/card/card.ts","../../src/components/icons/index.ts","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/close.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/collapse.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/expand.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/pause-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/play-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-x-fill.svg","raw:/workspace/source/ads-js/packages/ads-stories/src/components/icons/assets/speaker-high-fill.svg","../../src/components/media/media.ts","../../src/components/media/video-controller.ts","../../src/components/player/player.ts"],"sourcesContent":["import { css, html, LitElement } from \"lit\";\nimport { cardStyles, renderCard } from \"../components/card\";\nimport { VideoController } from \"../components/media\";\nimport { PlayerController, playerStyles } from \"../components/player\";\nimport type { StoryMedia } from \"../types\";\n\nexport const TAG_NAME = \"vtex-ads-story\";\n\n// Temporary: rendered until the element reads Stories from the ad API.\nconst PLACEHOLDER_STORY: StoryMedia = {\n poster_url: \"https://dummyimage.com/1080x1920/e31c57/fff\",\n media_url: \"https://samplelib.com/mp4/sample-10s-vp9.mp4\",\n};\n\n// Temporary: fixed texts until the store can set them by attribute.\nconst LABELS = {\n watch: \"Assistir vídeo\",\n player: \"Vídeo\",\n expand: \"Expandir vídeo\",\n collapse: \"Reduzir vídeo\",\n close: \"Fechar vídeo\",\n play: \"Reproduzir vídeo\",\n pause: \"Pausar vídeo\",\n soundOn: \"Ativar som\",\n soundOff: \"Desativar som\",\n};\n\n// LitElement is importable where HTMLElement doesn't exist (SSR, Node): Lit\n// falls back to a minimal shim there.\nexport class VtexAdsStoryElement extends LitElement {\n static readonly styles = [\n css`\n :host {\n display: block;\n font-family: var(--vtex-ads-story-font-family, inherit);\n color: var(--vtex-ads-story-color-text, inherit);\n }\n `,\n cardStyles,\n playerStyles,\n ];\n\n private readonly cardVideo = new VideoController(this);\n\n private readonly player = new PlayerController(this, {\n label: LABELS.player,\n expandLabel: LABELS.expand,\n collapseLabel: LABELS.collapse,\n closeLabel: LABELS.close,\n playLabel: LABELS.play,\n pauseLabel: LABELS.pause,\n soundOnLabel: LABELS.soundOn,\n soundOffLabel: LABELS.soundOff,\n onClose: () => this.closePlayer(),\n });\n\n // Lit keeps the card's DOM when the element is moved (removed and appended\n // again), but the browser pauses a removed video and doesn't autoplay it\n // again. resume() only plays a video that autoplays (not with reduced\n // motion), and does nothing on the first connection, before the render.\n // Not in VideoController.hostConnected: there, the player's video is still\n // in the DOM until the next update and would play for a moment.\n connectedCallback() {\n super.connectedCallback();\n this.cardVideo.resume();\n }\n\n private openPlayer() {\n if (this.player.isOpen) return;\n\n // While the player is open, the card behind it stops on its cover.\n this.cardVideo.pause();\n void this.player.open();\n }\n\n // The player also closes on its own when the element leaves the page,\n // without this: there is no card to give focus back to or to resume playing\n // (it would play out of sight).\n private closePlayer() {\n if (!this.player.isOpen) return;\n\n this.player.close();\n this.cardVideo.resume();\n this.renderRoot.querySelector<HTMLElement>(\".root .card-open\")?.focus();\n }\n\n protected render() {\n return html`\n <div class=\"root\">\n ${renderCard(PLACEHOLDER_STORY, this.cardVideo, {\n label: LABELS.watch,\n soundOnLabel: LABELS.soundOn,\n soundOffLabel: LABELS.soundOff,\n onSelect: () => this.openPlayer(),\n })}\n </div>\n ${this.player.render(PLACEHOLDER_STORY)}\n `;\n }\n}\n\n// Types `document.createElement(\"vtex-ads-story\")` and `querySelector` results\n// as VtexAdsStoryElement in any TypeScript project, no framework needed.\ndeclare global {\n interface HTMLElementTagNameMap {\n \"vtex-ads-story\": VtexAdsStoryElement;\n }\n}\n\nexport function defineVtexAdsStories() {\n if (typeof customElements === \"undefined\") return;\n if (customElements.get(TAG_NAME)) return;\n\n customElements.define(TAG_NAME, VtexAdsStoryElement);\n}\n","import { css, html, nothing } from \"lit\";\nimport { icon } from \"../icons\";\nimport { hasVideo, renderMedia, type VideoController } from \"../media\";\nimport type { StoryMedia } from \"../../types\";\n\nexport const cardStyles = css`\n .card {\n position: relative;\n box-sizing: border-box;\n width: calc(500px * 9 / 16);\n height: 500px;\n overflow: hidden;\n border-radius: var(--vtex-ads-story-radius, 20px);\n background: #000;\n }\n\n /* The card's button covers it. The focus ring goes on the card, since the\n card's overflow would clip the button's own. */\n .card-open {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n padding: 0;\n border: 0;\n background: transparent;\n cursor: pointer;\n }\n\n .card-open:focus-visible {\n outline: none;\n }\n\n .card:has(> .card-open:focus-visible) {\n outline: auto;\n }\n\n .poster,\n .video {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n }\n\n .play {\n position: absolute;\n top: 50%;\n left: 50%;\n display: grid;\n width: 66px;\n height: 66px;\n place-items: center;\n border-radius: 50%;\n background:\n linear-gradient(rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.06)),\n rgba(255, 255, 255, 0.15);\n backdrop-filter: blur(2px);\n box-shadow: inset 0 0 3px rgba(255, 255, 255, 0.42);\n transform: translate(-50%, -50%);\n transition:\n opacity 0.3s ease,\n transform 0.3s ease;\n }\n\n /* The icon's triangle is 18px tall and already sits slightly right of its\n box, so it looks centered in the circle. */\n .play .icon {\n width: 22px;\n height: 22px;\n color: #fff;\n }\n\n /* Sound on/off, at the top right of the card (as in the prototype). */\n .card-sound {\n position: absolute;\n top: 22px;\n right: 12px;\n z-index: 1;\n display: grid;\n width: 28px;\n height: 28px;\n place-items: center;\n padding: 0;\n border: 0;\n background: transparent;\n color: #fff;\n cursor: pointer;\n filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));\n }\n\n .card-sound .icon {\n width: 20px;\n height: 20px;\n }\n\n /* With a mouse, the play button shows only on hover or keyboard focus.\n Touch screens have no hover, so it stays visible there. */\n @media (hover: hover) and (pointer: fine) {\n .play {\n opacity: 0;\n transform: translate(-50%, -50%) scale(0.9);\n }\n\n .card:hover .play,\n .card-open:focus-visible .play {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n }\n`;\n\nexport interface CardOptions {\n /** Accessible name of the card. */\n label: string;\n /** Called on click, Enter or Space. */\n onSelect: () => void;\n /** Accessible names of the sound button, shown when the card has a video. */\n soundOnLabel: string;\n soundOffLabel: string;\n}\n\n/**\n * Sound on/off for the card's video, which starts muted. Only shown while the\n * video plays: a video that never plays (reduced motion, or autoplay blocked\n * as in iOS Low Power Mode) has no sound to turn on, and the card paused\n * behind the open player needs none.\n */\nfunction renderSoundButton(video: VideoController, options: CardOptions) {\n return html`\n <button\n type=\"button\"\n class=\"card-sound\"\n aria-label=${video.muted ? options.soundOnLabel : options.soundOffLabel}\n @click=${() => video.toggleMuted()}\n >\n ${icon(video.muted ? \"soundOff\" : \"soundOn\")}\n </button>\n `;\n}\n\n/**\n * A 9:16 card with the Story cover and, with a media_url, the video looping\n * muted over it (see renderMedia). A button with a play icon in the middle\n * covers the card, and a sound button shows while the video plays. The sound\n * button is beside the card's button, not inside it: a button can't contain\n * another.\n */\nexport function renderCard(\n story: StoryMedia,\n video: VideoController,\n options: CardOptions,\n) {\n return html`\n <div class=\"card\">\n ${renderMedia(story, video)}\n <button\n type=\"button\"\n class=\"card-open\"\n aria-label=${options.label}\n @click=${options.onSelect}\n >\n <span class=\"play\" aria-hidden=\"true\">${icon(\"play\")}</span>\n </button>\n ${hasVideo(story, video) && !video.paused\n ? renderSoundButton(video, options)\n : nothing}\n </div>\n `;\n}\n","/*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */\n\nimport type { DirectiveResult } from \"lit/directive.js\";\nimport {\n unsafeHTML,\n type UnsafeHTMLDirective,\n} from \"lit/directives/unsafe-html.js\";\n\n// SVG files as downloaded from Phosphor: regular weight, or the weight in the\n// file name. They are bundled as text: `?raw` is handled by Vite in dev and\n// tests, and by the raw plugin in tsup.config.ts.\nimport close from \"./assets/close.svg?raw\";\nimport collapse from \"./assets/collapse.svg?raw\";\nimport expand from \"./assets/expand.svg?raw\";\nimport pause from \"./assets/pause-fill.svg?raw\";\nimport play from \"./assets/play-fill.svg?raw\";\nimport soundOff from \"./assets/speaker-x-fill.svg?raw\";\nimport soundOn from \"./assets/speaker-high-fill.svg?raw\";\n\nexport const icons = {\n close,\n collapse,\n expand,\n pause,\n play,\n soundOff,\n soundOn,\n};\n\nexport type IconName = keyof typeof icons;\n\n// Each file as markup, marked decorative once so every render reuses it.\nconst markup = Object.fromEntries(\n Object.entries(icons).map(([name, svg]) => [\n name,\n svg.replace(\n \"<svg\",\n '<svg class=\"icon\" aria-hidden=\"true\" focusable=\"false\"',\n ),\n ]),\n) as Record<IconName, string>;\n\n/**\n * Renders a bundled icon in a Lit template. It is decorative: name the\n * control around it. The markup only ever comes from the bundled files.\n */\nexport function icon(\n name: IconName,\n): DirectiveResult<typeof UnsafeHTMLDirective> {\n return unsafeHTML(markup[name]);\n}\n","<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>\n","<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>\n","<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>\n","<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 256 256\" fill=\"currentColor\"><path d=\"M216,48V208a16,16,0,0,1-16,16H160a16,16,0,0,1-16-16V48a16,16,0,0,1,16-16h40A16,16,0,0,1,216,48ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Z\"/></svg>\n","<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>\n","<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 256 256\" fill=\"currentColor\"><path d=\"M245.66,146.34a8,8,0,0,1-11.32,11.32L216,139.31l-18.34,18.35a8,8,0,0,1-11.32-11.32L204.69,128l-18.35-18.34a8,8,0,0,1,11.32-11.32L216,116.69l18.34-18.35a8,8,0,0,1,11.32,11.32L227.31,128ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm97.15-54.15a8,8,0,0,0-10-.16l-65.57,51A4,4,0,0,0,80,79.84v96.32a4,4,0,0,0,1.55,3.15l65.57,51a8,8,0,0,0,9,.56,8.29,8.29,0,0,0,3.91-7.18V32.25A8.27,8.27,0,0,0,157.12,25.85Z\"/></svg>\n","<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 256 256\" fill=\"currentColor\"><path d=\"M160,32.25V223.69a8.29,8.29,0,0,1-3.91,7.18,8,8,0,0,1-9-.56l-65.57-51A4,4,0,0,1,80,176.16V79.84a4,4,0,0,1,1.55-3.15l65.57-51a8,8,0,0,1,10,.16A8.27,8.27,0,0,1,160,32.25ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm126.77,20.84a8,8,0,0,0-.72,11.3,24,24,0,0,1,0,31.72,8,8,0,1,0,12,10.58,40,40,0,0,0,0-52.88A8,8,0,0,0,186.74,100.84Zm40.89-26.17a8,8,0,1,0-11.92,10.66,64,64,0,0,1,0,85.34,8,8,0,1,0,11.92,10.66,80,80,0,0,0,0-106.66Z\"/></svg>\n","import { html, nothing } from \"lit\";\nimport { ref } from \"lit/directives/ref.js\";\nimport type { StoryMedia } from \"../../types\";\nimport type { VideoController } from \"./video-controller\";\n\n// Only absolute http(s) URLs reach the DOM. Relative and protocol-relative\n// ones (\"/a.jpg\", \"//host/a.jpg\") don't parse without a base, so they're out.\nexport function httpUrl(value: string | undefined) {\n if (!value) return null;\n try {\n const { protocol } = new URL(value);\n return protocol === \"https:\" || protocol === \"http:\" ? value : null;\n } catch {\n return null;\n }\n}\n\nfunction prefersReducedMotion() {\n return (\n globalThis.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches ?? false\n );\n}\n\n/** Whether renderMedia shows a video for this Story right now. */\nexport function hasVideo(story: StoryMedia, video: VideoController) {\n return !video.failed && httpUrl(story.media_url) !== null;\n}\n\n/**\n * The Story cover and, with a media_url, the video looping muted over it. If\n * the video can't load, it is removed and the cover underneath stays.\n */\nexport function renderMedia(story: StoryMedia, video: VideoController) {\n const posterUrl = httpUrl(story.poster_url);\n const mediaUrl = httpUrl(story.media_url);\n\n // Muted and inline is what browsers, iOS included, allow to autoplay. The\n // muted attribute and property are both set, since browsers differ on\n // which one they read for autoplay. No autoplay for people who asked for\n // reduced motion.\n return html`\n ${posterUrl\n ? html`<img class=\"poster\" alt=\"\" src=${posterUrl} />`\n : nothing}\n ${mediaUrl && !video.failed\n ? html`\n <video\n class=\"video\"\n muted\n playsinline\n loop\n preload=\"metadata\"\n .muted=${true}\n ?autoplay=${!prefersReducedMotion()}\n poster=${posterUrl ?? nothing}\n src=${mediaUrl}\n ${ref(video.ref)}\n @play=${video.sync}\n @pause=${video.sync}\n @volumechange=${video.sync}\n @error=${video.fail}\n ></video>\n `\n : nothing}\n `;\n}\n","import type { ReactiveControllerHost } from \"lit\";\nimport { createRef } from \"lit/directives/ref.js\";\n\n/**\n * Plays a video. A blocked play is ignored: the cover or the paused state\n * stays.\n */\nexport function playVideo(video: HTMLVideoElement) {\n video.play()?.catch(() => undefined);\n}\n\n/**\n * Follows one rendered <video> (see renderMedia) and re-renders its host when\n * it plays, pauses, is muted or unmuted, or fails to load. The state comes\n * from the video's own events, so a blocked autoplay shows as paused. It needs\n * no lifecycle hooks: the browser pauses a video removed from the page.\n */\nexport class VideoController {\n /** Bound to the <video> by renderMedia. */\n readonly ref = createRef<HTMLVideoElement>();\n\n paused = true;\n // The video always starts muted: that is what browsers allow to autoplay.\n muted = true;\n /** The video could not load: only the cover is shown. */\n failed = false;\n\n constructor(private readonly host: ReactiveControllerHost) {}\n\n /** For a new <video>, such as each time the player opens. */\n reset() {\n this.paused = true;\n this.muted = true;\n this.failed = false;\n }\n\n play() {\n const video = this.ref.value;\n if (video) playVideo(video);\n }\n\n pause() {\n this.ref.value?.pause();\n }\n\n /** Plays again if the video plays on its own (see renderMedia). */\n resume() {\n const video = this.ref.value;\n if (video?.autoplay) playVideo(video);\n }\n\n togglePlayback() {\n const video = this.ref.value;\n if (!video) return;\n if (video.paused) playVideo(video);\n else video.pause();\n }\n\n toggleMuted() {\n const video = this.ref.value;\n if (video) video.muted = !video.muted;\n }\n\n /** Listener for the video's play, pause and volumechange events. */\n readonly sync = () => {\n const video = this.ref.value;\n if (!video) return;\n this.paused = video.paused;\n this.muted = video.muted;\n this.host.requestUpdate();\n };\n\n /** Listener for the video's error event. */\n readonly fail = () => {\n this.failed = true;\n this.host.requestUpdate();\n };\n}\n","import { css, html, nothing } from \"lit\";\nimport type { ReactiveController, ReactiveControllerHost } from \"lit\";\nimport { createRef, ref } from \"lit/directives/ref.js\";\nimport { icon } from \"../icons\";\nimport { hasVideo, renderMedia, VideoController } from \"../media\";\nimport type { StoryMedia } from \"../../types\";\n\n// The player is a <dialog> shown in the top layer, so no store z-index,\n// overflow or transform can hide or move it.\n//\n// Mini: a manual popover (non-modal, the page stays usable), with the card at\n// the bottom right and the controls above it. Without popover support it is a\n// non-modal dialog, fixed with a high z-index.\n// Expanded: a modal dialog (showModal), so the browser makes the page behind\n// it inert. The card is centered over a dark backdrop, with the controls at\n// the top right of the screen. On narrow screens it fills the screen.\nexport const playerStyles = css`\n .player {\n position: fixed;\n inset: auto 16px 16px auto;\n z-index: 2147483000;\n box-sizing: border-box;\n width: calc(min(587px, 100vh - 80px) * 9 / 16);\n height: min(587px, 100vh - 80px);\n margin: 0;\n padding: 0;\n overflow: visible;\n border: 0;\n background: transparent;\n color: inherit;\n font: inherit;\n animation: player-in 0.25s ease;\n }\n\n /* A <dialog> shown as a popover has no open attribute, which the browser\n hides by default. */\n .player:popover-open {\n display: block;\n }\n\n /* Switching modes shows the dialog again; only the first opening fades in. */\n .player[data-switched] {\n animation: none;\n }\n\n .player::backdrop {\n background: transparent;\n }\n\n .player-frame {\n position: relative;\n width: 100%;\n height: 100%;\n }\n\n .player .card {\n width: 100%;\n height: 100%;\n box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);\n }\n\n .player .card:has(.video) {\n cursor: pointer;\n }\n\n /* Play/pause reuses the card's glass .play button, as a real button. */\n .player-toggle {\n padding: 0;\n border: 0;\n color: #fff;\n font: inherit;\n cursor: pointer;\n }\n\n .player[data-paused] .player-toggle,\n .player-toggle:focus-visible {\n opacity: 1;\n transform: translate(-50%, -50%);\n }\n\n /* On touch, tapping the video pauses it; the button shows while paused. */\n @media (hover: none) {\n .player:not([data-paused]) .player-toggle {\n opacity: 0;\n }\n }\n\n .player-sound {\n position: absolute;\n top: 12px;\n right: 12px;\n z-index: 1;\n border-color: transparent;\n background: rgba(0, 0, 0, 0.3);\n }\n\n .player-sound .icon {\n width: 20px;\n height: 20px;\n }\n\n /* Above the card: on narrow screens the expanded card can cover them. */\n .player-controls {\n position: absolute;\n right: 0;\n bottom: calc(100% + 8px);\n z-index: 1;\n display: flex;\n gap: 8px;\n }\n\n .player-button {\n display: grid;\n width: 36px;\n height: 36px;\n place-items: center;\n padding: 0;\n border: 1px solid rgba(133, 133, 133, 0.3);\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.32);\n backdrop-filter: blur(5px);\n color: #fff;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .player-button:hover {\n background: rgba(0, 0, 0, 0.55);\n }\n\n .player-button .icon {\n width: 18px;\n height: 18px;\n }\n\n .player[data-expanded] {\n inset: 0;\n display: grid;\n width: 100%;\n max-width: none;\n height: 100%;\n max-height: none;\n place-items: center;\n background: rgba(0, 0, 0, 0.85);\n animation: none;\n }\n\n .player[data-expanded] .player-frame {\n width: calc(min(78vh, 720px) * 9 / 16);\n height: min(78vh, 720px);\n }\n\n .player[data-expanded] .player-controls {\n position: fixed;\n top: 24px;\n right: 24px;\n bottom: auto;\n }\n\n .player[data-expanded] .player-button {\n width: 40px;\n height: 40px;\n }\n\n @media (max-width: 576px) {\n .player {\n right: 12px;\n width: 220px;\n height: 391px;\n }\n\n .player[data-expanded] {\n background: #000;\n }\n\n /* The video stays 9:16 and centered; the rest of the screen is black. */\n .player[data-expanded] .player-frame {\n width: min(100vw, 100vh * 9 / 16);\n height: min(100vh, 100vw * 16 / 9);\n }\n\n .player[data-expanded] .card {\n border-radius: 0;\n }\n\n /* Below the sound button, at the top right of the video. Placed in the\n video frame, not the screen: the frame is centered, so on screens\n taller than 9:16 its top is below the screen's. */\n .player[data-expanded] .player-controls {\n position: absolute;\n top: 60px;\n right: 12px;\n }\n }\n\n @keyframes player-in {\n from {\n opacity: 0;\n transform: translateY(8px);\n }\n }\n\n @media (prefers-reduced-motion: reduce) {\n .player {\n animation: none;\n }\n }\n`;\n\nexport interface PlayerOptions {\n /** Accessible name of the player. */\n label: string;\n /** Accessible names of the buttons. */\n expandLabel: string;\n collapseLabel: string;\n closeLabel: string;\n playLabel: string;\n pauseLabel: string;\n soundOnLabel: string;\n soundOffLabel: string;\n /**\n * Called by the close button, Escape and a click on the expanded view's\n * backdrop. Call close() from it.\n */\n onClose: () => void;\n}\n\nfunction supportsPopover(dialog: HTMLDialogElement) {\n return typeof dialog.showPopover === \"function\";\n}\n\n/**\n * The Story player, rendered by its host with render(). open() shows it as\n * the mini player; expand switches to the expanded view, a modal dialog, and\n * back. The video starts muted and plays when the player opens. It closes on\n * its own when the host leaves the page.\n */\nexport class PlayerController implements ReactiveController {\n readonly video: VideoController;\n\n /** Rendered and, once the host has updated, shown. */\n isOpen = false;\n private expanded = false;\n /** Switched modes at least once: only the first opening fades in. */\n private switched = false;\n /** The dialog is showing, in the mode `expanded` says. */\n private shown = false;\n\n private readonly dialog = createRef<HTMLDialogElement>();\n private readonly expandButton = createRef<HTMLButtonElement>();\n private readonly closeButton = createRef<HTMLButtonElement>();\n\n constructor(\n private readonly host: ReactiveControllerHost,\n private readonly options: PlayerOptions,\n ) {\n host.addController(this);\n this.video = new VideoController(host);\n }\n\n hostDisconnected() {\n this.close();\n }\n\n /** Opens the mini player, plays the video and focuses the close button. */\n async open() {\n if (this.isOpen) return;\n\n document.addEventListener(\"keydown\", this.onKeyDown);\n this.isOpen = true;\n this.expanded = false;\n this.switched = false;\n this.video.reset();\n this.host.requestUpdate();\n await this.host.updateComplete;\n\n const dialog = this.dialog.value;\n // Closed again before the host rendered it.\n if (!this.isOpen || !dialog) return;\n\n this.show(dialog);\n this.video.play();\n this.closeButton.value?.focus();\n }\n\n /** Hides the player now; the host removes it on its next update. */\n close() {\n if (!this.isOpen) return;\n\n document.removeEventListener(\"keydown\", this.onKeyDown);\n const dialog = this.dialog.value;\n if (dialog) this.hide(dialog);\n this.isOpen = false;\n this.host.requestUpdate();\n }\n\n // Mini: a manual popover, or a non-modal dialog without popover support.\n // Expanded: a modal dialog.\n // Already showing: a second expand click in the same task resumes after the\n // first one has shown the dialog, and showing it again would throw.\n private show(dialog: HTMLDialogElement) {\n if (this.shown) return;\n if (this.expanded) dialog.showModal();\n else if (supportsPopover(dialog)) dialog.showPopover();\n else dialog.show();\n this.shown = true;\n }\n\n // A dialog can't be shown as a popover and as a modal at the same time, so\n // switching modes hides it first, the same way it was shown.\n private hide(dialog: HTMLDialogElement) {\n if (!this.shown) return;\n if (!this.expanded && supportsPopover(dialog)) dialog.hidePopover();\n else dialog.close();\n this.shown = false;\n }\n\n private readonly toggleExpanded = async () => {\n const dialog = this.dialog.value;\n if (!dialog) return;\n\n this.hide(dialog);\n this.expanded = !this.expanded;\n this.switched = true;\n this.host.requestUpdate();\n await this.host.updateComplete;\n\n if (!this.isOpen) return;\n this.show(dialog);\n this.expandButton.value?.focus();\n };\n\n // Escape anywhere on the page, also in the mini player, which is not modal.\n private readonly onKeyDown = (event: KeyboardEvent) => {\n if (event.key === \"Escape\") this.options.onClose();\n };\n\n // Clicks on the backdrop of the expanded view reach the dialog itself.\n private readonly onDialogClick = (event: Event) => {\n if (this.expanded && event.target === event.currentTarget) {\n this.options.onClose();\n }\n };\n\n // Escape on a modal dialog would only hide it: close the player instead.\n private readonly onCancel = (event: Event) => {\n event.preventDefault();\n this.options.onClose();\n };\n\n /**\n * Play/pause in the middle of the video and sound on/off at its top right.\n * They follow the video's own events, so a blocked autoplay shows as\n * paused.\n */\n private renderVideoControls() {\n const { video, options } = this;\n\n // Not a .player-button: it keeps the card's larger glass look.\n const onToggle = (event: Event) => {\n event.stopPropagation();\n video.togglePlayback();\n };\n const onSound = (event: Event) => {\n event.stopPropagation();\n video.toggleMuted();\n };\n\n return html`\n <button\n type=\"button\"\n class=\"play player-toggle\"\n aria-label=${video.paused ? options.playLabel : options.pauseLabel}\n @click=${onToggle}\n >\n ${icon(video.paused ? \"play\" : \"pause\")}\n </button>\n <button\n type=\"button\"\n class=\"player-button player-sound\"\n aria-label=${video.muted ? options.soundOnLabel : options.soundOffLabel}\n @click=${onSound}\n >\n ${icon(video.muted ? \"soundOff\" : \"soundOn\")}\n </button>\n `;\n }\n\n /**\n * The player <dialog> while it is open. Clicking the video also plays or\n * pauses it.\n */\n render(story: StoryMedia) {\n if (!this.isOpen) return nothing;\n\n const { video, options, expanded } = this;\n const withVideo = hasVideo(story, video);\n\n return html`\n <dialog\n class=\"player\"\n popover=\"manual\"\n aria-label=${options.label}\n ?data-expanded=${expanded}\n ?data-switched=${this.switched}\n ?data-paused=${withVideo && video.paused}\n ${ref(this.dialog)}\n @click=${this.onDialogClick}\n @cancel=${this.onCancel}\n >\n <div class=\"player-frame\">\n <div class=\"player-controls\">\n <button\n type=\"button\"\n class=\"player-button player-expand\"\n aria-label=${expanded\n ? options.collapseLabel\n : options.expandLabel}\n ${ref(this.expandButton)}\n @click=${this.toggleExpanded}\n >\n ${icon(expanded ? \"collapse\" : \"expand\")}\n </button>\n <button\n type=\"button\"\n class=\"player-button player-close\"\n aria-label=${options.closeLabel}\n ${ref(this.closeButton)}\n @click=${options.onClose}\n >\n ${icon(\"close\")}\n </button>\n </div>\n <div class=\"card\" @click=${() => video.togglePlayback()}>\n ${renderMedia(story, video)}\n ${withVideo ? this.renderVideoControls() : nothing}\n </div>\n </div>\n </dialog>\n `;\n }\n}\n"],"mappings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WAAW;AAAA,MAC9C,OAAO,OAAO;AAAA,MACd,cAAc,OAAO;AAAA,MACrB,eAAe,OAAO;AAAA,MACtB,UAAU,MAAM,KAAK,WAAW;AAAA,IAClC,CAAC,CAAC;AAAA;AAAA,QAEF,KAAK,OAAO,OAAO,iBAAiB,CAAC;AAAA;AAAA,EAE3C;AACF;AAtEa,oBACK,SAAS;AAAA,EACvBC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA;AAAA,EACA;AACF;AAqEK,SAAS,uBAAuB;AACrC,MAAI,OAAO,mBAAmB,YAAa;AAC3C,MAAI,eAAe,IAAI,QAAQ,EAAG;AAElC,iBAAe,OAAO,UAAU,mBAAmB;AACrD;","names":["css","html","html","nothing","html","nothing","css","html","nothing","createRef","ref","css","createRef","html","nothing","ref","html","css"]}
@@ -1,5 +1,6 @@
1
1
  import { DetailedHTMLProps, HTMLAttributes } from 'react';
2
2
  import { VtexAdsStoryElement } from './index.js';
3
+ import 'lit';
3
4
 
4
5
  /**
5
6
  * React JSX types for <vtex-ads-story>. Types only: the JS output is empty.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtex/ads-stories",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "description": "VTEX Ads Stories web component",
6
6
  "main": "dist/cjs/index.cjs",
7
7
  "module": "dist/esm/index.js",
@@ -65,6 +65,9 @@
65
65
  "tsup": "^8.5.0",
66
66
  "vite": "^5.0.0"
67
67
  },
68
+ "dependencies": {
69
+ "lit": "^3.3.3"
70
+ },
68
71
  "scripts": {
69
72
  "build": "tsup",
70
73
  "build:watch": "tsup --watch",
@@ -1,7 +1,9 @@
1
- import { createIcon } from "../icons";
2
- import type { StoryAd } from "../../types";
1
+ import { css, html, nothing } from "lit";
2
+ import { icon } from "../icons";
3
+ import { hasVideo, renderMedia, type VideoController } from "../media";
4
+ import type { StoryMedia } from "../../types";
3
5
 
4
- export const cardStyles = `
6
+ export const cardStyles = css`
5
7
  .card {
6
8
  position: relative;
7
9
  box-sizing: border-box;
@@ -12,11 +14,29 @@ export const cardStyles = `
12
14
  background: #000;
13
15
  }
14
16
 
15
- .card[role="button"] {
17
+ /* The card's button covers it. The focus ring goes on the card, since the
18
+ card's overflow would clip the button's own. */
19
+ .card-open {
20
+ position: absolute;
21
+ inset: 0;
22
+ width: 100%;
23
+ height: 100%;
24
+ padding: 0;
25
+ border: 0;
26
+ background: transparent;
16
27
  cursor: pointer;
17
28
  }
18
29
 
19
- .poster {
30
+ .card-open:focus-visible {
31
+ outline: none;
32
+ }
33
+
34
+ .card:has(> .card-open:focus-visible) {
35
+ outline: auto;
36
+ }
37
+
38
+ .poster,
39
+ .video {
20
40
  position: absolute;
21
41
  inset: 0;
22
42
  width: 100%;
@@ -52,6 +72,29 @@ export const cardStyles = `
52
72
  color: #fff;
53
73
  }
54
74
 
75
+ /* Sound on/off, at the top right of the card (as in the prototype). */
76
+ .card-sound {
77
+ position: absolute;
78
+ top: 22px;
79
+ right: 12px;
80
+ z-index: 1;
81
+ display: grid;
82
+ width: 28px;
83
+ height: 28px;
84
+ place-items: center;
85
+ padding: 0;
86
+ border: 0;
87
+ background: transparent;
88
+ color: #fff;
89
+ cursor: pointer;
90
+ filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
91
+ }
92
+
93
+ .card-sound .icon {
94
+ width: 20px;
95
+ height: 20px;
96
+ }
97
+
55
98
  /* With a mouse, the play button shows only on hover or keyboard focus.
56
99
  Touch screens have no hover, so it stays visible there. */
57
100
  @media (hover: hover) and (pointer: fine) {
@@ -61,7 +104,7 @@ export const cardStyles = `
61
104
  }
62
105
 
63
106
  .card:hover .play,
64
- .card:focus-visible .play {
107
+ .card-open:focus-visible .play {
65
108
  opacity: 1;
66
109
  transform: translate(-50%, -50%);
67
110
  }
@@ -73,52 +116,56 @@ export interface CardOptions {
73
116
  label: string;
74
117
  /** Called on click, Enter or Space. */
75
118
  onSelect: () => void;
119
+ /** Accessible names of the sound button, shown when the card has a video. */
120
+ soundOnLabel: string;
121
+ soundOffLabel: string;
76
122
  }
77
123
 
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
- }
124
+ /**
125
+ * Sound on/off for the card's video, which starts muted. Only shown while the
126
+ * video plays: a video that never plays (reduced motion, or autoplay blocked
127
+ * as in iOS Low Power Mode) has no sound to turn on, and the card paused
128
+ * behind the open player needs none.
129
+ */
130
+ function renderSoundButton(video: VideoController, options: CardOptions) {
131
+ return html`
132
+ <button
133
+ type="button"
134
+ class="card-sound"
135
+ aria-label=${video.muted ? options.soundOnLabel : options.soundOffLabel}
136
+ @click=${() => video.toggleMuted()}
137
+ >
138
+ ${icon(video.muted ? "soundOff" : "soundOn")}
139
+ </button>
140
+ `;
86
141
  }
87
142
 
88
143
  /**
89
- * A 9:16 card with the Story cover. With options, the card is a button with
90
- * a play icon in the middle.
144
+ * A 9:16 card with the Story cover and, with a media_url, the video looping
145
+ * muted over it (see renderMedia). A button with a play icon in the middle
146
+ * covers the card, and a sound button shows while the video plays. The sound
147
+ * button is beside the card's button, not inside it: a button can't contain
148
+ * another.
91
149
  */
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;
150
+ export function renderCard(
151
+ story: StoryMedia,
152
+ video: VideoController,
153
+ options: CardOptions,
154
+ ) {
155
+ return html`
156
+ <div class="card">
157
+ ${renderMedia(story, video)}
158
+ <button
159
+ type="button"
160
+ class="card-open"
161
+ aria-label=${options.label}
162
+ @click=${options.onSelect}
163
+ >
164
+ <span class="play" aria-hidden="true">${icon("play")}</span>
165
+ </button>
166
+ ${hasVideo(story, video) && !video.paused
167
+ ? renderSoundButton(video, options)
168
+ : nothing}
169
+ </div>
170
+ `;
124
171
  }
@@ -1,2 +1,2 @@
1
- export { createCard, cardStyles } from "./card";
1
+ export { cardStyles, renderCard } from "./card";
2
2
  export type { CardOptions } from "./card";
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M216,48V208a16,16,0,0,1-16,16H160a16,16,0,0,1-16-16V48a16,16,0,0,1,16-16h40A16,16,0,0,1,216,48ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M160,32.25V223.69a8.29,8.29,0,0,1-3.91,7.18,8,8,0,0,1-9-.56l-65.57-51A4,4,0,0,1,80,176.16V79.84a4,4,0,0,1,1.55-3.15l65.57-51a8,8,0,0,1,10,.16A8.27,8.27,0,0,1,160,32.25ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm126.77,20.84a8,8,0,0,0-.72,11.3,24,24,0,0,1,0,31.72,8,8,0,1,0,12,10.58,40,40,0,0,0,0-52.88A8,8,0,0,0,186.74,100.84Zm40.89-26.17a8,8,0,1,0-11.92,10.66,64,64,0,0,1,0,85.34,8,8,0,1,0,11.92,10.66,80,80,0,0,0,0-106.66Z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M245.66,146.34a8,8,0,0,1-11.32,11.32L216,139.31l-18.34,18.35a8,8,0,0,1-11.32-11.32L204.69,128l-18.35-18.34a8,8,0,0,1,11.32-11.32L216,116.69l18.34-18.35a8,8,0,0,1,11.32,11.32L227.31,128ZM60,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H60a4,4,0,0,0,4-4V84A4,4,0,0,0,60,80Zm97.15-54.15a8,8,0,0,0-10-.16l-65.57,51A4,4,0,0,0,80,79.84v96.32a4,4,0,0,0,1.55,3.15l65.57,51a8,8,0,0,0,9,.56,8.29,8.29,0,0,0,3.91-7.18V32.25A8.27,8.27,0,0,0,157.12,25.85Z"/></svg>
@@ -1,25 +1,51 @@
1
1
  /*! Icons from Phosphor Icons (https://phosphoricons.com), MIT License, Copyright (c) 2023 Phosphor Icons */
2
2
 
3
+ import type { DirectiveResult } from "lit/directive.js";
4
+ import {
5
+ unsafeHTML,
6
+ type UnsafeHTMLDirective,
7
+ } from "lit/directives/unsafe-html.js";
8
+
3
9
  // SVG files as downloaded from Phosphor: regular weight, or the weight in the
4
10
  // file name. They are bundled as text: `?raw` is handled by Vite in dev and
5
11
  // tests, and by the raw plugin in tsup.config.ts.
6
12
  import close from "./assets/close.svg?raw";
7
13
  import collapse from "./assets/collapse.svg?raw";
8
14
  import expand from "./assets/expand.svg?raw";
15
+ import pause from "./assets/pause-fill.svg?raw";
9
16
  import play from "./assets/play-fill.svg?raw";
17
+ import soundOff from "./assets/speaker-x-fill.svg?raw";
18
+ import soundOn from "./assets/speaker-high-fill.svg?raw";
10
19
 
11
- export const icons = { close, collapse, expand, play };
20
+ export const icons = {
21
+ close,
22
+ collapse,
23
+ expand,
24
+ pause,
25
+ play,
26
+ soundOff,
27
+ soundOn,
28
+ };
12
29
 
13
30
  export type IconName = keyof typeof icons;
14
31
 
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];
32
+ // Each file as markup, marked decorative once so every render reuses it.
33
+ const markup = Object.fromEntries(
34
+ Object.entries(icons).map(([name, svg]) => [
35
+ name,
36
+ svg.replace(
37
+ "<svg",
38
+ '<svg class="icon" aria-hidden="true" focusable="false"',
39
+ ),
40
+ ]),
41
+ ) as Record<IconName, string>;
19
42
 
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;
43
+ /**
44
+ * Renders a bundled icon in a Lit template. It is decorative: name the
45
+ * control around it. The markup only ever comes from the bundled files.
46
+ */
47
+ export function icon(
48
+ name: IconName,
49
+ ): DirectiveResult<typeof UnsafeHTMLDirective> {
50
+ return unsafeHTML(markup[name]);
25
51
  }
@@ -0,0 +1,2 @@
1
+ export { hasVideo, httpUrl, renderMedia } from "./media";
2
+ export { playVideo, VideoController } from "./video-controller";
@@ -0,0 +1,66 @@
1
+ import { html, nothing } from "lit";
2
+ import { ref } from "lit/directives/ref.js";
3
+ import type { StoryMedia } from "../../types";
4
+ import type { VideoController } from "./video-controller";
5
+
6
+ // Only absolute http(s) URLs reach the DOM. Relative and protocol-relative
7
+ // ones ("/a.jpg", "//host/a.jpg") don't parse without a base, so they're out.
8
+ export function httpUrl(value: string | undefined) {
9
+ if (!value) return null;
10
+ try {
11
+ const { protocol } = new URL(value);
12
+ return protocol === "https:" || protocol === "http:" ? value : null;
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+
18
+ function prefersReducedMotion() {
19
+ return (
20
+ globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false
21
+ );
22
+ }
23
+
24
+ /** Whether renderMedia shows a video for this Story right now. */
25
+ export function hasVideo(story: StoryMedia, video: VideoController) {
26
+ return !video.failed && httpUrl(story.media_url) !== null;
27
+ }
28
+
29
+ /**
30
+ * The Story cover and, with a media_url, the video looping muted over it. If
31
+ * the video can't load, it is removed and the cover underneath stays.
32
+ */
33
+ export function renderMedia(story: StoryMedia, video: VideoController) {
34
+ const posterUrl = httpUrl(story.poster_url);
35
+ const mediaUrl = httpUrl(story.media_url);
36
+
37
+ // Muted and inline is what browsers, iOS included, allow to autoplay. The
38
+ // muted attribute and property are both set, since browsers differ on
39
+ // which one they read for autoplay. No autoplay for people who asked for
40
+ // reduced motion.
41
+ return html`
42
+ ${posterUrl
43
+ ? html`<img class="poster" alt="" src=${posterUrl} />`
44
+ : nothing}
45
+ ${mediaUrl && !video.failed
46
+ ? html`
47
+ <video
48
+ class="video"
49
+ muted
50
+ playsinline
51
+ loop
52
+ preload="metadata"
53
+ .muted=${true}
54
+ ?autoplay=${!prefersReducedMotion()}
55
+ poster=${posterUrl ?? nothing}
56
+ src=${mediaUrl}
57
+ ${ref(video.ref)}
58
+ @play=${video.sync}
59
+ @pause=${video.sync}
60
+ @volumechange=${video.sync}
61
+ @error=${video.fail}
62
+ ></video>
63
+ `
64
+ : nothing}
65
+ `;
66
+ }
@@ -0,0 +1,78 @@
1
+ import type { ReactiveControllerHost } from "lit";
2
+ import { createRef } from "lit/directives/ref.js";
3
+
4
+ /**
5
+ * Plays a video. A blocked play is ignored: the cover or the paused state
6
+ * stays.
7
+ */
8
+ export function playVideo(video: HTMLVideoElement) {
9
+ video.play()?.catch(() => undefined);
10
+ }
11
+
12
+ /**
13
+ * Follows one rendered <video> (see renderMedia) and re-renders its host when
14
+ * it plays, pauses, is muted or unmuted, or fails to load. The state comes
15
+ * from the video's own events, so a blocked autoplay shows as paused. It needs
16
+ * no lifecycle hooks: the browser pauses a video removed from the page.
17
+ */
18
+ export class VideoController {
19
+ /** Bound to the <video> by renderMedia. */
20
+ readonly ref = createRef<HTMLVideoElement>();
21
+
22
+ paused = true;
23
+ // The video always starts muted: that is what browsers allow to autoplay.
24
+ muted = true;
25
+ /** The video could not load: only the cover is shown. */
26
+ failed = false;
27
+
28
+ constructor(private readonly host: ReactiveControllerHost) {}
29
+
30
+ /** For a new <video>, such as each time the player opens. */
31
+ reset() {
32
+ this.paused = true;
33
+ this.muted = true;
34
+ this.failed = false;
35
+ }
36
+
37
+ play() {
38
+ const video = this.ref.value;
39
+ if (video) playVideo(video);
40
+ }
41
+
42
+ pause() {
43
+ this.ref.value?.pause();
44
+ }
45
+
46
+ /** Plays again if the video plays on its own (see renderMedia). */
47
+ resume() {
48
+ const video = this.ref.value;
49
+ if (video?.autoplay) playVideo(video);
50
+ }
51
+
52
+ togglePlayback() {
53
+ const video = this.ref.value;
54
+ if (!video) return;
55
+ if (video.paused) playVideo(video);
56
+ else video.pause();
57
+ }
58
+
59
+ toggleMuted() {
60
+ const video = this.ref.value;
61
+ if (video) video.muted = !video.muted;
62
+ }
63
+
64
+ /** Listener for the video's play, pause and volumechange events. */
65
+ readonly sync = () => {
66
+ const video = this.ref.value;
67
+ if (!video) return;
68
+ this.paused = video.paused;
69
+ this.muted = video.muted;
70
+ this.host.requestUpdate();
71
+ };
72
+
73
+ /** Listener for the video's error event. */
74
+ readonly fail = () => {
75
+ this.failed = true;
76
+ this.host.requestUpdate();
77
+ };
78
+ }
@@ -1,2 +1,2 @@
1
- export { createPlayer, playerStyles, showPlayer } from "./player";
1
+ export { PlayerController, playerStyles } from "./player";
2
2
  export type { PlayerOptions } from "./player";