From 371aad72ae23df40159184b4960376c28c6b869b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Tue, 23 Feb 2021 21:10:50 +0100 Subject: [PATCH] Split Appbar between ui and navigation logic This is necessary for split view because HomeScreen tabs need to provide their own Header, so they don't need the navigation logic --- src/RootNavigator.tsx | 4 +-- src/screens/NoteListScreen.tsx | 4 +-- src/screens/NotebookListScreen.tsx | 4 +-- src/widgets/Appbar.tsx | 40 ++++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/RootNavigator.tsx b/src/RootNavigator.tsx index e00cdde..930e28d 100644 --- a/src/RootNavigator.tsx +++ b/src/RootNavigator.tsx @@ -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(); @@ -55,7 +55,7 @@ export default React.memo(function RootNavigator() { <> , + header: (props) => , cardStyle: { maxHeight: "100%", }, diff --git a/src/screens/NoteListScreen.tsx b/src/screens/NoteListScreen.tsx index 153d826..272388b 100644 --- a/src/screens/NoteListScreen.tsx +++ b/src/screens/NoteListScreen.tsx @@ -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"; @@ -40,7 +40,7 @@ export default function NoteListScreen(props: PropsType) { } navigation.setOptions({ - header: (props) => , + header: (props) => , title: "Notes", headerRight: () => ( diff --git a/src/screens/NotebookListScreen.tsx b/src/screens/NotebookListScreen.tsx index 6dfcc75..741bb4c 100644 --- a/src/screens/NotebookListScreen.tsx +++ b/src/screens/NotebookListScreen.tsx @@ -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"; @@ -52,7 +52,7 @@ export default function NotebookListScreen(props: PropsType) { } navigation.setOptions({ - header: (props) => , + header: (props) => , title: notebook?.meta.name || "Notebooks", headerLeft: (notebook) ? () => setNotebook(undefined)} /> : undefined, headerRight: () => ( diff --git a/src/widgets/Appbar.tsx b/src/widgets/Appbar.tsx index b708d4d..be3ffb5 100644 --- a/src/widgets/Appbar.tsx +++ b/src/widgets/Appbar.tsx @@ -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 ( + + {left} + {(typeof title === "string") ? ( + + ) : title({ onLayout: () => null })} + {right} + + ); +} + +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; @@ -19,12 +41,10 @@ export default function Appbar(props: StackHeaderProps & { menuFallback: boolean const right = options.headerRight?.({}); return ( - - {left} - {(typeof title === "string") ? ( - - ) : title({ onLayout: () => null })} - {right} - + ); }