Skip to content
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

fix: ensure screens that are bottom sheets scroll correctly #5062

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/BottomSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import GorhomBottomSheet from '@gorhom/bottom-sheet'
import React, { useRef } from 'react'
import { ScrollView, StyleSheet, Text, TextStyle, View } from 'react-native'
import { StyleSheet, Text, TextStyle, View } from 'react-native'
import { ScrollView } from 'react-native-gesture-handler'
import BottomSheetBase from 'src/components/BottomSheetBase'
import BottomSheetScrollView from 'src/components/BottomSheetScrollView'
import fontStyles from 'src/styles/fonts'
Expand Down
31 changes: 27 additions & 4 deletions src/components/BottomSheetScrollView.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import { BottomSheetScrollView as GorhomBottomSheetScrollView } from '@gorhom/bottom-sheet'
import React, { useState } from 'react'
import { LayoutChangeEvent, ScrollView, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'
import { LayoutChangeEvent, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'
import { ScrollView } from 'react-native-gesture-handler'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { Spacing } from 'src/styles/styles'
import variables from 'src/styles/variables'

interface Props {
containerStyle?: StyleProp<ViewStyle>
testId?: string
forwardedRef?: React.RefObject<ScrollView>
isScreen?: boolean
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a small comment to explain when/why this is needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes updated

children: React.ReactNode
}

function BottomSheetScrollView({ forwardedRef, containerStyle, testId, children }: Props) {
function BottomSheetScrollView({
forwardedRef,
containerStyle,
testId,
isScreen,
children,
}: Props) {
const [containerHeight, setContainerHeight] = useState(0)
const [contentHeight, setContentHeight] = useState(0)

const insets = useSafeAreaInsets()
const scrollEnabled = contentHeight > containerHeight

// Note: scrolling views inside bottom sheet screens should use the relevant
// components from react-native-gesture-handler instead of directly from
// react-native, otherwise they do not scroll correctly. This isScreen prop
// should be set to true if the bottom sheet is registered as screen in the
// Navigator. It is still handy for screens and components to share this
// component for the styling and layout logic.
// https://github.com/osdnk/react-native-reanimated-bottom-sheet/issues/264#issuecomment-674757545
const ScrollViewComponent = isScreen ? ScrollView : GorhomBottomSheetScrollView
Copy link
Member

Choose a reason for hiding this comment

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

What happens if we use the RNGH ScrollView everywhere?
Does it break any gesture? Maybe the overscroll to dismiss?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes it is the overscroll to dismiss that is not available with ScrollView. i updated the comment to be more clear about this. it's annoying that this is an issue, but i think the overscroll / bottom sheet gestures are probably the thing preventing the BottomSheetScrollView to work correctly from the navigator

// use max height simulate max 90% snap point for screens. when bottom sheets
// take up the whole screen, it is no longer obvious that they are a bottom
// sheet / how to navigate away
const maxHeight = isScreen ? variables.height * 0.9 : undefined
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

annoyingly i couldn't find a better way to set the snappoints to be dynamic + set an upper bound on the height


return (
<GorhomBottomSheetScrollView
<ScrollViewComponent
ref={forwardedRef}
scrollEnabled={scrollEnabled}
style={{ maxHeight }}
onLayout={(event: LayoutChangeEvent) => {
setContainerHeight(event.nativeEvent.layout.height)
}}
Expand All @@ -40,7 +63,7 @@ function BottomSheetScrollView({ forwardedRef, containerStyle, testId, children
>
{children}
</View>
</GorhomBottomSheetScrollView>
</ScrollViewComponent>
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/dappkit/DappKitAccountScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const DappKitAccountScreen = ({ route }: Props) => {
}

return (
<BottomSheetScrollView>
<BottomSheetScrollView isScreen>
<RequestContent
type="confirm"
onAccept={handleAllow}
Expand Down
2 changes: 1 addition & 1 deletion src/dappkit/DappKitSignTxScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const DappKitSignTxScreen = ({ route }: Props) => {
}

return (
<BottomSheetScrollView>
<BottomSheetScrollView isScreen>
<RequestContent
type="confirm"
onAccept={handleAllow}
Expand Down
2 changes: 1 addition & 1 deletion src/dapps/DappShortcutTransactionRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function DappShortcutTransactionRequest({ route: { params } }: Props) {
}

return (
<BottomSheetScrollView>
<BottomSheetScrollView isScreen>
{pendingAcceptShortcut?.transactions?.length ? (
<RequestContent
type="confirm"
Expand Down
2 changes: 1 addition & 1 deletion src/fiatExchanges/FiatExchangeCurrencyBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function FiatExchangeCurrencyBottomSheet({ route }: Props) {
}

return (
<BottomSheetScrollView containerStyle={{ padding: undefined }}>
<BottomSheetScrollView isScreen containerStyle={{ padding: undefined }}>
{/* padding undefined to prevent android ripple bug */}
<Text style={styles.selectDigitalCurrency}>{t('sendEnterAmountScreen.selectToken')}</Text>
{tokenList.length &&
Expand Down
9 changes: 5 additions & 4 deletions src/navigator/Navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ const mainScreenNavOptions = () => ({
})

function nativeBottomSheets(BottomSheet: typeof RootStack) {
// Note: scrolling views inside bottom sheet screens should use the relevant
// components from react-native-gesture-handler instead of directly from
// react-native
// https://github.com/osdnk/react-native-reanimated-bottom-sheet/issues/264#issuecomment-674757545

return (
<>
<BottomSheet.Screen name={Screens.WalletConnectRequest} component={WalletConnectRequest} />
Expand Down Expand Up @@ -698,10 +703,6 @@ function RootStackScreen() {
[]
)

// Note: scrolling views inside bottom sheet screens should use the relevant
// components from react-native-gesture-handler instead of directly from
// react-native
// https://github.com/osdnk/react-native-reanimated-bottom-sheet/issues/264#issuecomment-674757545
return (
<RootStack.Navigator
screenOptions={{
Expand Down
1 change: 1 addition & 0 deletions src/walletConnect/screens/WalletConnectRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function WalletConnectRequest({ route: { params } }: Props) {

return (
<BottomSheetScrollView
isScreen
containerStyle={
params.type === WalletConnectRequestType.Loading ||
params.type === WalletConnectRequestType.TimeOut
Expand Down
Loading