-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds Avatar image display component (#172)
- Loading branch information
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { classify } from "utility/classify"; | ||
|
||
export const sizes = { | ||
sm: "sm", | ||
md: "md", | ||
}; | ||
|
||
/** | ||
* Circular image display component for a user avatar (Note: native img element props like `alt` are passed through) | ||
**/ | ||
export const Avatar = ({ className, src, size, ...props }) => { | ||
return ( | ||
<img | ||
src={src} | ||
className={classify("mainsail-avatar", size, className)} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
Avatar.propTypes = { | ||
/** Url path to image */ | ||
src: PropTypes.string.isRequired, | ||
/** Size of the image */ | ||
size: PropTypes.string, | ||
/** Style class to add to img */ | ||
className: PropTypes.string, | ||
}; | ||
|
||
Avatar.defaultProps = { | ||
size: sizes.md, | ||
}; | ||
|
||
Avatar.sizes = sizes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** Avatar Component Styles */ | ||
img.mainsail-avatar { | ||
border-radius: 50%; | ||
padding: 0; | ||
object-fit: cover; | ||
background: transparent; | ||
width: 64px; | ||
height: 64px; | ||
box-shadow: $mainsail-shadow-near; | ||
|
||
&.md { | ||
width: 64px; | ||
height: 64px; | ||
} | ||
|
||
&.sm { | ||
width: 32px; | ||
height: 32px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from "react"; | ||
|
||
import { Avatar } from "./Avatar"; | ||
|
||
export default { | ||
title: "Elements/Avatar", | ||
component: Avatar, | ||
argTypes: { | ||
size: { | ||
name: "size", | ||
type: { size: "string" }, | ||
control: { | ||
type: "select", | ||
}, | ||
options: Object.values(Avatar.sizes), | ||
}, | ||
}, | ||
}; | ||
|
||
const Template = (args) => <Avatar {...args} />; | ||
|
||
export const MediumAvatar = Template.bind({}); | ||
MediumAvatar.args = { | ||
size: "md", | ||
alt: "Medium Image", | ||
src: "https://i.pravatar.cc/300?u=fake1266783426679@pravatar.com", | ||
}; | ||
|
||
export const SmallAvatar = Template.bind({}); | ||
SmallAvatar.args = { | ||
size: "sm", | ||
alt: "Small Image", | ||
src: "https://i.pravatar.cc/300?u=abc1234", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Avatar.test.js | ||
|
||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import "@testing-library/jest-dom/extend-expect"; | ||
|
||
import { MediumAvatar, SmallAvatar } from "./Avatar.stories"; | ||
|
||
it("renders the avatar in the medium size", () => { | ||
render(<MediumAvatar {...MediumAvatar.args} />); | ||
expect(screen.getByRole("img")).toBeInTheDocument(); | ||
expect(screen.getByRole("img").classList.contains("md")).toBeTruthy(); | ||
}); | ||
|
||
it("renders the avatar in the small size", () => { | ||
render(<SmallAvatar {...SmallAvatar.args} />); | ||
expect(screen.getByRole("img")).toBeInTheDocument(); | ||
expect(screen.getByRole("img").classList.contains("sm")).toBeTruthy(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Avatar"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters