Skip to content

Commit 0e79784

Browse files
authored
[DevTools] Use documentElement to override cmd+F (facebook#34734)
We override Cmd+F to jump to our search input instead of searching through the HTML. This is ofc critical since our view virtualized. However, Chrome DevTools installs its own listener on the document as well (in the bubble phase) so if we prevent it at the document level it's too late and it ends up stealing the focus instead. If we instead listen at the documentElement it works as intended.
1 parent a2329c1 commit 0e79784

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/react-devtools-shared/src/devtools/views/SearchInput.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ export default function SearchInput({
6464
const handleKeyDown = (event: KeyboardEvent) => {
6565
const {key, metaKey} = event;
6666
if (key === 'f' && metaKey) {
67-
if (inputRef.current !== null) {
68-
inputRef.current.focus();
67+
const inputElement = inputRef.current;
68+
if (inputElement !== null) {
69+
inputElement.focus();
6970
event.preventDefault();
7071
event.stopPropagation();
7172
}
@@ -75,10 +76,14 @@ export default function SearchInput({
7576
// It's important to listen to the ownerDocument to support the browser extension.
7677
// Here we use portals to render individual tabs (e.g. Profiler),
7778
// and the root document might belong to a different window.
78-
const ownerDocument = inputRef.current.ownerDocument;
79-
ownerDocument.addEventListener('keydown', handleKeyDown);
79+
const ownerDocumentElement = inputRef.current.ownerDocument.documentElement;
80+
if (ownerDocumentElement === null) {
81+
return;
82+
}
83+
ownerDocumentElement.addEventListener('keydown', handleKeyDown);
8084

81-
return () => ownerDocument.removeEventListener('keydown', handleKeyDown);
85+
return () =>
86+
ownerDocumentElement.removeEventListener('keydown', handleKeyDown);
8287
}, []);
8388

8489
return (

0 commit comments

Comments
 (0)