UnisKB/ui/src/components/auto-tooltip/index.vue

40 lines
1.0 KiB
Vue
Raw Normal View History

2024-01-25 07:08:25 +00:00
<template>
<el-tooltip
v-bind="$attrs"
:disabled="!(containerWeight > contentWeight)"
effect="dark"
placement="bottom"
2024-04-17 13:35:03 +00:00
popper-class="auto-tooltip-popper"
2024-01-25 07:08:25 +00:00
>
2024-03-05 11:01:24 +00:00
<div ref="tagLabel" :class="['auto-tooltip', className]" :style="style">
2024-01-25 07:08:25 +00:00
<slot></slot>
</div>
</el-tooltip>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, nextTick } from 'vue'
defineOptions({ name: 'AutoTooltip' })
2024-03-05 11:01:24 +00:00
const props = defineProps({ className: String, style: Object })
2024-01-25 07:08:25 +00:00
const tagLabel = ref()
const containerWeight = ref(0)
const contentWeight = ref(0)
onMounted(() => {
nextTick(() => {
containerWeight.value = tagLabel.value?.scrollWidth
contentWeight.value = tagLabel.value?.clientWidth
})
window.addEventListener('resize', function () {
containerWeight.value = tagLabel.value?.scrollWidth
contentWeight.value = tagLabel.value?.clientWidth
})
})
</script>
<style lang="scss" scoped>
.auto-tooltip {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
</style>