BlueWallet/components/navigationStyle.tsx

135 lines
3.7 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';
2024-05-20 11:54:13 +02:00
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
},
});
enum CloseButtonPosition {
None = 'None',
Left = 'Left',
Right = 'Right',
}
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;
2024-06-07 19:20:27 +02:00
const getCloseButtonPosition = (
closeButtonPosition: CloseButtonPosition | undefined,
isFirstRouteInStack: boolean,
isModal: boolean,
): CloseButtonPosition => {
if (closeButtonPosition !== undefined) {
return closeButtonPosition;
}
if (isFirstRouteInStack && isModal) {
return CloseButtonPosition.Right;
}
return CloseButtonPosition.None;
};
const getHandleCloseAction = (
onCloseButtonPressed: ((args: { navigation: any; route: any }) => void) | undefined,
navigation: any,
route: any,
) => {
if (onCloseButtonPressed) {
return () => onCloseButtonPressed({ navigation, route });
}
return () => {
Keyboard.dismiss();
navigation.goBack(null);
};
};
const navigationStyle = (
{
closeButtonPosition,
onCloseButtonPressed,
2024-01-21 20:59:28 +01:00
headerBackVisible = true,
...opts
2024-02-24 12:27:17 +01:00
}: NativeStackNavigationOptions & {
closeButtonPosition?: CloseButtonPosition;
onCloseButtonPressed?: (deps: { navigation: any; route: any }) => void;
},
2024-02-27 04:04:51 +01:00
formatter?: OptionsFormatter,
): NavigationOptionsGetter => {
2022-01-26 18:50:41 +01:00
return theme =>
({ navigation, route }) => {
const isFirstRouteInStack = navigation.getState().index === 0;
const isModal = route.params?.presentation !== 'card';
2024-06-07 19:20:27 +02:00
const closeButton = getCloseButtonPosition(closeButtonPosition, isFirstRouteInStack, isModal);
const handleClose = getHandleCloseAction(onCloseButtonPressed, navigation, route);
2022-01-26 18:50:41 +01:00
let headerRight;
let headerLeft;
if (!headerBackVisible) {
headerLeft = () => <></>;
opts.headerLeft = headerLeft;
}
if (closeButton === CloseButtonPosition.Right) {
2022-01-26 18:50:41 +01:00
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>
);
} else if (closeButton === CloseButtonPosition.Left) {
headerLeft = () => (
<TouchableOpacity
accessibilityRole="button"
accessibilityLabel={loc._.close}
style={styles.button}
onPress={handleClose}
testID="NavigationCloseButton"
>
<Image source={theme.closeImage} />
</TouchableOpacity>
);
}
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,
},
headerBackTitleVisible: false,
headerTintColor: theme.colors.foregroundColor,
headerRight,
headerLeft,
2022-01-26 18:50:41 +01:00
...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 { CloseButtonPosition };