$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
AccessibilityintermediateNew

Focus Management

Share

Implement focus management

Works with OpenClaude

You are an accessibility specialist implementing focus management. The user wants to create a robust focus management system that handles keyboard navigation, focus trapping, focus restoration, and visible focus indicators across web applications.

What to check first

  • Verify your HTML has semantic elements (<button>, <a>, <input>) that are natively focusable
  • Check browser DevTools: press Tab and inspect which elements receive focus via the Elements panel
  • Run document.activeElement in console to see currently focused element

Steps

  1. Create a focus manager utility that tracks focus history using a stack data structure
  2. Implement focus trap logic that prevents Tab key from leaving a modal or popover using keydown event listeners
  3. Add visible focus indicators via CSS :focus-visible pseudo-class (not just :focus)
  4. Set up focus restoration by saving and restoring document.activeElement when modals open/close
  5. Create a function to programmatically focus elements using .focus() with preventScroll: false option
  6. Implement skip links at the top of your page using hidden anchor links
  7. Test all functionality with keyboard only (no mouse) and screen reader software like NVDA or JAWS
  8. Validate with ARIA attributes (role, aria-label, aria-describedby) for non-semantic elements

Code

class FocusManager {
  constructor() {
    this.focusStack = [];
    this.trapElement = null;
  }

  // Save current focus and trap it in a container
  trapFocus(containerElement) {
    const previouslyFocused = document.activeElement;
    this.focusStack.push(previouslyFocused);
    this.trapElement = containerElement;

    const focusableElements = containerElement.querySelectorAll(
      'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
    );

    if (focusableElements.length === 0) {
      console.warn('No focusable elements found in trap container');
      return;
    }

    const firstElement = focusableElements[0];
    const lastElement = focusableElements[focusableElements.length - 1];

    // Set initial focus
    firstElement.focus({ preventScroll: false });

    // Handle Tab/Shift+Tab at boundaries
    containerElement.addEventListener('keydown', (event) => {
      if (event.key !== 'Tab') return;

      if (event.shiftKey) {
        if (document.activeElement === firstElement) {
          lastElement.focus();
          event.preventDefault();
        }
      } else {
        if (document.activeElement === lastElement) {
          firstElement.focus();
          event.preventDefault();
        }
      }
    });
  }

  // Restore focus to previously active element
  restoreFocus() {
    const previousElement = this.focusStack.pop();

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Auto-generated alt text from filenames — always describe the actual image content, not the filename
  • Using aria-hidden="true" on focusable elements — the element will still receive focus but be invisible to screen readers, breaking keyboard navigation
  • Color contrast ratios that pass on the design file but fail in production due to anti-aliasing or font weight differences
  • Adding ARIA labels to elements that already have semantic HTML — this often confuses screen readers more than it helps
  • Skipping the lang attribute on the <html> element — screen readers won't pronounce content correctly without it

When NOT to Use This Skill

  • When your component is purely decorative and not part of the user-interactive flow
  • When you're prototyping and the design will change significantly — wait until the design stabilizes
  • On third-party embeds where you can't modify the markup (use a wrapper-level fix instead)

How to Verify It Worked

  • Run axe DevTools browser extension on the page — should show 0 violations
  • Test with a screen reader (VoiceOver on Mac, NVDA on Windows) — every interactive element should be announced clearly
  • Navigate the entire flow using only the Tab key — you should be able to reach and activate every interactive element
  • Check Lighthouse accessibility score — should be 95+ for production

Production Considerations

  • Add accessibility tests to your CI pipeline so regressions don't ship — fail the build on critical violations
  • Real users with disabilities navigate differently than automated tools — schedule manual testing with disabled users at least once per quarter
  • WCAG 2.1 AA is the legal minimum in most jurisdictions (ADA, EAA). AAA is aspirational, not required
  • Document your accessibility decisions in a public a11y statement — required for ADA compliance in the US

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
accessibilityfocusmanagement

Install command:

curl -o ~/.claude/skills/focus-management.md https://claude-skills-hub.vercel.app/skills/accessibility/focus-management.md

Related Accessibility Skills

Other Claude Code skills in the same category — free to download.

Want a Accessibility skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.