Files

95 lines
1.9 KiB
Vue

<template>
<div :class="$style.container">
<iframe
ref="iframe"
:class="$style.iframe"
width="100%"
height="100%"
:src="url.href"
:title="$props.title"
frameborder="0"
allow="
accelerometer;
autoplay;
encrypted-media;
gyroscope;
picture-in-picture;
fullscreen;
clipboard-write;
web-share;
"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen
>
</iframe>
</div>
</template>
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, useTemplateRef } from "vue";
const props = defineProps<{
id?: string;
list?: string;
title?: string;
}>();
const url = new URL(
`https://www.youtube.com/embed/${props.id || "videoseries"}`,
);
url.searchParams.set("enablejsapi", "1");
if (props.list) {
url.searchParams.set("list", props.list);
}
const iframe = useTemplateRef("iframe");
const player = ref();
function onOrientationChanged() {
if (player.value && iframe.value) {
if (
window.screen.orientation.type.startsWith("landscape") &&
player.value.getPlayerState() === 1
) {
iframe.value.requestFullscreen();
return;
}
}
if (document.fullscreenElement) {
document.exitFullscreen();
}
}
onMounted(() => {
const { YT } = window as any;
if (YT?.Player && iframe.value) {
player.value = new YT.Player(iframe.value);
}
window.screen.orientation.addEventListener("change", onOrientationChanged);
});
onBeforeUnmount(() => {
window.screen.orientation.removeEventListener("change", onOrientationChanged);
if (document.fullscreenElement) {
document.exitFullscreen();
}
});
</script>
<style module>
.container {
padding-bottom: 56.25%;
position: relative;
}
.iframe {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
</style>