v-overflow-ellipsis
v0.1.0
Published
溢出时才显示提示的 Vue 3 文本省略组件
Maintainers
Readme
v-overflow-ellipsis
Vue 3 文本省略组件,组件名为 VEllipsis,模板里写作 <v-ellipsis>。内容超出容器时才弹出 Element Plus 的文字提示,未溢出时不显示气泡。
只提供 ESM。
安装
pnpm add v-overflow-ellipsis
pnpm add vue element-plusvue 和 element-plus 是 peer 依赖,需要由使用方安装并注册。
| 依赖 | 版本 | | --- | --- | | vue | ^3.5.0 | | element-plus | ^2.11.0 |
注册
在应用入口注册 Element Plus,并单独引入本包样式。构建工具不会自动带上这份 CSS。
import { createApp } from "vue";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import "v-overflow-ellipsis/style.css";
import App from "./App.vue";
createApp(App).use(ElementPlus).mount("#app");示例
<script setup lang="ts">
import { VEllipsis } from "v-overflow-ellipsis";
</script>
<template>
<div style="width: 180px">
<VEllipsis content="很长的标题" placement="top-start" :max-width="320" />
</div>
</template>不传 content 时,气泡文案取自默认插槽的文本。需要判断输入框、下拉框内部是否溢出时,设置 measureSelector:
<VEllipsis
:content="name"
measure-selector=".el-input__inner"
:max-width="280"
>
<el-input v-model="name" />
</VEllipsis>本地示例在 playground/。仓库内执行 pnpm dev 后打开开发服务器即可查看。
属性
| 属性 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| content | string \| number \| null | — | 气泡文案。为空时使用插槽文本 |
| placement | 同 el-tooltip | top-start | 气泡位置 |
| effect | "light" \| "dark" | light | 气泡主题 |
| tooltipClass | string | "" | 加到气泡上的类名 |
| maxWidth | string \| number | "" | 气泡最大宽度。数字按 px 处理 |
| measureSelector | string | "" | 用这个选择器测量内部元素是否溢出 |
| enterable | boolean | 自动 | 鼠标能否移入气泡。不传时,触屏设备为 false |
根节点会透传 class 和原生事件。组件自身是一个 inline-block,宽度跟父容器走。父容器需要有明确宽度,省略才会生效。
本地开发
pnpm install
pnpm dev
pnpm buildpnpm build 产物在 dist/:index.js、index.d.ts、style.css。发布前会自动执行构建。
许可
MIT
