Skip to content

Instantly share code, notes, and snippets.

@mattquest
Created October 18, 2019 15:24
Show Gist options
  • Save mattquest/50986143dc0133a30f685c3ead7e549a to your computer and use it in GitHub Desktop.
Save mattquest/50986143dc0133a30f685c3ead7e549a to your computer and use it in GitHub Desktop.
A simple button component for react-native / react-navigation written in typescript.
/**
* WhButton
* a simple button component for react-native / react-navigation
* written in typescript, tested with:
* - react-native 0.59.8
* - react-navigation 4.0.10
*/
import React from 'react'
import { Text, StyleSheet } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { withNavigation, NavigationInjectedProps } from 'react-navigation';
interface Props {
text: string, // button text
navTo: string, // react-navagation route name
color?: string, // override default background color
light?: boolean // turns text black for light background colors
size?: number // override default font size
}
const WhButton:React.FC<Props & NavigationInjectedProps> = props => {
const buttonStyles:{backgroundColor?: string} = {};
const textStyles:{color?: string, fontSize?: number} = {};
if (props.color) {
buttonStyles.backgroundColor = props.color
}
if (props.light) {
textStyles.color = '#000'
}
if (props.size) {
textStyles.fontSize = props.size
}
return(
<TouchableOpacity
style={[styles.button, buttonStyles]}
onPress={ () => props.navigation.navigate(props.navTo) }
>
<Text style={[styles.buttonText, textStyles]}>{props.text}</Text>
</TouchableOpacity>
)
}
export default withNavigation(WhButton);
const styles = StyleSheet.create({
button: {
margin: 7,
padding: 7,
borderRadius: 5,
paddingHorizontal: 15,
backgroundColor: '#333',
justifyContent: 'center',
},
buttonText: {
fontSize: 18,
color: '#fff',
fontWeight: '700',
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment