Implement focus management
✓Works with OpenClaudeYou 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
Taband inspect which elements receive focus via the Elements panel - Run
document.activeElementin console to see currently focused element
Steps
- Create a focus manager utility that tracks focus history using a stack data structure
- Implement focus trap logic that prevents Tab key from leaving a modal or popover using
keydownevent listeners - Add visible focus indicators via CSS
:focus-visiblepseudo-class (not just:focus) - Set up focus restoration by saving and restoring
document.activeElementwhen modals open/close - Create a function to programmatically focus elements using
.focus()withpreventScroll: falseoption - Implement skip links at the top of your page using hidden anchor links
- Test all functionality with keyboard only (no mouse) and screen reader software like NVDA or JAWS
- 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
langattribute 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 DevToolsbrowser 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
Related Accessibility Skills
Other Claude Code skills in the same category — free to download.
A11y Audit
Audit accessibility issues in components
ARIA Fixer
Add proper ARIA attributes
Keyboard Nav
Implement keyboard navigation
Screen Reader Fix
Fix screen reader issues
Color Contrast
Fix color contrast issues
Skip Navigation
Add skip navigation links
A11y Testing
Set up automated accessibility testing
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.