Skip to content

Commit

Permalink
adds Avatar image display component (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
3CordGuy authored Sep 21, 2021
1 parent d775fd7 commit f40d93e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/components/Avatar/Avatar.js
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;
20 changes: 20 additions & 0 deletions src/components/Avatar/Avatar.scss
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;
}
}
34 changes: 34 additions & 0 deletions src/components/Avatar/Avatar.stories.js
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",
};
20 changes: 20 additions & 0 deletions src/components/Avatar/Avatar.test.js
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();
});
1 change: 1 addition & 0 deletions src/components/Avatar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Avatar";
1 change: 1 addition & 0 deletions src/components/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import "./index.scss";

// Components Global Export for plopjs
export { Avatar } from "../Avatar";
export { SearchInput } from "../SearchInput";
export { ProgressBar } from "../ProgressBar";
export { Switcher, useSwitcher } from "../Switcher";
Expand Down
1 change: 1 addition & 0 deletions src/components/core/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import "../../styles/Text.scss";

// SASS Global Import for plopjs
@import "../Avatar/Avatar.scss";
@import "../SearchInput/SearchInput.scss";
@import "../ProgressBar/ProgressBar.scss";
@import "../Switcher/Switcher.scss";
Expand Down

0 comments on commit f40d93e

Please # to comment.