Files
notesnook/apps/mobile/src/components/Sheet/index.js

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-02-17 16:04:51 +05:00
import React from 'react';
2021-12-25 11:16:33 +05:00
import {Platform, View} from 'react-native';
2020-12-29 11:24:00 +05:00
import ActionSheet from 'react-native-actions-sheet';
2021-12-25 11:16:33 +05:00
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {useTracked} from '../../provider';
import {useSettingStore} from '../../provider/stores';
import layoutmanager from '../../utils/layout-manager';
2021-12-25 11:16:33 +05:00
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,
2021-07-24 13:16:09 +05:00
keyboardMode
2020-12-19 13:15:34 +05:00
}) => {
const [state] = useTracked();
const {colors} = state;
2021-06-13 14:59:09 +05:00
const deviceMode = useSettingStore(state => state.deviceMode);
2021-12-27 18:09:00 +05:00
const sheetKeyboardHandler = useSettingStore(state => state.sheetKeyboardHandler);
2021-07-10 14:23:30 +05:00
const largeTablet = deviceMode === 'tablet';
const smallTablet = deviceMode === 'smallTablet';
const dimensions = useSettingStore(state => state.dimensions);
const pitchBlack = useSettingStore(state => state.settings.pitchBlack);
2021-02-17 16:04:51 +05:00
const insets = useSafeAreaInsets();
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,
2021-06-13 14:59:09 +05:00
borderTopRightRadius: 10,
borderTopLeftRadius: 10,
2020-12-19 13:15:34 +05:00
alignSelf: 'center',
2021-07-25 01:13:58 +05:00
borderBottomRightRadius: 0,
borderBottomLeftRadius: 0
2020-12-19 13:15:34 +05:00
};
2020-12-29 11:24:00 +05:00
}, [colors.bg, gestureEnabled]);
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();
}
};
2021-12-30 10:42:03 +05:00
console.log('Sheet keyboard handler',sheetKeyboardHandler)
2020-12-19 13:15:34 +05:00
return (
<ActionSheet
ref={fwdRef}
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"
2021-12-31 11:05:27 +05:00
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
<>
2021-06-11 10:38:14 +05:00
<Toast context="local" />
2021-12-25 10:59:02 +05:00
<PremiumToast
2021-06-11 10:38:14 +05:00
context="sheet"
close={() => fwdRef?.current?.hide()}
offset={50}
/>
2021-02-20 15:03:02 +05:00
</>
2020-12-19 13:15:34 +05:00
}
2021-02-15 11:02:09 +05:00
onClose={_onClose}>
<BouncingView>
{children}
2021-11-08 15:05:34 +05:00
<View
style={{
height:
Platform.OS === 'ios' && insets.bottom !== 0
2021-11-18 14:51:53 +05:00
? insets.bottom + 5
2021-11-08 15:05:34 +05:00
: 20
}}
/>
</BouncingView>
2020-12-19 13:15:34 +05:00
</ActionSheet>
);
};
2021-12-25 11:16:33 +05:00
export default SheetWrapper;