index.uvue 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
<template>
  <!-- ✅ UVUE 需要单一根节点,避免 v-if 导致片段渲染 -->
  <view class="custom-popup-root">
    <view
      v-if="show"
      class="popup-mask"
      :class="{ 'popup-mask-visible': maskVisible }"
      :style="maskStyle"
      @tap="onMaskTap"
    >
      <view
        class="popup-container"
        :class="[containerClass, { 'popup-container-visible': containerVisible }]"
        :style="containerStyle"
        @tap.stop
      >
      <!-- 顶部标题栏 -->
      <view v-if="showHeader" class="popup-header">
        <slot name="header">
          <view class="popup-header-inner">
            <text v-if="title != ''" class="popup-title">{{ title }}</text>
            <view
              v-if="showClose"
              class="popup-close"
              :style="closePositionStyle"
              @tap="handleClose"
            >
              <slot name="close-icon">
                <text class="close-icon-text">×</text>
              </slot>
            </view>
          </view>
        </slot>
      </view>

      <!-- 内容区:嵌套容器保证圆角处无断裂 -->
      <view class="popup-body-wrapper">
        <scroll-view
          class="popup-body popup-body-scroll"
          scroll-y
          :style="bodyStyle"
        >
          <slot></slot>
        </scroll-view>
      </view>

      <!-- 底部操作区 -->
      <view v-if="showFooter" class="popup-footer">
        <slot name="footer">
          <view class="popup-footer-buttons" :class="footerOrderClass">
            <button
              v-if="showCancelBtn"
              class="popup-btn popup-btn-cancel"
              :disabled="cancelDisabled"
              @tap="handleCancel"
            >
              <text class="popup-btn-text">{{ cancelText }}</text>
            </button>
            <button
              class="popup-btn popup-btn-confirm"
              :disabled="confirmDisabled || confirmLoading"
              @tap="handleConfirm"
            >
              <text v-if="!confirmLoading" class="popup-btn-text">{{ confirmText }}</text>
              <text v-else class="popup-btn-text">{{ confirmLoadingText }}</text>
            </button>
          </view>
        </slot>
      </view>
    </view>
  </view>
  </view>
</template>

<script setup lang="uts">
import { computed, nextTick, ref, watch } from 'vue'

/** 圆角预设:小(4px)、中(8px)、大(12px)、全圆角(9999px) */
type RadiusPreset = 'sm' | 'md' | 'lg' | 'full'

/** 定位模式 */
type PositionMode = 'center' | 'bottom' | 'top' | 'left' | 'right'

/** 关闭按钮位置 */
type ClosePosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'

function getRadiusPreset(preset: string): string {
  if (preset == 'sm') return '4px'
  if (preset == 'md') return '8px'
  if (preset == 'lg') return '12px'
  if (preset == 'full') return '9999px'
  return '8px'
}

const props = withDefaults(
  defineProps<{
    /** 受控显示 */
    visible?: boolean
    /** 定位模式:center | bottom | top | left | right */
    position?: PositionMode
    /** 圆角预设:sm | md | lg | full,或通过 borderRadius 自定义 */
    radiusPreset?: RadiusPreset
    /** 自定义圆角值,优先级高于 radiusPreset */
    borderRadius?: string
    /** 底部弹窗高度:固定值如 400px,或百分比如 50% */
    bottomHeight?: string
    /** 侧边弹窗宽度:固定值如 300px,或百分比如 80% */
    sideWidth?: string
    /** 是否显示遮罩 */
    showMask?: boolean
    /** 遮罩点击是否关闭 */
    maskClosable?: boolean
    /** 遮罩颜色 CSS 变量 --popup-mask-color,默认 #000000 */
    maskColor?: string
    /** 遮罩透明度 CSS 变量 --popup-mask-opacity,默认 0.5 */
    maskOpacity?: number
    /** 是否显示标题栏 */
    showHeader?: boolean
    /** 标题文本 */
    title?: string
    /** 是否显示关闭按钮 */
    showClose?: boolean
    /** 关闭按钮位置 */
    closePosition?: ClosePosition
    /** 是否显示标题栏底部分隔线 */
    headerBorder?: boolean
    /** 是否显示底部按钮区 */
    showFooter?: boolean
    /** 是否显示取消按钮 */
    showCancelBtn?: boolean
    /** 取消按钮文案 */
    cancelText?: string
    /** 确认按钮文案 */
    confirmText?: string
    /** 确认按钮加载中文案 */
    confirmLoadingText?: string
    /** 按钮顺序:cancel-first 取消左确认右,confirm-first 确认左取消右 */
    buttonOrder?: 'cancel-first' | 'confirm-first'
    /** 确认按钮是否加载中 */
    confirmLoading?: boolean
    /** 确认按钮是否禁用 */
    confirmDisabled?: boolean
    /** 取消按钮是否禁用 */
    cancelDisabled?: boolean
    /** 自定义容器样式 */
    customStyle?: UTSJSONObject | null
    /** 过渡时长(毫秒),遮罩与容器的动画时长 */
    transitionDuration?: number
  }>(),
  {
    visible: false,
    position: 'center',
    radiusPreset: 'md',
    borderRadius: '',
    bottomHeight: '50%',
    sideWidth: '80%',
    showMask: true,
    maskClosable: true,
    maskColor: '#000000',
    maskOpacity: 0.5,
    showHeader: true,
    title: '',
    showClose: true,
    closePosition: 'top-right',
    headerBorder: true,
    showFooter: true,
    showCancelBtn: true,
    cancelText: '取消',
    confirmText: '确认',
    confirmLoadingText: '加载中...',
    buttonOrder: 'cancel-first',
    confirmLoading: false,
    confirmDisabled: false,
    cancelDisabled: false,
    customStyle: null,
    transitionDuration: 2000
  }
)

