-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Description
This affects various Microsoft products that use child windows, and is an accessibility bug for users because focus is lost when they interact with an app and the active element is moved in DOM by React.
React version: 18.3.1
Steps To Reproduce
- Clone the repro repo https://github.com/ling1726/react-child-window-focus-repro
- Run
npm install
- Run
npm run dev
- Open the demo app in
http://localhost:5173/
- Reorder the items with
ArrowUp
andArrowDown
- Open example in child window
- Reorder the items with
ArrowUp
andArrowDown
- Notice that focus is lost immediately after reordering
Link to code example: https://github.com/ling1726/react-child-window-focus-repro
The current behavior
The current getActiveElementDeep
always uses the global window
keyword. Note this snippet is from the React codebase
react/packages/react-dom-bindings/src/client/ReactInputSelection.js
Lines 59 to 71 in 4f60494
function getActiveElementDeep() { | |
let win = window; | |
let element = getActiveElement(); | |
while (element instanceof win.HTMLIFrameElement) { | |
if (isSameOriginFrame(element)) { | |
win = element.contentWindow; | |
} else { | |
return element; | |
} | |
element = getActiveElement(win.document); | |
} | |
return element; | |
} |
This fails focus restore when the active element is moved by React because it never gets the correct active element in a child window.
The expected behavior
When React moves an active element it should restore focus to the active element in a child window