Frontend Blocks
typescript
Intermediate
Accessible Focus-Trap Modal
Keyboard-navigable modal with focus trap and ARIA attributes. Closes on Escape.
typescript
import { useEffect, useRef } from 'react';
export function Modal({ open, onClose, children }) {
const ref = useRef(null);
useEffect(() => {
if (!open) return;
ref.current && ref.current.focus();
document.body.style.overflow = 'hidden';
const h = (e) => { if (e.key === 'Escape') onClose(); };
window.addEventListener('keydown', h);
return () => { document.body.style.overflow = ''; window.removeEventListener('keydown', h); };
}, [open, onClose]);
if (!open) return null;
return (
<div className='fixed inset-0 z-50 flex items-center justify-center bg-black/70' onClick={onClose}>
<div ref={ref} tabIndex={-1} role='dialog' aria-modal='true'
className='bg-slate-900 border border-slate-700 rounded-2xl p-6 w-full max-w-md'
onClick={(e) => e.stopPropagation()}>{children}</div>
</div>
);
}
Prerequisites
- React >= 17
- Tailwind CSS
Pro Tips
- → Render inside createPortal to escape CSS stacking contexts.
Commercial Licensing
Acquire a production access token or template license for enterprise deployment.
Module Info
- Language
- typescript
- Level
- Intermediate
- Read time
- 4 min
- Published
- Oct 2024
- License
- Commercial License
Newsletter
New modules every week, directly to your inbox.
Similar Modules
Copy to Clipboard Button
React component that copies text to clipboard and shows a Copied! confirmation.
typescript
·
Beginner
Framer Motion Fade-In Wrapper
Reusable component that fades any child element in on mount.
typescript
·
Beginner
Compound Component Pattern
Implement a flexible Tabs component using React Context and compound component pattern.
typescript
·
Intermediate