文本溢出组件封装
文本溢出检测与提示: 当文本内容超出容器宽度时自动显示省略号,并在hover时显示完整内容的Tooltip。类似 el-table 的 show-overflow-tooltip 效果
1. 涉及知识点
- lodash的 throttle 方法
- 浏览器内置API ResizeObserver
- JSX语法
2. 原理
使用ResizeObserver监听指定元素的宽度,对比元素实际内容宽度与元素可视区域宽度,控制悬浮是否展示,结合样式实现文本溢出...展示
3. 代码实现
- index.js
import { throttle } from 'lodash';
import './index.less';
export default {
name: 'qEllipsis',
inheritAttrs: false,
props: {
content: {
type: [String, Number],
default: null
}
},
data() {
return {
disabled: false,
resizeObserverInstance: null
};
},
mounted() {
this.init();
},
beforeDestroy() {
if (this.resizeObserverInstance) {
this.resizeObserverInstance.disconnect?.();
this.resizeObserverInstance = null;
}
},
methods: {
init() {
const { contentRef } = this.$refs;
if (!contentRef) {
return;
}
const resizeObserverInstance = new ResizeObserver(
throttle(entries => {
entries.forEach(item => {
if (item.target !== contentRef) {
return;
}
const { scrollWidth, clientWidth } = contentRef;
this.disabled = clientWidth >= scrollWidth;
});
}, 1e2)
);
resizeObserverInstance.observe(contentRef);
this.resizeObserverInstance = resizeObserverInstance;
}
},
render() {
const { content, disabled, $listeners, $attrs, $slots } = this;
const tooltipAttrs = {
placement: 'top',
...$attrs,
disabled
};
return (
<el-tooltip {...{ attrs: tooltipAttrs }}>
<template slot="content">{$slots.content || content || $slots.default}</template>
<div ref="contentRef" class="q-ellipsis" {...{ on: $listeners }}>
{$slots.content || content || $slots.default}
</div>
</el-tooltip>
);
}
};
- index.less
.q-ellipsis {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
4. 使用示例
<!-- 三种使用方式 -->
<Ellipsis content="简短文本" effect="light" />
<Ellipsis effect="light">
<template #content>长文本内容...</template>
</Ellipsis>
<Ellipsis effect="light">
默认插槽内容...
</Ellipsis>