BlueWallet/components/navigationStyle.tsx

150 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-12-25 17:09:53 +01:00
import React from 'react';
import { Image, Keyboard, TouchableOpacity, StyleSheet } from 'react-native';
import { Theme } from './themes';
import loc from '../loc';
2020-12-25 17:09:53 +01:00
const styles = StyleSheet.create({
button: {
minWidth: 40,
height: 40,
justifyContent: 'center',
2021-09-13 19:43:26 +02:00
alignItems: 'center',
2020-12-25 17:09:53 +01:00
},
});
type NavigationOptions = {
headerStyle?: {
borderBottomWidth: number;
elevation: number;
shadowOpacity?: number;
shadowOffset: { height?: number; width?: number };
};
headerTitleStyle?: {
fontWeight: string;
color: string;
};
2023-10-23 20:50:17 +02:00
headerLargeTitle?: boolean;
2023-12-14 19:40:43 +01:00
headerBackVisible?: boolean;
2023-10-23 03:59:16 +02:00
gestureEnabled?: boolean;
swipeEnabled?: boolean;
headerTransparent?: boolean;
2023-10-23 03:59:16 +02:00
headerHideBackButton?: boolean;
headerLeft?: (() => React.ReactElement) | null;
headerRight?: (() => React.ReactElement) | null;
headerBackTitleVisible?: false;
headerBackButtonMenuEnabled?: boolean;
headerShadowVisible?: boolean;
headerTintColor?: string;
title?: string;
};
type OptionsFormatter = (options: NavigationOptions, deps: { theme: Theme; navigation: any; route: any }) => NavigationOptions;
export type NavigationOptionsGetter = (theme: Theme) => (deps: { navigation: any; route: any }) => NavigationOptions;
const navigationStyle = (
{
closeButton = false,
closeButtonFunc,
headerBackVisible,
...opts
}: NavigationOptions & {
closeButton?: boolean;
2023-04-02 02:16:00 +02:00
closeButtonFunc?: (deps: { navigation: any; route: any }) => React.ReactElement;
},
formatter: OptionsFormatter,
): NavigationOptionsGetter => {
2022-01-26 18:50:41 +01:00
return theme =>
({ navigation, route }) => {
let headerRight;
let headerLeft;
2022-01-26 18:50:41 +01:00
if (closeButton) {
const handleClose = closeButtonFunc
? () => closeButtonFunc({ navigation, route })
: () => {
Keyboard.dismiss();
navigation.goBack(null);
};
headerRight = () => (
2023-04-02 02:16:00 +02:00
<TouchableOpacity
accessibilityRole="button"
accessibilityLabel={loc._.close}
style={styles.button}
onPress={handleClose}
testID="NavigationCloseButton"
>
2022-01-26 18:50:41 +01:00
<Image source={theme.closeImage} />
</TouchableOpacity>
);
}
2020-12-25 17:09:53 +01:00
// Workaround for https://github.com/BlueWallet/BlueWallet/issues/6030
if (!headerBackVisible) {
headerLeft = () => <></>;
opts.headerLeft = headerLeft;
}
//
2022-01-26 18:50:41 +01:00
let options: NavigationOptions = {
headerShadowVisible: false,
2022-01-26 18:50:41 +01:00
headerTitleStyle: {
fontWeight: '600',
color: theme.colors.foregroundColor,
},
2022-10-31 13:25:26 +01:00
headerRight,
2022-01-26 18:50:41 +01:00
headerBackTitleVisible: false,
headerTintColor: theme.colors.foregroundColor,
...opts,
};
2020-12-25 17:09:53 +01:00
2022-01-26 18:50:41 +01:00
if (formatter) {
options = formatter(options, { theme, navigation, route });
}
2020-12-25 17:09:53 +01:00
2022-01-26 18:50:41 +01:00
return options;
};
2020-12-25 17:09:53 +01:00
};
export default navigationStyle;
export const navigationStyleTx = (opts: NavigationOptions, formatter: OptionsFormatter): NavigationOptionsGetter => {
2022-01-26 18:50:41 +01:00
return theme =>
({ navigation, route }) => {
let options: NavigationOptions = {
headerStyle: {
borderBottomWidth: 0,
elevation: 0,
shadowOffset: { height: 0, width: 0 },
},
headerTitleStyle: {
fontWeight: '600',
color: theme.colors.foregroundColor,
},
// headerBackTitle: null,
headerBackTitleVisible: false,
headerTintColor: theme.colors.foregroundColor,
headerLeft: () => (
2023-04-02 02:16:00 +02:00
<TouchableOpacity
accessibilityRole="button"
accessibilityLabel={loc._.close}
style={styles.button}
2022-01-26 18:50:41 +01:00
onPress={() => {
Keyboard.dismiss();
navigation.goBack(null);
}}
>
<Image source={theme.closeImage} />
</TouchableOpacity>
),
...opts,
};
2020-12-25 17:09:53 +01:00
2022-01-26 18:50:41 +01:00
if (formatter) {
options = formatter(options, { theme, navigation, route });
}
2020-12-25 17:09:53 +01:00
2022-01-26 18:50:41 +01:00
return options;
};
2020-12-25 17:09:53 +01:00
};