Created
May 4, 2016 09:36
-
-
Save knowbody/7eb76bc06cbbb7d0cba61a7d40131609 to your computer and use it in GitHub Desktop.
Profile badge component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PropTypes } from 'react'; | |
import { View, Text, StyleSheet } from 'react-native'; | |
const colors = [ | |
'salmon', 'tomato', 'gold', 'yellowgreen', 'deepskyblue', | |
'mediumspringgreen', 'darkturquoise', 'lightcoral', 'teal', | |
'maroon', 'mediumseagreen', 'peru', 'plum', 'cadetblue', | |
'darkorchid', 'palevioletred', 'lightgreen', 'limegreen', | |
'indigo', 'coral', 'chocolate', 'orangered', 'lightskyblue' | |
]; | |
function pickRandomColor(array) { | |
return array[Math.floor(Math.random() * array.length)]; | |
} | |
function getInitials(name) { | |
return name.replace(/[^A-Z]/g, ''); | |
} | |
const ProfileBadge = ({ size = 160, name, color = pickRandomColor(colors) }) => ( | |
<View | |
style={[styles.container, { | |
height: size, | |
width: size, | |
borderRadius: size / 2, | |
backgroundColor: color | |
}]} | |
> | |
<Text | |
style={[styles.initials, { | |
fontSize: size / (getInitials(name).length < 2 ? 2 : getInitials(name).length) | |
}]} | |
> | |
{getInitials(name)} | |
</Text> | |
</View> | |
); | |
const styles = StyleSheet.create({ | |
container: { | |
justifyContent: 'center', | |
alignItems: 'center' | |
}, | |
initials: { | |
color: 'white', | |
fontWeight: '300' | |
} | |
}); | |
ProfileBadge.propTypes = { | |
size: PropTypes.number, | |
name: PropTypes.string.isRequired, | |
color: PropTypes.string | |
}; | |
export default ProfileBadge; |
Author
knowbody
commented
May 4, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment