#49181·material-ui

[FocusTrap] restoreLastFocus crashes when relatedTarget is not a focusable node (TypeError: nodeToRestore.current.focus is not a function)

Author: mwb100Created Sep 21, 2026Updated Sep 21, 2026
Labelsstatus: waiting for maintainer

Steps to reproduce

FocusTrap records the node to restore focus to from a focus event's relatedTarget, and restores it on unmount guarded only by truthiness:

javascript
// FocusTrap.mjs — onFocus (:285) and handleFocusSentinel (:296)
if (nodeToRestore.current === null) {
  nodeToRestore.current = event.relatedTarget;
}

// FocusTrap.mjs — restoreLastFocus (:139)
if (!disableRestoreFocus && nodeToRestore.current) {
  ignoreNextEnforceFocus.current = true;
  nodeToRestore.current.focus();   // <- assumes a focusable node
  nodeToRestore.current = null;
}

relatedTarget is typed EventTarget | null, and the guard only tests that it is not null/undefined — not that it has a focus() method. Any non-element EventTarget reaching that line throws.

I hit this under jsdom 30.1.0, whose changelog includes "Fixed focus state after removing a focused element or its ancestor". With that change, unmounting a focused Dialog/Menu dispatches the focus transition with relatedTarget set to the Document, and Document has no focus():

TypeError: nodeToRestore.current.focus is not a function
 ❯ node_modules/@mui/material/Unstable_TrapFocus/FocusTrap.mjs:141:31
 ❯ commitHookEffectListUnmount node_modules/react-dom/cjs/react-dom-client.development.js:13815:17

Isolated by pinning the version rather than inferred: the same test file passes 27/27 on jsdom 30.0.1 and fails 4 on 30.1.0, with nothing else changed. Instrumenting the restore branch to print the value shows [object Document] Document nodeName= #document typeof focus= undefined isConnected= true under 30.1.0, and shows the branch is never entered under 30.0.1 (relatedTarget was null there, so the guard skipped it).

Current behavior

restoreLastFocus calls .focus() on whatever relatedTarget was, and throws a TypeError during the unmount commit phase if that value is a non-element EventTarget. Because it throws inside a layout-effect cleanup, React reports it as an error in the <FocusTrap> component and the surrounding test or unmount fails.

Expected behavior

Restoring focus should be a no-op when the recorded node cannot be focused, e.g. testing for the method rather than for truthiness:

javascript
if (!disableRestoreFocus && typeof nodeToRestore.current?.focus === 'function') {

That keeps the existing behaviour for every element and degrades safely for anything else.

Context

I am not claiming jsdom is right to report the Document here — a real browser does not, and it may well be a jsdom deviation worth reporting separately. But relatedTarget's type genuinely admits a non-element EventTarget, so the guard is load-bearing regardless of which side is at fault, and hardening it is a one-line change that does not depend on settling that question.

Reproducing requires jsdom 30.1.0 rather than a browser, which is why there is no CodeSandbox — the triggering relatedTarget value does not occur in a browser. Happy to open a PR for the guard if you would take one.

I am working around it for now with a Document.prototype.focus stub in the test setup, which is the kind of workaround that should not have to exist.

Your environment

  • @mui/material 9.4.0
  • react / react-dom 19.3.0
  • jsdom 30.1.0 (30.0.1 unaffected)
  • vitest 5.0.1, @testing-library/react 16.3.3
  • Node 24.21.0

Search keywords: