Skip to content
On this page

Modal 模态框

vue
<template>
  <ta-group title="基础用法">
    <ta-cell label="默认" isLink @click="visible = true"></ta-cell>
    <ta-cell
      label="蒙层可点击"
      isLink
      @click="
        () => {
          maskClosable = true
          visible = true
        }
      "
    ></ta-cell>
    <ta-cell
      label="隐藏关闭按钮"
      isLink
      @click="
        () => {
          maskClosable = true
          showClose = false
          visible = true
        }
      "
    ></ta-cell>
  </ta-group>
  <ta-group title="Slot default">
    <ta-cell label="图片" isLink @click="visible2 = true"></ta-cell>
  </ta-group>
  <ta-group title="事件监听">
    <ta-cell
      label="cancel"
      isLink
      @click="
        () => {
          callbackEvent = true
          maskClosable = true
          visible = true
        }
      "
    ></ta-cell>
    <ta-cell
      label="visible-state-change"
      isLink
      @click="
        () => {
          visibleEvent = true
          visible = true
        }
      "
    ></ta-cell>
  </ta-group>
  <ta-modal
    v-model:visible="visible"
    :maskClosable="maskClosable"
    :showClose="showClose"
    @cancel="onClose"
    @visibleStateChange="onVisibleStateChange"
  >
  </ta-modal>
  <ta-modal v-model:visible="visible2">
    <ta-image class="exp-image-image" :src="imageUrl" :aspectRatio="1"></ta-image>
  </ta-modal>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { type PopupOnCancel, type PopupOnVisibleStateChange, showToast } from '@/index'

export default defineComponent({
  name: 'ExpModal',
  setup() {
    const visible = ref(false)
    const maskClosable = ref(false)
    const showClose = ref(true)
    const callbackEvent = ref(false)
    const visibleEvent = ref(false)
    const visible2 = ref(false)

    const onVisibleStateChange: PopupOnVisibleStateChange = ({ state }) => {
      if (visibleEvent.value) {
        console.log('visible-state-change', state)
        showToast(`${state} 事件触发`)
      }
      if (state === 'hidden') {
        callbackEvent.value = false
        visibleEvent.value = false
        maskClosable.value = false
        showClose.value = true
      }
    }

    const onClose: PopupOnCancel = res => {
      console.log('cancel', res)
      if (callbackEvent.value) {
        if (res.source === 'closeClick') {
          showToast('点击了关闭按钮')
        } else if (res.source === 'maskClick') {
          showToast('点击了蒙层')
        }
      }
    }

    return {
      visible,
      maskClosable,
      showClose,

      callbackEvent,
      visibleEvent,

      visible2,
      imageUrl: 'https://cdn.fox2.cn/vfox/swiper/center-2.jpg',

      onVisibleStateChange,
      onClose
    }
  }
})
</script>

Import

js
import { TaModal } from 'tantalum-ui-mobile'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

ts
import type { VisibleState, PopupOnVisibleStateChange, PopupOnCancel } from 'tantalum-ui-mobile'

Props

属性类型默认值必填说明
v-model:visiblebooleanfalse是否显示
widthstring模态框的宽度,支持 CSS 宽度值
mask-closablebooleanfalse点击蒙层是否触发关闭操作
show-closebooleantrue是否显示关闭按钮

Events

事件描述回调函数参数TypeScript 函数
visible-state-change展示隐藏时触发payload: { state: VisibleState }PopupOnVisibleStateChange
cancel关闭按钮或者点击蒙层关闭时触发payload: { source: string }PopupOnCancel

VisibleState 值说明

说明备注
show展示时触发
shown展示且动画结束后触发
hide隐藏时触发可能携带其他参数 cancel, maskClick, closeClick 等
hidden隐藏且动画结束后触发可能携带其他参数 cancel, maskClick, closeClick 等