Web accessibility is not just a nice-to-have — it is a legal requirement in many jurisdictions and a moral imperative. This guide covers practical techniques for building truly accessible web applications.
// Accessible modal component
function AccessibleModal({ isOpen, onClose, title, children }) {
const modalRef = useRef(null);
const previousFocus = useRef(null);
useEffect(() => {
if (isOpen) {
previousFocus.current = document.activeElement;
modalRef.current?.focus();
const handleKeyDown = (e) => {
if (e.key === "Escape") onClose();
if (e.key === "Tab") trapFocus(e, modalRef.current);
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
} else {
previousFocus.current?.focus();
}
}, [isOpen, onClose]);
if (!isOpen) return null;
return (
<div role="dialog" aria-modal="true" aria-labelledby="modal-title"
ref={modalRef} tabIndex={-1}>
<h2 id="modal-title">{title}</h2>
{children}
<button onClick={onClose} aria-label="Close dialog">Close</button>
</div>
);
}




