Files
notesnook/apps/mobile/src/components/Button/index.js

97 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-09-07 13:12:41 +05:00
import React from 'react';
2020-11-10 17:18:19 +05:00
import {ActivityIndicator, StyleSheet, Text} from 'react-native';
2020-09-09 11:10:35 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-11-10 17:18:19 +05:00
import {useTracked} from '../../provider';
import {PressableButton} from '../PressableButton';
import {ph, pv, SIZE, WEIGHT} from '../../utils/SizeUtils';
2020-09-07 13:12:41 +05:00
2020-11-14 11:31:25 +05:00
const BUTTON_TYPES = {
transparent: {
primary: 'transparent',
text: 'accent',
selected: 'shade',
},
gray: {
primary: 'transparent',
text: 'icon',
selected: 'nav',
},
accent: {
primary: 'accent',
text: 'white',
selected: 'accent',
},
};
2020-09-07 13:12:41 +05:00
export const Button = ({
2020-09-09 11:10:35 +05:00
height = 40,
2020-11-14 11:31:25 +05:00
width = null,
2020-09-07 13:12:41 +05:00
onPress = () => {},
loading = false,
2020-09-09 11:10:35 +05:00
title = '',
icon,
2020-11-10 17:18:19 +05:00
fontSize = SIZE.sm,
2020-11-14 11:31:25 +05:00
type = 'transparent',
2020-09-07 13:12:41 +05:00
}) => {
2020-11-14 10:14:06 +05:00
const [state] = useTracked();
2020-09-24 23:02:30 +05:00
const {colors} = state;
2020-09-07 13:12:41 +05:00
return (
2020-09-09 11:10:35 +05:00
<PressableButton
2020-09-07 13:12:41 +05:00
onPress={onPress}
2020-11-14 11:31:25 +05:00
disabled={loading}
color={colors[BUTTON_TYPES[type].primary]}
selectedColor={colors[BUTTON_TYPES[type].selected]}
alpha={!colors.night ? -0.04 : 0.04}
2020-09-09 11:10:35 +05:00
customStyle={{
height: height,
2020-11-10 17:18:19 +05:00
width: width ? width : null,
2020-09-09 11:10:35 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
alignSelf: 'center',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
2020-11-14 11:31:25 +05:00
{loading ? (
<ActivityIndicator color={colors[BUTTON_TYPES[type].text]} />
) : null}
2020-09-09 11:10:35 +05:00
{icon && !loading ? (
<Icon
name={icon}
style={{
2020-11-14 10:14:06 +05:00
marginRight: 0,
2020-09-09 11:10:35 +05:00
}}
2020-11-14 11:31:25 +05:00
color={color[BUTTON_TYPES[type].text]}
2020-11-14 10:14:06 +05:00
size={SIZE.md}
2020-09-09 11:10:35 +05:00
/>
) : null}
2020-09-07 13:12:41 +05:00
<Text
2020-11-10 17:18:19 +05:00
style={[
styles.buttonText,
2020-11-14 11:31:25 +05:00
{color: colors[BUTTON_TYPES[type].text], fontSize: fontSize},
2020-11-10 17:18:19 +05:00
]}>
2020-09-07 13:12:41 +05:00
{title}
</Text>
2020-09-09 11:10:35 +05:00
</PressableButton>
2020-09-07 13:12:41 +05:00
);
};
const styles = StyleSheet.create({
activityText: {
fontSize: SIZE.sm,
textAlign: 'center',
},
activityContainer: {
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
2020-11-14 10:14:06 +05:00
fontFamily: WEIGHT.bold,
2020-09-07 13:12:41 +05:00
color: 'white',
2020-11-14 10:14:06 +05:00
fontSize: SIZE.md,
2020-09-07 13:12:41 +05:00
marginLeft: 5,
},
});