const emit = defineEmits<{
  'update:visible': [value: boolean]
  close: []
  cancel: []
  confirm: []
}>()

const maskVisible = ref(false)
const containerVisible = ref(false)
const internalVisible = ref(false)
const show = ref(false)

/** 关闭延迟定时器,用于取消快速切换时的竞态 */
let closeTimer: number | null = null

const visible = computed((): boolean => {
  if (props.visible != null) {
    // ✅ UTS 避免 Boolean === true 比较
    const val = props.visible;
    return val != null && val;
  }
  return internalVisible.value
})

watch(visible, (val: boolean) => {
  const timer = closeTimer
  if (timer != null) {
    closeTimer = null
    clearTimeout(timer)
  }
  if (val) {
    show.value = true
    nextTick(() => {
      setTimeout(() => {
        maskVisible.value = true
        containerVisible.value = true
      }, 30)
    })
  } else {
    maskVisible.value = false
    containerVisible.value = false
    closeTimer = setTimeout(() => {
      show.value = false
      closeTimer = null
    }, props.transitionDuration)
  }
})

const radiusValue = computed((): string => {
  if (props.borderRadius != null && props.borderRadius != '') {
    return props.borderRadius
  }
  return getRadiusPreset(props.radiusPreset)
})

const containerClass = computed((): string => {
  const list: string[] = ['popup-position-' + props.position]
  if (props.headerBorder) {
    list.push('popup-header-border')
  }
  return list.join(' ')
})

const maskStyle = computed((): UTSJSONObject => {
  const obj: UTSJSONObject = {}
  const duration = props.transitionDuration > 0 ? props.transitionDuration : 2000
  obj['transition'] = 'opacity ' + duration + 'ms cubic-bezier(0.25, 0.46, 0.45, 0.94)'
  if (!props.showMask) {
    obj['background'] = 'transparent'
  } else {
    const opacity = props.maskOpacity > 0 ? props.maskOpacity : 0.5
    obj['background'] = 'rgba(0,0,0,' + opacity + ')'
  }
  return obj
})

const containerStyle = computed((): UTSJSONObject => {
  const obj: UTSJSONObject = {}
  const duration = props.transitionDuration > 0 ? props.transitionDuration : 2000
  obj['transition'] =
    'transform ' +
    duration +
    'ms cubic-bezier(0.25, 0.46, 0.45, 0.94), opacity ' +
    duration +
    'ms cubic-bezier(0.25, 0.46, 0.45, 0.94)'
  obj['borderRadius'] = radiusValue.value
  if (props.position == 'bottom' || props.position == 'top') {
    obj['height'] = props.bottomHeight
  }
  if (props.position == 'left' || props.position == 'right') {
    obj['width'] = props.sideWidth
  }
  if (props.customStyle != null) {
    const w = props.customStyle!['width']
    if (w != null) obj['width'] = w
    const h = props.customStyle!['height']
    if (h != null) obj['height'] = h
    const mw = props.customStyle!['maxWidth']
    if (mw != null) obj['maxWidth'] = mw
    const mh = props.customStyle!['maxHeight']
    if (mh != null) obj['maxHeight'] = mh
  }
  return obj
})

const bodyStyle = computed((): UTSJSONObject => {
  const obj: UTSJSONObject = {}
  obj['borderRadius'] = radiusValue.value
  return obj
})

const closePositionStyle = computed((): UTSJSONObject => {
  const pos = props.closePosition
  const obj: UTSJSONObject = {}
  if (pos == 'top-right') {
    obj['right'] = '12px'
    obj['top'] = '50%'
    obj['transform'] = 'translateY(-50%)'
  } else if (pos == 'top-left') {
    obj['left'] = '12px'
    obj['top'] = '50%'
    obj['transform'] = 'translateY(-50%)'
  } else if (pos == 'bottom-right') {
    obj['right'] = '12px'
    obj['bottom'] = '0'
  } else {
    obj['left'] = '12px'
    obj['bottom'] = '0'
  }
  return obj
})

