forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avatar.tsx
44 lines (39 loc) · 1.17 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
import styled from 'styled-components'
import {get} from './constants'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'
type StyledAvatarProps = {
/** Sets the width and height of the avatar. */
size?: number
/** Sets the shape of the avatar to a square if true. If false, the avatar will be circular. */
square?: boolean
/** URL of the avatar image. */
src: string
/** Provide alt text when the Avatar is used without the user's name next to it. */
alt?: string
} & SxProp
function getBorderRadius({size, square}: StyledAvatarProps) {
if (square) {
return size && size <= 24 ? '4px' : '6px'
} else {
return '50%'
}
}
const Avatar = styled.img.attrs<StyledAvatarProps>(props => ({
height: props.size,
width: props.size
}))<StyledAvatarProps>`
display: inline-block;
overflow: hidden; // Ensure page layout in Firefox should images fail to load
line-height: ${get('lineHeights.condensedUltra')};
vertical-align: middle;
border-radius: ${props => getBorderRadius(props)};
${sx}
`
Avatar.defaultProps = {
size: 20,
alt: '',
square: false
}
export type AvatarProps = ComponentProps<typeof Avatar>
export default Avatar