复杂后台经常同时包含确认弹窗、编辑弹窗和帮助面板。自行维护遮罩、层级和键盘行为容易产生焦点丢失。原生 dialog 提供模态展示机制,本文围绕一个资料编辑弹窗说明如何正确打开、提交、关闭和测试。示例均为局部结构,可按需要组合。
1. 定义弹窗语义
dialog 的模态打开方式是 showModal,而不是手动切换 display。标题通过 aria-labelledby 关联,输入字段继续使用正常的 label。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/76418c12ac0758d3987580462ce057d664688372">
<dialog data-editor aria-labelledby="editor-title">
<h2 id="editor-title">编辑资料</h2>
<label>名称 <input name="name" required></label>
</dialog>
</div>
2. 控制打开入口
重复调用打开操作前先检查状态。对于同一个组件,缓存节点引用比每次点击重新搜索整页更清晰。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/7cd950c7ae4dbe0fcf4eed58d3136e3d39070abb">
<button type="button" data-open-editor>编辑资料</button>
</div>
const dialog = document.querySelector('[data-editor]');
const opener = document.querySelector('[data-open-editor]');
opener.addEventListener('click', () => {
if (!dialog.open) dialog.showModal();
});
3. 选择初始焦点
表单弹窗通常把焦点放在第一个必要字段;复杂提示弹窗可以先让用户阅读标题或说明。不要每次状态更新都强行聚焦输入框。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/0e8282925a6a8a9c894c6e06617f6575ce9101e9">
<dialog aria-label="修改显示名称">
<label>显示名称 <input name="displayName" autofocus required></label>
<button type="button" data-close>取消</button>
</dialog>
</div>
4. 使用原生关闭语义
method="dialog" 的表单提交会关闭所在弹窗,并设置 returnValue;它并不向服务器发送 HTTP 请求。适合纯本地确认。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/4f9a787a218b40a582497a6234eb752663a6306b">
<dialog data-confirm>
<form method="dialog">
<p>是否放弃尚未保存的修改?</p>
<button value="cancel">继续编辑</button>
<button value="discard">放弃修改</button>
</form>
</dialog>
</div>
const confirmDialog = document.querySelector('[data-confirm]');
confirmDialog.addEventListener('close', () => {
if (confirmDialog.returnValue === 'discard') resetDraft();
});
5. 区别关闭事件与取消事件
Escape 通常触发 cancel,随后可能关闭弹窗。若业务确实需要阻止放弃草稿,可在 cancel 时调用 preventDefault;不要无条件禁用所有键盘关闭方式。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/0fb7e47d9217cda8c2c7870940b65285219a2f1a">
<p data-unsaved-notice role="status">修改尚未保存</p>
<button type="button" data-discard>放弃修改</button>
</div>
dialog.addEventListener('cancel', event => {
if (hasUnsavedChanges()) {
event.preventDefault();
showDiscardConfirmation();
}
});
6. 异步保存不等于原生提交关闭
需要服务器确认的编辑表单不应使用 method="dialog" 自动关闭。应先阻止默认导航,再等待业务保存成功。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/5687b48cf61c04b9e0b75d455ec8aede8ace71f6">
<form data-save-form>
<label>备注 <textarea name="note"></textarea></label>
<button type="submit">保存</button>
<p role="alert" data-save-error></p>
</form>
</div>
async function submitEditor(event) {
event.preventDefault();
const form = event.currentTarget;
const button = event.submitter;
if (form.dataset.saving === 'true') return;
const data = new FormData(form);
form.dataset.saving = 'true';
if (button) button.disabled = true;
try {
await saveProfile(data); dialog.close('saved'); }
catch {
form.querySelector('[data-save-error]').textContent = '保存失败'; }
finally {
delete form.dataset.saving; if (button) button.disabled = false; }
}
7. 关闭后恢复焦点
浏览器有原生焦点恢复行为,但触发按钮可能在异步列表更新中被移除。准备一个合理的后备焦点目标,避免键盘用户丢失位置。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/cedc8253b2750a0b31800e5be4d570c6ac666958">
<h2 tabindex="-1" data-list-heading>资料列表</h2>
<button type="button" data-edit-row>编辑选中记录</button>
</div>
let previousFocus;
function openEditor() {
previousFocus = document.activeElement;
dialog.showModal();
}
dialog.addEventListener('close', () => {
const target = previousFocus?.isConnected
? previousFocus : document.querySelector('[data-list-heading]');
target?.focus();
});
8. 通过 backdrop 管理遮罩样式
模态弹窗位于浏览器的顶层,其遮罩可通过 ::backdrop 定义。普通容器的 z-index 无法等价替代顶层机制。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/4759071e38859cf641eaf059b4a791d334cc119c">
<dialog class="editor-modal" aria-label="编辑信息">
<p>此处展示编辑表单。</p>
</dialog>
</div>
.editor-modal {
max-inline-size: min(36rem, 90vw); border: 0; border-radius: .75rem; }
.editor-modal::backdrop {
background: rgb(0 0 0 / 45%); }
9. 处理移动端可滚动内容
弹窗内容较长时,限制弹窗自身高度,并让正文内部滚动。固定高度表单可能在软键盘弹出后遮挡关键操作。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/097eae212646aab4eb3f7216f9e635b589692066">
<dialog class="long-dialog">
<header><h2>详细设置</h2></header>
<section class="dialog-body">设置项</section>
<footer><button type="button">完成</button></footer>
</dialog>
</div>
.long-dialog {
max-block-size: 85dvh; }
.dialog-body {
max-block-size: 60dvh; overflow: auto; }
10. 验证交互边界
使用真实键盘检查 Tab、Shift+Tab、Escape 和提交行为。还要测试保存失败、打开多个弹窗以及触发按钮被移除的情况。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/75af52f5c33999f7dc136f021f71bc9fe4a0e57d">
<ul aria-label="弹窗验收项目">
<li>初始焦点正确</li><li>关闭后焦点可恢复</li>
<li>网络错误不丢失输入</li>
</ul>
</div>
自动化测试应检查可访问名称及实际焦点,而不仅是断言 open 属性。对于不支持 dialog 的运行环境,再根据兼容要求安排降级。
总结
原生 dialog 可以减少许多重复的模态交互实现,但业务保存状态和草稿恢复仍需要应用负责。清晰的关闭协议、合理的焦点管理和实际键盘测试比单纯调整遮罩样式更重要。
推荐阅读
Svelte 动画实战:transition、animate 与列表重排交互
https://github.com/tiktokwuyu-ux/technical-notes/commit/c67ad9feb927a1c3c728a53bd04418c3bf624e63
Angular 依赖注入实战:服务作用域、InjectionToken 与可替换架构
https://github.com/tiktokwuyu-ux/technical-notes/commit/ff03ff0cbae91e018fc6cb04c5e41e7c80a1b835
JavaScript History API 实战:单页导航、前进后退与滚动恢复
https://github.com/tiktokwuyu-ux/technical-notes/commit/4759071e38859cf641eaf059b4a791d334cc119c
Vue 3 插槽实战:作用域插槽、可复用表格与组件 API 设计
https://github.com/tiktokwuyu-ux/technical-notes/commit/f7fb53f771a9cd0b32c7b7206177a5f51ac94d67
Vue 3 Composables 实战:组合式函数、响应式参数与资源释放
https://github.com/tiktokwuyu-ux/technical-notes/commit/76418c12ac0758d3987580462ce057d664688372
Vite 工程化实战:环境变量、动态模块加载与多环境构建
https://github.com/tiktokwuyu-ux/technical-notes/commit/b79a134c126e9bd7f27da79f2a33bbad350b7ef1
Vue 3 provide 与 inject 实战:跨层组件依赖和类型安全设计
https://github.com/tiktokwuyu-ux/technical-notes/commit/7cd950c7ae4dbe0fcf4eed58d3136e3d39070abb
Angular Reactive Forms 实战:动态字段、交叉校验与复杂表单
https://github.com/tiktokwuyu-ux/technical-notes/commit/5687b48cf61c04b9e0b75d455ec8aede8ace71f6
React useReducer 实战:复杂组件状态转换与可测试的业务流程
https://github.com/tiktokwuyu-ux/technical-notes/commit/9230820adc2173383c177b3abd09fd51a5c65e47
SvelteKit load 实战:服务端数据读取、页面依赖与失效更新
https://github.com/tiktokwuyu-ux/technical-notes/commit/cc161de407f18c17088cb3532b8784fb6cbe3fe9