ReactMedium

Modal component

A good modal renders above the page, traps focus inside while open, closes on Escape and backdrop click, returns focus to the opener when closed, prevents background scrolling and interaction, and is announced to screen readers as a dialog. The simplest robust implementation today uses the native <dialog> element with showModal(), which provides focus trapping, Escape handling and an inert background for free.

Native <dialog> approach

  • dialogRef.current.showModal() when open becomes true; close() when false.
  • The browser makes the rest of the page inert, traps focus, and closes on Escape (listen to the close and cancel events to sync state).
  • Style the backdrop with dialog::backdrop.
  • Detect backdrop clicks by checking whether the click target is the dialog element itself.

Portal approach

createPortal(children, document.body) renders outside parent containers that clip or stack content (overflow hidden, z-index). Then you must implement focus trapping, aria-modal, Escape handling, scroll locking and inert on the background yourself, or use a library.

Accessibility checklist

  • aria-labelledby pointing at the title (and aria-describedby for the description).
  • Initial focus on the first meaningful control (or the dialog itself).
  • Return focus to the trigger on close.
  • Do not rely on the backdrop as the only way to close; include a visible close button.

Libraries

Radix Dialog, React Aria Modal, Headless UI Dialog, and shadcn/ui handle these details.

Code example

tsx
1import { useEffect, useId, useRef } from "react";
2
3type ModalProps = { open: boolean; onClose: () => void; title: string; children: React.ReactNode };
4
5export function Modal({ open, onClose, title, children }: ModalProps) {
6 const ref = useRef<HTMLDialogElement>(null);
7 const titleId = useId();
8
9 useEffect(() => {
10 const dialog = ref.current;
11 if (!dialog) return;
12 if (open && !dialog.open) dialog.showModal(); // focus trap, inert background, Escape support
13 if (!open && dialog.open) dialog.close(); // browser restores focus to the opener
14 }, [open]);
15
16 return (
17 <dialog
18 ref={ref}
19 aria-labelledby={titleId}
20 onClose={onClose} // fired on Escape and on close()
21 onClick={(e) => e.target === e.currentTarget && onClose()} // backdrop click
22 style={{ padding: 0, border: "none", borderRadius: 12 }}
23 >
24 <div style={{ padding: 24, minWidth: 320 }}>
25 <h2 id={titleId}>{title}</h2>
26 {children}
27 <button onClick={onClose} autoFocus>Close</button>
28 </div>
29 </dialog>
30 );
31}
32
33// Usage
34// const [open, setOpen] = useState(false);
35// <button onClick={() => setOpen(true)}>Delete account</button>
36// <Modal open={open} onClose={() => setOpen(false)} title="Delete account?"><p>This cannot be undone.</p></Modal>
37// CSS: dialog::backdrop { background: rgb(0 0 0 / 0.5); }

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app