-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
106 lines (97 loc) · 2.66 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
104
105
106
import React, { Component } from 'react'
import { View, Text } from 'react-native'
import initials from 'initials'
// Copied from https://stackoverflow.com/a/1026087
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
class TextAvatar extends Component {
constructor(props) {
super(props)
}
render() {
let {
children,
size = 60,
backgroundColor = '#333',
textColor = '#fff',
type,
style
} = this.props
if (typeof children !== 'string') throw new Error('Children must be only a string \n Ex: Technology')
let abbr = initials(capitalizeFirstLetter(children))
if (typeof size !== 'number') throw new Error('Size must be an integer')
let containerStyle = {
width: size,
height: size,
backgroundColor: backgroundColor,
alignItems: 'center',
justifyContent: 'center',
}
let textStyle = {
color: textColor,
fontSize: size / 3.14,
fontWeight: 'bold',
letterSpacing: 1
}
// Hexagon style inspired from https://codedaily.io/tutorials/22/The-Shapes-of-React-Native
let hexagon = {
height: size / 2
}
let hexagonAfter = {
position: 'absolute',
bottom: - size / 3,
left: 0,
width: 0,
height: 0,
borderStyle: 'solid',
borderLeftWidth: size / 2,
borderLeftColor: 'transparent',
borderRightWidth: size / 2,
borderRightColor: 'transparent',
borderTopWidth: size / 3,
borderTopColor: backgroundColor
}
let hexagonBefore = {
position: 'absolute',
top: -size / 3,
left: 0,
width: 0,
height: 0,
borderStyle: 'solid',
borderLeftWidth: size / 2,
borderLeftColor: 'transparent',
borderRightWidth: size / 2,
borderRightColor: 'transparent',
borderBottomWidth: size / 3,
borderBottomColor: backgroundColor
}
if (type == 'circle'){
containerStyle.borderRadius = size / 2
return (
<View style={[ style , containerStyle]}>
<Text style={textStyle}
adjustsFontSizeToFit={true}>{abbr}</Text>
</View>
);
}
else if(type == 'hexagon'){
return (
<View style={[ style, containerStyle, hexagon ]}>
<View style={hexagonBefore} />
<View style={hexagonAfter} />
<Text style={textStyle}>{abbr}</Text>
</View>
);
}
else {
return (
<View style={[ style , containerStyle]}>
<Text style={textStyle}
adjustsFontSizeToFit={true}>{abbr}</Text>
</View>
);
}
}
}
export default TextAvatar;