Skip to content

fix: make sure the play button works without any issues on all browsers #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions apps/website/src/sections/Spotlight.astro
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,45 @@ import PlayButtonSymbol from "../assets/videos/play-button-symbol.svg";
document.addEventListener("DOMContentLoaded", () => {
const video = document.querySelector("video") as HTMLVideoElement;
const playButton = document.querySelector(".play-button") as HTMLButtonElement;
let wasSeeking = false;

const togglePlayButton = () => {
if (!video.seeking && !wasSeeking) {
playButton.classList.toggle("hidden");
video.toggleAttribute("controls");
} else {
wasSeeking = !wasSeeking;
}

let ignoreNextClick = false;
let videoPlaying = false;
video.controls = false;

const updatePlayButtonVisibility = () => {
const showPlayButton = video.paused && !video.seeking;
playButton.classList.toggle("hidden", !showPlayButton);
};

playButton.addEventListener("click", () => video.play());
video.addEventListener("play", togglePlayButton);
video.addEventListener("pause", togglePlayButton);
video.addEventListener("click", () => {
if (video.paused) {
video.play();
} else {
video.pause();
}
playButton.addEventListener("click", (e) => {
e.stopPropagation();
ignoreNextClick = true;
video.controls = true;
videoPlaying = true;
video.play();
});

if (videoPlaying) {
video.addEventListener("click", () => {
if (ignoreNextClick) {
ignoreNextClick = false;
return;
}

if (video.paused) {
video.controls = true;
video.play();
} else {
video.pause();
}
});
}

video.addEventListener("play", updatePlayButtonVisibility);
video.addEventListener("pause", updatePlayButtonVisibility);
video.addEventListener("seeking", updatePlayButtonVisibility);
video.addEventListener("seeked", updatePlayButtonVisibility);
updatePlayButtonVisibility();
});
</script>

Expand Down