index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div
  3. class="dropdown-wrap"
  4. :class="{ hover: props.trigger === 'hover' }"
  5. @mouseenter="handleMouseEnter"
  6. @mouseleave="handleMouseLeave"
  7. @click="handleClick"
  8. >
  9. <div class="btn">
  10. <slot name="btn"></slot>
  11. </div>
  12. <div
  13. class="container"
  14. :class="{ [props.positon]: 1 }"
  15. :style="{
  16. display: show ? 'block' : 'none',
  17. }"
  18. >
  19. <div class="wrap">
  20. <slot name="list"></slot>
  21. </div>
  22. </div>
  23. </div>
  24. </template>
  25. <script lang="ts" setup>
  26. import { ref } from 'vue';
  27. const show = ref(false);
  28. const props = withDefaults(
  29. defineProps<{
  30. trigger?: 'hover' | 'click';
  31. positon?: 'left' | 'right';
  32. }>(),
  33. {
  34. trigger: 'hover',
  35. positon: 'right',
  36. }
  37. );
  38. function handleClick() {
  39. show.value = true;
  40. }
  41. function handleMouseEnter() {
  42. if (props.trigger === 'hover') {
  43. show.value = true;
  44. }
  45. }
  46. function handleMouseLeave() {
  47. show.value = false;
  48. }
  49. </script>
  50. <style lang="scss" scoped>
  51. .dropdown-wrap {
  52. display: inline-block;
  53. position: relative;
  54. cursor: initial;
  55. &.hover {
  56. &:hover {
  57. .container {
  58. display: block;
  59. }
  60. }
  61. }
  62. .btn {
  63. cursor: pointer;
  64. user-select: none;
  65. }
  66. .container {
  67. position: absolute;
  68. top: 100%;
  69. right: 0;
  70. z-index: 3;
  71. display: none;
  72. &.right {
  73. right: 0;
  74. }
  75. &.left {
  76. left: 0;
  77. }
  78. .wrap {
  79. box-sizing: border-box;
  80. margin-top: 5px;
  81. padding: 10px 0;
  82. border-radius: 5px;
  83. background-color: #fff;
  84. box-shadow:
  85. 0 12px 32px rgba(0, 0, 0, 0.1),
  86. 0 2px 6px rgba(0, 0, 0, 0.08);
  87. color: black;
  88. font-size: 14px;
  89. }
  90. }
  91. }
  92. </style>