BlueWallet/components/navigationStyle.tsx

125 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-02-24 12:27:17 +01:00
import { NativeStackNavigationOptions } from '@react-navigation/native-stack';
2020-12-25 17:09:53 +01:00
import React from 'react';
2024-02-24 12:27:17 +01:00
import { Image, Keyboard, StyleSheet, TouchableOpacity } from 'react-native';
import loc from '../loc';
2024-02-24 12:27:17 +01:00
import { Theme } from './themes';
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
},
});
2024-02-24 12:27:17 +01:00
type OptionsFormatter = (
options: NativeStackNavigationOptions,
deps: { theme: Theme; navigation: any; route: any },
) => NativeStackNavigationOptions;
2024-02-24 12:27:17 +01:00
export type NavigationOptionsGetter = (theme: Theme) => (deps: { navigation: any; route: any }) => NativeStackNavigationOptions;
const navigationStyle = (
{
closeButtonFunc,
2024-01-21 20:59:28 +01:00
headerBackVisible = true,
...opts
2024-02-24 12:27:17 +01:00
}: NativeStackNavigationOptions & {
closeButton?: boolean;
2024-05-10 03:08:59 +02:00
closeButtonFunc?: (deps: { navigation: any; route: any }) => React.ReactElement | void;
},
2024-02-27 04:04:51 +01:00
formatter?: OptionsFormatter,
): NavigationOptionsGetter => {
2022-01-26 18:50:41 +01:00
return theme =>
({ navigation, route }) => {
// Determine if the current screen is the first one in the stack using the updated method
const isFirstRouteInStack = navigation.getState().index === 0;
// Default closeButton to true if the current screen is the first one in the stack
const closeButton = opts.closeButton !== undefined ? opts.closeButton : isFirstRouteInStack;
2022-01-26 18:50:41 +01:00
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
if (!headerBackVisible) {
headerLeft = () => <></>;
opts.headerLeft = headerLeft;
}
2024-02-24 12:27:17 +01:00
let options: NativeStackNavigationOptions = {
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;
2024-02-24 12:27:17 +01:00
export const navigationStyleTx = (opts: NativeStackNavigationOptions, formatter: OptionsFormatter): NavigationOptionsGetter => {
2022-01-26 18:50:41 +01:00
return theme =>
({ navigation, route }) => {
2024-02-24 12:27:17 +01:00
let options: NativeStackNavigationOptions = {
2022-01-26 18:50:41 +01:00
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
};