Files
notesnook/apps/mobile/app/components/container/floating-button.js

145 lines
4.0 KiB
JavaScript
Raw Normal View History

/*
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-30 16:13:11 +05:00
2022-08-29 16:19:17 +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";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import { notesnook } from "../../../e2e/test.ids";
import { editorState } from "../../screens/editor/tiptap/utils";
import { useSelectionStore } from "../../stores/use-selection-store";
import { useSettingStore } from "../../stores/use-setting-store";
import { getElevation, showTooltip, TOOLTIP_POSITIONS } from "../../utils";
import { normalize, SIZE } from "../../utils/size";
import { PressableButton } from "../ui/pressable";
2022-08-30 18:27:09 +05:00
import { useCallback } from "react";
2020-05-10 22:14:34 +05:00
export const FloatingButton = ({
title,
onPress,
color = "accent",
shouldShow = false
}) => {
const deviceMode = useSettingStore((state) => state.deviceMode);
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);
2022-08-30 18:27:09 +05:00
}, [animate, selectionMode]);
2021-03-06 14:35:37 +05:00
2022-08-30 18:27:09 +05:00
const animate = useCallback(
(toValue) => {
translate.value = withTiming(toValue, {
duration: 250,
easing: Easing.elastic(1)
});
},
[translate]
);
2020-11-09 17:32:31 +05:00
2022-08-30 18:27:09 +05:00
const onKeyboardHide = useCallback(async () => {
2022-03-26 16:05:58 +05:00
editorState().keyboardState = false;
if (deviceMode !== "mobile") return;
2020-11-09 17:32:31 +05:00
animate(0);
2022-08-30 18:27:09 +05:00
}, [animate, deviceMode]);
2020-11-03 10:02:09 +05:00
2022-08-30 18:27:09 +05:00
const onKeyboardShow = useCallback(async () => {
2022-03-26 16:05:58 +05:00
editorState().keyboardState = true;
if (deviceMode !== "mobile") return;
2020-11-09 17:32:31 +05:00
animate(150);
2022-08-30 18:27:09 +05:00
}, [animate, deviceMode]);
2020-11-03 10:02:09 +05:00
2020-11-04 20:29:45 +05:00
useEffect(() => {
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
};
2022-08-30 18:27:09 +05:00
}, [deviceMode, onKeyboardHide, onKeyboardShow]);
2022-07-09 16:02:44 +05:00
const paddings = {
2022-07-20 12:05:59 +05:00
ios: 20,
android: 20,
2022-07-20 12:05:59 +05:00
iPad: 20
2022-07-09 16:02:44 +05:00
};
2021-03-06 14:35: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",
2022-04-20 17:55:47 +05:00
right: 12,
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"}
2020-12-01 17:51:39 +05:00
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
}}
onLongPress={(event) => {
2021-06-26 09:13:37 +05:00
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",
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"}
2020-11-04 20:29:45 +05:00
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
};