|
| 1 | +export const ExamplePlayer = ({ src, poster, title, className }) => { |
| 2 | + // Keep this bootstrap self-contained: Mintlify evaluates snippet exports only. |
| 3 | + const mount = (source, image) => { |
| 4 | + const fallback = document.createElement("video"); |
| 5 | + fallback.controls = true; |
| 6 | + fallback.playsInline = true; |
| 7 | + fallback.preload = "metadata"; |
| 8 | + fallback.poster = image || ""; |
| 9 | + fallback.style.cssText = "display:block;width:100%;height:100%;object-fit:contain"; |
| 10 | + document.body.appendChild(fallback); |
| 11 | + const upgrade = () => { |
| 12 | + if (!customElements.get("hyperframes-player") || !Number.isFinite(fallback.duration)) return; |
| 13 | + const width = fallback.videoWidth || 1920; |
| 14 | + const height = fallback.videoHeight || 1080; |
| 15 | + const composition = document.createElement("div"); |
| 16 | + composition.setAttribute("data-composition-id", "example-film"); |
| 17 | + composition.setAttribute("data-width", String(width)); |
| 18 | + composition.setAttribute("data-height", String(height)); |
| 19 | + composition.setAttribute("data-duration", String(fallback.duration)); |
| 20 | + const clip = document.createElement("video"); |
| 21 | + clip.src = source; |
| 22 | + clip.className = "clip"; |
| 23 | + clip.setAttribute("data-start", "0"); |
| 24 | + clip.setAttribute("data-duration", String(fallback.duration)); |
| 25 | + clip.setAttribute("playsinline", ""); |
| 26 | + clip.style.cssText = "width:100%;height:100%;object-fit:contain"; |
| 27 | + composition.appendChild(clip); |
| 28 | + const player = document.createElement("hyperframes-player"); |
| 29 | + // Fit posters in published player builds that predate the sizing fix. |
| 30 | + const posterStyle = document.createElement("style"); |
| 31 | + posterStyle.textContent = ".hfp-poster{width:100%;height:100%}"; |
| 32 | + player.shadowRoot.appendChild(posterStyle); |
| 33 | + player.setAttribute("controls", ""); |
| 34 | + player.setAttribute("low-power-idle", ""); |
| 35 | + player.setAttribute("width", String(width)); |
| 36 | + player.setAttribute("height", String(height)); |
| 37 | + if (image) player.setAttribute("poster", image); |
| 38 | + player.style.cssText = "position:absolute;inset:0;width:100%;height:100%;visibility:hidden"; |
| 39 | + player.setAttribute("srcdoc", "<!doctype html><html><head><style>html,body{margin:0;width:100%;height:100%}body>div{width:100%;height:100%}</style></head><body>" + composition.outerHTML + "</body></html>"); |
| 40 | + player.addEventListener("ready", () => { |
| 41 | + const resume = !fallback.paused; |
| 42 | + player.seek(fallback.currentTime); |
| 43 | + player.muted = fallback.muted; |
| 44 | + player.volume = fallback.volume; |
| 45 | + fallback.pause(); |
| 46 | + fallback.removeAttribute("src"); |
| 47 | + fallback.load(); |
| 48 | + fallback.remove(); |
| 49 | + player.style.visibility = "visible"; |
| 50 | + if (resume) player.play(); |
| 51 | + }, { once: true }); |
| 52 | + document.body.appendChild(player); |
| 53 | + }; |
| 54 | + const script = document.createElement("script"); |
| 55 | + script.src = "https://cdn.jsdelivr.net/npm/@hyperframes/player@latest/dist/hyperframes-player.global.js"; |
| 56 | + script.onload = () => { |
| 57 | + if (fallback.readyState >= 1) upgrade(); |
| 58 | + else fallback.addEventListener("loadedmetadata", upgrade, { once: true }); |
| 59 | + }; |
| 60 | + document.head.appendChild(script); |
| 61 | + fallback.src = source; |
| 62 | + }; |
| 63 | + const args = JSON.stringify([src, poster]).replace(/</g, "\\u003c"); |
| 64 | + const html = "<!doctype html><html><head><meta charset='utf-8'><style>html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#111}</style>" + |
| 65 | + "</head><body><script>(" + |
| 66 | + mount.toString() + ")(..." + args + ");</" + "script></body></html>"; |
| 67 | + return <iframe title={title} className={className} style={{ display: "block", width: "100%", aspectRatio: "16 / 9", border: 0 }} srcDoc={html} loading="lazy" allow="fullscreen" allowFullScreen />; |
| 68 | +}; |
0 commit comments