BlueWallet/components/FloatButtons.tsx

153 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-11-05 15:50:16 -04:00
import React, { useState, useRef, forwardRef, ReactNode } from 'react';
2023-11-12 08:00:41 -04:00
import { View, Text, TouchableOpacity, StyleSheet, Dimensions, PixelRatio } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2023-10-23 21:28:44 -04:00
import { useTheme } from './themes';
2020-09-07 20:46:37 +03:00
const BORDER_RADIUS = 30;
2020-10-02 12:57:25 +03:00
const PADDINGS = 8;
2020-09-20 12:44:04 +03:00
const ICON_MARGIN = 7;
2020-09-07 20:46:37 +03:00
const cStyles = StyleSheet.create({
root: {
alignSelf: 'center',
height: '6.3%',
minHeight: 44,
},
2021-03-23 15:16:32 +03:00
rootAbsolute: {
position: 'absolute',
},
rootInline: {},
2020-09-07 20:46:37 +03:00
rootPre: {
2021-03-23 15:16:32 +03:00
position: 'absolute',
2023-11-12 07:58:32 -04:00
bottom: -1000,
2020-09-07 20:46:37 +03:00
},
rootPost: {
borderRadius: BORDER_RADIUS,
flexDirection: 'row',
2020-09-20 12:24:00 +03:00
overflow: 'hidden',
2020-09-07 20:46:37 +03:00
},
});
2023-11-05 15:50:16 -04:00
interface FContainerProps {
children: ReactNode | ReactNode[];
inline?: boolean;
}
2024-01-05 21:15:42 -04:00
export const FContainer = forwardRef<View, FContainerProps>((props, ref) => {
const insets = useSafeAreaInsets();
2023-11-05 15:50:16 -04:00
const [newWidth, setNewWidth] = useState<number | undefined>(undefined);
const layoutCalculated = useRef(false);
const bottomInsets = { bottom: insets.bottom ? insets.bottom + 10 : 30 };
2020-09-07 20:46:37 +03:00
2023-11-05 15:50:16 -04:00
const onLayout = (event: { nativeEvent: { layout: { width: number } } }) => {
if (layoutCalculated.current) return;
2020-10-02 12:57:25 +03:00
const maxWidth = Dimensions.get('window').width - BORDER_RADIUS - 20;
2020-09-07 20:46:37 +03:00
const { width } = event.nativeEvent.layout;
2020-10-02 12:57:25 +03:00
const withPaddings = Math.ceil(width + 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 15:50:16 -04:00
if (len === 1 && newW < 90) newW = 90;
setNewWidth(newW);
layoutCalculated.current = true;
2020-09-07 20:46:37 +03:00
};
return (
2023-11-12 08:00:41 -04:00
<View
2021-03-23 15:16:32 +03:00
ref={ref}
onLayout={onLayout}
2023-11-11 20:28:49 -04:00
style={[
cStyles.root,
props.inline ? cStyles.rootInline : cStyles.rootAbsolute,
bottomInsets,
2023-11-12 07:58:32 -04:00
newWidth ? cStyles.rootPost : cStyles.rootPre,
2023-11-11 20:28:49 -04:00
]}
2021-03-23 15:16:32 +03:00
>
2020-09-07 20:46:37 +03:00
{newWidth
? React.Children.toArray(props.children)
.filter(Boolean)
2024-01-05 21:15:42 -04:00
.map((child, index, array) => {
2023-11-05 15:50:16 -04:00
if (typeof child === 'string') {
return (
<View key={index} style={{ width: newWidth }}>
<Text>{child}</Text>
</View>
);
}
return React.cloneElement(child as React.ReactElement<any>, {
width: newWidth,
key: index,
first: index === 0,
last: index === array.length - 1,
2023-11-05 15:50:16 -04:00
});
})
: props.children}
2023-11-12 08:00:41 -04:00
</View>
2020-09-07 20:46:37 +03:00
);
});
2020-09-07 20:46:37 +03: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 12:24:00 +03:00
marginLeft: ICON_MARGIN,
2020-09-07 20:46:37 +03:00
backgroundColor: 'transparent',
},
});
2023-11-05 15:50:16 -04:00
interface FButtonProps {
text: string;
2023-11-05 15:50:29 -04:00
icon: ReactNode;
2023-11-05 15:50:16 -04:00
width?: number;
first?: boolean;
last?: boolean;
disabled?: boolean;
}
export const FButton = ({ text, icon, width, first, last, ...props }: FButtonProps) => {
2020-10-08 18:23:58 -04:00
const { colors } = useTheme();
const bStylesHook = StyleSheet.create({
root: {
backgroundColor: colors.buttonBackgroundColor,
},
text: {
color: colors.buttonAlternativeTextColor,
},
2021-03-23 15:16:32 +03:00
textDisabled: {
color: colors.formBorder,
},
2020-10-08 18:23:58 -04:00
});
2023-11-05 15:50:16 -04:00
const style: Record<string, any> = {};
2020-10-08 18:23:58 -04:00
2020-09-07 20:46:37 +03:00
if (width) {
2020-10-02 12:57:25 +03:00
const paddingLeft = first ? BORDER_RADIUS / 2 : PADDINGS;
const paddingRight = last ? BORDER_RADIUS / 2 : PADDINGS;
style.paddingRight = paddingRight;
style.paddingLeft = paddingLeft;
style.width = width + paddingRight + paddingLeft;
2020-09-07 20:46:37 +03:00
}
return (
<TouchableOpacity accessibilityLabel={text} accessibilityRole="button" style={[bStyles.root, bStylesHook.root, style]} {...props}>
2020-09-07 20:46:37 +03:00
<View style={bStyles.icon}>{icon}</View>
2021-03-23 15:16:32 +03:00
<Text numberOfLines={1} style={[bStyles.text, props.disabled ? bStylesHook.textDisabled : bStylesHook.text]}>
2020-10-02 12:57:25 +03:00
{text}
</Text>
2020-09-07 20:46:37 +03:00
</TouchableOpacity>
);
};