Files
notesnook/apps/mobile/src/components/container/floatingbutton.js

119 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import React, { useEffect } from 'react';
import { Keyboard, Platform, View } from 'react-native';
2022-04-20 17:55:47 +05:00
import Animated, {
Easing,
useAnimatedStyle,
useSharedValue,
withTiming
} from 'react-native-reanimated';
2022-01-22 12:57:05 +05:00
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2020-05-10 22:14:34 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2022-01-22 12:57:05 +05:00
import { notesnook } from '../../../e2e/test.ids';
2022-03-26 16:05:58 +05:00
import { editorState } from '../../screens/editor/tiptap/utils';
2022-05-20 14:49:38 +05:00
import { useSelectionStore } from '../../stores/use-selection-store';
import { useSettingStore } from '../../stores/use-setting-store';
import { getElevation, showTooltip, TOOLTIP_POSITIONS } from '../../utils';
2022-02-28 13:48:59 +05:00
import { normalize, SIZE } from '../../utils/size';
import { PressableButton } from '../ui/pressable';
2020-05-10 22:14:34 +05:00
2022-02-28 13:48:59 +05:00
export const FloatingButton = ({ title, onPress, color = 'accent', shouldShow = false }) => {
2020-11-04 20:29:45 +05:00
const insets = useSafeAreaInsets();
2021-06-26 09:13:37 +05:00
const deviceMode = useSettingStore(state => state.deviceMode);
2022-02-21 09:57:38 +05:00
const selectionMode = useSelectionStore(state => state.selectionMode);
2022-04-20 17:55:47 +05:00
const translate = useSharedValue(0);
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: translate.value
},
{
translateY: translate.value
}
]
};
});
2022-02-21 09:57:38 +05:00
useEffect(() => {
animate(selectionMode ? 150 : 0);
}, [selectionMode]);
2021-03-06 14:35:37 +05:00
2022-04-20 17:55:47 +05:00
function animate(toValue) {
translate.value = withTiming(toValue, {
2020-11-09 17:32:31 +05:00
duration: 250,
2022-01-22 12:57:05 +05:00
easing: Easing.elastic(1)
2022-04-20 17:55:47 +05:00
});
2020-11-09 17:32:31 +05:00
}
const onKeyboardHide = async () => {
2022-03-26 16:05:58 +05:00
editorState().keyboardState = false;
2021-06-26 09:13:37 +05:00
if (deviceMode !== 'mobile') return;
2020-11-09 17:32:31 +05:00
animate(0);
2020-11-04 20:29:45 +05:00
};
2020-11-03 10:02:09 +05:00
2020-11-09 17:32:31 +05:00
const onKeyboardShow = async () => {
2022-03-26 16:05:58 +05:00
editorState().keyboardState = true;
2021-06-26 09:13:37 +05:00
if (deviceMode !== 'mobile') return;
2020-11-09 17:32:31 +05:00
animate(150);
2020-11-04 20:29:45 +05:00
};
2020-11-03 10:02:09 +05:00
2020-11-04 20:29:45 +05:00
useEffect(() => {
2022-01-22 12:57:05 +05:00
let sub1 = Keyboard.addListener('keyboardDidShow', onKeyboardShow);
let sub2 = Keyboard.addListener('keyboardDidHide', onKeyboardHide);
2020-11-04 20:29:45 +05:00
return () => {
2021-10-25 13:32:14 +05:00
sub1?.remove();
sub2?.remove();
2020-11-04 20:29:45 +05:00
};
2021-06-26 09:13:37 +05:00
}, [deviceMode]);
2022-07-09 16:02:44 +05:00
const paddings = {
ios: insets.bottom === 0 ? 70 : insets.bottom + 15,
2022-07-20 09:16:38 +05:00
android: insets.bottom,
2022-07-09 16:02:44 +05:00
iPad: insets.bottom
};
2021-03-06 14:35:37 +05:00
2021-06-26 09:13:37 +05:00
return deviceMode !== 'mobile' && !shouldShow ? null : (
2020-11-09 17:32:31 +05:00
<Animated.View
2022-04-20 17:55:47 +05:00
style={[
{
position: 'absolute',
right: 12,
2022-07-09 16:02:44 +05:00
bottom: paddings[Platform.isPad ? 'iPad' : Platform.OS],
2022-04-20 17:55:47 +05:00
zIndex: 10
},
animatedStyle
]}
2022-01-22 12:57:05 +05:00
>
2020-11-04 20:29:45 +05:00
<PressableButton
2021-12-06 12:52:55 +05:00
testID={notesnook.buttons.add}
2020-12-01 17:51:39 +05:00
type="accent"
accentColor={color || 'accent'}
accentText="light"
2020-11-04 20:29:45 +05:00
customStyle={{
...getElevation(5),
2022-01-22 12:57:05 +05:00
borderRadius: 100
2020-11-04 20:29:45 +05:00
}}
2021-06-26 09:13:37 +05:00
onLongPress={event => {
showTooltip(event, title, TOOLTIP_POSITIONS.LEFT);
2020-12-29 11:49:41 +05:00
}}
2022-01-22 12:57:05 +05:00
onPress={onPress}
>
2020-05-10 22:14:34 +05:00
<View
2020-11-04 20:29:45 +05:00
style={{
alignItems: 'center',
2020-11-09 17:32:31 +05:00
justifyContent: 'center',
2020-11-04 20:29:45 +05:00
height: normalize(60),
2022-01-22 12:57:05 +05:00
width: normalize(60)
}}
>
2020-11-04 20:29:45 +05:00
<Icon
name={title === 'Clear all trash' ? 'delete' : 'plus'}
color="white"
2020-11-10 17:18:19 +05:00
size={SIZE.xxl}
2020-11-04 20:29:45 +05:00
/>
2020-05-10 22:14:34 +05:00
</View>
2020-11-04 20:29:45 +05:00
</PressableButton>
2020-11-09 17:32:31 +05:00
</Animated.View>
2020-11-04 20:29:45 +05:00
);
2020-05-10 22:14:34 +05:00
};