Files
notesnook/apps/mobile/src/components/Dialog/BaseDialog.js

110 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-01-02 02:05:09 +05:00
import React, {useEffect} from 'react';
2020-09-30 15:58:19 +05:00
import {
2022-01-08 00:35:52 +05:00
Keyboard,
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,
2021-06-07 11:53:27 +05:00
TouchableOpacity
2020-09-30 15:58:19 +05:00
} from 'react-native';
2021-12-27 18:09:00 +05:00
import {useSettingStore} from '../../provider/stores';
import useIsFloatingKeyboard from '../../utils/use-is-floating-keyboard';
2021-12-27 18:09:00 +05:00
import {BouncingView} from '../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-01-02 02:05:09 +05:00
closeOnTouch = 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
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}
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();
}}>
2021-04-08 10:27:47 +05:00
<SafeAreaView
style={{
backgroundColor: background
? background
: transparent
2021-04-08 10:27:47 +05:00
? 'transparent'
: 'rgba(0,0,0,0.3)'
}}>
2020-11-17 20:17:50 +05:00
<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,
{
justifyContent: centered
? 'center'
: bottom
? 'flex-end'
: 'flex-start'
}
]}>
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>
</SafeAreaView>
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;