Menu.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <script lang="tsx">
  2. import { 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 { isUrl } from '@/utils/is'
  8. import { useDesign } from '@/hooks/web/useDesign'
  9. import { LayoutType } from '@/types/layout'
  10. const { getPrefixCls } = useDesign()
  11. const prefixCls = getPrefixCls('menu')
  12. export default defineComponent({
  13. // eslint-disable-next-line vue/no-reserved-component-names
  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="scss" scoped>
  108. $prefix-cls: #{$namespace}-menu;
  109. .#{$prefix-cls} {
  110. position: relative;
  111. transition: width var(--transition-time-02);
  112. :deep(.#{$elNamespace}-menu) {
  113. width: 100% !important;
  114. border-right: none;
  115. // 设置选中时子标题的颜色
  116. .is-active {
  117. & > .#{$elNamespace}-sub-menu__title {
  118. color: var(--left-menu-text-active-color) !important;
  119. }
  120. }
  121. // 设置子菜单悬停的高亮和背景色
  122. .#{$elNamespace}-sub-menu__title,
  123. .#{$elNamespace}-menu-item {
  124. &:hover {
  125. color: var(--left-menu-text-active-color) !important;
  126. background-color: var(--left-menu-bg-color) !important;
  127. }
  128. }
  129. // 设置选中时的高亮背景和高亮颜色
  130. .#{$elNamespace}-menu-item.is-active {
  131. color: var(--left-menu-text-active-color) !important;
  132. background-color: var(--left-menu-bg-active-color) !important;
  133. &:hover {
  134. background-color: var(--left-menu-bg-active-color) !important;
  135. }
  136. }
  137. .#{$elNamespace}-menu-item.is-active {
  138. position: relative;
  139. }
  140. // 设置子菜单的背景颜色
  141. .#{$elNamespace}-menu {
  142. .#{$elNamespace}-sub-menu__title,
  143. .#{$elNamespace}-menu-item:not(.is-active) {
  144. background-color: var(--left-menu-bg-light-color) !important;
  145. }
  146. }
  147. }
  148. // 折叠时的最小宽度
  149. :deep(.#{$elNamespace}-menu--collapse) {
  150. width: var(--left-menu-min-width);
  151. & > .is-active,
  152. & > .is-active > .#{$elNamespace}-sub-menu__title {
  153. position: relative;
  154. background-color: var(--left-menu-collapse-bg-active-color) !important;
  155. }
  156. }
  157. // 折叠动画的时候,就需要把文字给隐藏掉
  158. :deep(.horizontal-collapse-transition) {
  159. // transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out !important;
  160. .#{$prefix-cls}__title {
  161. display: none;
  162. }
  163. }
  164. // 水平菜单
  165. &__horizontal {
  166. height: calc(var(--top-tool-height)) !important;
  167. :deep(.#{$elNamespace}-menu--horizontal) {
  168. height: calc(var(--top-tool-height));
  169. border-bottom: none;
  170. // 重新设置底部高亮颜色
  171. & > .#{$elNamespace}-sub-menu.is-active {
  172. .#{$elNamespace}-sub-menu__title {
  173. border-bottom-color: var(--el-color-primary) !important;
  174. }
  175. }
  176. .#{$elNamespace}-menu-item.is-active {
  177. position: relative;
  178. &::after {
  179. display: none !important;
  180. }
  181. }
  182. .#{$prefix-cls}__title {
  183. /* stylelint-disable-next-line */
  184. max-height: calc(var(--top-tool-height) - 2px) !important;
  185. /* stylelint-disable-next-line */
  186. line-height: calc(var(--top-tool-height) - 2px);
  187. }
  188. }
  189. }
  190. }
  191. </style>
  192. <style lang="scss">
  193. $prefix-cls: #{$namespace}-menu-popper;
  194. .#{$prefix-cls}--vertical,
  195. .#{$prefix-cls}--horizontal {
  196. // 设置选中时子标题的颜色
  197. .is-active {
  198. & > .el-sub-menu__title {
  199. color: var(--left-menu-text-active-color) !important;
  200. }
  201. }
  202. // 设置子菜单悬停的高亮和背景色
  203. .el-sub-menu__title,
  204. .el-menu-item {
  205. &:hover {
  206. color: var(--left-menu-text-active-color) !important;
  207. background-color: var(--left-menu-bg-color) !important;
  208. }
  209. }
  210. // 设置选中时的高亮背景
  211. .el-menu-item.is-active {
  212. position: relative;
  213. background-color: var(--left-menu-bg-active-color) !important;
  214. &:hover {
  215. background-color: var(--left-menu-bg-active-color) !important;
  216. }
  217. }
  218. }
  219. </style>