fix: 菜单名称过长添加tooltip (#78)

* fix: 菜单名称过长添加tooltip
This commit is contained in:
一万 2021-10-29 18:41:57 +08:00 committed by GitHub
parent 3389f3118a
commit ae72cccbb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 4 deletions

View File

@ -1,8 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import path from "path"; import path from "path";
import { PropType, ref } from "vue"; import { storageLocal } from "/@/utils/storage";
import { PropType, ref, nextTick } from "vue";
import { childrenType } from "../../types"; import { childrenType } from "../../types";
import Icon from "/@/components/ReIcon/src/Icon.vue"; import Icon from "/@/components/ReIcon/src/Icon.vue";
const layout = ref(
storageLocal.getItem("responsive-layout") || "vertical-dark"
);
const menuMode = layout.value.layout.split("-")[0] === "vertical";
const props = defineProps({ const props = defineProps({
item: { item: {
@ -19,6 +24,28 @@ const props = defineProps({
}); });
const onlyOneChild: childrenType = ref(null); const onlyOneChild: childrenType = ref(null);
// showTooltip
const hoverMenuMap = new WeakMap();
// dom
const menuTextRef = ref(null);
function hoverMenu(key) {
// showTooltip退
if (hoverMenuMap.get(key)) return;
nextTick(() => {
//
menuTextRef.value?.scrollWidth > menuTextRef.value?.clientWidth
? Object.assign(key, {
showTooltip: true
})
: Object.assign(key, {
showTooltip: false
});
hoverMenuMap.set(key, true);
});
}
function hasOneShowingChild( function hasOneShowingChild(
children: childrenType[] = [], children: childrenType[] = [],
@ -64,8 +91,23 @@ function resolvePath(routePath) {
" "
/> />
<template #title> <template #title>
<div style="display: flex; align-items: center"> <div style="display: flex; align-items: center; overflow: hidden">
<span>{{ $t(onlyOneChild.meta.title) }}</span> <span v-if="!menuMode">{{ $t(onlyOneChild.meta.title) }}</span>
<el-tooltip
v-else
placement="top"
:offset="-10"
:disabled="!onlyOneChild.showTooltip"
>
<template #content> {{ $t(onlyOneChild.meta.title) }} </template>
<span
ref="menuTextRef"
style="overflow: hidden; text-overflow: ellipsis"
@mouseover="hoverMenu(onlyOneChild)"
>
{{ $t(onlyOneChild.meta.title) }}
</span>
</el-tooltip>
<Icon <Icon
v-if="onlyOneChild.meta.extraIcon" v-if="onlyOneChild.meta.extraIcon"
:svg="onlyOneChild.meta.extraIcon.svg ? true : false" :svg="onlyOneChild.meta.extraIcon.svg ? true : false"
@ -84,7 +126,28 @@ function resolvePath(routePath) {
> >
<template #title> <template #title>
<i v-show="props.item.meta.icon" :class="props.item.meta.icon"></i> <i v-show="props.item.meta.icon" :class="props.item.meta.icon"></i>
<span>{{ $t(props.item.meta.title) }}</span> <span v-if="!menuMode">{{ $t(props.item.meta.title) }}</span>
<el-tooltip
v-else
placement="top"
:offset="-10"
:disabled="!props.item.showTooltip"
>
<template #content> {{ $t(props.item.meta.title) }} </template>
<div
ref="menuTextRef"
style="
display: inline-block;
overflow: hidden;
text-overflow: ellipsis;
width: 125px;
"
@mouseover="hoverMenu(props.item)"
>
{{ $t(props.item.meta.title) }}
</div>
</el-tooltip>
<Icon <Icon
v-if="props.item.meta.extraIcon" v-if="props.item.meta.extraIcon"
:svg="props.item.meta.extraIcon.svg ? true : false" :svg="props.item.meta.extraIcon.svg ? true : false"

View File

@ -62,4 +62,5 @@ export type childrenType = {
name?: string; name?: string;
}; };
}; };
showTooltip?: boolean;
}; };