示例工程 32-states-focus

32-states-focus/app/index.html

html · 4.7 KB

运行:状态与焦点

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>状态与焦点</title>
    <link rel="stylesheet" href="tokens.css" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <a class="skip" href="#main">跳到正文</a>

    <div class="app">
      <header class="site-header">
        <span class="brand">STATE LAB</span>
        <nav aria-label="主导航">
          <a href="#matrix" aria-current="page">状态</a>
          <a href="#selected">选中</a>
          <a href="#focus">焦点</a>
          <a href="#tokens">token</a>
        </nav>
      </header>

      <main class="page" id="main">
        <p class="eyebrow">LESSON / 32</p>
        <h1>每个状态都要看得见</h1>
        <p class="lead">
          默认、hover、按下、焦点、禁用、选中——用一套选择器与 token 统一管理,别让组件各自发明。
        </p>

        <section class="panel" id="matrix">
          <h2>1. 按钮状态矩阵</h2>
          <p>前四个是预览类,最后一个是真的禁用;用 Tab 与鼠标试真实状态。</p>

          <div class="matrix">
            <div class="cell">
              <span class="tag">默认</span>
              <button class="btn" type="button">按钮</button>
            </div>
            <div class="cell">
              <span class="tag">hover</span>
              <button class="btn is-hover" type="button">按钮</button>
            </div>
            <div class="cell">
              <span class="tag">active</span>
              <button class="btn is-active" type="button">按钮</button>
            </div>
            <div class="cell">
              <span class="tag">focus</span>
              <button class="btn is-focus" type="button">按钮</button>
            </div>
            <div class="cell">
              <span class="tag">disabled</span>
              <button class="btn" type="button" disabled>按钮</button>
            </div>
          </div>

          <p class="hint">预览类只复制视觉,不改变真实交互;生产代码用伪类。</p>
        </section>

        <section class="panel" id="selected">
          <h2>2. 选中与切换</h2>

          <h3>切换按钮</h3>
          <div class="row">
            <button class="toggle" type="button" aria-pressed="true">已订阅</button>
            <button class="toggle" type="button" aria-pressed="false">未订阅</button>
            <button class="toggle" type="button" aria-disabled="true">不可用</button>
          </div>

          <h3>导航当前项</h3>
          <nav class="subnav" aria-label="示例导航">
            <a href="#selected" aria-current="page">概览</a>
            <a href="#selected">成员</a>
            <a href="#selected">设置</a>
          </nav>

          <h3>开关</h3>
          <label class="switch-row">
            <span>自动续费</span>
            <input class="switch" type="checkbox" role="switch" checked />
          </label>
        </section>

        <section class="panel" id="focus">
          <h2>3. 焦点管理</h2>
          <p>卡片内任意控件获得焦点时,整张卡片显示焦点环。</p>

          <div class="focus-card">
            <label class="field">
              <span class="label">邮箱</span>
              <input class="input" type="email" placeholder="you@example.com" />
            </label>
            <button class="btn primary" type="button">保存</button>
          </div>

          <p class="hint">键盘按 Tab 进入卡片;鼠标点击输入框也会触发 <code>:focus-within</code>。</p>
        </section>

        <section class="panel" id="tokens">
          <h2>4. 状态 token</h2>
          <div class="table-wrap">
            <table>
              <thead>
                <tr><th scope="col">状态</th><th scope="col">选择器</th><th scope="col">token</th></tr>
              </thead>
              <tbody>
                <tr><td>hover</td><td><code>:hover</code></td><td><code>--state-hover</code></td></tr>
                <tr><td>active</td><td><code>:active</code></td><td><code>--state-active</code></td></tr>
                <tr><td>focus</td><td><code>:focus-visible</code></td><td><code>--state-focus</code></td></tr>
                <tr><td>disabled</td><td><code>:disabled</code></td><td><code>--state-disabled</code></td></tr>
                <tr><td>selected</td><td><code>[aria-pressed="true"]</code></td><td><code>--state-selected</code></td></tr>
              </tbody>
            </table>
          </div>
        </section>
      </main>

      <footer class="site-footer">
        <p>示例数据,仅用于教程演示。</p>
        <p>STATES &amp; FOCUS · 2026</p>
      </footer>
    </div>
  </body>
</html>