Modal 响应式弹窗
WModal 参考 bookStore-platform 的弹窗样式,在 PrimeVue Dialog 上封装桌面居中、移动端底部弹出的布局。使用 24px 圆角、细边框和轻阴影;低于 850px 时铺满底部宽度,带顶部把手和安全区留白。
基础用法
使用 v-model:visible 控制显示。取消和关闭会请求更新为 false;确认只触发 confirm,由使用方决定何时关闭。
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WModal } from '@wizdomt/ui'
import { CloseIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
const result = shallowRef('等待操作')
function confirm() {
result.value = '已确认'
visible.value = false
}
</script>
<template>
<WButton label="打开 Modal" @click="visible = true" />
<span role="status">{{ result }}</span>
<WModal
v-model:visible="visible"
title="操作确认"
description="确认提交当前信息?在手机上,此弹窗会从底部弹出。"
@confirm="confirm"
@cancel="result = '已取消'"
>
<template #closeicon><CloseIcon :size="20" /></template>
</WModal>
</template>自动聚焦
autoFocus 默认为 false,打开时保留原来的焦点,不主动聚焦弹窗、标题、按钮或输入框,包括插槽内带 autofocus 的控件。需要自动聚焦时设置 auto-focus:
<!-- 默认不移动焦点 -->
<WModal v-model:visible="visible" title="提示" />
<!-- 启用自动聚焦;优先选择带 autofocus 的控件,否则聚焦关闭按钮 -->
<WModal v-model:visible="formVisible" title="编辑信息" auto-focus>
<WInputText v-model="name" autofocus />
</WModal>表单与校验
使用 confirmDisabled 控制确认按钮。示例默认不聚焦,勾选「打开时自动聚焦输入框」可切换 autoFocus;输入框上的 autofocus 指定自动聚焦的目标。断点切换只改变 CSS 布局,保持同一个 Dialog 实例和表单状态。
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WCheckbox, WInputText, WModal, WTextarea } from '@wizdomt/ui'
import { CloseIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
const autoFocus = shallowRef(false)
const name = shallowRef('')
const notes = shallowRef('')
const result = shallowRef('尚未保存')
function save() {
result.value = `已保存:${name.value}`
visible.value = false
}
</script>
<template>
<label class="modal-focus-option">
<WCheckbox v-model="autoFocus" binary />打开时自动聚焦输入框
</label>
<WButton label="打开表单弹窗" @click="visible = true" />
<span role="status">{{ result }}</span>
<WModal
v-model:visible="visible"
title="编辑信息"
confirm-text="保存"
:auto-focus="autoFocus"
:confirm-disabled="!name.trim()"
@confirm="save"
>
<template #closeicon><CloseIcon :size="20" /></template>
<div class="modal-form">
<label>名称<WInputText v-model="name" placeholder="请输入名称" autofocus /></label>
<label>备注<WTextarea v-model="notes" :rows="4" placeholder="请输入备注" /></label>
</div>
</WModal>
</template>
<style scoped>
.modal-focus-option {
display: inline-flex;
align-items: center;
gap: 8px;
}
.modal-form,
.modal-form label {
display: grid;
min-width: 0;
gap: 8px;
}
.modal-form {
gap: 16px;
}
.modal-form input,
.modal-form textarea {
width: 100%;
min-width: 0;
}
</style>异步确认
loading 会禁用默认按钮,并暂停关闭按钮、遮罩点击及 Escape 关闭。完成异步操作后,使用方仍可设置 visible = false 关闭。
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue'
import { WButton, WModal } from '@wizdomt/ui'
import { CloseIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
const loading = shallowRef(false)
const result = shallowRef('等待保存')
let timer: number | undefined
function save() {
loading.value = true
timer = window.setTimeout(() => {
result.value = '保存成功'
visible.value = false
loading.value = false
}, 1500)
}
onBeforeUnmount(() => window.clearTimeout(timer))
</script>
<template>
<WButton label="打开异步弹窗" @click="visible = true" />
<span role="status">{{ result }}</span>
<WModal
v-model:visible="visible"
title="保存设置"
description="点击保存模拟异步操作;处理期间关闭入口和操作按钮会暂时禁用。"
confirm-text="保存"
:loading="loading"
@confirm="save"
>
<template #closeicon><CloseIcon :size="20" /></template>
</WModal>
</template>长内容
桌面最大高度为可用视口减 32px,移动端为 85dvh。正文独立滚动,标题与底部操作区保持可见。
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WModal } from '@wizdomt/ui'
import { CloseIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
</script>
<template>
<WButton label="打开长内容弹窗" @click="visible = true" />
<WModal
v-model:visible="visible"
title="阅读说明"
:width="640"
confirm-text=""
cancel-text="关闭"
>
<template #closeicon><CloseIcon :size="20" /></template>
<p v-for="index in 30" :key="index" class="modal-paragraph">
第
{{ index }} 项:内容较多时只滚动正文,标题和底部操作区保持可见。移动端内容高度随可用视口调整。
</p>
</WModal>
</template>
<style scoped>
.modal-paragraph {
margin: 0;
padding: 10px 0;
line-height: 24px;
}
</style>自定义内容
提供 header、默认内容、footer 和 closeicon 插槽。内容插槽的 close() 请求关闭,遵循 loading 状态。图标示例使用 @wizdomt/icons。
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WModal } from '@wizdomt/ui'
import { CloseIcon, FileIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
</script>
<template>
<WButton label="打开自定义弹窗" @click="visible = true" />
<WModal v-model:visible="visible" title="资源详情">
<template #closeicon><CloseIcon :size="20" /></template>
<template #header
><span class="modal-custom-title"><FileIcon :size="20" />资源详情</span></template
>
<p class="modal-custom-body">通过 header、默认插槽和 footer 组合自己的弹窗内容。</p>
<template #footer="{ close }"
><WButton class="modal-custom-done" label="完成" @click="close"
/></template>
</WModal>
</template>
<style scoped>
.modal-custom-title {
display: inline-flex;
align-items: center;
gap: 8px;
}
.modal-custom-body {
margin: 0;
line-height: 24px;
}
.modal-custom-done {
min-height: 44px;
}
</style>危险操作
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WModal } from '@wizdomt/ui'
import { CloseIcon } from '@wizdomt/icons'
const visible = shallowRef(false)
</script>
<template>
<WButton label="打开危险操作确认" severity="danger" outlined @click="visible = true" />
<WModal
v-model:visible="visible"
title="确认移除?"
description="此处仅演示危险操作的确认样式。"
theme="danger"
confirm-text="移除"
@confirm="visible = false"
>
<template #closeicon><CloseIcon :size="20" /></template>
</WModal>
</template>API
| Props | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | false | 使用 v-model:visible 绑定 |
title | string | '提示' | 标题及默认可访问名称 |
description | string | — | 未提供默认插槽时的正文 |
width | number | string | 480 | 桌面宽度,数字按 px;移动端铺满宽度 |
theme | 'primary' | 'danger' | 'primary' | 默认确认按钮主题 |
confirmText | string | '确认' | 确认文案,空字符串隐藏按钮 |
cancelText | string | '取消' | 取消文案,空字符串隐藏按钮 |
confirmDisabled | boolean | false | 禁用默认确认按钮 |
loading | boolean | false | 处理中状态 |
closable | boolean | true | 显示右上角关闭按钮 |
maskClosable | boolean | true | 允许点击遮罩关闭 |
closeOnEscape | boolean | true | 允许按 Escape 关闭 |
autoFocus | boolean | false | 是否在打开时自动将焦点移入弹窗 |
showFooter | boolean | true | 显示底部区域;自定义 footer 同样受此项控制 |
| 事件 | 参数 | 说明 |
|---|---|---|
update:visible | boolean | 请求更新显示状态 |
confirm | — | 点击默认确认按钮,保持打开 |
cancel | — | 点击默认取消按钮,并请求关闭 |
close | — | 关闭按钮、遮罩、Escape 或插槽 close() 请求关闭 |
show / hide / after-hide | — | 原生 Dialog 生命周期,after-hide 在退出动画完成后触发 |
header、默认内容与 footer 插槽接收 { close };closeicon 无参数。可导入 WModalProps、WModalEmits 和 WModalSlots。
响应式与可访问性
850px 及以上居中展示,以下从底部滑入;窗口缩放时内容和输入保持不变。底部预留 safe-area-inset-bottom,关闭按钮和默认操作按钮在窄屏扩大触控区域。系统偏好减少动态效果时关闭入场和退出动画。
默认打开时保留原来的焦点,标题和控件均不会自动获取焦点。弹窗仍可通过鼠标或键盘操作;进入弹窗后,Tab 焦点约束与焦点提示保持有效。设置 auto-focus 可恢复 PrimeVue 的自动聚焦行为(优先查找带 autofocus 的控件,否则聚焦关闭按钮)。
焦点约束、关闭后的焦点恢复、页面滚动锁定和浮层层级由 PrimeVue Dialog 处理。自定义标题仍会关联对话框的可访问名称;也可传入 aria-label 或 aria-labelledby。自定义 footer 的按钮状态由使用方负责。
引入 @wizdomt/ui/style.css 并注册 WizdomtUI;在 html 上添加 .dark 启用暗色。WModal 是扩展组件,原生 Dialog 保持原样导出。