LayoutSider.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div v-if="getMenuFixed && !getIsMobile" :style="getHiddenDomStyle" v-show="showClassSideBarRef"></div>
  3. <Sider
  4. v-show="showClassSideBarRef"
  5. ref="sideRef"
  6. breakpoint="lg"
  7. collapsible
  8. :class="getSiderClass"
  9. :width="getMenuWidth"
  10. :collapsed="getCollapsed"
  11. :collapsedWidth="getCollapsedWidth"
  12. :theme="getMenuTheme"
  13. @breakpoint="onBreakpointChange"
  14. :trigger="getTrigger"
  15. v-bind="getTriggerAttr"
  16. >
  17. <template #trigger v-if="getShowTrigger">
  18. <LayoutTrigger />
  19. </template>
  20. <LayoutMenu :theme="getMenuTheme" :menuMode="getMode" :splitType="getSplitType" />
  21. <DragBar ref="dragBarRef" />
  22. </Sider>
  23. </template>
  24. <script lang="ts">
  25. import { computed, defineComponent, ref, unref, CSSProperties, h } from 'vue';
  26. import { Layout } from 'ant-design-vue';
  27. import LayoutMenu from '../menu/index.vue';
  28. import LayoutTrigger from '/@/layouts/default/trigger/index.vue';
  29. import { MenuModeEnum, MenuSplitTyeEnum } from '/@/enums/menuEnum';
  30. import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  31. import { useTrigger, useDragLine, useSiderEvent } from './useLayoutSider';
  32. import { useAppInject } from '/@/hooks/web/useAppInject';
  33. import { useDesign } from '/@/hooks/web/useDesign';
  34. import DragBar from './DragBar.vue';
  35. export default defineComponent({
  36. name: 'LayoutSideBar',
  37. components: { Sider: Layout.Sider, LayoutMenu, DragBar, LayoutTrigger },
  38. setup() {
  39. const dragBarRef = ref<ElRef>(null);
  40. const sideRef = ref<ElRef>(null);
  41. const { getCollapsed, getMenuWidth, getSplit, getMenuTheme, getRealWidth, getMenuHidden, getMenuFixed, getIsMixMode, toggleCollapsed } =
  42. useMenuSetting();
  43. const { prefixCls } = useDesign('layout-sideBar');
  44. const { getIsMobile } = useAppInject();
  45. const { getTriggerAttr, getShowTrigger } = useTrigger(getIsMobile);
  46. useDragLine(sideRef, dragBarRef);
  47. const { getCollapsedWidth, onBreakpointChange } = useSiderEvent();
  48. const getMode = computed(() => {
  49. return unref(getSplit) ? MenuModeEnum.INLINE : null;
  50. });
  51. const getSplitType = computed(() => {
  52. return unref(getSplit) ? MenuSplitTyeEnum.LEFT : MenuSplitTyeEnum.NONE;
  53. });
  54. const showClassSideBarRef = computed(() => {
  55. return unref(getSplit) ? !unref(getMenuHidden) : true;
  56. });
  57. const getSiderClass = computed(() => {
  58. return [
  59. prefixCls,
  60. {
  61. [`${prefixCls}--fixed`]: unref(getMenuFixed),
  62. [`${prefixCls}--mix`]: unref(getIsMixMode) && !unref(getIsMobile),
  63. },
  64. ];
  65. });
  66. const getHiddenDomStyle = computed((): CSSProperties => {
  67. const width = `${unref(getRealWidth)}px`;
  68. return {
  69. width: width,
  70. overflow: 'hidden',
  71. flex: `0 0 ${width}`,
  72. maxWidth: width,
  73. minWidth: width,
  74. transition: 'all 0.2s',
  75. };
  76. });
  77. // 在此处使用计算量可能会导致sider异常
  78. // andv 更新后,如果trigger插槽可用,则此处代码可废弃
  79. const getTrigger = h(LayoutTrigger);
  80. return {
  81. prefixCls,
  82. sideRef,
  83. dragBarRef,
  84. getIsMobile,
  85. getHiddenDomStyle,
  86. getSiderClass,
  87. getTrigger,
  88. getTriggerAttr,
  89. getCollapsedWidth,
  90. getMenuFixed,
  91. showClassSideBarRef,
  92. getMenuWidth,
  93. getCollapsed,
  94. getMenuTheme,
  95. onBreakpointChange,
  96. getMode,
  97. getSplitType,
  98. getShowTrigger,
  99. toggleCollapsed,
  100. };
  101. },
  102. });
  103. </script>
  104. <style lang="less">
  105. @prefix-cls: ~'@{namespace}-layout-sideBar';
  106. .@{prefix-cls} {
  107. z-index: @layout-sider-fixed-z-index;
  108. &--fixed {
  109. position: fixed !important;
  110. top: 0;
  111. left: 0;
  112. height: 100%;
  113. }
  114. &--mix {
  115. top: @header-height;
  116. height: calc(100% - @header-height);
  117. }
  118. &.ant-layout-sider-dark {
  119. background-color:#ffffff;
  120. .ant-layout-sider-trigger {
  121. color: darken(@white, 25%);
  122. background-color: @trigger-dark-bg-color;
  123. &:hover {
  124. color: @white;
  125. background-color: @trigger-dark-hover-bg-color;
  126. }
  127. }
  128. }
  129. &:not(.ant-layout-sider-dark) {
  130. // box-shadow: 2px 0 8px 0 rgba(29, 35, 41, 0.05);
  131. .ant-layout-sider-trigger {
  132. color: @text-color-base;
  133. border-top: 1px solid @border-color-light;
  134. }
  135. }
  136. .ant-layout-sider-zero-width-trigger {
  137. top: 40%;
  138. z-index: 10;
  139. }
  140. & .ant-layout-sider-trigger {
  141. height: 36px;
  142. line-height: 36px;
  143. }
  144. }
  145. </style>