Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created May 4, 2016 09:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knowbody/7eb76bc06cbbb7d0cba61a7d40131609 to your computer and use it in GitHub Desktop.
Save knowbody/7eb76bc06cbbb7d0cba61a7d40131609 to your computer and use it in GitHub Desktop.
Profile badge component
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;
@knowbody
Copy link
Author

knowbody commented May 4, 2016

screen shot 2016-05-04 at 10 28 06

screen shot 2016-05-04 at 10 34 54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment