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: Web rendering issues and welcome screen layout issue #2809

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
31 changes: 22 additions & 9 deletions boilerplate/app/components/Screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useScrollToTop } from "@react-navigation/native"
import { StatusBar, StatusBarProps, StatusBarStyle } from "expo-status-bar"
import { ReactNode, useRef, useState } from "react"
import {
KeyboardAvoidingView,
KeyboardAvoidingViewProps,
LayoutChangeEvent,
Platform,
Expand All @@ -14,7 +13,7 @@ import {
} from "react-native"
import { $styles } from "../theme"
import { ExtendedEdge, useSafeAreaInsetsStyle } from "../utils/useSafeAreaInsetsStyle"
import { KeyboardAwareScrollView } from "react-native-keyboard-controller"
import { KeyboardAwareScrollView, KeyboardAvoidingView } from "react-native-keyboard-controller"
import { useAppTheme } from "@/utils/useAppTheme"

export const DEFAULT_BOTTOM_OFFSET = 50
Expand Down Expand Up @@ -253,6 +252,25 @@ export function Screen(props: ScreenProps) {

const $containerInsets = useSafeAreaInsetsStyle(safeAreaEdges)

{
/**
* KeyboardAvoidingView crashes in web,
* therefore we want to use ScrollView just for web
*/
}
const ContentWrapperView =
Platform.OS === "web"
? ScrollView
: (props: KeyboardAvoidingViewProps) => (
<KeyboardAvoidingView
behavior={isIos ? "padding" : "height"}
keyboardVerticalOffset={keyboardOffset}
{...KeyboardAvoidingViewProps}
style={[$styles.flex1, KeyboardAvoidingViewProps?.style]}
{...props}
/>
)

return (
<View
style={[
Expand All @@ -266,18 +284,13 @@ export function Screen(props: ScreenProps) {
{...StatusBarProps}
/>

<KeyboardAvoidingView
behavior={isIos ? "padding" : "height"}
keyboardVerticalOffset={keyboardOffset}
{...KeyboardAvoidingViewProps}
style={[$styles.flex1, KeyboardAvoidingViewProps?.style]}
>
<ContentWrapperView>
{isNonScrolling(props.preset) ? (
<ScreenWithoutScrolling {...props} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Also meant to ask: why does web only get ScreeenWithoutScrolling? Shouldn't it have both options?

I think that's why this appeared to fix things, it removes KeyboardAwareScrollView from the document.

) : (
<ScreenWithScrolling {...props} />
)}
</KeyboardAvoidingView>
</ContentWrapperView>
</View>
)
}
Expand Down
11 changes: 8 additions & 3 deletions boilerplate/app/screens/WelcomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { isRTL } from "../i18n"
import { useStores } from "../models" // @demo remove-current-line
import { AppStackScreenProps } from "../navigators"
import type { ThemedStyle } from "@/theme"
import { spacing, type ThemedStyle } from "@/theme"
import { useHeader } from "../utils/useHeader" // @demo remove-current-line
import { useSafeAreaInsetsStyle } from "../utils/useSafeAreaInsetsStyle"
import { useAppTheme } from "@/utils/useAppTheme"
Expand Down Expand Up @@ -57,7 +57,7 @@ export const WelcomeScreen: FC<WelcomeScreenProps> = observer(
tx="welcomeScreen:readyForLaunch"
preset="heading"
/>
<Text tx="welcomeScreen:exciting" preset="subheading" />
<Text tx="welcomeScreen:exciting" preset="subheading" style={$subheading} />
Copy link
Contributor

Choose a reason for hiding this comment

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

These changes don't seem to render right for me on web:
Screenshot 2024-10-18 at 3 51 56 PM

<Image
style={$welcomeFace}
source={welcomeFace}
Expand Down Expand Up @@ -87,8 +87,9 @@ const $topContainer: ThemedStyle<ViewStyle> = ({ spacing }) => ({
flexShrink: 1,
flexGrow: 1,
flexBasis: "57%",
justifyContent: "center",
justifyContent: "space-around",
paddingHorizontal: spacing.lg,
paddingBottom: spacing.sm,
})

const $bottomContainer: ThemedStyle<ViewStyle> = ({ colors, spacing }) => ({
Expand All @@ -108,6 +109,10 @@ const $welcomeLogo: ThemedStyle<ImageStyle> = ({ spacing }) => ({
marginBottom: spacing.xxl,
})

const $subheading: TextStyle = {
paddingRight: spacing.xxxl,
}

const $welcomeFace: ImageStyle = {
height: 169,
width: 269,
Expand Down
14 changes: 10 additions & 4 deletions boilerplate/app/utils/useHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useLayoutEffect } from "react"
import { useEffect, useLayoutEffect } from "react"
import { useNavigation } from "@react-navigation/native"
import { Header, HeaderProps } from "../components"
import { Platform } from "react-native"

/**
* A hook that can be used to easily set the Header of a react-navigation screen from within the screen's component.
Expand All @@ -13,10 +14,15 @@ export function useHeader(
deps: Parameters<typeof useLayoutEffect>[1] = [],
) {
const navigation = useNavigation()
/**
* We need to have multiple implementations of this hook for web and mobile.
* Web needs to use useEffect to avoid a rendering loop.
* In mobile and also to avoid a visible header jump when navigating between screens, we use
* `useLayoutEffect`, which will apply the settings before the screen renders.
*/
const useAppropriateEffect = Platform.OS === "web" ? useEffect : useLayoutEffect
fpena marked this conversation as resolved.
Show resolved Hide resolved

// To avoid a visible header jump when navigating between screens, we use
// `useLayoutEffect`, which will apply the settings before the screen renders.
useLayoutEffect(() => {
useAppropriateEffect(() => {
navigation.setOptions({
headerShown: true,
header: () => <Header {...headerProps} />,
Expand Down