Skip to content

Commit 2c0ca2d

Browse files
authored
fix(android): keyboard and focus handling inside RN Modal (#387)
- Use targetView.rootView.findFocus() instead of activity.currentFocus in keyboard observer - Use delegate.findRootContainerView().findFocus() for saveFocusedView - Add findRootContainerView to TrueSheetViewControllerDelegate interface
1 parent eb7f975 commit 2c0ca2d

3 files changed

Lines changed: 6 additions & 4 deletions

File tree

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ class TrueSheetView(private val reactContext: ThemedReactContext) :
489489
* of whichever window this view is in - whether that's the activity's window or a
490490
* Modal's dialog window.
491491
*/
492-
private fun findRootContainerView(): ViewGroup? {
492+
override fun findRootContainerView(): ViewGroup? {
493493
var current: android.view.ViewParent? = parent
494494

495495
while (current != null) {

android/src/main/java/com/lodev09/truesheet/TrueSheetViewController.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ data class DetentInfo(val index: Int, val position: Float)
4646

4747
interface TrueSheetViewControllerDelegate {
4848
val eventDispatcher: EventDispatcher?
49+
fun findRootContainerView(): ViewGroup?
4950
fun viewControllerWillPresent(index: Int, position: Float, detent: Float)
5051
fun viewControllerDidPresent(index: Int, position: Float, detent: Float)
5152
fun viewControllerWillDismiss()
@@ -892,7 +893,8 @@ class TrueSheetViewController(private val reactContext: ThemedReactContext) :
892893
}
893894

894895
fun saveFocusedView() {
895-
focusedViewBeforeBlur = reactContext.currentActivity?.currentFocus
896+
focusedViewBeforeBlur = delegate?.findRootContainerView()?.findFocus()
897+
?: reactContext.currentActivity?.currentFocus
896898
}
897899

898900
fun restoreFocusedView() {

android/src/main/java/com/lodev09/truesheet/core/TrueSheetKeyboardObserver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TrueSheetKeyboardObserver(private val targetView: View, private val reactC
3535
private set
3636

3737
fun isFocusedViewWithinSheet(sheetView: View): Boolean {
38-
val focusedView = reactContext.currentActivity?.currentFocus ?: return false
38+
val focusedView = targetView.rootView?.findFocus() ?: return false
3939
var current: View? = focusedView
4040
while (current != null && current !== targetView) {
4141
if (current === sheetView) return true
@@ -131,7 +131,7 @@ class TrueSheetKeyboardObserver(private val targetView: View, private val reactC
131131
// Ensure we don't add duplicate listeners
132132
if (globalLayoutListener != null) return
133133

134-
val rootView = reactContext.currentActivity?.window?.decorView?.rootView ?: return
134+
val rootView = targetView.rootView ?: return
135135
activityRootView = rootView
136136

137137
globalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {

0 commit comments

Comments
 (0)