forked from wbinnssmith/react-user-avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (87 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from 'react';
import PropTypes from 'prop-types';
import initials from 'initials';
import contrast from 'contrast';
// from https://flatuicolors.com/
const defaultColors = [
'#2ecc71', // emerald
'#3498db', // peter river
'#8e44ad', // wisteria
'#e67e22', // carrot
'#e74c3c', // alizarin
'#1abc9c', // turquoise
'#2c3e50', // midnight blue
];
function sumChars(str) {
let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += str.charCodeAt(i);
}
return sum;
}
class UserAvatar extends React.Component {
render() {
const abbr = initials(this.props.name);
// size = addPx(size);
const imageStyle = {
display: 'block',
borderRadius: this.props.borderRadius
};
const innerStyle = {
lineHeight: this.props.size + "px",
width: this.props.size + "px",
height: this.props.size + "px",
textAlign: 'center',
fontSize: Math.floor(this.props.size / this.props.textSizeRatio),
borderRadius: this.props.borderRadius
};
if (this.props.size) {
imageStyle.width = innerStyle.width = innerStyle.maxWidth = this.props.size + "px";
imageStyle.height = innerStyle.height = innerStyle.maxHeight = this.props.size + "px";
}
let inner, classes = [this.props.className, 'UserAvatar'];
if (this.props.src || this.props.srcset) {
inner = <img className="UserAvatar--img" style={imageStyle} src={this.props.src} srcSet={this.props.srcset} alt={this.props.name} />
} else {
let background;
if (this.props.color) {
background = this.props.color;
} else {
// pick a deterministic color from the list
let i = sumChars(this.props.name) % this.props.colors.length;
background = this.props.colors[i];
}
innerStyle.backgroundColor = background;
inner = abbr;
}
if (innerStyle.backgroundColor) {
classes.push(`UserAvatar--${contrast(innerStyle.backgroundColor)}`);
}
return (
<div aria-label={name} className={classes.join(' ')} style={this.props.style}>
<div className="UserAvatar--inner" style={innerStyle}>
{inner}
</div>
</div>
)
}
}
UserAvatar.propTypes = {
borderRadius: PropTypes.string,
src: PropTypes.string,
srcset: PropTypes.string,
name: PropTypes.string,
color: PropTypes.string,
colors: PropTypes.array,
size: PropTypes.number.isRequired,
style: PropTypes.object,
textSizeRatio: PropTypes.number,
className: PropTypes.string
};
UserAvatar.defaultProps = {
name: 'John Doe',
borderRadius: '100%',
colors: defaultColors,
textSizeRatio: 3
};
module.exports = UserAvatar;