const footerOrderClass = computed((): string => {
  return props.buttonOrder == 'confirm-first' ? 'popup-footer-reverse' : ''
})

function doClose(): void {
  internalVisible.value = false
  emit('update:visible', false)
  emit('close')
}

function onMaskTap(): void {
  if (props.maskClosable) {
    doClose()
  }
}

function handleClose(): void {
  doClose()
}

function handleCancel(): void {
  emit('cancel')
  doClose()
}

function handleConfirm(): void {
  emit('confirm')
}

function open(): void {
  internalVisible.value = true
  emit('update:visible', true)
}

function close(): void {
  doClose()
}

defineExpose({
  open,
  close
})
</script>

<style scoped lang="scss">
/* 通过 CSS 变量覆盖主题:--popup-primary-color --popup-border-radius --popup-shadow --popup-spacing --popup-font-size */
.popup-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  /* transition 由 maskStyle 根据 transitionDuration 动态设置 */
}

.popup-mask-visible {
  opacity: 1;
}

/* 居中定位 */
.popup-mask .popup-position-center {
  align-self: center;
  margin: 24px;
  max-width: 90%;
  max-height: 80%;
  min-width: 280px;
}

/* 底部定位:贴底,预留安全区域 */
.popup-mask .popup-position-bottom {
  align-self: flex-end;
  width: 100%;
  max-height: 90%;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  padding-bottom: env(safe-area-inset-bottom);
}

/* 顶部定位 */
.popup-mask .popup-position-top {
  align-self: flex-start;
  width: 100%;
  max-height: 90%;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

/* 左侧定位 */
.popup-mask .popup-position-left {
  align-self: stretch;
  height: 100%;
  max-width: 90%;
  margin-right: auto;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

/* 右侧定位 */
.popup-mask .popup-position-right {
  align-self: stretch;
  height: 100%;
  max-width: 90%;
  margin-left: auto;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.popup-container {
  background: #ffffff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  /* transition 由 containerStyle 根据 transitionDuration 动态设置 */
}

/* 居中:缩放 + 透明度 */
.popup-position-center {
  transform: scale(0.9);
}
.popup-position-center.popup-container-visible {
  opacity: 1;
  transform: scale(1);
}

/* 底部:自下而上滑入 */
.popup-position-bottom {
  transform: translateY(100%);
}
.popup-position-bottom.popup-container-visible {
  opacity: 1;
  transform: translateY(0);
}

/* 顶部:自上而下滑入 */
.popup-position-top {
  transform: translateY(-100%);
}
.popup-position-top.popup-container-visible {
  opacity: 1;
  transform: translateY(0);
}

/* 左侧:自左滑入 */
.popup-position-left {
  transform: translateX(-100%);
}
.popup-position-left.popup-container-visible {
  opacity: 1;
  transform: translateX(0);
}

/* 右侧:自右滑入 */
.popup-position-right {
  transform: translateX(100%);
}
.popup-position-right.popup-container-visible {
  opacity: 1;
  transform: translateX(0);
}

.popup-header {
  flex-shrink: 0;
  padding: 16px 24px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 48px;
}

.popup-header-border {
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: #e8e8e8;
}

.popup-header-inner {
  flex: 1;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  position: relative;
}

.popup-title {
  font-size: 18px;
  font-weight: bold;
  color: #1a1a1a;
  text-align: center;
}

.popup-close {
  position: absolute;
  padding: 8px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.close-icon-text {
  font-size: 28px;
  color: #1a1a1a;
  line-height: 1;
}

.popup-body-wrapper {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.popup-body {
  flex: 1;
  min-height: 0;
}

.popup-body-scroll {
  padding: 16px 24px;
}

.popup-footer {
  flex-shrink: 0;
  padding: 16px 24px;
  border-top-width: 1px;
  border-top-style: solid;
  border-top-color: #e8e8e8;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.popup-footer-buttons {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 100%;
}

.popup-footer-reverse {
  flex-direction: row-reverse;
}

.popup-btn {
  flex: 1;
  height: 44px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border-width: 0;
  margin-left: 12px;
  padding: 0 20px;
}

.popup-footer-reverse .popup-btn {
  margin-left: 0;
  margin-right: 12px;
}

.popup-footer-reverse .popup-btn:last-child {
  margin-right: 0;
}

.popup-btn:first-child {
  margin-left: 0;
}

.popup-btn-cancel {
  background: #f3f4f6;
}

.popup-btn-cancel .popup-btn-text {
  color: #6b7280;
  font-size: 16px;
}

.popup-btn-confirm {
  background: #2c66f7;
}

.popup-btn-confirm .popup-btn-text {
  color: #ffffff;
  font-size: 16px;
}

.popup-btn-confirm:active {
  opacity: 0.9;
}

.popup-btn:disabled {
  opacity: 0.6;
}
</style>