Files
notesnook/apps/mobile/src/components/ActionSheetComponent/ActionSheetWrapper.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-02-17 16:04:51 +05:00
import React from 'react';
2021-07-10 14:23:30 +05:00
import {Platform, View} from 'react-native';
2020-12-29 11:24:00 +05:00
import ActionSheet from 'react-native-actions-sheet';
2021-07-10 14:23:30 +05:00
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {useTracked} from '../../provider';
import {useSettingStore} from '../../provider/stores';
import {editing} from '../../utils';
import {hexToRGBA} from '../../utils/ColorUtils';
import {sleep} from '../../utils/TimeUtils';
import {EditorWebView, textInput} from '../../views/Editor/Functions';
import tiny from '../../views/Editor/tiny/tiny';
2021-07-10 14:23:30 +05:00
import {focusEditor} from '../../views/Editor/tiny/toolbar/constants';
import {Toast} from '../Toast';
import {GetPremium} from './GetPremium';
2020-12-19 13:15:34 +05:00
const ActionSheetWrapper = ({
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,
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-07-10 14:23:30 +05:00
const largeTablet = deviceMode === 'tablet';
const smallTablet = deviceMode === 'smallTablet';
2021-02-17 16:04:51 +05:00
const insets = useSafeAreaInsets();
2020-12-19 13:15:34 +05:00
const style = React.useMemo(() => {
return {
2020-12-19 14:26:44 +05:00
width: largeTablet || smallTablet ? 500 : '100%',
2020-12-19 13:15:34 +05:00
backgroundColor: colors.bg,
zIndex: 10,
2021-07-10 14:23:30 +05:00
paddingTop: 10,
paddingBottom: 0,
2020-12-19 13:15:34 +05:00
borderBottomRightRadius: largeTablet ? 10 : 1,
borderBottomLeftRadius: largeTablet ? 10 : 1,
2021-06-13 14:59:09 +05:00
borderTopRightRadius: 10,
borderTopLeftRadius: 10,
2020-12-19 13:15:34 +05:00
marginBottom: largeTablet ? 50 : 0,
alignSelf: 'center',
};
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 (editing.isFocused === true) {
textInput.current?.focus();
await sleep(10);
if (editing.focusType == 'editor') {
focusEditor();
} else {
Platform.OS === 'android' && EditorWebView.current.requestFocus();
tiny.call(EditorWebView, tiny.focusTitle);
}
}
if (onClose) {
onClose();
}
};
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}
extraScroll={largeTablet ? 50 : 0}
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}
closeOnPressBack={closeOnTouchBackdrop}
2020-12-19 13:15:34 +05:00
indicatorColor={
Platform.OS === 'ios'
? hexToRGBA(colors.accent + '19')
: hexToRGBA(colors.shade)
}
2021-02-15 11:02:09 +05:00
onOpen={_onOpen}
2020-12-19 13:15:34 +05:00
keyboardShouldPersistTaps="always"
premium={
2021-02-20 15:03:02 +05:00
<>
2021-06-11 10:38:14 +05:00
<Toast context="local" />
<GetPremium
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}>
2020-12-19 13:15:34 +05:00
{children}
2021-07-10 14:23:30 +05:00
<View style={{height: Platform.OS === 'ios' ? insets.bottom / 2 : 0}} />
2020-12-19 13:15:34 +05:00
</ActionSheet>
);
};
export default ActionSheetWrapper;