Line data Source code
1 : <template>
2 640 : <div ref="containerRef" class="w-full">
3 : <div
4 : ref="measureRef"
5 : class="pointer-events-none invisible absolute inset-x-0 top-0 overflow-hidden whitespace-nowrap"
6 : aria-hidden="true"
7 : v-text="text"
8 : />
9 : <div class="flex w-full flex-col">
10 800 : <MarqueeLine v-for="(line, index) in lines" :key="index">
11 : {{ line }}
12 : </MarqueeLine>
13 : </div>
14 : </div>
15 : </template>
16 :
17 : <script setup lang="ts">
18 : import { useElementSize } from '@vueuse/core'
19 : import { computed, useTemplateRef } from 'vue'
20 :
21 : import MarqueeLine from './MarqueeLine.vue'
22 : import { splitTextAtWordBoundary } from './textTickerUtils'
23 :
24 : const { text } = defineProps<{ text: string }>()
25 :
26 160 : const measureRef = useTemplateRef('measureRef')
27 160 : const containerRef = useTemplateRef('containerRef')
28 160 : const { width: textWidth } = useElementSize(measureRef)
29 160 : const { width: containerWidth } = useElementSize(containerRef)
30 :
31 160 : const lines = computed(() => {
32 640 : if (!textWidth.value || !containerWidth.value) return [text]
33 :
34 160 : const safeRenderWidth = containerWidth.value - 30
35 640 : if (textWidth.value <= safeRenderWidth) return [text]
36 :
37 160 : return splitTextAtWordBoundary(text, safeRenderWidth / textWidth.value)
38 : })
39 : </script>
|