Files
cz-web/src/components/EllipsisText.vue
2026-10-10 09:57:56 +08:00

70 lines
1.3 KiB
Vue

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { ElTooltip } from 'element-plus'
interface Props {
text: string
placement?: 'top' | 'bottom' | 'left' | 'right'
}
const props = withDefaults(defineProps<Props>(), {
placement: 'top',
})
const root = ref<HTMLElement | null>(null)
const overflow = ref(false)
function check(): void {
const el = root.value
if (!el) return
overflow.value = el.scrollWidth > el.clientWidth + 1
}
let ro: ResizeObserver | null = null
onMounted(() => {
check()
if (root.value && typeof ResizeObserver !== 'undefined') {
ro = new ResizeObserver(() => check())
ro.observe(root.value)
}
})
onBeforeUnmount(() => {
ro?.disconnect()
ro = null
})
watch(
() => props.text,
() => check(),
)
</script>
<template>
<span ref="root" class="ellipsis-text">
<ElTooltip :content="text" :disabled="!overflow" :placement="placement" effect="dark">
<span class="ellipsis-text__inner">{{ text }}</span>
</ElTooltip>
</span>
</template>
<style lang="less" scoped>
.ellipsis-text {
display: block;
width: 100%;
margin-top: 6px;
font-size: @font-size-base;
font-weight: 600;
color: @profile-ink;
line-height: 20px;
.ellipsis();
&__inner {
display: inline-block;
max-width: 100%;
vertical-align: bottom;
}
}
</style>