BlueWallet/components/FloatButtons.tsx

176 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-05-20 11:54:13 +02:00
import React, { forwardRef, ReactNode, useEffect, useRef, useState } from 'react';
import { Animated, Dimensions, PixelRatio, StyleSheet, Text, TouchableOpacity, useWindowDimensions, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2023-10-24 03:28:44 +02:00
import { useTheme } from './themes';
2020-09-07 19:46:37 +02:00
2024-04-15 02:14:12 +02:00
const BORDER_RADIUS = 8;
const PADDINGS = 24;
2020-09-20 11:44:04 +02:00
const ICON_MARGIN = 7;
2020-09-07 19:46:37 +02:00
const cStyles = StyleSheet.create({
root: {
alignSelf: 'center',
2024-04-18 03:19:28 +02:00
height: '6.9%',
2020-09-07 19:46:37 +02:00
minHeight: 44,
},
2021-03-23 13:16:32 +01:00
rootAbsolute: {
position: 'absolute',
},
rootInline: {},
2020-09-07 19:46:37 +02:00
rootPre: {
2021-03-23 13:16:32 +01:00
position: 'absolute',
2023-11-12 12:58:32 +01:00
bottom: -1000,
2020-09-07 19:46:37 +02:00
},
rootPost: {
flexDirection: 'row',
2020-09-20 11:24:00 +02:00
overflow: 'hidden',
2020-09-07 19:46:37 +02:00
},
});
2023-11-05 20:50:16 +01:00
interface FContainerProps {
children: ReactNode | ReactNode[];
inline?: boolean;
}
2024-01-06 02:15:42 +01:00
export const FContainer = forwardRef<View, FContainerProps>((props, ref) => {
const insets = useSafeAreaInsets();
2023-11-05 20:50:16 +01:00
const [newWidth, setNewWidth] = useState<number | undefined>(undefined);
const layoutCalculated = useRef(false);
const bottomInsets = { bottom: insets.bottom ? insets.bottom + 10 : 30 };
const { height, width } = useWindowDimensions();
const slideAnimation = useRef(new Animated.Value(height)).current;
useEffect(() => {
slideAnimation.setValue(height);
Animated.spring(slideAnimation, {
toValue: 0,
useNativeDriver: true,
speed: 100,
bounciness: 3,
}).start();
}, [height, slideAnimation]);
2020-09-07 19:46:37 +02:00
2023-11-05 20:50:16 +01:00
const onLayout = (event: { nativeEvent: { layout: { width: number } } }) => {
if (layoutCalculated.current) return;
const maxWidth = width - BORDER_RADIUS - 140;
const layoutWidth = event.nativeEvent.layout.width;
const withPaddings = Math.ceil(layoutWidth + PADDINGS * 2);
const len = React.Children.toArray(props.children).filter(Boolean).length;
let newW = withPaddings * len > maxWidth ? Math.floor(maxWidth / len) : withPaddings;
2023-11-05 20:50:16 +01:00
if (len === 1 && newW < 90) newW = 90;
setNewWidth(newW);
layoutCalculated.current = true;
2020-09-07 19:46:37 +02:00
};
return (
<Animated.View
2021-03-23 13:16:32 +01:00
ref={ref}
onLayout={onLayout}
2023-11-12 01:28:49 +01:00
style={[
cStyles.root,
props.inline ? cStyles.rootInline : cStyles.rootAbsolute,
bottomInsets,
2023-11-12 12:58:32 +01:00
newWidth ? cStyles.rootPost : cStyles.rootPre,
{ transform: [{ translateY: slideAnimation }] },
2023-11-12 01:28:49 +01:00
]}
2021-03-23 13:16:32 +01:00
>
2020-09-07 19:46:37 +02:00
{newWidth
? React.Children.toArray(props.children)
.filter(Boolean)
2024-01-06 02:15:42 +01:00
.map((child, index, array) => {
2023-11-05 20:50:16 +01:00
if (typeof child === 'string') {
return (
<View key={index} style={{ width: newWidth }}>
<Text adjustsFontSizeToFit numberOfLines={1}>
{child}
</Text>
2023-11-05 20:50:16 +01:00
</View>
);
}
return React.cloneElement(child as React.ReactElement<any>, {
width: newWidth,
key: index,
first: index === 0,
last: index === array.length - 1,
2023-11-05 20:50:16 +01:00
});
})
: props.children}
</Animated.View>
2020-09-07 19:46:37 +02:00
);
});
2020-09-07 19:46:37 +02:00
const buttonFontSize =
PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26) > 22
? 22
: PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26);
const bStyles = StyleSheet.create({
root: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
},
icon: {
alignItems: 'center',
},
text: {
fontSize: buttonFontSize,
fontWeight: '600',
2020-09-20 11:24:00 +02:00
marginLeft: ICON_MARGIN,
2020-09-07 19:46:37 +02:00
backgroundColor: 'transparent',
},
});
2023-11-05 20:50:16 +01:00
interface FButtonProps {
text: string;
2023-11-05 20:50:29 +01:00
icon: ReactNode;
2023-11-05 20:50:16 +01:00
width?: number;
first?: boolean;
last?: boolean;
disabled?: boolean;
2024-04-15 03:01:30 +02:00
onPress: () => void;
2024-06-01 15:48:22 +02:00
onLongPress?: () => void;
2023-11-05 20:50:16 +01:00
}
export const FButton = ({ text, icon, width, first, last, ...props }: FButtonProps) => {
2020-10-09 00:23:58 +02:00
const { colors } = useTheme();
const bStylesHook = StyleSheet.create({
root: {
backgroundColor: colors.buttonBackgroundColor,
2024-04-15 02:14:12 +02:00
borderRadius: BORDER_RADIUS,
2020-10-09 00:23:58 +02:00
},
text: {
color: colors.buttonAlternativeTextColor,
},
2021-03-23 13:16:32 +01:00
textDisabled: {
color: colors.formBorder,
},
2024-04-15 02:14:12 +02:00
marginRight: {
marginRight: 10,
},
2020-10-09 00:23:58 +02:00
});
2023-11-05 20:50:16 +01:00
const style: Record<string, any> = {};
2024-04-15 02:14:12 +02:00
const additionalStyles = !last ? bStylesHook.marginRight : {};
2020-10-09 00:23:58 +02:00
2020-09-07 19:46:37 +02:00
if (width) {
2024-04-15 02:14:12 +02:00
style.paddingHorizontal = PADDINGS;
style.width = width + PADDINGS * 2;
2020-09-07 19:46:37 +02:00
}
return (
2024-04-15 02:14:12 +02:00
<TouchableOpacity
accessibilityLabel={text}
accessibilityRole="button"
style={[bStyles.root, bStylesHook.root, style, additionalStyles]}
{...props}
>
2020-09-07 19:46:37 +02:00
<View style={bStyles.icon}>{icon}</View>
<Text numberOfLines={1} adjustsFontSizeToFit style={[bStyles.text, props.disabled ? bStylesHook.textDisabled : bStylesHook.text]}>
2020-10-02 11:57:25 +02:00
{text}
</Text>
2020-09-07 19:46:37 +02:00
</TouchableOpacity>
);
};