diff --git a/docs/content/Avatar.mdx b/docs/content/Avatar.mdx index 864d528cd49..1f46586b364 100644 --- a/docs/content/Avatar.mdx +++ b/docs/content/Avatar.mdx @@ -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` | diff --git a/index.d.ts b/index.d.ts index 309e1b315b7..2c8b3958b98 100644 --- a/index.d.ts +++ b/index.d.ts @@ -114,6 +114,7 @@ declare module '@primer/components' { export interface AvatarProps extends CommonProps, Omit, 'color'> { isChild?: boolean size?: number + shape?: 'square' | 'round' } export const Avatar: React.FunctionComponent diff --git a/src/Avatar.js b/src/Avatar.js index 83db587e413..d852e9f09de 100644 --- a/src/Avatar.js +++ b/src/Avatar.js @@ -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}) } } @@ -27,7 +36,8 @@ const Avatar = styled.img.attrs(props => ({ Avatar.defaultProps = { theme, size: 20, - alt: '' + alt: '', + shape: 'square' } Avatar.propTypes = { @@ -35,6 +45,7 @@ Avatar.propTypes = { size: PropTypes.number, src: PropTypes.string, ...systemPropTypes.space, + shape: PropTypes.PropTypes.oneOf(['square', 'round']), theme: PropTypes.object } diff --git a/src/__tests__/Avatar.js b/src/__tests__/Avatar.js index 7537d04367f..753a7602900 100644 --- a/src/__tests__/Avatar.js +++ b/src/__tests__/Avatar.js @@ -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' @@ -43,4 +43,8 @@ describe('Avatar', () => { it('respects margin props', () => { expect(render()).toHaveStyleRule('margin', px(theme.space[2])) }) + + it('respects shape prop', () => { + expect(render()).toHaveStyleRule('border-radius', percent(50)) + }) }) diff --git a/src/__tests__/__snapshots__/Avatar.js.snap b/src/__tests__/__snapshots__/Avatar.js.snap index 3b07d986ff9..f76b5eb14e4 100644 --- a/src/__tests__/__snapshots__/Avatar.js.snap +++ b/src/__tests__/__snapshots__/Avatar.js.snap @@ -13,6 +13,7 @@ exports[`Avatar renders default props 1`] = ` alt="" className="c0" height={20} + shape="square" size={20} width={20} /> diff --git a/src/utils/testing.js b/src/utils/testing.js index c75252c531c..b3b0ae2b054 100644 --- a/src/utils/testing.js +++ b/src/utils/testing.js @@ -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}