Skip to content

Commit 146dbd6

Browse files
authored
fix: mobile keyboard popping up when app returns from background (#14289)
1 parent 92ac54e commit 146dbd6

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

packages/mobile/src/screens/contest-screen/ContestScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { useDispatch } from 'react-redux'
3737
import { Button, Divider, Flex, Text } from '@audius/harmony-native'
3838
import { Screen, ScreenContent } from 'app/components/core'
3939
import { ProfilePicture } from 'app/components/core/ProfilePicture'
40+
import { make, track as trackEvent } from 'app/services/analytics'
4041
import {
4142
CollapsibleTabNavigator,
4243
collapsibleTabScreen

packages/mobile/src/screens/explore-screen/components/SearchExploreHeader.tsx

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React, {
77
} from 'react'
88

99
import { exploreMessages as messages } from '@audius/common/messages'
10-
import { useFocusEffect, useNavigation } from '@react-navigation/native'
10+
import { useNavigation } from '@react-navigation/native'
1111
import type { ScrollView } from 'react-native'
1212
import { Keyboard } from 'react-native'
1313
import { useSafeAreaInsets } from 'react-native-safe-area-context'
@@ -53,27 +53,19 @@ export const SearchExploreHeader = (props: SearchExploreHeaderProps) => {
5353
// eslint-disable-next-line react-hooks/exhaustive-deps
5454
}, [query])
5555

56-
useFocusEffect(
57-
useCallback(() => {
58-
if (params?.autoFocus === true && textInputRef.current) {
59-
textInputRef.current?.focus()
60-
}
61-
}, [params?.autoFocus])
62-
)
63-
56+
// Focus the search input when navigated here with autoFocus: true (e.g.,
57+
// user taps the search icon). Use a plain useEffect — NOT useFocusEffect —
58+
// so this fires only when the param transitions, not on every screen focus.
59+
// Without this, the keyboard re-appears whenever the app returns from
60+
// background while explore is the active tab. Consume the param by setting
61+
// it back to false so a subsequent search-icon tap can re-trigger focus.
6462
useEffect(() => {
65-
const keyboardDidHideListener = Keyboard.addListener(
66-
'keyboardDidHide',
67-
() => {
68-
if (params?.autoFocus === true) {
69-
// @ts-expect-error: setParams is not typed on the generic NavigationProp, but is available on StackNavigationProp
70-
navigation.setParams?.({ autoFocus: false })
71-
}
72-
}
73-
)
74-
75-
return () => keyboardDidHideListener?.remove()
76-
}, [navigation, params?.autoFocus])
63+
if (params?.autoFocus === true) {
64+
textInputRef.current?.focus()
65+
// @ts-expect-error: setParams is not typed on the generic NavigationProp, but is available on StackNavigationProp
66+
navigation.setParams?.({ autoFocus: false })
67+
}
68+
}, [params?.autoFocus, navigation])
7769

7870
const handleOpenLeftNavDrawer = useCallback(() => {
7971
if (isNowPlayingDrawerOpen) return
@@ -117,7 +109,6 @@ export const SearchExploreHeader = (props: SearchExploreHeaderProps) => {
117109
<TextInput
118110
ref={textInputRef}
119111
label='Search'
120-
autoFocus={params?.autoFocus}
121112
autoCorrect={false}
122113
placeholder={messages.searchPlaceholder}
123114
size={TextInputSize.SMALL}

packages/web/src/pages/contest-page/components/desktop/ContestPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { UserGeneratedText } from 'components/user-generated-text'
5353
import { VideoEmbed } from 'components/video-embed/VideoEmbed'
5454
import { useRequiresAccountCallback } from 'hooks/useRequiresAccount'
5555
import { useTrackCoverArt } from 'hooks/useTrackCoverArt'
56+
import { make, track as trackEvent } from 'services/analytics'
5657
import { useRemixPageParams } from 'pages/remixes-page/hooks'
5758
import { useUpdateSearchParams } from 'pages/search-page/hooks'
5859
import { make, track as trackEvent } from 'services/analytics'

0 commit comments

Comments
 (0)