Skip to content

Rules for an AI coding agent to filter out generic AI-generated UI designs, text, and code.

7.0 KB
Raw Blame

Accessibility Code Patterns

Reference for antislop. Adapted from the MIT-licensed web-quality-skills by Addy Osmani (https://github.com/addyosmani/web-quality-skills); see LICENSE-web-quality-skills in the skill folder.

Practical, copy-paste-ready patterns for common accessibility requirements. Each pattern is self-contained and linked from the main SKILL.md.


Pointer cursor base layer

Set once, in the base CSS layer, so no component has to remember it (R-39). A CSS reset such as Tailwind preflight sets button to cursor: default, so this layer comes after the reset.

a[href], button, summary, select, label[for],
label:has(> input[type="checkbox"], > input[type="radio"]),
[role="button"], [role="link"], [role="tab"], [role="menuitem"], [role="option"], [role="switch"],
input[type="checkbox"], input[type="radio"], input[type="file"],
input[type="submit"], input[type="button"], input[type="reset"], input[type="range"], input[type="color"] {
  cursor: pointer;
}
:disabled, [aria-disabled="true"] { cursor: not-allowed; }

Text inputs keep cursor: text, and drag handles use grab / grabbing. An implicit label that wraps a checkbox or radio is included, since clicking it toggles the control.

Modal focus trap

Trap keyboard focus inside a modal dialog so Tab/Shift+Tab cycle through its focusable elements and Escape closes it.

function trapFocus(modal, onClose) {
  const focusable = modal.querySelectorAll(
    'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
  );
  if (focusable.length === 0) return () => {};

  const first = focusable[0];
  const last = focusable[focusable.length - 1];

  function onKeydown(e) {
    if (e.key === 'Escape') return onClose();
    if (e.key !== 'Tab') return;
    if (e.shiftKey && document.activeElement === first) {
      e.preventDefault();
      last.focus();
    } else if (!e.shiftKey && document.activeElement === last) {
      e.preventDefault();
      first.focus();
    }
  }

  modal.addEventListener('keydown', onKeydown);
  first.focus();
  return () => modal.removeEventListener('keydown', onKeydown);
}

Call the returned function when the dialog closes, or a second open stacks a second listener:

const release = trapFocus(dialog, () => dialog.close());
dialog.addEventListener('close', release, { once: true });

The native <dialog> element handles focus trapping automatically, prefer it when browser support allows.


Skip link

Allows keyboard users to bypass repetitive navigation and jump straight to main content.

<body>
  <a href="#main-content" class="skip-link">Skip to main content</a>
  <header><!-- navigation --></header>
  <main id="main-content" tabindex="-1">
    <!-- main content -->
  </main>
</body>
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: #000;
  color: #fff;
  padding: 8px 16px;
  z-index: 100;
}

.skip-link:focus {
  top: 0;
}

Error handling

Announce errors to screen readers and focus the first invalid field on submit.

<form novalidate>
  <div id="error-summary" tabindex="-1" hidden></div>
  <div class="field" aria-live="polite">
    <label for="email">Email</label>
    <input type="email" id="email"
           aria-invalid="true"
           aria-describedby="email-error">
    <p id="email-error" class="error" role="alert">
      Please enter a valid email address (e.g., [email protected])
    </p>
  </div>
</form>
const form = document.querySelector('form');
const summary = document.getElementById('error-summary');

form.addEventListener('submit', (e) => {
  const invalid = [...form.querySelectorAll('[aria-invalid="true"]')];
  if (invalid.length === 0) return;

  e.preventDefault();
  summary.textContent =
    `${invalid.length} errors found. Please fix them and try again.`;
  summary.hidden = false;
  invalid[0].focus();
});

This handler reports what the validation already marked: set aria-invalid="true" and the field's error text before submit runs.


Form labels

Every input needs an associated label, either explicit (for/id) or implicit (wrapping <label>).

<!-- ❌ No label association -->
<input type="email" placeholder="Email">

<!-- ✅ Explicit label -->
<label for="email">Email address</label>
<input type="email" id="email" name="email"
       autocomplete="email" required>

<!-- ✅ Implicit label -->
<label>
  Email address
  <input type="email" name="email" autocomplete="email" required>
</label>

<!-- ✅ With instructions -->
<label for="password">Password</label>
<input type="password" id="password"
       aria-describedby="password-requirements">
<p id="password-requirements">
  Must be at least 8 characters with one number.
</p>

Dragging movements

Any action triggered by dragging must offer a single-pointer alternative (WCAG 2.5.7).

<!-- ❌ Drag-only reorder -->
<ul class="sortable-list" draggable="true">
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

<!-- ✅ Drag + button alternatives -->
<ul class="sortable-list">
  <li>
    <span>Item 1</span>
    <button aria-label="Move Item 1 up">↑</button>
    <button aria-label="Move Item 1 down">↓</button>
  </li>
  <li>
    <span>Item 2</span>
    <button aria-label="Move Item 2 up">↑</button>
    <button aria-label="Move Item 2 down">↓</button>
  </li>
</ul>

Also applies to sliders, map panning, colour pickers, and similar drag-based widgets, always provide an equivalent click/tap or keyboard path.


ARIA tabs

Tabs require role="tablist", role="tab", and role="tabpanel" with proper aria-selected, aria-controls, and keyboard support.

<div role="tablist" aria-label="Product information">
  <button role="tab" id="tab-1" aria-selected="true"
          aria-controls="panel-1">Description</button>
  <button role="tab" id="tab-2" aria-selected="false"
          aria-controls="panel-2" tabindex="-1">Reviews</button>
</div>
<div role="tabpanel" id="panel-1" aria-labelledby="tab-1">
  <!-- Panel content -->
</div>
<div role="tabpanel" id="panel-2" aria-labelledby="tab-2" hidden>
  <!-- Panel content -->
</div>

Arrow keys should move focus between tabs; the active tab receives tabindex="0" while inactive tabs use tabindex="-1".


Live regions and notifications

Use aria-live to announce dynamic content changes to screen readers without moving focus.

<!-- Status updates (polite, waits for pause in speech) -->
<div id="polite-announcer" aria-live="polite" aria-atomic="true" class="status">
  <!-- Content updates announced to screen readers -->
</div>

<!-- Urgent alerts (assertive, interrupts) -->
<div id="assertive-announcer" role="alert" aria-live="assertive">
  <!-- Interrupts current announcement -->
</div>
function showNotification(message, type = 'polite') {
  const container = document.getElementById(`${type}-announcer`);
  container.textContent = '';
  requestAnimationFrame(() => {
    container.textContent = message;
  });
}

Clear the container before writing to ensure the same message triggers a new announcement.