瀏覽代碼

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

* fix: 菜单名称过长添加tooltip
一万 3 年之前
父節點
當前提交
ae72cccbb9
共有 2 個文件被更改,包括 68 次插入4 次删除
  1. 67 4
      src/layout/components/sidebar/sidebarItem.vue
  2. 1 0
      src/layout/types.ts

+ 67 - 4
src/layout/components/sidebar/sidebarItem.vue

@@ -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">
-          <span>{{ $t(onlyOneChild.meta.title) }}</span>
+        <div style="display: flex; align-items: center; overflow: hidden">
+          <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"

+ 1 - 0
src/layout/types.ts

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