BlueWallet/components/FloatButtons.js

139 lines
3.8 KiB
JavaScript
Raw Normal View History

import React, { useState, useRef, forwardRef } from 'react';
2020-09-07 19:46:37 +02:00
import PropTypes from 'prop-types';
import { View, Text, TouchableOpacity, StyleSheet, Dimensions, PixelRatio } from 'react-native';
2020-10-09 00:23:58 +02:00
import { useTheme } from '@react-navigation/native';
2020-09-07 19:46:37 +02:00
const BORDER_RADIUS = 30;
2020-10-02 11:57:25 +02:00
const PADDINGS = 8;
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',
height: '6.3%',
minHeight: 44,
},
2021-03-23 13:16:32 +01:00
rootAbsolute: {
position: 'absolute',
bottom: 30,
},
rootInline: {},
2020-09-07 19:46:37 +02:00
rootPre: {
2021-03-23 13:16:32 +01:00
position: 'absolute',
2020-09-07 19:46:37 +02:00
bottom: -1000,
},
rootPost: {
borderRadius: BORDER_RADIUS,
flexDirection: 'row',
2020-09-20 11:24:00 +02:00
overflow: 'hidden',
2020-09-07 19:46:37 +02:00
},
});
export const FContainer = forwardRef((props, ref) => {
2020-09-07 19:46:37 +02:00
const [newWidth, setNewWidth] = useState();
const layoutCalculated = useRef(false);
2020-09-07 19:46:37 +02:00
const onLayout = event => {
if (layoutCalculated.current) return;
2020-10-02 11:57:25 +02:00
const maxWidth = Dimensions.get('window').width - BORDER_RADIUS - 20;
2020-09-07 19:46:37 +02:00
const { width } = event.nativeEvent.layout;
2020-10-02 11:57:25 +02:00
const withPaddings = Math.ceil(width + PADDINGS * 2);
const len = React.Children.toArray(props.children).filter(Boolean).length;
2020-10-02 11:57:25 +02:00
let newWidth = withPaddings * len > maxWidth ? Math.floor(maxWidth / len) : withPaddings;
2020-09-21 11:58:21 +02:00
if (len === 1 && newWidth < 90) newWidth = 90; // to add Paddings for lonely small button, like Scan on main screen
2020-09-07 19:46:37 +02:00
setNewWidth(newWidth);
layoutCalculated.current = true;
2020-09-07 19:46:37 +02:00
};
return (
2021-03-23 13:16:32 +01:00
<View
ref={ref}
onLayout={onLayout}
style={[cStyles.root, props.inline ? cStyles.rootInline : cStyles.rootAbsolute, newWidth ? cStyles.rootPost : cStyles.rootPre]}
>
2020-09-07 19:46:37 +02:00
{newWidth
? React.Children.toArray(props.children)
.filter(Boolean)
.map((c, index, array) =>
React.cloneElement(c, {
width: newWidth,
key: index,
first: index === 0,
last: index === array.length - 1,
}),
)
: props.children}
2020-09-07 19:46:37 +02:00
</View>
);
});
2020-09-07 19:46:37 +02:00
FContainer.propTypes = {
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.element), PropTypes.element]),
2021-03-23 13:16:32 +01:00
inline: PropTypes.bool,
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',
},
});
export const FButton = ({ text, icon, width, first, last, ...props }) => {
2020-10-09 00:23:58 +02:00
const { colors } = useTheme();
const bStylesHook = StyleSheet.create({
root: {
backgroundColor: colors.buttonBackgroundColor,
},
text: {
color: colors.buttonAlternativeTextColor,
},
2021-03-23 13:16:32 +01:00
textDisabled: {
color: colors.formBorder,
},
2020-10-09 00:23:58 +02:00
});
2020-09-07 19:46:37 +02:00
const style = {};
2020-10-09 00:23:58 +02:00
2020-09-07 19:46:37 +02:00
if (width) {
2020-10-02 11:57:25 +02: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 19:46:37 +02:00
}
return (
<TouchableOpacity accessibilityRole="button" style={[bStyles.root, bStylesHook.root, style]} {...props}>
2020-09-07 19:46:37 +02:00
<View style={bStyles.icon}>{icon}</View>
2021-03-23 13:16:32 +01:00
<Text numberOfLines={1} 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>
);
};
FButton.propTypes = {
text: PropTypes.string,
icon: PropTypes.element,
width: PropTypes.number,
first: PropTypes.bool,
last: PropTypes.bool,
2021-03-23 13:16:32 +01:00
disabled: PropTypes.bool,
2020-09-07 19:46:37 +02:00
};