示例工程 28-buttons-dialog

28-buttons-dialog/app/index.html

html · 6.6 KB

运行:按钮与对话框 · button / dialog / popover

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>按钮与对话框 · button / dialog / popover</title>
    <meta name="description" content="按钮类型、对话框与 popover 的语义与焦点管理。" />
    <style>
      * { box-sizing: border-box; }
      body {
        margin: 0;
        min-height: 100vh;
        padding: 32px 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%, 780px); 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: 28px 0; }
      .eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); 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: 22px; }
      p { margin: 0 0 12px; color: #334155; }
      ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      button, .button {
        margin: 0 8px 8px 0;
        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; }
      button:disabled { opacity: 0.55; cursor: not-allowed; }
      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, 260px);
        padding: 12px;
        border: 1px solid #cbd5e1;
        border-radius: 10px;
        background: #fff;
        box-shadow: 0 8px 24px rgb(15 23 42 / 0.12);
      }
      [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: 30px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        body { padding: 20px 14px 32px; }
        .site-header { flex-direction: column; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">UI NOTES</span>
        <nav aria-label="页内导航">
          <a href="#buttons">按钮</a>
          <a href="#dialog">对话框</a>
          <a href="#popover">Popover</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 28</p>
        <h1>按钮与对话框</h1>
        <p class="lead">
          按钮触发动作,链接负责跳转;对话框与 popover 都需要清晰的名称、焦点与关闭方式。
        </p>

        <section id="buttons">
          <h2>1. 按钮类型</h2>
          <form action="preview.html" method="post">
            <button type="submit">保存</button>
            <button type="button" class="secondary">预览</button>
            <button type="reset" class="secondary">重置</button>
            <button type="submit" formaction="draft.html" formnovalidate class="secondary">保存草稿</button>
            <button type="button" disabled>不可用</button>
          </form>
          <p class="note">表单里的按钮默认是 submit;不需要提交时,显式写 <code>type="button"</code>。</p>
        </section>

        <section id="dialog">
          <h2>2. 对话框</h2>
          <p>下面是静态打开的 <code>dialog</code>,真实项目用 <code>showModal()</code> 打开模态对话框。</p>
          <dialog open aria-labelledby="confirm-title" aria-describedby="confirm-desc">
            <h2 id="confirm-title">删除这条记录?</h2>
            <p id="confirm-desc">删除后无法恢复。请确认后再操作。</p>
            <form method="dialog" class="actions">
              <button type="submit" class="secondary">取消</button>
              <button type="submit" value="delete">删除</button>
            </form>
          </dialog>
          <pre><code>dialog.showModal();   // 打开模态
dialog.close("ok");   // 关闭并返回值
Esc 默认关闭</code></pre>
        </section>

        <section id="popover">
          <h2>3. Popover</h2>
          <button type="button" popovertarget="menu">打开菜单</button>
          <div id="menu" popover>
            <ul>
              <li><a href="#dialog">查看对话框</a></li>
              <li><a href="#rules">查看规则</a></li>
            </ul>
          </div>
          <p><code>popover</code> 是轻量浮层,点击外部或 Esc 关闭;不锁定页面滚动。</p>
        </section>

        <section id="rules">
          <h2>4. 规则</h2>
          <ul>
            <li>动作按钮用 <code>button</code>,跳转用 <code>a</code>。</li>
            <li>表单内按钮默认 submit,不需要提交时显式写 type。</li>
            <li>对话框要有可访问名称,关闭后焦点回到触发按钮。</li>
            <li>模态对话框要锁定背景焦点与滚动。</li>
            <li><code>method="dialog"</code> 的表单可以关闭对话框。</li>
            <li>popover 适合轻量菜单,不适合关键确认。</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例数据为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>