-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathMediaVideoTop.vue
More file actions
78 lines (66 loc) · 1.74 KB
/
Copy pathMediaVideoTop.vue
File metadata and controls
78 lines (66 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<template>
<div
class="relative size-full overflow-hidden rounded-sm bg-black"
@mouseenter="isHovered = true"
@mouseleave="isHovered = false"
>
<video
ref="videoElement"
:controls="shouldShowControls"
preload="metadata"
muted
loop
playsinline
class="relative size-full object-contain transition-transform duration-300 group-hover:scale-105 group-data-[selected=true]:scale-105"
@click="onVideoClick"
@play="onVideoPlay"
@pause="onVideoPause"
>
<source
v-if="asset.src"
:src="asset.src"
:type="asset.mime_type ?? undefined"
/>
</video>
<VideoPlayOverlay :visible="!isPlaying" size="md" />
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import type { AssetMeta } from '../schemas/mediaAssetSchema'
import VideoPlayOverlay from './VideoPlayOverlay.vue'
const { asset, showNativeControls = true } = defineProps<{
asset: AssetMeta
showNativeControls?: boolean
}>()
const videoElement = ref<HTMLVideoElement | null>(null)
const isHovered = ref(false)
const isPlaying = ref(false)
// Show native controls only while actively playing and hovered.
const shouldShowControls = computed(
() => showNativeControls && isPlaying.value && isHovered.value
)
const onVideoPlay = () => {
isPlaying.value = true
}
const onVideoPause = () => {
isPlaying.value = false
}
async function onVideoClick(event: MouseEvent) {
if (
event.shiftKey ||
event.metaKey ||
event.ctrlKey ||
shouldShowControls.value
) {
return
}
const video = videoElement.value
if (!video) return
if (video.paused || video.ended) {
await video.play().catch(() => {})
return
}
video.pause()
}
</script>