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C,EAAMC,cAE5B,CAEA,SAASF,EAAkBxB,GACzB,MAAMC,EAASD,EAAWE,cAAc,UAClCN,EAAQI,EAAWE,cAAc,SACvCD,EAAOM,aAAa,eAAgBX,EAAMiC,OAAS,OAAS,QAC9D,ECtEO,SAAeE,EACpBC,GACA,QAD2B,IAA3BA,IAAAA,EAA8B,oBACF,aAAxBC,SAASC,YAAuC,qBAATF,GACjB,gBAAxBC,SAASC,WAA+B,CACxC,MAAMC,EAAQC,WAAWL,EAAU,GAEnC,MAD8B,IAAMM,aAAaF,EAEnD,CACEhC,iBAAiB6B,EAAMD,EAI3B,CD4DAO,CA9GA,WACE,MAAMC,EAAcN,SAASO,iBAAiB,mCAC9C,IAAK,MAAMxC,KAAcuC,EACvBxC,EAAeC,EAEnB","sources":["webpack://lange/./lib/video.ts","webpack://lange/./sections/landing-video-hero/index.ts","webpack://lange/./lib/dom-ready.ts"],"sourcesContent":["/**\n * Try to play a video, and always report the outcome as a promise.\n *\n * Do not inline this function. Playing a video is inconsistent across browsers in several ways, and\n * wrapping the call in an async function is what smooths all of them over at once.\n *\n * Some browsers have no `play` function on the element at all, so the call throws a TypeError. Some\n * have one that returns undefined instead of a promise, which is how `play` worked before the spec\n * gained a return value. Some return a promise that rejects later, which is the modern behavior and\n * is how a browser reports that it refused to autoplay. And some throw synchronously, right out of\n * the call, rather than rejecting.\n *\n * An async function turns all four into the same thing. Whatever happens inside it, the caller gets\n * a promise back, so a synchronous throw arrives as a rejection and a missing or non-promise `play`\n * still produces a promise that resolves. That means one `catch` on the call site covers every\n * failure mode, and the caller never has to check whether it got a promise before attaching a\n * handler.\n *\n * Nothing here is actionable, because a refusal is the browser enforcing its own autoplay policy.\n * Restrictive in-app browsers and low power mode refuse even after a user gesture, and interrupted\n * or unsupported sources reject too. The video simply stays paused, so callers should keep a debug\n * breadcrumb and never warn.\n */\nexport async function play(video: HTMLVideoElement) {\n  return video.play();\n}\n","import { ready } from '../../lib/dom-ready.js';\nimport { play } from '../../lib/video.js';\n\n/**\n * Matches from 768px up, where the desktop video shows.\n */\nconst desktopQuery = matchMedia('(min-width: 768px)');\n\n/**\n * Wires up the background video of each hero on the page, together with its pause button. The video\n * plays on its own and keeps looping, and moving content like that needs a way to stop it for\n * visitors who find it distracting. The button is a toggle, and its aria-pressed state says whether\n * the video is paused.\n *\n * Liquid renders the desktop and the mobile video inside templates, so neither one loads with the\n * page. Only the one for the current screen width goes on the page, and it is swapped when the\n * width crosses the breakpoint, so a visitor downloads a single video.\n *\n * Each hero only looks inside itself, so every hero on a page works on its own.\n */\nfunction onReady() {\n  const backgrounds = document.querySelectorAll('.landing-video-hero .background');\n  for (const background of backgrounds) {\n    initBackground(background);\n  }\n}\n\nfunction initBackground(background: Element) {\n  const button = background.querySelector('button');\n  button.addEventListener('click', onButtonClicked);\n\n  // Play and pause do not bubble, so we listen in the capture phase to hear them from the video\n  // inside the background.\n  background.addEventListener('play', onPlaybackChanged, true);\n  background.addEventListener('pause', onPlaybackChanged, true);\n\n  desktopQuery.addEventListener('change', function onMediaQueryChanged() {\n    renderVideo(background);\n  });\n\n  renderVideo(background);\n\n  // Autoplay is skipped in the theme editor and for reduced motion, so a video without it starts\n  // out paused. A video that will autoplay may not have started yet, so we go by the attribute\n  // rather than by whether it is playing right now. The play and pause events take over from there,\n  // and so does the browser refusing autoplay.\n  const video = background.querySelector('video');\n  button.setAttribute('aria-pressed', video.autoplay ? 'false' : 'true');\n  button.hidden = false;\n}\n\n/**\n * Puts the video for the current screen width on the page, in place of the one already there. The\n * first video does not autoplay for a visitor who asked for reduced motion. After that, a visitor\n * who paused the old video expects the new one to stay paused, so it does not autoplay either.\n */\nfunction renderVideo(background: Element) {\n  const viewport = desktopQuery.matches ? 'desktop' : 'mobile';\n  const template = background.querySelector(`template[data-video-viewport=\"${viewport}\"]`);\n  if (!(template instanceof HTMLTemplateElement)) {\n    return;\n  }\n\n  const fragment = <DocumentFragment>template.content.cloneNode(true);\n  const video = fragment.querySelector('video');\n  video.setAttribute('aria-hidden', 'true');\n\n  const button = background.querySelector('button');\n  const current = background.querySelector('video');\n\n  if (current) {\n    const pressed = button.getAttribute('aria-pressed') === 'true';\n    if (pressed) {\n      video.autoplay = false;\n    }\n\n    current.replaceWith(video);\n  } else {\n    // A looping background video is the kind of motion the reduced motion setting asks us to hold\n    // back, so the visitor gets it paused and can still start it with the button.\n    if (matchMedia('(prefers-reduced-motion: reduce)').matches) {\n      video.autoplay = false;\n    }\n\n    button.before(video);\n  }\n\n  // A browser can refuse autoplay, for example in low power mode. The video then stays paused\n  // without firing a pause event, so the button would keep showing it as playing. Asking it to play\n  // ourselves gets us a promise that rejects when that happens.\n  if (video.autoplay) {\n    play(video).catch(error => {\n      // The rejection can also come from the video being paused or swapped out before it started,\n      // so we read the state of whatever video is there now rather than assuming it is paused.\n      console.debug(error);\n      renderPausedState(background);\n    });\n  }\n}\n\n/**\n * We go by whether the video is actually paused rather than by the button, since a browser can\n * refuse autoplay, for example in low power mode, and leave a video paused that the button shows as\n * playing.\n */\nfunction onButtonClicked(event: MouseEvent) {\n  if (!(event.currentTarget instanceof Element)) {\n    return;\n  }\n\n  const video = event.currentTarget.closest('.background').querySelector('video');\n  if (video.paused) {\n    play(video).catch(console.debug);\n  } else {\n    video.pause();\n  }\n}\n\nfunction onPlaybackChanged(event: Event) {\n  if (event.currentTarget instanceof Element) {\n    renderPausedState(event.currentTarget);\n  }\n}\n\nfunction renderPausedState(background: Element) {\n  const button = background.querySelector('button');\n  const video = background.querySelector('video');\n  button.setAttribute('aria-pressed', video.paused ? 'true' : 'false');\n}\n\nready(onReady);\n","/**\n * Releases the handle returned from the ready() function.\n *\n * This is idempotent. Additional calls are pointless but harmless.\n */\nexport type ReleaseHandle = () => void;\n\n/**\n * A subset of events that pertain to document ready state.\n */\ntype LoadingEventMap = Pick<DocumentEventMap, 'DOMContentLoaded' | 'load'>;\n\n/**\n * Calls the provided callback when the document is ready. This works both when the document is\n * already ready and when it is not, so callers do not have to care. This exists because it ensures\n * that each caller consistently expresses its dependency on document readiness, and to save a bit\n * of boilerplate.\n *\n * A document is considered ready when it has been parsed. This corresponds to the ready state of\n * 'interactive'. When the document has been fully loaded, the ready state is 'complete'. The\n * callback runs either way, so callers do not have to check which one they are in.\n *\n * Ready means the html is parsed and the DOM is there to query. It does not mean the page's other\n * scripts have run. Deferred scripts and module scripts run after the ready state turns\n * 'interactive' and before DOMContentLoaded fires, so a callback here can run before them. When a\n * caller needs some other script to have run already, this is the wrong tool, and it should wait on\n * whatever that script produces instead.\n *\n * That remedy has a catch of its own when the calling bundle is loaded with async. An async bundle\n * can execute after DOMContentLoaded has already fired, so a plain listener for that event never\n * runs and the work silently never happens. Nothing here can detect that, because by the time this\n * file executes the event is long gone. Catching it needs something registered at parse time,\n * before any bundle loads.\n *\n * There is a second trap right next to that one, and it applies only when waiting for\n * DOMContentLoaded. The ready state stays 'interactive' after DOMContentLoaded has fired, all the\n * way until 'complete', so the ready state cannot tell you whether DOMContentLoaded is still coming\n * or has already gone. Waiting for load is not exposed to this, because the ready state turns\n * 'complete' and load fires in the same task, so seeing 'complete' means load has fired too.\n *\n * When the document is already ready, the callback is invoked via setTimeout. This way, if the\n * callback throws, the error surfaces to the global error handler in a way that is similar to what\n * happens when an event listener throws.\n *\n * We might one day want to move this to an NPM library.\n *\n * Beware of a quirk here. setTimeout yields, and the busier the frontend, the longer it takes to\n * fire. Page load is exactly when the frontend is busiest. The timeout is a floor and not a\n * guarantee, so there is no real bound on how long that yield lasts. What this means is that using\n * this library can introduce more delay than you might expect. Compare code that runs on async load\n * and checks ready state and invokes immediately to code that runs after setTimeout. The former\n * runs in the same tick, the latter runs in \"some later tick\". That later tick can be surprisingly\n * much later. That can cause initial paint issues.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event\n * @see https://html.spec.whatwg.org/multipage/parsing.html#the-end\n */\nexport function ready(callback: (event?: Event) => void,\n  type: keyof LoadingEventMap = 'DOMContentLoaded') {\n  if (document.readyState === 'complete' || (type === 'DOMContentLoaded' &&\n    document.readyState === 'interactive')) {\n    const timer = setTimeout(callback, 0);\n    const handle: ReleaseHandle = () => clearTimeout(timer);\n    return handle;\n  } else {\n    addEventListener(type, callback);\n    const handle: ReleaseHandle = () => removeEventListener(type, callback);\n    return handle;\n  }\n}\n"],"names":["async","play","video","desktopQuery","matchMedia","initBackground","background","button","querySelector","addEventListener","onButtonClicked","onPlaybackChanged","renderVideo","setAttribute","autoplay","hidden","viewport","matches","template","HTMLTemplateElement","content","cloneNode","current","getAttribute","replaceWith","before","catch","error","console","debug","renderPausedState","event","currentTarget","Element","closest","paused","pause","callback","type","document","readyState","timer","setTimeout","clearTimeout","ready","backgrounds","querySelectorAll"],"sourceRoot":""}