Skip to content

Commit e9e42e2

Browse files
authored
Merge pull request #44 from primer/avatars
Add Avatar component
2 parents 698f8b0 + 8d1a424 commit e9e42e2

File tree

7 files changed

+122
-27
lines changed

7 files changed

+122
-27
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88

99
Currently we link to the latest built Primer CSS so that we may use current Primer styles to start to build components. This does not include `primer-base` so as to avoid unwanted base overrides.
1010

11-
## Getting started
11+
## Installation
1212

13-
`npm install`
13+
Install primer-react in your project with:
1414

15-
Run app with:
15+
`npm install primer-react`
1616

17-
`npm run start`
17+
## Local Development
1818

19-
Build with:
19+
Run `primer-react` locally when adding or updating components.
2020

21-
`npm run build`
21+
Clone this repo: `$ git clone https://github.com/primer/primer-react.git`
22+
23+
Install dependencies: `npm install`
24+
25+
Run app with: `npm run start`
26+
27+
Build docs before publishing: `npm run build`
2228

2329

2430
## Principles

docs/bundle.js

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

examples/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Detail
66
} from '@compositor/kit'
77
import {
8-
Page,
8+
Avatar,
99
Box,
1010
Button,
1111
ButtonDanger,
@@ -14,12 +14,13 @@ import {
1414
ButtonLink,
1515
BranchName,
1616
CounterLabel,
17+
Flash,
1718
Heading,
1819
Label,
1920
Link,
20-
Text,
21-
Flash,
21+
Page,
2222
StateLabel,
23+
Text,
2324
Tooltip,
2425
theme
2526
} from '../src'
@@ -37,6 +38,14 @@ const Swatch = ({name, index, color, ...rest}) => (
3738
</div>
3839
)
3940

41+
const GitHubAvatar = ({username, size = 20, ...rest}) => (
42+
<Avatar
43+
src={`https://avatars.githubusercontent.com/${username}?v=3&s=${size * 2}`}
44+
size={size}
45+
{...rest}
46+
/>
47+
)
48+
4049
const Index = props => (
4150
<Page>
4251
<Library title='Primer-react'>
@@ -48,6 +57,19 @@ const Index = props => (
4857
))}
4958
</Detail>
5059
</Example>
60+
<Example name='Avatar'>
61+
<Box mb={2}>
62+
<GitHubAvatar username='primer' size={128} />
63+
</Box>
64+
<Box mb={2}>
65+
<GitHubAvatar username='github' size={64} />
66+
</Box>
67+
<Box mb={2}>
68+
<GitHubAvatar username='reactjs' size={32} />
69+
{' '}
70+
<GitHubAvatar username='npm' />
71+
</Box>
72+
</Example>
5173
<Example name='Label'>
5274
<Box mb={3}>
5375
<Label>Default label</Label>

src/Avatar.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import classnames from 'classnames'
4+
5+
const Avatar = props => {
6+
const {
7+
alt,
8+
isChild,
9+
size = 20,
10+
src,
11+
...rest
12+
} = props
13+
14+
const className = classnames(
15+
'avatar',
16+
{
17+
'avatar-small': size <= 24,
18+
'avatar-child': isChild,
19+
}
20+
)
21+
22+
return (
23+
<img
24+
className={className}
25+
alt={alt}
26+
src={src}
27+
width={size}
28+
height={size}
29+
/>
30+
)
31+
}
32+
33+
Avatar.propTypes = {
34+
alt: PropTypes.string,
35+
isChild: PropTypes.bool,
36+
size: PropTypes.number,
37+
src: PropTypes.string,
38+
}
39+
40+
export default Avatar

src/__tests__/Avatar.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
import Avatar from '../Avatar'
3+
import {render} from '../utils/testing'
4+
5+
describe('Avatar', () => {
6+
it('renders small by default', () => {
7+
expect(render(<Avatar />))
8+
.toEqual(render(<img className='avatar avatar-small' width={20} height={20} />))
9+
})
10+
11+
it('respects the size prop', () => {
12+
expect(render(<Avatar size={40} />))
13+
.toEqual(render(<img className='avatar' width={40} height={40} />))
14+
})
15+
16+
it('respects isChild', () => {
17+
expect(render(<Avatar isChild />))
18+
.toEqual(render(<img className='avatar avatar-small avatar-child' width={20} height={20} />))
19+
})
20+
21+
it('passes through the src prop', () => {
22+
expect(render(<Avatar src='primer.png' />))
23+
.toEqual(render(<img className='avatar avatar-small' src='primer.png' width={20} height={20} />))
24+
})
25+
})

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export { default as Page } from './Page'
99
export { default as Box } from './Box'
1010

1111
// Components
12+
export { default as Avatar } from './Avatar'
13+
1214
export { default as Button } from './Button'
1315
export { default as ButtonDanger } from './ButtonDanger'
1416
export { default as ButtonPrimary } from './ButtonPrimary'

0 commit comments

Comments
 (0)