Responsive accessible ui

From Changan-Su/Forsion · View interactive version

Responsive accessible ui is an AI agent skill from the GitHub repository Changan-Su/Forsion. 当在 Coding Space 构建或修改任何网页 UI(页面、布局、组件、表单、弹窗/对话框、导航、图标按钮)时使用,提供移动优先响应式布局、flexbox/grid 套路、clamp 流式尺寸、语义化 HTML、ARIA 取舍、键盘可达与焦点管理(focus trap)、WCAG 对比度、prefers-reduced-motion/prefers-color-scheme、表单可访问性、触摸目标 44×44 等 a11y 能力,附可直接复制的 CSS/JSX 片段与交付前自检清单。

How to use this skill

Skill file

Scope: written for Forsion Coding Space's static preview runtime (files served as-is, .ts/.tsx transpiled on the fly, bare imports through an importmap to esm.sh, no bundler, no npm install). In a repository that has its own build system, follow that build system instead of these rules.

响应式布局与无障碍(a11y)

默认移动优先、语义先行、ARIA 兜底。下面每条都可落地。

0. Forsion 环境铁律(代码必须遵守)

<!-- index.html -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="importmap">{ "imports": {
  "react": "https://esm.sh/react@18.3.1",
  "react-dom/client": "https://esm.sh/react-dom@18.3.1/client",
  "react/jsx-runtime": "https://esm.sh/react@18.3.1/jsx-runtime"
}}</script>
<link rel="stylesheet" href="./styles.css">
<div id="root"></div>
<script type="module" src="./main.tsx"></script>

1. 移动优先 + 断点

2. Flexbox / Grid 常用套路

/* 自适应网格:不写媒体查询,列数随宽度自动增减 */
.auto-grid { display: grid; gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); }
/* 内容居中限宽,两侧留白 */
.page { display: grid; grid-template-columns: 1fr min(70ch, 100%) 1fr; }
.page > * { grid-column: 2; }
/* 弹性行:超窄自动换行,别硬撑溢出 */
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.toolbar .spacer { margin-inline-start: auto; }   /* 把右侧推到底 */

3. 流式尺寸:clamp

:root {
  --step-0: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);    /* 流式正文 */
  --step-2: clamp(1.5rem, 1.2rem + 1.5vw, 2.25rem);   /* 流式标题 */
  --gutter: clamp(1rem, 5vw, 2.5rem);                 /* 流式留白 */
}
h1 { font-size: var(--step-2); }

clamp(最小, 首选, 最大):最小/最大用 rem 保证可缩放,首选含 vw 随视口流动——一行替代多数媒体查询。

4. 语义化 HTML(第一道 a11y 防线)

5. ARIA:何时用 / 何时别用

铁律:No ARIA is better than bad ARIA。 有原生元素就别加;用了就得补齐它承诺的行为(键盘、状态)。

<button aria-label="搜索"><SearchIcon aria-hidden="true" /></button>
<button aria-expanded={open} aria-controls="menu">菜单</button>
<div role="status" aria-live="polite">{msg}</div>   {/* polite 不打断 */}

6. 键盘可达与焦点管理

:focus-visible { outline: 3px solid var(--focus, #2563eb); outline-offset: 2px; }
.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 1rem; top: 1rem; }
function Modal({ onClose, children }: { onClose: () => void; children: React.ReactNode }) {
  const ref = React.useRef<HTMLDivElement>(null);
  React.useEffect(() => {
    const prev = document.activeElement as HTMLElement;          // 记住触发元素
    const box = ref.current!;
    (box.querySelector<HTMLElement>('[autofocus],button,[href],input') ?? box).focus();
    const onKey = (e: KeyboardEvent) => {
      if (e.key === 'Escape') return onClose();
      if (e.key !== 'Tab') return;
      const f = box.querySelectorAll<HTMLElement>(
        'a[href],button,input,select,textarea,[tabindex]:not([tabindex="-1"])');
      if (!f.length) return;
      const first = f[0], last = f[f.length - 1];
      if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
      else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
    };
    document.addEventListener('keydown', onKey);
    return () => { document.removeEventListener('keydown', onKey); prev?.focus(); }; // 归还焦点
  }, [onClose]);
  return (
    <div className="overlay" onClick={onClose}>
      <div ref={ref} role="dialog" aria-modal="true" aria-labelledby="dlg-t"
           onClick={(e) => e.stopPropagation()}>
        <h2 id="dlg-t">标题</h2>{children}
      </div>
    </div>
  );
}

7. 对比度与可读性

8. prefers-reduced-motion / prefers-color-scheme

@media (prefers-reduced-motion: reduce) {          /* 前庭敏感用户友好 */
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important; scroll-behavior: auto !important;
  }
}
:root { color-scheme: light dark; --bg: #fff; --fg: #1a1a1a; }  /* 控件/滚动条跟随 */
@media (prefers-color-scheme: dark) { :root { --bg: #0f0f0f; --fg: #ededed; } }
body { background: var(--bg); color: var(--fg); }

9. 表单可访问性

<label htmlFor="email">邮箱</label>
<input id="email" type="email" autoComplete="email"
       aria-invalid={!!err} aria-describedby={err ? 'email-err' : undefined} />
{err && <p id="email-err" role="alert">{err}</p>}

10. 触摸目标尺寸

.icon-btn { min-width: 44px; min-height: 44px; display: inline-grid; place-items: center; }
@media (pointer: coarse) { .btn { min-height: 48px; } }

交付前自检清单

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.