forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatButtons.tsx
152 lines (139 loc) · 4.23 KB
/
FloatButtons.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useState, useRef, forwardRef, ReactNode } from 'react';
import { View, Text, TouchableOpacity, StyleSheet, Dimensions, PixelRatio } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useTheme } from './themes';
const BORDER_RADIUS = 30;
const PADDINGS = 8;
const ICON_MARGIN = 7;
const cStyles = StyleSheet.create({
root: {
alignSelf: 'center',
height: '6.3%',
minHeight: 44,
},
rootAbsolute: {
position: 'absolute',
},
rootInline: {},
rootPre: {
position: 'absolute',
bottom: -1000,
},
rootPost: {
borderRadius: BORDER_RADIUS,
flexDirection: 'row',
overflow: 'hidden',
},
});
interface FContainerProps {
children: ReactNode | ReactNode[];
inline?: boolean;
}
export const FContainer = forwardRef<View, FContainerProps>((props, ref) => {
const insets = useSafeAreaInsets();
const [newWidth, setNewWidth] = useState<number | undefined>(undefined);
const layoutCalculated = useRef(false);
const bottomInsets = { bottom: insets.bottom ? insets.bottom + 10 : 30 };
const onLayout = (event: { nativeEvent: { layout: { width: number } } }) => {
if (layoutCalculated.current) return;
const maxWidth = Dimensions.get('window').width - BORDER_RADIUS - 20;
const { width } = event.nativeEvent.layout;
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;
if (len === 1 && newW < 90) newW = 90;
setNewWidth(newW);
layoutCalculated.current = true;
};
return (
<View
ref={ref}
onLayout={onLayout}
style={[
cStyles.root,
props.inline ? cStyles.rootInline : cStyles.rootAbsolute,
bottomInsets,
newWidth ? cStyles.rootPost : cStyles.rootPre,
]}
>
{newWidth
? React.Children.toArray(props.children)
.filter(Boolean)
.map((child, index, array) => {
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,
});
})
: props.children}
</View>
);
});
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',
marginLeft: ICON_MARGIN,
backgroundColor: 'transparent',
},
});
interface FButtonProps {
text: string;
icon: ReactNode;
width?: number;
first?: boolean;
last?: boolean;
disabled?: boolean;
}
export const FButton = ({ text, icon, width, first, last, ...props }: FButtonProps) => {
const { colors } = useTheme();
const bStylesHook = StyleSheet.create({
root: {
backgroundColor: colors.buttonBackgroundColor,
},
text: {
color: colors.buttonAlternativeTextColor,
},
textDisabled: {
color: colors.formBorder,
},
});
const style: Record<string, any> = {};
if (width) {
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;
}
return (
<TouchableOpacity accessibilityLabel={text} accessibilityRole="button" style={[bStyles.root, bStylesHook.root, style]} {...props}>
<View style={bStyles.icon}>{icon}</View>
<Text numberOfLines={1} style={[bStyles.text, props.disabled ? bStylesHook.textDisabled : bStylesHook.text]}>
{text}
</Text>
</TouchableOpacity>
);
};