Skip to content

Commit

Permalink
Use custom AppBar
Browse files Browse the repository at this point in the history
Allows us to always provide navigation actions, especially on Web.
  • Loading branch information
Kévin Commaille committed Jan 23, 2021
1 parent d6b2d09 commit 2e8bbbe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/RootNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

import * as React from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigation } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { DrawerNavigationProp } from "@react-navigation/drawer";
import { Appbar, useTheme, Snackbar } from "react-native-paper";
import { Snackbar } from "react-native-paper";

import { SyncManager } from "./sync/SyncManager";

Expand All @@ -33,20 +31,14 @@ import { useAppStateCb } from "./helpers";
import * as C from "./constants";
import { StoreState } from "./store";
import { RootStackParamList } from "./RootStackParamList";
import MenuButton from "./widgets/MenuButton";
import AppBar from "./widgets/AppBar";

const Stack = createStackNavigator<RootStackParamList>();

const MenuButton = React.memo(function MenuButton() {
const navigation = useNavigation() as DrawerNavigationProp<any>;
return (
<Appbar.Action icon="menu" accessibilityLabel="Main menu" onPress={() => navigation.openDrawer()} />
);
});

export default React.memo(function RootNavigator() {
const dispatch = useDispatch();
const etebase = useCredentials();
const theme = useTheme();

// Sync app when it goes to background
useAppStateCb((_foreground) => {
Expand All @@ -61,14 +53,7 @@ export default React.memo(function RootNavigator() {
<>
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: theme.colors.primary,
},
headerTintColor: "#000000",
headerBackTitleVisible: false,
headerBackTitleStyle: {
backgroundColor: "black",
},
header: (props) => <AppBar {...props} menuFallback />,
cardStyle: {
maxHeight: "100%",
},
Expand Down
31 changes: 31 additions & 0 deletions src/widgets/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from "react";
import { StackHeaderProps } from "@react-navigation/stack";
import { Appbar } from "react-native-paper";

import MenuButton from "./MenuButton";

export default function AppBar(props: StackHeaderProps & { menuFallback: boolean }) {
const { insets, menuFallback, navigation, previous, scene } = props;
const { options } = scene.descriptor;
const title = (options.headerTitle)
? options.headerTitle
: (options.title)
? options.title
: scene.route.name;
const left = (options.headerLeft)
? options.headerLeft({})
: (previous)
? <Appbar.BackAction onPress={navigation.goBack} />
: (menuFallback)
? <MenuButton />
: null;
const right = options.headerRight?.({});

return (
<Appbar.Header statusBarHeight={insets.top}>
{left}
<Appbar.Content title={title} />
{right}
</Appbar.Header>
);
}
13 changes: 13 additions & 0 deletions src/widgets/MenuButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from "react";
import { useNavigation } from "@react-navigation/native";
import { DrawerNavigationProp } from "@react-navigation/drawer";
import { Appbar } from "react-native-paper";

const MenuButton = React.memo(function MenuButton() {
const navigation = useNavigation() as DrawerNavigationProp<any>;
return (
<Appbar.Action icon="menu" accessibilityLabel="Main menu" onPress={() => navigation.openDrawer()} />
);
});

export default MenuButton;

0 comments on commit 2e8bbbe

Please sign in to comment.