-
Notifications
You must be signed in to change notification settings - Fork 324
/
Avatar.tsx
93 lines (86 loc) · 2.2 KB
/
Avatar.tsx
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
import styled from '@emotion/styled'
import React, {forwardRef, useState} from 'react'
import defaultUserAvatar from '../../styles/theme/images/avatar-user.svg'
import AvatarBadge from '../AvatarBadge/AvatarBadge'
type ImageBlockProps = Pick<Props, 'sansRadius' | 'sansShadow' | 'picture' | 'size' | 'onClick'>
const ImageBlock = styled('div')<ImageBlockProps>(
({sansRadius, sansShadow, picture, size, onClick}) => ({
backgroundImage: `url(${picture})`,
backgroundPosition: 'center center',
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
borderRadius: sansRadius ? 0 : '100%',
boxShadow: sansShadow ? 'none' : undefined,
cursor: onClick ? 'pointer' : 'default',
display: 'block',
flexShrink: 0,
width: size,
height: size
})
)
const BadgeBlock = styled('div')({
alignItems: 'center',
display: 'flex',
height: '25%',
justifyContent: 'center',
position: 'absolute',
right: 0,
top: 0,
width: '25%'
})
const BadgeBlockInner = styled('div')({
flexShrink: 0
})
interface Props {
className?: string
hasBadge?: boolean
isConnected?: boolean
onClick?: (e?: React.MouseEvent) => void
onMouseEnter?: () => void
onTransitionEnd?: () => void
picture: string
sansRadius?: boolean
sansShadow?: boolean
size: number
}
const Avatar = forwardRef((props: Props, ref: any) => {
const {
className,
hasBadge,
isConnected,
onClick,
onMouseEnter,
onTransitionEnd,
picture,
sansRadius,
sansShadow,
size
} = props
const [imageUrl, setImageUrl] = useState(picture || defaultUserAvatar)
const onError = () => {
setImageUrl(defaultUserAvatar)
}
return (
<ImageBlock
onTransitionEnd={onTransitionEnd}
className={className}
ref={ref}
onClick={onClick}
onMouseEnter={onMouseEnter}
sansRadius={sansRadius}
sansShadow={sansShadow}
picture={imageUrl}
size={size}
>
<img src={imageUrl} className='hidden' onError={onError} />
{hasBadge && (
<BadgeBlock>
<BadgeBlockInner>
<AvatarBadge isConnected={isConnected || false} />
</BadgeBlockInner>
</BadgeBlock>
)}
</ImageBlock>
)
})
export default Avatar