Skip to content

Instantly share code, notes, and snippets.

@boschDev
Created June 25, 2018 06:20
Show Gist options
  • Save boschDev/326a8cc5173c741ec947ce223bef73d9 to your computer and use it in GitHub Desktop.
Save boschDev/326a8cc5173c741ec947ce223bef73d9 to your computer and use it in GitHub Desktop.
React Material UI User Avatar
.UserAvatar {
--UserAvatarColor: #000;
--UserAvatarSize: 40px;
position: relative;
border-radius: 100%;
text-align: center;
background-color: var(--UserAvatarColor);
font-size: var(--UserAvatarSize);
height: var(--UserAvatarSize);
width: var(--UserAvatarSize);
}
.UserAvatar .UserAvatar__Avatar {
width: 100%;
height: 100%;
z-index: 1;
}
.UserAvatar .UserAvatar__Name {
position: absolute;
top: 0;
display: flex;
color: #fff;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
font-size: .45em;
}
import React, {Component} from 'react'
import Avatar from 'material-ui/Avatar'
import randomMaterialColor from 'random-material-color'
import './UserAvatar.css'
export default class UserAvatar extends Component {
namePrefix: null;
getNamePrefix(firstName, lastName) {
return (firstName.substr(0, 1) + lastName.substr(0, 1)).toUpperCase();
}
getColor(namePrefix) {
return randomMaterialColor.getColor({text: namePrefix});
}
render() {
let {
firstName,
lastName,
image = null,
size = null,
} = this.props;
let namePrefix = this.getNamePrefix(firstName, lastName);
let style = {
'--UserAvatarColor': this.getColor(namePrefix),
};
if (size) {
style['--UserAvatarSize'] = size+'px';
}
return <div className="UserAvatar" style={style}>
<Avatar src={image} alt={firstName + ' ' + lastName} className="UserAvatar__Avatar"/>
<div className="UserAvatar__Name">{namePrefix}</div>
</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment