Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/content/Avatar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ Avatar components get `COMMON` system props. Read our [System Props](/system-pro
| isChild | Boolean | | adds the `avatar-child` class if present |
| size | Number | 20 | adds the `avatar-small` class if less than 24 |
| src | String | | The source url of the avatar image |
| shape | String | `square` | The shape of the avatar image. Can be `square` or `round` |
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ declare module '@primer/components' {
export interface AvatarProps extends CommonProps, Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'color'> {
isChild?: boolean
size?: number
shape?: 'square' | 'round'
}

export const Avatar: React.FunctionComponent<AvatarProps>
Expand Down
17 changes: 14 additions & 3 deletions src/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import {space} from 'styled-system'
import systemPropTypes from '@styled-system/prop-types'
import theme from './theme'

function borderRadius({size}) {
function borderRadiusValue({size, shape}) {
switch (shape) {
case 'round':
return '50%'
default:
return size <= 24 ? '2px' : '3px'
}
}

function borderRadius({size, shape}) {
return {
borderRadius: size <= 24 ? '2px' : '3px'
borderRadius: borderRadiusValue({size, shape})
}
}

Expand All @@ -27,14 +36,16 @@ const Avatar = styled.img.attrs(props => ({
Avatar.defaultProps = {
theme,
size: 20,
alt: ''
alt: '',
shape: 'square'
}

Avatar.propTypes = {
alt: PropTypes.string.isRequired,
size: PropTypes.number,
src: PropTypes.string,
...systemPropTypes.space,
shape: PropTypes.PropTypes.oneOf(['square', 'round']),
theme: PropTypes.object
}

Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import Avatar from '../Avatar'
import theme from '../theme'
import {px, render} from '../utils/testing'
import {px, render, percent} from '../utils/testing'
import {render as HTMLRender, cleanup} from '@testing-library/react'
import {axe, toHaveNoViolations} from 'jest-axe'
import 'babel-polyfill'
Expand Down Expand Up @@ -43,4 +43,8 @@ describe('Avatar', () => {
it('respects margin props', () => {
expect(render(<Avatar m={2} alt="" />)).toHaveStyleRule('margin', px(theme.space[2]))
})

it('respects shape prop', () => {
expect(render(<Avatar shape="round" alt="" />)).toHaveStyleRule('border-radius', percent(50))
})
})
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/Avatar.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`Avatar renders default props 1`] = `
alt=""
className="c0"
height={20}
shape="square"
size={20}
width={20}
/>
Expand Down
4 changes: 4 additions & 0 deletions src/utils/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export function px(value) {
return typeof value === 'number' ? `${value}px` : value
}

export function percent(value) {
return typeof value === 'number' ? `${value}%` : value
}

export function renderStyles(node) {
const {
props: {className}
Expand Down