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

Split Appbar between ui and navigation logic #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/RootNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import * as C from "./constants";
import { StoreState } from "./store";
import { RootStackParamList } from "./RootStackParamList";
import MenuButton from "./widgets/MenuButton";
import Appbar from "./widgets/Appbar";
import { NavigationAppbar } from "./widgets/Appbar";

const Stack = createStackNavigator<RootStackParamList>();

Expand All @@ -55,7 +55,7 @@ export default React.memo(function RootNavigator() {
<>
<Stack.Navigator
screenOptions={{
header: (props) => <Appbar {...props} menuFallback />,
header: (props) => <NavigationAppbar {...props} menuFallback />,
cardStyle: {
maxHeight: "100%",
},
Expand Down
4 changes: 2 additions & 2 deletions src/screens/NoteListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { performSync, setSettings } from "../store/actions";
import { useCredentials } from "../credentials";

import NoteList from "../components/NoteList";
import Appbar from "../widgets/Appbar";
import { NavigationAppbar } from "../widgets/Appbar";
import Menu from "../widgets/Menu";
import MenuItem from "../widgets/MenuItem";
import AppbarAction from "../widgets/AppbarAction";
Expand All @@ -40,7 +40,7 @@ export default function NoteListScreen(props: PropsType) {
}

navigation.setOptions({
header: (props) => <Appbar {...props} menuFallback />,
header: (props) => <NavigationAppbar {...props} menuFallback />,
title: "Notes",
headerRight: () => (
<RightAction />
Expand Down
4 changes: 2 additions & 2 deletions src/screens/NotebookListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { performSync } from "../store/actions";
import { useCredentials } from "../credentials";

import NoteList from "../components/NoteList";
import Appbar from "../widgets/Appbar";
import { NavigationAppbar } from "../widgets/Appbar";
import AppbarAction from "../widgets/AppbarAction";
import Menu from "../widgets/Menu";
import MenuItem from "../widgets/MenuItem";
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function NotebookListScreen(props: PropsType) {
}

navigation.setOptions({
header: (props) => <Appbar {...props} menuFallback />,
header: (props) => <NavigationAppbar {...props} menuFallback />,
title: notebook?.meta.name || "Notebooks",
headerLeft: (notebook) ? () => <PaperAppbar.BackAction onPress={() => setNotebook(undefined)} /> : undefined,
headerRight: () => (
Expand Down
40 changes: 30 additions & 10 deletions src/widgets/Appbar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import * as React from "react";
import { StackHeaderProps } from "@react-navigation/stack";
import { StackHeaderProps, StackHeaderTitleProps } from "@react-navigation/stack";
import { Appbar as PaperAppbar } from "react-native-paper";
import { useSafeAreaInsets } from "react-native-safe-area-context";

import MenuButton from "./MenuButton";

export default function Appbar(props: StackHeaderProps & { menuFallback: boolean }) {
const { insets, menuFallback, navigation, previous, scene } = props;
type PropsType = {
title: string | ((props: StackHeaderTitleProps) => React.ReactNode);
left?: React.ReactNode;
right?: React.ReactNode;
};

export default function Appbar(props: PropsType) {
const { title, left, right } = props;
const insets = useSafeAreaInsets();

return (
<PaperAppbar.Header statusBarHeight={insets.top}>
{left}
{(typeof title === "string") ? (
<PaperAppbar.Content title={title} />
) : title({ onLayout: () => null })}
{right}
</PaperAppbar.Header>
);
}

export function NavigationAppbar(props: StackHeaderProps & { menuFallback: boolean }) {
const { menuFallback, navigation, previous, scene } = props;
const { options } = scene.descriptor;
const title = options.headerTitle ?? options.title ?? scene.route.name;
let left: React.ReactNode = null;
Expand All @@ -19,12 +41,10 @@ export default function Appbar(props: StackHeaderProps & { menuFallback: boolean
const right = options.headerRight?.({});

return (
<PaperAppbar.Header statusBarHeight={insets.top}>
{left}
{(typeof title === "string") ? (
<PaperAppbar.Content title={title} />
) : title({ onLayout: () => null })}
{right}
</PaperAppbar.Header>
<Appbar
title={title}
left={left}
right={right}
/>
);
}