temp.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <div class="context-menus-container"
  3. v-if="contextMenu.show"
  4. @click="close">
  5. <ul class="context-menu"
  6. :style="styleParam">
  7. <li v-for="(item, index) in list"
  8. :key="index"
  9. @click.stop="choose(item)">{{item.lable}}</li>
  10. </ul>
  11. </div>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent, PropType, computed, watch, watchEffect, ref, reactive } from 'vue';
  15. import { ContextMenuTemp } from './interface';
  16. import { BtnListType } from '@/common/components/btnList/interface';
  17. export default defineComponent({
  18. name: 'context-menu',
  19. props: {
  20. contextMenu: {
  21. default: {
  22. position: { clientX: 0, clientY: 0 },
  23. show: false,
  24. },
  25. type: Object as PropType<ContextMenuTemp>,
  26. },
  27. list: {
  28. default: [],
  29. type: Array as PropType<BtnListType[]>,
  30. },
  31. },
  32. setup(props, context) {
  33. function choose(item: BtnListType) {
  34. console.log(`${item.lable}:${item.code}`);
  35. context.emit('cancel', item);
  36. }
  37. function close() {
  38. context.emit('cancel', null);
  39. }
  40. const styleParam = reactive<{ left: string; top: string }>({ left: '0px', top: '0px' });
  41. watchEffect(() => {
  42. if (props.contextMenu.show) {
  43. styleParam.left = `${props.contextMenu.position.clientX}px`;
  44. styleParam.top = `${props.contextMenu.position.clientY}px`;
  45. console.log(styleParam);
  46. }
  47. });
  48. return { choose, styleParam, close };
  49. },
  50. });
  51. </script>
  52. <style lang="less">
  53. .context-menus-container {
  54. position: fixed;
  55. top: 0;
  56. right: 0;
  57. bottom: 0;
  58. right: 0;
  59. width: 100%;
  60. height: 100%;
  61. }
  62. .context-menu {
  63. min-width: 100px;
  64. user-select: none;
  65. position: fixed;
  66. background-color: @m-black25;
  67. z-index: 999;
  68. border-radius: 2px;
  69. border: 1px solid @m-black17;
  70. padding-left: 0;
  71. list-style: none;
  72. margin-bottom: 0;
  73. cursor: pointer;
  74. li {
  75. padding: 6px;
  76. color: @m-grey2;
  77. font-size: 16px;
  78. border-bottom: 1px solid @m-black17;
  79. text-align: center;
  80. &:hover {
  81. color: @m-white1;
  82. background-color: @m-grey26;
  83. }
  84. }
  85. }
  86. </style>;