Skip to content

Instantly share code, notes, and snippets.

@zoxon
Created May 2, 2018 08:09
Show Gist options
  • Save zoxon/37dfddb313aa663896463a05afd1c644 to your computer and use it in GitHub Desktop.
Save zoxon/37dfddb313aa663896463a05afd1c644 to your computer and use it in GitHub Desktop.
Start and pause youtube video on open and close modal window
// Micromodal
import MicroModal from "micromodal";
const youtubeCommand = func =>
window.JSON.stringify({ event: "command", func });
const execCommand = (frame, command) => {
if (null === frame.contentWindow) {
return;
}
frame.contentWindow.postMessage(
youtubeCommand(command),
"https://www.youtube.com"
);
};
const modalHandler = (modal, command) => {
const iframes = modal.querySelectorAll("iframe");
[].forEach.call(iframes, frame => {
execCommand(frame, command);
});
};
MicroModal.init({
hasAnimation: true,
onClose: modal => {
modalHandler(modal, "pauseVideo");
},
onShow: modal => {
modalHandler(modal, "playVideo");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment