Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Avatar to TypeScript #984

Merged
merged 7 commits into from
Feb 4, 2021
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
5 changes: 5 additions & 0 deletions .changeset/spotty-wombats-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `Avatar` to TypeScript
25 changes: 16 additions & 9 deletions src/Avatar.js → src/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import PropTypes from 'prop-types'
import styled from 'styled-components'
import {get, COMMON} from './constants'
import {COMMON, get, SystemCommonProps} from './constants'
import sx, {SxProp} from './sx'
import theme from './theme'
import sx from './sx'
import {ComponentProps} from './utils/types'

function getBorderRadius(props) {
if (props.square) {
return props.size <= 24 ? '4px' : '6px'
type StyledAvatarProps = {
size?: number
square?: boolean
} & SystemCommonProps &
SxProp

function getBorderRadius({size, square}: StyledAvatarProps) {
if (square) {
return size && size <= 24 ? '4px' : '6px'
} else {
return '50%'
}
}

const Avatar = styled.img.attrs(props => ({
const Avatar = styled.img.attrs<StyledAvatarProps>(props => ({
height: props.size,
width: props.size,
alt: props.alt
}))`
width: props.size
}))<StyledAvatarProps>`
display: inline-block;
overflow: hidden; // Ensure page layout in Firefox should images fail to load
line-height: ${get('lineHeights.condensedUltra')};
Expand All @@ -41,4 +47,5 @@ Avatar.propTypes = {
theme: PropTypes.object
}

export type AvatarProps = ComponentProps<typeof Avatar>
export default Avatar
8 changes: 2 additions & 6 deletions src/__tests__/Avatar.js → src/__tests__/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {px, render, percent, behavesAsComponent, checkExports} from '../utils/te
import {render as HTMLRender, cleanup} from '@testing-library/react'
import {axe, toHaveNoViolations} from 'jest-axe'
import 'babel-polyfill'
import systemPropTypes from '@styled-system/prop-types'
import {COMMON} from '../constants'
expect.extend(toHaveNoViolations)

describe('Avatar', () => {
behavesAsComponent(Avatar, [{propTypes: systemPropTypes.space}])
behavesAsComponent(Avatar, [COMMON])

checkExports('Avatar', {
default: Avatar
Expand Down Expand Up @@ -42,8 +42,4 @@ 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))
})
})