示例工程 33-dialog-popover
33-dialog-popover/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>dialog 与 popover · 模态、焦点与浮层</title>
<meta name="description" content="原生 dialog 的模态、焦点管理,以及 popover 的轻量浮层。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 30px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 800px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 26px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 10px; font-size: clamp(30px, 6vw, 42px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 21px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; cursor: pointer; }
button.secondary { background: #fff; color: #334155; border-color: #cbd5e1; }
dialog {
width: min(92vw, 420px);
padding: 20px;
border: 1px solid #cbd5e1;
border-radius: 12px;
background: #fff;
color: #0f172a;
}
dialog::backdrop { background: rgb(15 23 42 / 0.45); }
dialog h2 { margin: 0 0 8px; font-size: 20px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
[popover] {
width: min(88vw, 280px);
padding: 14px;
border: 1px solid #cbd5e1;
border-radius: 10px;
background: #fff;
box-shadow: 0 8px 24px rgb(15 23 42 / 0.14);
}
[popover] p { margin: 0 0 8px; }
[popover] ul { margin: 0; padding-left: 18px; }
[popover] a { color: #2563eb; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
pre code { padding: 0; background: none; color: inherit; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 18px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">UI / 2026</span>
<nav aria-label="页内导航">
<a href="#dialog">对话框</a>
<a href="#popover">浮层</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 33</p>
<h1>dialog 与 popover</h1>
<p class="lead">
dialog 负责模态确认,popover 负责轻量浮层。两者都要有名称、焦点与关闭方式。
</p>
<section id="dialog">
<h2>1. 对话框</h2>
<p>下面是静态打开的 dialog,真实项目用 <code>showModal()</code> 打开模态:</p>
<dialog open aria-labelledby="confirm-title" aria-describedby="confirm-desc" closedby="any">
<h2 id="confirm-title">删除这条记录?</h2>
<p id="confirm-desc">删除后无法恢复。请确认后再操作。</p>
<form method="dialog" class="actions">
<button type="submit" class="secondary" autofocus>取消</button>
<button type="submit" value="delete">删除</button>
</form>
</dialog>
<pre><code>dialog.showModal(); // 打开模态
dialog.close("delete"); // 关闭并返回
dialog.addEventListener("close", …);
dialog.addEventListener("cancel", …);</code></pre>
<p>模态打开时背景不可交互,Esc 默认关闭,关闭后焦点回到触发按钮。</p>
</section>
<section id="popover">
<h2>2. 浮层</h2>
<p><code>popover="auto"</code> 点击外部关闭,适合菜单与提示。</p>
<button type="button" popovertarget="menu">打开菜单</button>
<div id="menu" popover="auto">
<ul>
<li><a href="#dialog">查看对话框</a></li>
<li><a href="#rules">查看规则</a></li>
</ul>
</div>
<p><code>popover="manual"</code> 不会自动关闭,需要显式按钮。</p>
<button type="button" popovertarget="notice">打开说明</button>
<div id="notice" popover="manual">
<p>这是一个手动关闭的浮层。</p>
<button type="button" popovertarget="notice" popovertargetaction="hide" class="secondary">关闭</button>
</div>
<p class="note">需要用户做决定时用 dialog;只是补充信息时用 popover。</p>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li>dialog 要有可访问名称,通常用 <code>aria-labelledby</code>。</li>
<li>打开后焦点进入对话框,关闭后回到触发按钮。</li>
<li>模态对话框要锁定背景焦点与滚动。</li>
<li>popover 不强制焦点,适合菜单与提示。</li>
<li>manual popover 要提供显式关闭按钮。</li>
<li>不要用 popover 做删除确认。</li>
</ul>
</section>
</main>
<footer>
<p>示例内容为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>