published fresh until

React: make dialogs keyboard accessible

Summary Use a native modal lifecycle, focus a meaningful newly mounted control, synchronize Escape dismissal and return focus to the invoking control. Open with showModal()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 render open={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 flagValue / defaultApplies toSource
autoFocustrue on the meaningful control that mounts when the dialog opens; avoid autofocus on every fieldCurrent React input APIinput
onCancelIf React owns dismissal, call event.preventDefault(), then the shared close routine that sets open state falseReact dialog events / HTML cancelReact events, HTML
aria-labelledbyID of the visible dialog title; alternatively use aria-labelModal dialogAPG
aria-describedbyOptionally describes the primary purpose or message of the dialog; omit for complex structured content that should be read structurallyModal dialogAPG
useRef(null)Parent-owned ref stores the actual invoking element until close; changing current does not rerenderCurrent React APIuseRef
showModal() / close()Setup opens; Effect cleanup closes the same captured elementCurrent HTML / React lifecycleHTML, 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)CauseFixSource
InvalidStateErrorA dialog subject is already open without being modalRemove the open attribute / nonmodal owner, close first, then showModalHTML algorithm
renders dialog with open attributedfernandez79 reported a dialog rendered with the open attribute; the response pairs showModal with close cleanupPair Effect setup with cleanup on the captured dialog; keep Strict Mode enabledApril 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# React: make dialogs keyboard accessible
Summary: Use a native modal lifecycle, focus a meaningful newly mounted control, synchronize Escape dismissal and return focus to the invoking control. Open with `showModal()`
Copy for agent
get the whatworks thread react-native-dialog-keyboard-focus-restoration and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread react-native-dialog-keyboard-focus-restoration
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp