Repository navigation
Conversation
ykdhan
force-pushed
the
fix/modal-key-events
branch
from
December 3, 2026 12:39
31384e3 to
bc95fca
Compare
With enableKeyEvents on, React Native Android sends onKeyDown/onKeyUp from the React root's own key dispatch (ReactRootView, ReactSurfaceView). A Modal is shown in its own dialog window whose root, ReactModalHostView.DialogRootViewGroup, had no key dispatch and no focus tracking, so a focused View inside a Modal never received key events. Give DialogRootViewGroup the same JSKeyDispatcher handling that ReactSurfaceView has: track the focused child, and send keydown/keyup to JS from dispatchKeyEvent before children handle the event. The dialog's OnKeyListener consumes some key ups before they reach the dialog's views: BACK/ESCAPE (which close the Modal) and keys the activity handles (dev menu, double-tap R). Forward those key ups to JS too, so JS gets a keyup for every keydown it received. Fixes react#59601 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: YK <21373658+ykdhan@users.noreply.github.com>
ykdhan
force-pushed
the
fix/modal-key-events
branch
from
December 3, 2026 12:42
bc95fca to
e0968f4
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fixes #59601.
With
ReactNativeFeatureFlags.enableKeyEvents()on, React Native Android sendsonKeyDown/onKeyUpfrom the React root's own key dispatch:ReactRootView.dispatchKeyEventandReactSurfaceView, each with aJSKeyDispatcherthat tracks the focused child. A<Modal>is shown in its own dialog window, and that window's root,ReactModalHostView.DialogRootViewGroup, had no key dispatch and no focus tracking. As a result, a View inside a Modal never received key events, even while it held focus.This change gives
DialogRootViewGroupthe same handlingReactSurfaceViewhas:JSKeyDispatcherwhenenableKeyEventsis on.requestChildFocus/onFocusChanged.dispatchKeyEventbefore children handle the event, the same capture-phase orderReactRootViewuses.The dialog's
OnKeyListenerruns before the window's views see an event, and it consumes some key ups:onRequestClose.onKeyUphandles: the dev menu key, and double-tap R in debug builds.Those key ups never reach
DialogRootViewGroup, so the listener now forwards them to JS itself. This way JS gets a keyup for each keydown it received. One exception predates this change: on API 36,Dialog.onKeyDownalready dismisses a cancelable dialog on ESCAPE keydown, before the listener runs. In that case the ESCAPE keyup never arrives (see Test Plan).When
enableKeyEventsis off,jSKeyDispatcherstaysnulland behavior is unchanged.Changelog:
[ANDROID] [FIXED] - Fix views inside
Modalnot receivingonKeyDown/onKeyUpwhen key events are enabledTest Plan:
Unit tests. A new Robolectric test,
DialogRootViewGroupKeyEventsTest, covers these cases:dispatchJSKeyEvent(the path the dialog's key listener uses) reaches the focused view;enableKeyEventsis off, or when there is no event dispatcher.BUILD SUCCESSFUL; the JUnit report showstests="6" skipped="0" failures="0" errors="0".Neighboring tests still pass:
BUILD SUCCESSFUL:DialogRootViewGroupKeyEventsTest6/6,JSPointerDispatcherTest1/1. I did not run the fulltestDebugUnitTestsuite.Public API snapshot.
DialogRootViewGroupgains two public overrides,dispatchKeyEventandrequestChildFocus.yarn android-api-buildregeneratedReactAndroid.apiwith no diff against the committed file.Formatting.
yarn format-check-kotlinpassed.Lint.
yarn lintpassed.JS API snapshot.
yarn build-types --validatepassed:API snapshot is up to date.Manual check. I used RNTester built from source on an Android emulator (API 36, arm64-v8a), not the reproducer from #59601, because it pins the published 0.88.1. In a local-only change that is not part of this PR, I turned
enableKeyEventson inRNTesterApplicationand replaced the Playground with the reproducer's layout: a focusable View withonKeyDownoutside a<Modal>, the same View inside it, and an on-screen log of keydown, keyup andonRequestClose. I sent keys withadb shell input keyevent.KEYCODE_TABthenKEYCODE_A: the View showslast key: aand logs keydown and keyup fora, before and after this change.KEYCODE_TABthenKEYCODE_A: with this change, the View inside the Modal showslast key: aand logs keydown and keyup. Before this change it received nothing.KEYCODE_BACK: with this change, the focused View gets keydown, thenonRequestCloseis called, then the View gets keyup. Before this changeonRequestClosewas called and the View got no key events.KEYCODE_ESCAPEcould not be verified on this emulator. On API 36,Dialog.onKeyDowncancels a cancelable dialog on ESCAPE keydown, before this listener (which only handles key ups) sees the event. The dialog is dismissed natively:onRequestCloseis not called and the ESCAPE keyup never reaches the dialog. That happens the same way before and after this change. With this change, JS additionally receives the ESCAPE keydown. That is out of scope here, and I can file it separately.Before (no key events inside the Modal)
https://github.com/user-attachments/assets/21c2e96c-b34f-4dcc-a46e-a32d36b4d7e2
After
https://github.com/user-attachments/assets/a965a8c6-e2ac-4057-ab69-c63fd86cf857