|
| 1 | +<script setup> |
| 2 | +const props = defineProps({ |
| 3 | + title: { type: String, default: 'YouTube video player' }, |
| 4 | + style: { type: String, default: 'margin: 16px 0' }, |
| 5 | + src: { type: String, default: null }, // Source URL overrides all other parameters |
| 6 | + videoId: { type: String, default: null }, // Video ID used with other parameters |
| 7 | + autoplay: { type: String, default: '0' }, |
| 8 | + controls: { type: String, default: '1' }, |
| 9 | + loop: { type: String, default: '0' }, |
| 10 | + rel: { type: String, default: '0' }, |
| 11 | + start: { type: String, default: null }, |
| 12 | +}) |
| 13 | +
|
| 14 | +let srcUrl |
| 15 | +if (props.src) { |
| 16 | + srcUrl = props.src |
| 17 | +} else if (props.videoId) { |
| 18 | + const params = new URLSearchParams({ |
| 19 | + autoplay: props.autoplay, |
| 20 | + controls: props.controls, |
| 21 | + loop: props.loop, |
| 22 | + rel: props.rel, |
| 23 | + }) |
| 24 | + if (props.start) params.set('start', props.start) |
| 25 | + srcUrl = `https://www.youtube-nocookie.com/embed/${props.videoId}?${params.toString()}` |
| 26 | +} else { |
| 27 | + console.warn('%c YouTubeEmbed', 'color: Red', 'Missing param: videoId or src') |
| 28 | +} |
| 29 | +// console.debug('%c YouTubeEmbed', 'color: Red', 'srcUrl:', srcUrl) |
| 30 | +</script> |
| 31 | + |
| 32 | +<template> |
| 33 | + <iframe |
| 34 | + width="100%" |
| 35 | + :style="props.style" |
| 36 | + :src="srcUrl" |
| 37 | + :title="props.title" |
| 38 | + frameborder="0" |
| 39 | + allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" |
| 40 | + referrerpolicy="strict-origin-when-cross-origin" |
| 41 | + allowfullscreen |
| 42 | + ></iframe> |
| 43 | +</template> |
| 44 | + |
| 45 | +<style scoped> |
| 46 | +iframe { |
| 47 | + aspect-ratio: 16 / 9; |
| 48 | +} |
| 49 | +</style> |
0 commit comments