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

142 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-08-30 16:13:11 +05:00
/* This file is part of the Notesnook project (https://notesnook.com/)
*
* Copyright (C) 2022 Streetwriters (Private) Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-29 16:19:17 +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
} from "react-native";
import useIsFloatingKeyboard from "../../hooks/use-is-floating-keyboard";
2022-08-29 16:19:17 +05:00
import { useSettingStore } from "../../stores/use-setting-store";
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"
2022-01-12 21:41:44 +05:00
]}
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={{
backgroundColor: background
? background
: transparent
? "transparent"
: "rgba(0,0,0,0.3)"
2022-01-22 12:57:05 +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"
}
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%",
position: "absolute"
}
2020-09-27 13:05:26 +05:00
});
export default BaseDialog;