Skip to content

Commit

Permalink
Pass { flex: 1 } as default style to GestureHandlerRootView (#2757)
Browse files Browse the repository at this point in the history
## Description
Adding `{ flex: 1 }` style is [described in the installation
docs](https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation#js)
but I _always_ forget to add it when creating a new app that uses
gestures. I think others developers can relate.

This PR adds a `{ flex: 1 }` fallback style to `GestureHandlerRootView`
so users won't see a blank screen when they wrap their app with
`GestureHandlerRootView`.
 
## Test plan

Run an example app
  • Loading branch information
kacperkapusciak authored Feb 12, 2024
1 parent f58166c commit 49379f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
5 changes: 1 addition & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const Stack = createStackNavigator<RootStackParamList>();

export default function App() {
return (
<GestureHandlerRootView style={styles.root}>
<GestureHandlerRootView>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
Expand Down Expand Up @@ -220,9 +220,6 @@ function MainScreenItem({ name, onPressItem }: MainScreenItemProps) {
}

const styles = StyleSheet.create({
root: {
flex: 1,
},
sectionTitle: {
...Platform.select({
ios: {
Expand Down
18 changes: 13 additions & 5 deletions src/components/GestureHandlerRootView.android.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import * as React from 'react';
import { PropsWithChildren } from 'react';
import { ViewProps } from 'react-native';
import { ViewProps, StyleSheet } from 'react-native';
import { maybeInitializeFabric } from '../init';
import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext';
import GestureHandlerRootViewNativeComponent from '../specs/RNGestureHandlerRootViewNativeComponent';

export interface GestureHandlerRootViewProps
extends PropsWithChildren<ViewProps> {}

export default function GestureHandlerRootView(
props: GestureHandlerRootViewProps
) {
export default function GestureHandlerRootView({
style,
...rest
}: GestureHandlerRootViewProps) {
// try initialize fabric on the first render, at this point we can
// reliably check if fabric is enabled (the function contains a flag
// to make sure it's called only once)
maybeInitializeFabric();

return (
<GestureHandlerRootViewContext.Provider value>
<GestureHandlerRootViewNativeComponent {...props} />
<GestureHandlerRootViewNativeComponent
style={style ?? styles.container}
{...rest}
/>
</GestureHandlerRootViewContext.Provider>
);
}

const styles = StyleSheet.create({
container: { flex: 1 },
});
15 changes: 10 additions & 5 deletions src/components/GestureHandlerRootView.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import * as React from 'react';
import { PropsWithChildren } from 'react';
import { View, ViewProps } from 'react-native';
import { View, ViewProps, StyleSheet } from 'react-native';
import { maybeInitializeFabric } from '../init';
import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext';

export interface GestureHandlerRootViewProps
extends PropsWithChildren<ViewProps> {}

export default function GestureHandlerRootView(
props: GestureHandlerRootViewProps
) {
export default function GestureHandlerRootView({
style,
...rest
}: GestureHandlerRootViewProps) {
// try initialize fabric on the first render, at this point we can
// reliably check if fabric is enabled (the function contains a flag
// to make sure it's called only once)
maybeInitializeFabric();

return (
<GestureHandlerRootViewContext.Provider value>
<View {...props} />
<View style={style ?? styles.container} {...rest} />
</GestureHandlerRootViewContext.Provider>
);
}

const styles = StyleSheet.create({
container: { flex: 1 },
});
15 changes: 10 additions & 5 deletions src/components/GestureHandlerRootView.web.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import * as React from 'react';
import { PropsWithChildren } from 'react';
import { View, ViewProps } from 'react-native';
import { View, ViewProps, StyleSheet } from 'react-native';
import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext';

export interface GestureHandlerRootViewProps
extends PropsWithChildren<ViewProps> {}

export default function GestureHandlerRootView(
props: GestureHandlerRootViewProps
) {
export default function GestureHandlerRootView({
style,
...rest
}: GestureHandlerRootViewProps) {
return (
<GestureHandlerRootViewContext.Provider value>
<View {...props} />
<View style={style ?? styles.container} {...rest} />
</GestureHandlerRootViewContext.Provider>
);
}

const styles = StyleSheet.create({
container: { flex: 1 },
});

0 comments on commit 49379f3

Please sign in to comment.