Skip to content

Commit b5aa123

Browse files
authored
Merge pull request #30 from arturbien/avatar
added Avatar component
2 parents 7cfb670 + bb8b18b commit b5aa123

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

src/components/Avatar/Avatar.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from "react";
2+
import propTypes from "prop-types";
3+
import styled from "styled-components";
4+
import { blockSizes } from "../common/system";
5+
6+
const StyledAvatar = styled.div`
7+
display: inline-block;
8+
box-sizing: border-box;
9+
object-fit: contain;
10+
height: calc(${props => blockSizes.md} - 2px);
11+
width: calc(${props => blockSizes.md} - 2px);
12+
border-radius: ${({ square }) => (square ? 0 : "50%")};
13+
overflow: hidden;
14+
${({ noBorder, theme }) =>
15+
!noBorder &&
16+
`
17+
border-top: 2px solid ${theme.borderDark};
18+
border-left: 2px solid ${theme.borderDark};
19+
border-bottom: 2px solid ${theme.borderLightest};
20+
border-right: 2px solid ${theme.borderLightest};
21+
background: ${theme.material};
22+
`}
23+
${({ src }) =>
24+
!src &&
25+
`
26+
display: flex;
27+
align-items: center;
28+
justify-content: space-around;
29+
font-weight: bold;
30+
font-size: 1rem;
31+
`}
32+
`;
33+
const SlyledAvatarIMG = styled.img`
34+
display: block;
35+
object-fit: contain;
36+
width: 100%;
37+
height: 100%;
38+
`;
39+
// src takes priority over children
40+
41+
const Avatar = ({ children, noBorder, square, src, alt, ...otherProps }) => {
42+
return (
43+
<StyledAvatar noBorder={noBorder} square={square} {...otherProps}>
44+
{src ? <SlyledAvatarIMG src={src} alt={alt} /> : children}
45+
</StyledAvatar>
46+
);
47+
};
48+
49+
Avatar.defaultProps = {
50+
square: false,
51+
noBorder: false,
52+
src: undefined,
53+
children: null,
54+
alt: ""
55+
};
56+
Avatar.propTypes = {
57+
square: propTypes.bool,
58+
noBorder: propTypes.bool,
59+
children: propTypes.node,
60+
src: propTypes.string,
61+
alt: propTypes.string
62+
};
63+
export default Avatar;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from "react";
2+
import { storiesOf } from "@storybook/react";
3+
4+
import Avatar from "./Avatar";
5+
import AppBar from "../AppBar/AppBar";
6+
import Toolbar from "../Toolbar/Toolbar";
7+
import Button from "../Button/Button";
8+
9+
storiesOf("Avatar", module)
10+
.addDecorator(story => (
11+
<div
12+
style={{
13+
padding: "5rem",
14+
background: "teal"
15+
}}
16+
>
17+
{story()}
18+
</div>
19+
))
20+
.add("default", () => (
21+
<AppBar>
22+
<Toolbar
23+
style={{ justifyContent: "space-between", paddingRight: "1rem" }}
24+
>
25+
<Button flat accent>
26+
Menu
27+
</Button>
28+
<Avatar src="https://sphoto.nasza-klasa.pl/33278012/1/square/2658174fbd.jpeg?v=1" />
29+
</Toolbar>
30+
</AppBar>
31+
))
32+
.add("noBorder", () => (
33+
<AppBar>
34+
<Toolbar
35+
style={{ justifyContent: "space-between", paddingRight: "1rem" }}
36+
>
37+
<Button flat accent>
38+
Menu
39+
</Button>
40+
<Avatar
41+
src="https://sphoto.nasza-klasa.pl/33278012/1/square/2658174fbd.jpeg?v=1"
42+
noBorder
43+
/>
44+
</Toolbar>
45+
</AppBar>
46+
))
47+
.add("letter", () => (
48+
<AppBar>
49+
<Toolbar
50+
style={{ justifyContent: "space-between", paddingRight: "1rem" }}
51+
>
52+
<Button flat accent>
53+
Menu
54+
</Button>
55+
<Avatar style={{ background: "palevioletred" }}>AK</Avatar>
56+
</Toolbar>
57+
</AppBar>
58+
))
59+
.add("square", () => (
60+
<AppBar>
61+
<Toolbar
62+
style={{ justifyContent: "space-between", paddingRight: "1rem" }}
63+
>
64+
<Button flat accent>
65+
Menu
66+
</Button>
67+
<Avatar square>🚀</Avatar>
68+
</Toolbar>
69+
</AppBar>
70+
));

src/components/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import reset from "./common/reset";
33

44
import Anchor from "./Anchor/Anchor";
55
import AppBar from "./AppBar/AppBar";
6+
import Avatar from "./Avatar/Avatar";
67
import Bar from "./Bar/Bar";
78
import Button from "./Button/Button";
89
import Checkbox from "./Checkbox/Checkbox";
@@ -42,6 +43,7 @@ export {
4243
reset,
4344
Anchor,
4445
AppBar,
46+
Avatar,
4547
Bar,
4648
Button,
4749
Checkbox,

0 commit comments

Comments
 (0)