fix(Android, Stack v4): fix keyboard navigation focus for form sheet #3245
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.
Description
Fixes keyboard navigation focus handling for form sheets on Android.
Previously, it was possible to focus elements in the view displayed below the form sheet.
Closes #3188.
focus_before.mp4
focus_after.mp4
Context
In
ScreenStack, there is already logic to handle accessibility focus viaimportantForAccessibilityprop. I checked that the prop is changed correctly - there were no regressions here.I stumbled upon an issue in
react-nativerepo where one of the users explained that Android handles screen readers focus and keyboard navigation focus differently. In order to block keyboard navigation focus, you need to setfocusableanddescendantFocusabilityproperties - when implemented inScreenStack, this started working correctly (after clickingarrow-down, focus did not leave the formsheet).Another problem that appeared is that the button used to open the form sheet remained focused after form sheet had been already opened. I attempted to use
requestFocus()on the form sheet's screen but if there is a text input in the form sheet, it started working as if we setautoFocuson the input field.focus_withRequestFocus.mp4
This also impacted regular touch navigation. I decided to use
clearFocuson previous screen instead - this works as expected: first button in the screen is focused after opening the form sheet.One thing I noticed is that when you go back from a screen, nothing is focused (I checked with layout inspector) - this is not a regression but we should have a look at it in the future. Native app (settings) on API 36 keeps focus when screen is popped. I created an issue to investigate this: https://github.com/software-mansion/react-native-screens-labs/issues/474.
Changing focusability on Android
Starting from API 26,
focusablecan be set toNOT_FOCUSABLE/FOCUSABLE/FOCUSABLE_AUTO. Prior to API 26, this was a boolean prop - that's why there is some extra code to handle both cases (changeFocusabilityandchangeFocusabilityCompatfunctions inScreen.kt).Changes
Screenfor API <= 25 and API >= 26importantForAccessibilityfocusabletofalse/NOT_FOCUSABLEforDimmingView(this caused missing focus after popping apushscreen that was pushed over form sheet)onUpdateTest code and steps to reproduce
Run
TestFormSheetand use the keyboard to navigate.Checklist