Created
May 2, 2018 08:09
-
-
Save zoxon/37dfddb313aa663896463a05afd1c644 to your computer and use it in GitHub Desktop.
Start and pause youtube video on open and close modal window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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