cwg-global-popup.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <cwg-popup v-model:visible="popupState.visible" type="center" :mask-click="false" :show-footers="showFooters"
  3. width="400px">
  4. <view class="popup-content">
  5. <view class="confirm-title">{{ popupState.title }}</view>
  6. <view class="confirm-content">{{ popupState.content }}</view>
  7. </view>
  8. <template #footer>
  9. <button hover-class="" v-if="popupState.showCancel && !popupState.autoClose" @click="close">{{
  10. popupState.cancelText }}</button>
  11. <button hover-class="" v-if="!popupState.autoClose" class="btn btn-secondary btn-shadow waves-effect"
  12. @click="handleConfirm">{{ popupState.confirmText }}</button>
  13. </template>
  14. </cwg-popup>
  15. </template>
  16. <script setup>
  17. import { watch, ref, computed, onUnmounted } from 'vue'
  18. import { usePopup } from '@/hooks/usePopup'
  19. const { popupState, close, handleConfirm } = usePopup()
  20. const popupRef = ref(null)
  21. let autoCloseTimer = null
  22. const showFooters = computed(() => !popupState.value.autoClose)
  23. watch(() => popupState.value.visible, (val) => {
  24. if (val) {
  25. popupRef.value?.open()
  26. if (popupState.value.autoClose) {
  27. autoCloseTimer = setTimeout(() => {
  28. close()
  29. }, popupState.value.autoCloseDelay)
  30. }
  31. } else {
  32. popupRef.value?.close()
  33. if (autoCloseTimer) {
  34. clearTimeout(autoCloseTimer)
  35. autoCloseTimer = null
  36. }
  37. }
  38. }, { immediate: true })
  39. onUnmounted(() => {
  40. if (autoCloseTimer) {
  41. clearTimeout(autoCloseTimer)
  42. }
  43. })
  44. </script>
  45. <style scoped lang="scss">
  46. @import "@/uni.scss";
  47. :deep(.cwg-dialog) {
  48. width: px2rpx(500);
  49. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  50. border-radius: px2rpx(16);
  51. text-align: center;
  52. box-shadow: 0 px2rpx(10) px2rpx(20) rgba(0, 0, 0, 0.1);
  53. }
  54. .confirm-title {
  55. font-size: px2rpx(24);
  56. font-weight: 600;
  57. color: var(--bs-heading-color);
  58. margin-bottom: px2rpx(30);
  59. }
  60. .confirm-content {
  61. font-size: px2rpx(20);
  62. color: var(--bs-heading-color);
  63. margin-bottom: px2rpx(30);
  64. line-height: 1.5;
  65. word-break: break-word;
  66. }
  67. </style>