Skip to content

Commit

Permalink
Allow browser back button to close youtube player overlay window
Browse files Browse the repository at this point in the history
  • Loading branch information
mkanet committed Jun 26, 2024
1 parent 5feb046 commit c304cb4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.9
0.6.0
10 changes: 10 additions & 0 deletions upcoming-media-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class UpcomingMediaCard extends HTMLElement {
const closeOverlayAndCleanup = () => {
clearInterval(checkVideoEndedInterval);
window.removeEventListener('message', handleMessage);
window.removeEventListener('popstate', handlePopState);
closeOverlay();
};
const handleMessage = (event) => {
Expand All @@ -59,7 +60,11 @@ class UpcomingMediaCard extends HTMLElement {
}
}
};
const handlePopState = (event) => {
closeOverlayAndCleanup();
};
window.addEventListener('message', handleMessage);
window.addEventListener('popstate', handlePopState);
const closeButton = document.createElement('button');
closeButton.innerHTML = '×';
closeButton.style.position = 'absolute';
Expand Down Expand Up @@ -118,8 +123,12 @@ class UpcomingMediaCard extends HTMLElement {
const closeOverlay = () => {
clearInterval(checkVideoEndedInterval);
window.removeEventListener('message', handleMessage);
window.removeEventListener('popstate', handlePopState);
document.body.removeChild(overlay);
document.body.style.overflow = '';
if (history.state && history.state.overlayOpen) {
history.back();
}
};
closeButton.onclick = closeOverlay;
const handleKeyPress = (event) => {
Expand All @@ -131,6 +140,7 @@ class UpcomingMediaCard extends HTMLElement {
overlay.addEventListener('remove', () => {
document.removeEventListener('keydown', handleKeyPress);
});
history.pushState({ overlayOpen: true }, '');
return overlay;
};
let touchStartTime;
Expand Down

0 comments on commit c304cb4

Please sign in to comment.