1 | # Avatar
|
2 | The standard component for user avatars.
|
3 |
|
4 | [Example](#example)
|
5 |
|
6 | **`Why?`** — 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`** — *ReactJS Node* — ReactJS component to render
|
12 | as a default avatar image;
|
13 | - **`theme`** — *Object* — `react-css-super-themr` map to apply to
|
14 | the `Avatar`. At the moment, only one field is accepted:
|
15 | - **`avatar`** — *String* — CSS class to apply to the rendered
|
16 | avatar;
|
17 | - **`url`** — *String* — 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 | ```
|