Line data Source code
1 2 : <template>
2 3 : <div v-if="sources.length && !failed" class="relative">
3 : <video
4 : ref="video"
5 : v-bind="$attrs"
6 : :poster="posterSrc || undefined"
7 : :autoplay="shouldAutoplay"
8 : :muted
9 : :loop
10 : :playsinline="playsInline"
11 : @play="playing = true"
12 : @pause="playing = false"
13 : @error="failed = true"
14 : >
15 6 : <source
16 : v-for="(source, index) in sources"
17 : :key="source.src"
18 : :src="source.src"
19 : :type="source.type"
20 : @error="failed = index === sources.length - 1"
21 : />
22 : </video>
23 : <Button
24 : v-if="playbackLabels"
25 : variant="overlay-white"
26 : size="icon"
27 : class="absolute right-3 bottom-3"
28 : :aria-label="playing ? playbackLabels.pause : playbackLabels.play"
29 : @click="togglePlayback"
30 : >
31 : <i
32 : :class="playing ? 'icon-[lucide--pause]' : 'icon-[lucide--play]'"
33 : aria-hidden="true"
34 : />
35 : </Button>
36 : </div>
37 : <slot v-else name="fallback" />
38 : </template>
39 :
40 : <script setup lang="ts">
41 : import { usePreferredReducedMotion } from '@vueuse/core'
42 : import { computed, ref, useTemplateRef, watchEffect } from 'vue'
43 :
44 : import Button from '@comfyorg/design-system/button/Button'
45 :
46 : const {
47 : sources = [],
48 : posterSrc = '',
49 : autoplay = false,
50 : muted = false,
51 : loop = false,
52 : playsInline = false,
53 : playbackLabels
54 : } = defineProps<{
55 : sources?: Array<{ src: string; type: string }>
56 : posterSrc?: string
57 : autoplay?: boolean
58 : muted?: boolean
59 : loop?: boolean
60 : playsInline?: boolean
61 : playbackLabels?: { play: string; pause: string }
62 : }>()
63 :
64 : defineOptions({ inheritAttrs: false })
65 :
66 2 : const video = useTemplateRef('video')
67 2 : const failed = ref(false)
68 2 : const playing = ref(false)
69 :
70 2 : const reducedMotion = usePreferredReducedMotion()
71 2 : const shouldAutoplay = computed(
72 : () => autoplay && reducedMotion.value !== 'reduce'
73 : )
74 :
75 2 : watchEffect(() => {
76 2 : if (autoplay && !shouldAutoplay.value) video.value?.pause()
77 : })
78 :
79 0 : function togglePlayback() {
80 0 : const element = video.value
81 0 : if (!element) return
82 0 : if (!element.paused) element.pause()
83 : else
84 0 : element.play().catch(() => {
85 0 : playing.value = false
86 : })
87 : }
88 : </script>
|