summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/MkMarqueeText.vue
blob: a2c365afe96157fa4a88ca7c12344eaad4d88d79 (plain)
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
<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<div :class="$style.wrap">
	<span
		ref="contentEl"
		:class="[$style.content, {
			[$style.paused]: paused,
			[$style.reverse]: reverse,
		}]"
	>
		<span v-for="key in repeat" :key="key" :class="$style.text">
			<slot></slot>
		</span>
	</span>
</div>
</template>

<script lang="ts" setup>
import { onMounted, useTemplateRef, watch } from 'vue';

const props = withDefaults(defineProps<{
	duration?: number;
	repeat?: number;
	paused?: boolean;
	reverse?: boolean;
}>(), {
	duration: 15,
	repeat: 2,
	paused: false,
	reverse: false,
});

const contentEl = useTemplateRef('contentEl');

function calcDuration() {
	if (contentEl.value == null) return;
	const eachLength = contentEl.value.offsetWidth / props.repeat;
	const factor = 3000;
	const duration = props.duration / ((1 / eachLength) * factor);
	contentEl.value.style.animationDuration = `${duration}s`;
}

watch(() => props.duration, calcDuration);

onMounted(calcDuration);
</script>

<style lang="scss" module>
.wrap {
	overflow: clip;
	animation-play-state: running;

	&:hover {
		animation-play-state: paused;
	}
}

.content {
	display: inline-block;
	white-space: nowrap;
	animation-play-state: inherit;
}

.text {
	display: inline-block;
	animation-name: marquee;
	animation-timing-function: linear;
	animation-iteration-count: infinite;
	animation-duration: inherit;
	animation-play-state: inherit;
}

.paused .text {
	animation-play-state: paused;
}

.reverse .text {
	animation-direction: reverse;
}

@keyframes marquee {
	0% { transform: translateX(0); }
	100% { transform: translateX(-100%); }
}
</style>