Files
notesnook/apps/mobile/app/components/ui/sheet/index.js

133 lines
3.8 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 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-30 16:13:11 +05:00
import React from "react";
import { Platform, View } from "react-native";
import ActionSheet from "react-native-actions-sheet";
import useGlobalSafeAreaInsets from "../../../hooks/use-global-safe-area-insets";
import { useSettingStore } from "../../../stores/use-setting-store";
import { useThemeStore } from "../../../stores/use-theme-store";
import { PremiumToast } from "../../premium/premium-toast";
import { Toast } from "../../toast";
import { BouncingView } from "../transitions/bouncing-view";
2020-12-19 13:15:34 +05:00
2021-12-25 11:16:33 +05:00
const SheetWrapper = ({
2020-12-19 13:15:34 +05:00
children,
fwdRef,
2020-12-19 13:59:41 +05:00
gestureEnabled = true,
2020-12-19 13:15:34 +05:00
onClose,
onOpen,
2020-12-19 13:59:41 +05:00
closeOnTouchBackdrop = true,
2021-07-10 14:23:30 +05:00
onHasReachedTop,
2022-02-07 14:44:48 +05:00
keyboardMode,
overlay,
overlayOpacity = 0.3
2020-12-19 13:15:34 +05:00
}) => {
const colors = useThemeStore((state) => state.colors);
const deviceMode = useSettingStore((state) => state.deviceMode);
const sheetKeyboardHandler = useSettingStore(
(state) => state.sheetKeyboardHandler
);
const largeTablet = deviceMode === "tablet";
const smallTablet = deviceMode === "smallTablet";
const dimensions = useSettingStore((state) => state.dimensions);
const pitchBlack = useSettingStore((state) => state.settings.pitchBlack);
const insets = useGlobalSafeAreaInsets();
let width = dimensions.width > 600 ? 600 : 500;
2020-12-19 13:15:34 +05:00
const style = React.useMemo(() => {
return {
width: largeTablet || smallTablet ? width : "100%",
2020-12-19 13:15:34 +05:00
backgroundColor: colors.bg,
zIndex: 10,
2021-11-08 15:05:34 +05:00
paddingTop: 5,
2021-07-10 14:23:30 +05:00
paddingBottom: 0,
borderTopRightRadius: 20,
borderTopLeftRadius: 20,
alignSelf: "center",
2021-07-25 01:13:58 +05:00
borderBottomRightRadius: 0,
borderBottomLeftRadius: 0
2020-12-19 13:15:34 +05:00
};
2022-08-30 18:27:09 +05:00
}, [colors.bg, largeTablet, smallTablet, width]);
2020-12-19 13:15:34 +05:00
2021-02-15 11:02:09 +05:00
const _onOpen = () => {
onOpen && onOpen();
};
const _onClose = async () => {
if (onClose) {
onClose();
}
};
2020-12-19 13:15:34 +05:00
return (
<ActionSheet
ref={fwdRef}
2022-04-01 10:00:33 +05:00
testIDs={{
backdrop: "sheet-backdrop"
2022-04-01 10:00:33 +05:00
}}
2023-01-09 16:06:24 +05:00
indicatorStyle={{
width: 100
}}
2021-06-11 10:38:14 +05:00
drawUnderStatusBar={false}
2020-12-19 13:15:34 +05:00
containerStyle={style}
gestureEnabled={gestureEnabled}
2020-12-19 13:59:41 +05:00
initialOffsetFromBottom={1}
2021-07-07 12:54:48 +05:00
onPositionChanged={onHasReachedTop}
2020-12-19 13:59:41 +05:00
closeOnTouchBackdrop={closeOnTouchBackdrop}
2021-07-10 14:23:30 +05:00
keyboardMode={keyboardMode}
2021-12-27 18:09:00 +05:00
keyboardHandlerEnabled={sheetKeyboardHandler}
2021-07-10 14:23:30 +05:00
closeOnPressBack={closeOnTouchBackdrop}
2021-11-08 15:05:34 +05:00
indicatorColor={colors.nav}
2021-02-15 11:02:09 +05:00
onOpen={_onOpen}
keyboardDismissMode="none"
2022-02-07 14:44:48 +05:00
defaultOverlayOpacity={overlayOpacity}
overlayColor={pitchBlack ? "#585858" : "#000000"}
2020-12-19 13:15:34 +05:00
keyboardShouldPersistTaps="always"
2021-08-03 11:17:06 +05:00
ExtraOverlayComponent={
2021-02-20 15:03:02 +05:00
<>
2022-02-07 14:44:48 +05:00
{overlay}
<PremiumToast
context="sheet"
close={() => fwdRef?.current?.hide()}
offset={50}
/>
<Toast context="local" />
2021-02-20 15:03:02 +05:00
</>
2020-12-19 13:15:34 +05:00
}
2022-01-22 12:57:05 +05:00
onClose={_onClose}
>
<BouncingView>
{children}
2021-11-08 15:05:34 +05:00
<View
style={{
height:
Platform.OS === "ios" && insets.bottom !== 0
? insets.bottom + 5
: 20
2021-11-08 15:05:34 +05:00
}}
/>
</BouncingView>
2020-12-19 13:15:34 +05:00
</ActionSheet>
);
};
2021-12-25 11:16:33 +05:00
export default SheetWrapper;