2023-07-06 20:03:57 +02:00
|
|
|
import * as React from 'react';
|
2024-05-27 10:05:15 +02:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { Modal, ModalProps, StyleSheet, View, ViewStyle, useWindowDimensions } from 'react-native';
|
2023-07-06 20:03:57 +02:00
|
|
|
import { hasNotch } from 'react-native-device-info';
|
|
|
|
|
|
|
|
interface ModalElementProps extends ModalProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
containerStyle?: ViewStyle;
|
2024-05-27 10:05:15 +02:00
|
|
|
backgroundColor?: string;
|
2023-07-06 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 10:05:15 +02:00
|
|
|
const useStyles = (backgroundColor?: string) => {
|
|
|
|
const { width: windowWidth, height: windowHeight } = useWindowDimensions();
|
|
|
|
const isLandscape = windowWidth > windowHeight;
|
|
|
|
return useMemo(() => {
|
|
|
|
return StyleSheet.create({
|
|
|
|
contentWrapper: isLandscape ? {
|
|
|
|
marginRight: hasNotch() ? 60 : 0,
|
|
|
|
marginLeft: hasNotch() ? 60 : 0,
|
|
|
|
marginTop: 15,
|
|
|
|
marginBottom: 15,
|
|
|
|
} : {
|
|
|
|
marginTop: hasNotch() ? 65 : 15,
|
|
|
|
marginBottom: hasNotch() ? 35 : 15,
|
|
|
|
},
|
|
|
|
modalBackground: { backgroundColor, flexGrow: 1 },
|
|
|
|
});
|
|
|
|
}, [isLandscape, backgroundColor]);
|
|
|
|
};
|
|
|
|
|
2023-07-06 20:03:57 +02:00
|
|
|
const ModalElement: React.FC<ModalElementProps> = ({
|
|
|
|
children,
|
|
|
|
containerStyle,
|
2024-05-27 10:05:15 +02:00
|
|
|
backgroundColor,
|
2023-07-06 20:03:57 +02:00
|
|
|
...modalProps
|
|
|
|
}) => {
|
2024-05-27 10:05:15 +02:00
|
|
|
const styles = useStyles(backgroundColor);
|
|
|
|
|
|
|
|
// contentWrapper adds padding. To allow styling the region outside of the modal
|
|
|
|
// (e.g. to add a background), the content is wrapped twice.
|
|
|
|
const content = (
|
|
|
|
<View style={[styles.contentWrapper, containerStyle]}>
|
|
|
|
{children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2024-03-25 13:39:48 +02:00
|
|
|
// supportedOrientations: On iOS, this allows the dialog to be shown in non-portrait orientations.
|
2023-07-06 20:03:57 +02:00
|
|
|
return (
|
2024-03-25 13:39:48 +02:00
|
|
|
<Modal
|
|
|
|
supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
|
|
|
|
{...modalProps}
|
|
|
|
>
|
2024-05-27 10:05:15 +02:00
|
|
|
<View style={styles.modalBackground}>{content}</View>
|
2023-07-06 20:03:57 +02:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ModalElement;
|