Files
notesnook/apps/mobile/app/components/dialog/base-dialog.js

113 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import React, { useEffect } from 'react';
2020-09-30 15:58:19 +05:00
import {
2020-11-17 20:17:50 +05:00
KeyboardAvoidingView,
2020-09-30 15:58:19 +05:00
Modal,
2020-11-17 20:17:50 +05:00
Platform,
SafeAreaView,
2020-09-30 15:58:19 +05:00
StyleSheet,
2022-02-07 14:44:48 +05:00
TouchableOpacity,
View
2020-09-30 15:58:19 +05:00
} from 'react-native';
2022-04-24 05:59:14 +05:00
import { useSettingStore } from '../../stores/use-setting-store';
2022-08-16 16:48:10 +05:00
import useIsFloatingKeyboard from '../../hooks/use-is-floating-keyboard';
2022-02-28 13:48:59 +05:00
import { BouncingView } from '../ui/transitions/bouncing-view';
2020-09-27 13:05:26 +05:00
2020-09-30 15:58:19 +05:00
const BaseDialog = ({
visible,
onRequestClose,
children,
onShow,
animation = 'fade',
2020-11-17 20:17:50 +05:00
premium,
2021-01-14 18:58:29 +05:00
statusBarTranslucent = true,
transparent,
centered = true,
2021-07-19 09:44:02 +05:00
bottom = false,
2021-11-22 15:12:26 +05:00
background = null,
2021-12-31 15:05:30 +05:00
animated = true,
2022-01-08 00:35:52 +05:00
bounce = true,
2022-02-07 14:44:48 +05:00
closeOnTouch = true,
useSafeArea = true
2020-09-30 15:58:19 +05:00
}) => {
const floating = useIsFloatingKeyboard();
2021-12-27 18:09:00 +05:00
useEffect(() => {
return () => {
useSettingStore.getState().setSheetKeyboardHandler(true);
2022-01-02 02:05:09 +05:00
};
}, []);
2021-12-27 18:09:00 +05:00
2022-02-07 14:44:48 +05:00
const Wrapper = useSafeArea ? SafeAreaView : View;
2020-09-27 13:05:26 +05:00
return (
<Modal
visible={visible}
transparent={true}
animated
2020-11-25 11:46:44 +05:00
statusBarTranslucent={statusBarTranslucent}
2022-01-12 21:41:44 +05:00
supportedOrientations={[
'portrait',
'portrait-upside-down',
'landscape',
'landscape-left',
'landscape-right'
]}
2020-09-30 15:58:19 +05:00
onShow={() => {
if (onShow) {
onShow();
2021-12-27 18:09:00 +05:00
useSettingStore.getState().setSheetKeyboardHandler(false);
2020-09-30 15:58:19 +05:00
}
}}
animationType={animation}
2021-12-27 18:09:00 +05:00
onRequestClose={() => {
2022-01-02 02:05:09 +05:00
if (!closeOnTouch) return null;
2021-12-27 18:09:00 +05:00
useSettingStore.getState().setSheetKeyboardHandler(true);
onRequestClose && onRequestClose();
2022-01-22 12:57:05 +05:00
}}
>
2022-02-07 14:44:48 +05:00
<Wrapper
style={{
2022-01-22 12:57:05 +05:00
backgroundColor: background ? background : transparent ? 'transparent' : 'rgba(0,0,0,0.3)'
}}
>
<KeyboardAvoidingView enabled={!floating && Platform.OS === 'ios'} behavior="padding">
<BouncingView
2021-11-11 13:08:39 +05:00
duration={400}
2021-11-22 15:12:26 +05:00
animated={animated}
2021-12-31 15:05:30 +05:00
initialScale={bounce ? 0.9 : 1}
style={[
styles.backdrop,
{
2022-01-22 12:57:05 +05:00
justifyContent: centered ? 'center' : bottom ? 'flex-end' : 'flex-start'
}
2022-01-22 12:57:05 +05:00
]}
>
2021-11-11 13:08:39 +05:00
<TouchableOpacity
2022-01-02 02:02:18 +05:00
onPress={closeOnTouch ? onRequestClose : null}
2021-11-11 13:08:39 +05:00
style={styles.overlayButton}
/>
{premium}
{children}
</BouncingView>
2020-11-17 20:17:50 +05:00
</KeyboardAvoidingView>
2022-02-07 14:44:48 +05:00
</Wrapper>
2020-09-27 13:05:26 +05:00
</Modal>
);
};
const styles = StyleSheet.create({
backdrop: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center'
2020-09-27 13:05:26 +05:00
},
overlayButton: {
width: '100%',
height: '100%',
2021-11-11 13:08:39 +05:00
position: 'absolute'
}
2020-09-27 13:05:26 +05:00
});
export default BaseDialog;