What this thread does
Do first: open the native dialog with showModal() in a symmetric Effect and capture its invoking button before changing React's open state.
Short answer
As of November 2026, React 19.3 and current HTML dialog semantics are the baseline. Use one React state owner and one close routine.
Choose / don't choose
- Native modal: choose
showModal()when background controls must be inert and keyboard focus stays in the dialog; don't renderopen={true}and then call showModal. - Nonmodal panel: choose
show()or the open attribute only when interacting with the surrounding document remains appropriate; don't present that as a modal. - Custom dialog library: choose when its focus/portal behavior fits the application; don't layer a second independent focus trap over a native modal.
Not for: form field validation; see case-modal-form-validation-error-states-react-shadcn.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
autoFocus | true on the meaningful control that mounts when the dialog opens; avoid autofocus on every field | Current React input API | input |
onCancel | If React owns dismissal, call event.preventDefault(), then the shared close routine that sets open state false | React dialog events / HTML cancel | React events, HTML |
aria-labelledby | ID of the visible dialog title; alternatively use aria-label | Modal dialog | APG |
aria-describedby | Optionally describes the primary purpose or message of the dialog; omit for complex structured content that should be read structurally | Modal dialog | APG |
useRef(null) | Parent-owned ref stores the actual invoking element until close; changing current does not rerender | Current React API | useRef |
showModal() / close() | Setup opens; Effect cleanup closes the same captured element | Current HTML / React lifecycle | HTML, React issue |
For a long explanation, initial focus may belong on a static element at the start of the content with tabIndex={-1}, rather than a lower input that scrolls the title away. For irreversible actions choose the least destructive control. Keep a visible closing button in the dialog tab sequence (APG).
Order of operations
1. In the trigger's event handler, save event.currentTarget into a ref owned outside the conditional dialog, then set open state true. The ref survives the dialog unmount; capturing after showModal would capture an interior control instead. 2. Mount the dialog without an open attribute. Mount the selected autofocus control at that time. In its Effect capture the dialog node, call showModal, and return cleanup calling close. With a persistently mounted dialog, focus the chosen control deliberately on each opening; React autoFocus is a mount behavior. 3. If legacy code already opened the node nonmodally, close it before promoting it with showModal. Prefer removing mixed ownership. An if (!dialog.open) guard alone silently preserves an already-nonmodal dialog rather than making it modal. 4. Route the Close button, completed form and onCancel through one idempotent dismissal routine: close the native element, set React open state false and restore focus to the saved trigger if connected and focusable. If it disappeared, use a logical successor such as the newly created row's control. Do not restore external focus from every Effect cleanup: development remount cleanup is not a user dismissal. 5. With keyboard only, open from each trigger, inspect initial focus, traverse Tab/Shift+Tab through both ends, press Escape and reopen repeatedly. Confirm background controls cannot receive focus and that the intended trigger regains it. Check removal of the trigger, submit-to-close and navigation/unmount paths separately. 6. If using onClose as an additional state sync, distinguish programmatic teardown/remount closes from actual dismissal; avoid a queued close event from cleanup closing a newly reopened dialog.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
InvalidStateError | A dialog subject is already open without being modal | Remove the open attribute / nonmodal owner, close first, then showModal | HTML algorithm |
renders dialog with open attribute | dfernandez79 reported a dialog rendered with the open attribute; the response pairs showModal with close cleanup | Pair Effect setup with cleanup on the captured dialog; keep Strict Mode enabled | April 19 report and April 20 gaearon response |
Brent Fulgham's August 12 WebKit report documents focus-restoration differences for nonmodal dialogs. It does not establish a browser-release support floor; include actual browser focus checks.
Missing
- The dialog's content and trigger lifetime determine the initial target and fallback. Native semantics do not establish screen-reader behavior for every browser/assistive-technology pair.
Started from a post by @WHATWG, packaged by the editor. Original post