forked from Easterok/telegram-onboarding-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoPreset.vue
139 lines (112 loc) · 2.85 KB
/
VideoPreset.vue
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
<div v-bind="props" :class="$style.container">
<video
v-if="loaded"
ref="videoRef"
playsinline
muted
loop
:controls="false"
:class="$style.video"
:poster="loadedPoster"
>
<source :src="loaded" type="video/mp4" />
Your browser does not support the video tag.
</video>
<div v-if="!videoPlaying" :class="$style.tapAnimation" />
<button v-if="!videoPlaying" :class="$style.fallback" @click="forceRefresh">
Video not playing?<br />Tap here
</button>
</div>
</template>
<script setup lang="ts">
import { WAS_INTERACTION_TOKEN } from '@tok/generation/tokens';
import { noop } from '@tok/ui/utility/noop';
import { inject, ref, toRefs, watch } from 'vue';
import { VideoPresetProps } from './Media.preset.props';
import { useLoadedImage } from './useLoadedImage';
const props = defineProps<VideoPresetProps>();
const { src, poster } = toRefs(props);
const loaded = useLoadedImage(src);
const loadedPoster = useLoadedImage(poster);
const videoRef = ref<HTMLVideoElement | null>(null);
const wasInteraction = inject(WAS_INTERACTION_TOKEN, ref(false));
// Required for iOS devices to initiate the first interaction with the page;
// otherwise, the video will not play automatically
// Note: MainButton is located outside of the miniapp, so the browser doesn't register the first interaction event
const forceRefreshEvents = ref(NaN);
const videoPlaying = ref(false);
const forceRefresh = () => {
forceRefreshEvents.value = Date.now();
};
const onVideoPlay = () => {
videoPlaying.value = true;
};
watch(
[videoRef, loaded, wasInteraction, forceRefreshEvents],
([_video], _, onCleanup) => {
onCleanup(() => {
_video?.removeEventListener('play', onVideoPlay);
});
if (_video) {
_video.addEventListener('play', onVideoPlay);
_video.play().catch(noop);
}
},
{ immediate: true }
);
</script>
<style lang="scss" module>
@import '@tok/ui/styles/local.scss';
.container {
position: relative;
aspect-ratio: 16/9;
}
.video {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -1;
background: transparent;
}
.fallback {
@include clearbutton;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
font: var(--tok-body-xs);
color: var(--tok-text-color-64);
}
.tapAnimation {
position: absolute;
left: 50%;
top: 50%;
width: 2rem;
height: 2rem;
border-radius: 100%;
pointer-events: none;
user-select: none;
background: var(--tok-text-color-16);
transform: translate(-50%, -50%);
margin-left: -1rem;
margin-top: -1rem;
animation: _tapAnimation 1s infinite ease-in-out;
}
@keyframes _tapAnimation {
0% {
opacity: 0;
transform: scale(0);
}
60% {
opacity: 0.82;
}
100% {
opacity: 0;
transform: scale(2);
}
}
</style>