ModernDialog.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <van-overlay :show="show" class="custom-overlay" @click="handleClose" />
  3. <transition name="dialog-pop">
  4. <div v-if="show" class="modern-dialog">
  5. <div class="dialog-content">
  6. <div class="dialog-title">{{ title }}</div>
  7. <div class="dialog-message">{{ message }}</div>
  8. <!-- 按钮区:支持 1 或 2 个按钮 -->
  9. <div
  10. class="dialog-actions"
  11. :class="{
  12. two: hasConfirm && hasCancel,
  13. one: hasConfirm !== hasCancel,
  14. }"
  15. >
  16. <!-- 失败/取消:无背景,边框+文字色由 color 控制 -->
  17. <van-button
  18. v-if="hasCancel"
  19. plain
  20. class="btn btn--fail"
  21. :color="cancelColor"
  22. round
  23. @click="handleCancel"
  24. >
  25. {{ cancelText }}
  26. </van-button>
  27. <!-- 成功/确定:填充背景,支持渐变 -->
  28. <van-button
  29. v-if="hasConfirm"
  30. type="primary"
  31. class="btn btn--success"
  32. :color="confirmColor"
  33. round
  34. @click="handleConfirm"
  35. >
  36. {{ confirmText }}
  37. </van-button>
  38. </div>
  39. </div>
  40. </div>
  41. </transition>
  42. </template>
  43. <script setup lang="ts">
  44. import { computed } from 'vue'
  45. const props = defineProps({
  46. show: { type: Boolean, required: true },
  47. title: { type: String, default: '提示' },
  48. message: { type: String, default: '' },
  49. /** 成功按钮(右侧) */
  50. confirmText: { type: String, default: '' },
  51. /** 失败按钮(左侧),无背景 */
  52. cancelText: { type: String, default: '' },
  53. /** 成功按钮背景色(可用渐变) */
  54. confirmColor: {
  55. type: String,
  56. default: 'linear-gradient(90deg, #ff7a00, #ffa94d)',
  57. },
  58. /** 失败按钮文字/边框颜色(plain 模式下作为描边+文字色) */
  59. cancelColor: {
  60. type: String,
  61. default: '#fe783d',
  62. },
  63. /** 点击遮罩是否关闭 */
  64. closeOnClickOverlay: { type: Boolean, default: true },
  65. })
  66. const emit = defineEmits<{
  67. (e: 'update:show', v: boolean): void
  68. (e: 'confirm'): void
  69. (e: 'cancel'): void
  70. (e: 'close'): void
  71. }>()
  72. const hasConfirm = computed(() => !!props.confirmText)
  73. const hasCancel = computed(() => !!props.cancelText)
  74. const handleConfirm = () => emit('confirm')
  75. const handleCancel = () => emit('cancel')
  76. const handleClose = () => {
  77. if (props.closeOnClickOverlay) emit('update:show', false)
  78. emit('close')
  79. }
  80. </script>
  81. <style scoped lang="scss">
  82. .custom-overlay {
  83. backdrop-filter: blur(8px);
  84. background: rgba(0, 0, 0, 0.25);
  85. z-index: 9998;
  86. animation: fadeIn 0.3s ease;
  87. }
  88. .modern-dialog {
  89. position: fixed;
  90. inset: 50% auto auto 50%;
  91. transform: translate(-50%, -50%);
  92. z-index: 9999;
  93. width: 80vw;
  94. border-radius: 5vw;
  95. background: #fff;
  96. box-shadow: 0 8px 28px rgba(0, 0, 0, 0.15);
  97. text-align: center;
  98. animation: dialogPop 0.28s ease-out;
  99. box-sizing: border-box;
  100. .dialog-content {
  101. display: flex;
  102. flex-direction: column;
  103. align-items: center;
  104. padding: 6vw 5vw 5vw;
  105. gap: 5vw;
  106. }
  107. .dialog-title {
  108. font-size: 4.4vw;
  109. font-weight: 700;
  110. color: #222;
  111. }
  112. .dialog-message {
  113. font-size: 3.6vw;
  114. line-height: 1.7;
  115. color: #555;
  116. word-break: break-word;
  117. }
  118. /* 按钮区域布局 */
  119. .dialog-actions {
  120. width: 100%;
  121. padding: 0 5vw 2.5vw;
  122. box-sizing: border-box;
  123. display: grid;
  124. gap: 3vw;
  125. &.one {
  126. grid-template-columns: 1fr;
  127. .btn {
  128. height: 12vw;
  129. border-radius: 3vw;
  130. font-size: 4vw;
  131. font-weight: 600;
  132. letter-spacing: 0.2vw;
  133. }
  134. }
  135. &.two {
  136. grid-template-columns: 1fr 1fr;
  137. .btn {
  138. height: 12vw;
  139. border-radius: 3vw;
  140. font-size: 4vw;
  141. font-weight: 600;
  142. letter-spacing: 0.2vw;
  143. }
  144. /* 失败按钮:无背景(plain),加淡边框 */
  145. .btn--fail {
  146. /* 让边框略淡一些,视觉层次更好 */
  147. border-width: 1px;
  148. }
  149. }
  150. }
  151. }
  152. /* 过渡动画 */
  153. .dialog-pop-enter-active,
  154. .dialog-pop-leave-active {
  155. transition: all 0.25s ease;
  156. }
  157. .dialog-pop-enter-from,
  158. .dialog-pop-leave-to {
  159. opacity: 0;
  160. transform: translate(-50%, -45%) scale(0.9);
  161. }
  162. @keyframes dialogPop {
  163. from {
  164. transform: translate(-50%, -45%) scale(0.9);
  165. opacity: 0;
  166. }
  167. to {
  168. transform: translate(-50%, -50%) scale(1);
  169. opacity: 1;
  170. }
  171. }
  172. </style>