Skip to content

Commit 8986b0b

Browse files
committed
Adds Avatar component
1 parent bd01125 commit 8986b0b

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ configurations for [ESLint](https://eslint.org/);
4848
- [**Webpack Configurations**](docs/webpack-config.md) — Standard configurations for [Webpack](https://webpack.js.org/).
4949

5050
### <a name="components">Components</a>
51+
- [**`Avatar`**](docs/avatar.md) &mdash; The standard component for user avatars;
5152
- [**`Button`**](docs/button.md) &mdash; Handles buttons and button-like links
5253
(components that look like regular buttons, but behave as links) in the same
5354
uniform manner;

docs/avatar.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Avatar
2+
The standard component for user avatars.
3+
4+
[Example](#example)
5+
6+
**`Why?`** &mdash; User avatars are usual elements in the applications we
7+
create. The component provided by this package separates avatar styling from
8+
its logic, and ensures that it works the same across our projects.
9+
10+
**Accepted Props:**
11+
- **`DefaultAvatar`** &mdash; *ReactJS Node* &mdash; ReactJS component to render
12+
as a default avatar image;
13+
- **`theme`** &mdash; *Object* &mdash; `react-css-super-themr` map to apply to
14+
the `Avatar`. At the moment, only one field is accepted:
15+
- **`avatar`** &mdash; *String* &mdash; CSS class to apply to the rendered
16+
avatar;
17+
- **`url`** &mdash; *String* &mdash; Optional. URL of the image to render as
18+
the user's avatar. When not provided, the `DefaultAvatar` will be rendered
19+
instead. Defaults to *null*.
20+
21+
### Example
22+
First of all, you should wrap the `Avatar` into your own theme and provide
23+
the default avatar image, e.g.:
24+
```scss
25+
// theme.scss
26+
27+
.avatar {
28+
border-radius: 16px;
29+
height: 32px;
30+
width: 32px;
31+
}
32+
```
33+
```jsx
34+
// MyThemedAvatar.jsx
35+
36+
import React from 'react';
37+
import { Avatar } from 'topcoder-react-utils';
38+
import { themr } from 'react-css-super-themr';
39+
40+
import DefaultAvatarImage from './default-avatar-image.svg';
41+
import theme from './theme.scss';
42+
43+
/* Note that injection of "props" after "DefaultAvatar" props will allow
44+
* to override the default image for specific instances, should you need to. */
45+
export themr('MyThemedAvatar', theme)(props =>
46+
<Avatar DefaultAvatar={DefaultAvatarImage} {...props} />);
47+
```
48+
49+
Now, in your code you can render your themed avatar as:
50+
```scss
51+
// style.scss
52+
53+
/* To demonstrate ad-hoc styling, say you need larger size of the avatar
54+
* in some specific place. */
55+
.largeAvatar {
56+
border-radius: 128px;
57+
height: 256px;
58+
width: 256px;
59+
}
60+
```
61+
62+
```jsx
63+
// Example.jsx
64+
65+
import React from 'react';
66+
import Avatar from './MyThemedAvatar';
67+
68+
/* To demonstrate ad-hoc styling. */
69+
import style from './style.scss';
70+
71+
export default function Example() {
72+
return (
73+
<div>
74+
{/* Renders default avatar. */}
75+
<Avatar />
76+
77+
{/* Renders the specified avatar image. */}
78+
<Avatar url="url/of/the/user/avatar/image.jpg" />
79+
80+
{/* A large avatar. */}
81+
<Avatar theme={{ avatar: style.largeAvatar }} />
82+
</div>
83+
);
84+
}
85+
```

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Avatar from 'components/Avatar';
12
import Button from 'components/Button';
23
import Link from 'components/Link';
34
import NavLink from 'components/NavLink';
@@ -6,6 +7,7 @@ import ScalableRect from 'components/ScalableRect';
67
import 'styles/global.scss';
78

89
export {
10+
Avatar,
911
Button,
1012
Link,
1113
NavLink,

src/shared/components/Avatar.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* The standard Avatar component.
3+
* To use it you will have to properly wrap it with react-css-super-themr,
4+
* the component provided here takes care about the code interface and logic
5+
* only.
6+
*/
7+
8+
import PT from 'prop-types';
9+
import React from 'react';
10+
11+
function Avatar({ DefaultAvatar, theme, url }) {
12+
return url
13+
? <img alt="Avatar" src={url} className={theme.avatar} />
14+
: <DefaultAvatar className={theme.avatar} />;
15+
}
16+
17+
Avatar.defaultProps = {
18+
url: null,
19+
};
20+
21+
Avatar.propTypes = {
22+
DefaultAvatar: PT.node.isRequired,
23+
theme: PT.shape({
24+
avatar: PT.string.isRequired,
25+
}).isRequired,
26+
url: PT.string,
27+
};

0 commit comments

Comments
 (0)