Menu.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <script lang="tsx">
  2. import { computed, defineComponent, unref, PropType } from 'vue'
  3. import { ElMenu, ElScrollbar } from 'element-plus'
  4. import { useAppStore } from '@/store/modules/app'
  5. import { usePermissionStore } from '@/store/modules/permission'
  6. import { useRenderMenuItem } from './components/useRenderMenuItem'
  7. import { useRouter } from 'vue-router'
  8. import { isUrl } from '@/utils/is'
  9. import { useDesign } from '@/hooks/web/useDesign'
  10. import { LayoutType } from '@/types/layout'
  11. const { getPrefixCls } = useDesign()
  12. const prefixCls = getPrefixCls('menu')
  13. export default defineComponent({
  14. name: 'Menu',
  15. props: {
  16. menuSelect: {
  17. type: Function as PropType<(index: string) => void>,
  18. default: undefined
  19. }
  20. },
  21. setup(props) {
  22. const appStore = useAppStore()
  23. const layout = computed(() => appStore.getLayout)
  24. const { push, currentRoute } = useRouter()
  25. const permissionStore = usePermissionStore()
  26. const menuMode = computed((): 'vertical' | 'horizontal' => {
  27. // 竖
  28. const vertical: LayoutType[] = ['classic', 'topLeft', 'cutMenu']
  29. if (vertical.includes(unref(layout))) {
  30. return 'vertical'
  31. } else {
  32. return 'horizontal'
  33. }
  34. })
  35. const routers = computed(() =>
  36. unref(layout) === 'cutMenu' ? permissionStore.getMenuTabRouters : permissionStore.getRouters
  37. )
  38. const collapse = computed(() => appStore.getCollapse)
  39. const uniqueOpened = computed(() => appStore.getUniqueOpened)
  40. const activeMenu = computed(() => {
  41. const { meta, path } = unref(currentRoute)
  42. // if set path, the sidebar will highlight the path you set
  43. if (meta.activeMenu) {
  44. return meta.activeMenu as string
  45. }
  46. return path
  47. })
  48. const menuSelect = (index: string) => {
  49. if (props.menuSelect) {
  50. props.menuSelect(index)
  51. }
  52. // 自定义事件
  53. if (isUrl(index)) {
  54. window.open(index)
  55. } else {
  56. push(index)
  57. }
  58. }
  59. const renderMenuWrap = () => {
  60. if (unref(layout) === 'top') {
  61. return renderMenu()
  62. } else {
  63. return <ElScrollbar>{renderMenu()}</ElScrollbar>
  64. }
  65. }
  66. const renderMenu = () => {
  67. return (
  68. <ElMenu
  69. defaultActive={unref(activeMenu)}
  70. mode={unref(menuMode)}
  71. collapse={
  72. unref(layout) === 'top' || unref(layout) === 'cutMenu' ? false : unref(collapse)
  73. }
  74. uniqueOpened={unref(layout) === 'top' ? false : unref(uniqueOpened)}
  75. backgroundColor="var(--left-menu-bg-color)"
  76. textColor="var(--left-menu-text-color)"
  77. activeTextColor="var(--left-menu-text-active-color)"
  78. onSelect={menuSelect}
  79. >
  80. {{
  81. default: () => {
  82. const { renderMenuItem } = useRenderMenuItem(unref(menuMode))
  83. return renderMenuItem(unref(routers))
  84. }
  85. }}
  86. </ElMenu>
  87. )
  88. }
  89. return () => (
  90. <div
  91. id={prefixCls}
  92. class={[
  93. `${prefixCls} ${prefixCls}__${unref(menuMode)}`,
  94. 'h-[100%] overflow-hidden flex-col bg-[var(--left-menu-bg-color)]',
  95. {
  96. 'w-[var(--left-menu-min-width)]': unref(collapse) && unref(layout) !== 'cutMenu',
  97. 'w-[var(--left-menu-max-width)]': !unref(collapse) && unref(layout) !== 'cutMenu'
  98. }
  99. ]}
  100. >
  101. {renderMenuWrap()}
  102. </div>
  103. )
  104. }
  105. })
  106. </script>
  107. <style lang="less" scoped>
  108. @prefix-cls: ~'@{namespace}-menu';
  109. .is-active--after {
  110. position: absolute;
  111. top: 0;
  112. right: 0;
  113. width: 4px;
  114. height: 100%;
  115. background-color: var(--el-color-primary);
  116. content: '';
  117. }
  118. .@{prefix-cls} {
  119. position: relative;
  120. transition: width var(--transition-time-02);
  121. &:after {
  122. position: absolute;
  123. top: 0;
  124. right: 0;
  125. height: 100%;
  126. border-left: 1px solid var(--left-menu-border-color);
  127. content: '';
  128. }
  129. :deep(.@{elNamespace}-menu) {
  130. width: 100% !important;
  131. border-right: none;
  132. // 设置选中时子标题的颜色
  133. .is-active {
  134. & > .@{elNamespace}-sub-menu__title {
  135. color: var(--left-menu-text-active-color) !important;
  136. }
  137. }
  138. // 设置子菜单悬停的高亮和背景色
  139. .@{elNamespace}-sub-menu__title,
  140. .@{elNamespace}-menu-item {
  141. &:hover {
  142. color: var(--left-menu-text-active-color) !important;
  143. background-color: var(--left-menu-bg-color) !important;
  144. }
  145. }
  146. // 设置选中时的高亮背景和高亮颜色
  147. .@{elNamespace}-sub-menu.is-active,
  148. .@{elNamespace}-menu-item.is-active {
  149. color: var(--left-menu-text-active-color) !important;
  150. background-color: var(--left-menu-bg-active-color) !important;
  151. &:hover {
  152. background-color: var(--left-menu-bg-active-color) !important;
  153. }
  154. }
  155. .@{elNamespace}-menu-item.is-active {
  156. position: relative;
  157. &:after {
  158. .is-active--after;
  159. }
  160. }
  161. // 设置子菜单的背景颜色
  162. .@{elNamespace}-menu {
  163. .@{elNamespace}-sub-menu__title,
  164. .@{elNamespace}-menu-item:not(.is-active) {
  165. background-color: var(--left-menu-bg-light-color) !important;
  166. }
  167. }
  168. }
  169. // 折叠时的最小宽度
  170. :deep(.@{elNamespace}-menu--collapse) {
  171. width: var(--left-menu-min-width);
  172. & > .is-active,
  173. & > .is-active > .@{elNamespace}-sub-menu__title {
  174. position: relative;
  175. background-color: var(--left-menu-collapse-bg-active-color) !important;
  176. &:after {
  177. .is-active--after;
  178. }
  179. }
  180. }
  181. // 折叠动画的时候,就需要把文字给隐藏掉
  182. :deep(.horizontal-collapse-transition) {
  183. // transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out !important;
  184. .@{prefix-cls}__title {
  185. display: none;
  186. }
  187. }
  188. // 水平菜单
  189. &__horizontal {
  190. height: calc(~'var( - -top-tool-height)') !important;
  191. :deep(.@{elNamespace}-menu--horizontal) {
  192. height: calc(~'var( - -top-tool-height)');
  193. border-bottom: none;
  194. // 重新设置底部高亮颜色
  195. & > .@{elNamespace}-sub-menu.is-active {
  196. .@{elNamespace}-sub-menu__title {
  197. border-bottom-color: var(--el-color-primary) !important;
  198. }
  199. }
  200. .@{elNamespace}-menu-item.is-active {
  201. position: relative;
  202. &:after {
  203. display: none !important;
  204. }
  205. }
  206. .@{prefix-cls}__title {
  207. /* stylelint-disable-next-line */
  208. max-height: calc(~'var(--top-tool-height) - 2px') !important;
  209. /* stylelint-disable-next-line */
  210. line-height: calc(~'var(--top-tool-height) - 2px');
  211. }
  212. }
  213. }
  214. }
  215. </style>
  216. <style lang="less">
  217. @prefix-cls: ~'@{namespace}-menu-popper';
  218. .is-active--after {
  219. position: absolute;
  220. top: 0;
  221. right: 0;
  222. width: 4px;
  223. height: 100%;
  224. background-color: var(--el-color-primary);
  225. content: '';
  226. }
  227. .@{prefix-cls}--vertical,
  228. .@{prefix-cls}--horizontal {
  229. // 设置选中时子标题的颜色
  230. .is-active {
  231. & > .el-sub-menu__title {
  232. color: var(--left-menu-text-active-color) !important;
  233. }
  234. }
  235. // 设置子菜单悬停的高亮和背景色
  236. .el-sub-menu__title,
  237. .el-menu-item {
  238. &:hover {
  239. color: var(--left-menu-text-active-color) !important;
  240. background-color: var(--left-menu-bg-color) !important;
  241. }
  242. }
  243. // 设置选中时的高亮背景
  244. .el-menu-item.is-active {
  245. position: relative;
  246. background-color: var(--left-menu-bg-active-color) !important;
  247. &:hover {
  248. background-color: var(--left-menu-bg-active-color) !important;
  249. }
  250. &:after {
  251. .is-active--after;
  252. }
  253. }
  254. }
  255. </style>