Skip to content

Modal 模态框

带遮罩的对话框,适用于重要信息确认、表单填写等需要打断用户流程的场景。

基础用法

vue
<script setup lang="ts">
import { ref } from 'vue'
const show = ref(false)
</script>

<template>
  <lk-button @click="show = true">打开模态框</lk-button>

  <lk-modal v-model="show" title="提示">
    <view>这是弹框内容,请确认操作。</view>
    <template #footer>
      <lk-button variant="text" @click="show = false">取消</lk-button>
      <lk-button @click="show = false">确认</lk-button>
    </template>
  </lk-modal>
</template>

确认对话框

vue
<script setup lang="ts">
import { ref } from 'vue'
const show = ref(false)

function onConfirm() {
  show.value = false
  // 执行删除逻辑...
}
</script>

<template>
  <lk-button type="danger" variant="outline" @click="show = true">删除账号</lk-button>

  <lk-modal v-model="show" title="确认删除">
    <view style="text-align:center; padding: 16rpx 0">
      <lk-icon name="exclamation-triangle-fill" color="#ef4444" :size="48" />
      <view style="margin-top:16rpx">此操作不可撤销,确定要删除账号吗?</view>
    </view>
    <template #footer>
      <lk-button block variant="outline" @click="show = false">取消</lk-button>
      <lk-button block type="danger" @click="onConfirm">确认删除</lk-button>
    </template>
  </lk-modal>
</template>

无头部弹框

vue
<script setup lang="ts">
import { ref } from 'vue'
const show = ref(false)
</script>

<template>
  <lk-button @click="show = true">纯内容弹框</lk-button>

  <lk-modal v-model="show" :show-header="false" :show-footer="false">
    <view style="padding:48rpx; text-align:center">
      <lk-icon name="check-circle-fill" color="#22c55e" :size="64" />
      <view style="font-size:36rpx; font-weight:600; margin-top:24rpx">支付成功!</view>
      <view style="color:#64748b; margin-top:8rpx">金额 ¥128.00 已到账</view>
      <lk-button block style="margin-top:48rpx" @click="show = false">我知道了</lk-button>
    </view>
  </lk-modal>
</template>

表单弹框

vue
<script setup lang="ts">
import { ref } from 'vue'
const show = ref(false)
const form = ref({ name: '', phone: '' })
</script>

<template>
  <lk-button @click="show = true">填写信息</lk-button>

  <lk-modal v-model="show" title="完善资料" width="640rpx">
    <lk-form :model="form" style="padding:0">
      <lk-form-item label="姓名">
        <lk-input v-model="form.name" placeholder="请输入真实姓名" />
      </lk-form-item>
      <lk-form-item label="手机">
        <lk-input v-model="form.phone" type="tel" placeholder="请输入手机号" />
      </lk-form-item>
    </lk-form>
    <template #footer>
      <lk-button variant="text" @click="show = false">取消</lk-button>
      <lk-button @click="show = false">保存</lk-button>
    </template>
  </lk-modal>
</template>

动画类型

vue
<template>
  <view class="demo-row">
    <lk-button @click="show1 = true">缩放(默认)</lk-button>
    <lk-button @click="show2 = true">从下方滑入</lk-button>
    <lk-button @click="show3 = true">渐显</lk-button>
  </view>

  <lk-modal v-model="show1" animation="scale" title="缩放动画">内容</lk-modal>
  <lk-modal v-model="show2" animation="slide-up" title="滑入动画">内容</lk-modal>
  <lk-modal v-model="show3" animation="fade" title="渐显动画">内容</lk-modal>
</template>

API

Props

参数说明类型默认值
customClass组件可视根节点自定义类名string | object | array''
customStyle组件可视根节点自定义样式string | object''
modelValue是否显示(v-model)booleanfalse
zIndex层级number1500
title标题文字string''
width弹框宽度string600rpx
showClose显示右上角关闭按钮booleantrue
closeOnOverlay点击遮罩关闭booleantrue
showHeader是否显示标题栏booleantrue
showFooter是否显示底部区域booleantrue
confirmText默认确认按钮文字string确定
cancelText默认取消按钮文字string取消
animation动画预设scale | slide-up | fade | slide-downscale
animationType自定义动画类型TransitionConfig['name']undefined
duration动画持续时间numberundefined
delay动画延迟numberundefined
easing动画缓动函数TransitionConfig['easing']undefined

Events

事件名说明回调参数
update:modelValue显示状态变化(visible: boolean)
open入场动画结束后触发()
close离场动画结束后触发()
confirm点击默认确认按钮时触发()
cancel点击默认取消按钮时触发()
click-overlay点击遮罩时触发()
click-close点击右上角关闭按钮时触发()
after-enter入场动画结束后触发()
after-leave离场动画结束后触发()

WARNING

confirm 只由默认确认按钮触发;遮罩、右上角关闭按钮和取消按钮不会触发 confirm

Slots

插槽名说明
default弹框主体内容
header自定义头部(会覆盖 title)
footer自定义底部按钮区域

使用建议

TIP

如果只需要一个自定义浮层容器,请用 lk-popup;如果需要标准对话框结构与默认操作按钮,请用 lk-modal

发布验收

lk-modal 已纳入 needs-hardening showcase 回归,发布前按下面边界验收:

场景验收方式要点
展示台基线自动回归tests/visual/needs-hardening-showcase.spec.ts 校验组件路由、verified 状态与中风险标记
操作链路人工验收confirm/cancel/click-overlay/click-close 语义互不串扰
弹层表现人工验收fixed 遮罩、动画结束事件和异步确认在 H5/App/小程序端稳定

基于 MIT 协议开源 · 粤ICP备2022114240号-3

Modal 模态框

正在连接预览服务...

请先运行 pnpm run dev:h5