Skip to content

fix(Android): fix formSheet sliding from top when it contains input with autofocus #2909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2025

Conversation

kligarski
Copy link
Contributor

@kligarski kligarski commented May 8, 2025

Description

On API 36, formSheet that contains input with autoFocus would sometimes slide from the top.

Even though react-native-screens used com.google.android.material:material:1.6.1, InsetsAnimationCallback (which is not present in 1.6.1) is called (it probably comes from com.google.android.material:material:1.12.0 dependency in react-native-edge-to-edge). This callback interferes with our formSheet entering/closing animations:

  1. Values used to calculate start and end position in InsetsAnimationCallback's onPrepare and onStart are usually incorrect. This has become a significant issue since API 36: for some reason, animation created in ScreenStackFragment's onCreateAnimator is sometimes incorrecly accounted for in calculating startTranslationY. The value becomes negative which means that the formSheet slides from the top.
  2. Both InsetsAnimationCallback and formSheet animations use setTranslationY and they are overwriting each other's changes. InsetsAnimationCallback's onProgress is called just after animation's setTranslationY so it takes precedence.

We decided to replace the window insets animation callback which is set in BottomSheetBehavior's onLayoutChild with our own implementation. Currently it is empty in order to bring back previous behavior.

FormSheet that contains input with autoFocus still does not behave as intented - only rarely it gets expanded with the keyboard. This seems to be a timing issue and needs more investigation. It might be necessary to implement logic in new window insets animation callback.

Fixes #2895.

Changes

  • change com.google.android.material:material version to 1.12.0
  • replace BottomSheetBehavior's InsetsAnimationCallback with empty implementation
  • add test screen Test2895

Test code and steps to reproduce

Open Test2895 screen in the example app and open the formSheet.

Checklist

  • Included code example that can be used to test this change
  • Ensured that CI passes

Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some notes that need to be taken into account. Great job overall.

@kligarski kligarski requested a review from kkafar May 9, 2025 11:23
Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested the runtime this time. Provided that you did - let's proceed.

@kligarski kligarski merged commit 6b1de35 into main May 9, 2025
5 of 6 checks passed
@kligarski kligarski deleted the @kligarski/form-sheet-autofocus-1 branch May 9, 2025 12:02
kligarski added a commit that referenced this pull request May 20, 2025
…ns input with autofocus (#2911)

## Description

Sometimes, formSheet that contains input with autoFocus would not
account for the keyboard and would remain behind it. This is because
`SheetDelegate` is set as a `OnApplyWindowInsetsListener` when fragment
is resumed - this happens after finishing formSheet entering animations
(animators impact fragment lifecycle). Sometimes, IME insets were
applied before this handler was set. Now, we can use
`WindowInsetsAnimationCallback` added in [this
PR](#2909).
Its `onPrepare` method is called before `onApplyWindowInsets` therefore
making it possible to set `OnApplyWindowInsetsListener` in time.

## Changes

- change `insetsObserverProxy`'s `listeners` to `HashSet` (we might try
to add the same listener multiple times)
- override `onPrepare` method in `WindowInsetsAnimationCallback`
- change visibility of `SheetDelegate` to `internal`

## Test code and steps to reproduce

Open `Test2895` screen in the example app, open & close the formSheet
(multiple times).

## Checklist

- [x] Included code example that can be used to test this change
- [x] Ensured that CI passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[4.10.0] formSheet slides from top, when it contains Input with autoFocus
2 participants