Skip to content

Commit

Permalink
fix: automatically set top inset on Android in native stack
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 21, 2021
1 parent 3e67f64 commit 2cb44a5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/src/Screens/NativeStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default function NativeStackScreen({
}, [navigation]);

return (
<NativeStack.Navigator screenOptions={{ headerTopInsetEnabled: false }}>
<NativeStack.Navigator>
<NativeStack.Screen
name="Article"
component={ArticleScreen}
Expand Down
4 changes: 3 additions & 1 deletion packages/native-stack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"clean": "del lib"
},
"dependencies": {
"@react-navigation/elements": "^1.0.0-next.10",
"warn-once": "^0.1.0"
},
"devDependencies": {
Expand All @@ -58,7 +59,8 @@
"@react-navigation/native": "^6.0.0",
"react": "*",
"react-native": "*",
"react-native-screens": ">= 3.0.0"
"react-native-screens": ">= 3.0.0",
"react-native-safe-area-context": ">= 3.0.0"
},
"react-native-builder-bob": {
"source": "src",
Expand Down
25 changes: 17 additions & 8 deletions packages/native-stack/src/views/NativeStackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
useTheme,
Route,
} from '@react-navigation/native';
import { SafeAreaProviderCompat } from '@react-navigation/elements';
import {
Screen,
ScreenStack,
StackPresentationTypes,
} from 'react-native-screens';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import warnOnce from 'warn-once';
import HeaderConfig from './HeaderConfig';
import type {
Expand Down Expand Up @@ -114,16 +116,12 @@ type Props = {
descriptors: NativeStackDescriptorMap;
};

export default function NativeStackView({
state,
navigation,
descriptors,
}: Props): JSX.Element {
const { key, routes } = state;
function NativeStackViewInner({ state, navigation, descriptors }: Props) {
const insets = useSafeAreaInsets();

return (
<ScreenStack style={styles.container}>
{routes.map((route, index) => {
{state.routes.map((route, index) => {
const { options, render: renderScene } = descriptors[route.key];
const {
gestureEnabled,
Expand Down Expand Up @@ -191,14 +189,17 @@ export default function NativeStackView({
navigation.dispatch({
...StackActions.pop(),
source: route.key,
target: key,
target: state.key,
});
}}
>
<HeaderConfig
{...options}
route={route}
headerShown={isHeaderInPush}
headerTopInsetEnabled={
options.headerTopInsetEnabled ?? insets.top !== 0
}
/>
<MaybeNestedStack
options={options}
Expand All @@ -214,6 +215,14 @@ export default function NativeStackView({
);
}

export default function NativeStackView(props: Props) {
return (
<SafeAreaProviderCompat>
<NativeStackViewInner {...props} />
</SafeAreaProviderCompat>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down
3 changes: 2 additions & 1 deletion packages/native-stack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"references": [
{ "path": "../core" },
{ "path": "../routers" },
{ "path": "../native" }
{ "path": "../native" },
{ "path": "../elements" }
],
"compilerOptions": {
"outDir": "./lib/typescript"
Expand Down

0 comments on commit 2cb44a5

Please sign in to comment.