Skip to content

Commit

Permalink
feat(atoms): adds avatar component
Browse files Browse the repository at this point in the history
Adds a simple avatar element for styles

closes #139
  • Loading branch information
anguspiv committed Nov 15, 2022
1 parent fc51e5b commit 9ef1172
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
18 changes: 16 additions & 2 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import createCache from '@emotion/cache';
import { CacheProvider, Global, ThemeProvider } from '@emotion/react';
import * as NextImage from 'next/image';
import { global } from '@styles/global';
import { theme } from '@styles/theme';

const cache = createCache({ key: 'css', prepend: true });

const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, 'default', {
configurable: true,
value: (props) => (
<OriginalNextImage
{...props}
unoptimized
blurDataURL="data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAADAAQDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAbEAADAAMBAQAAAAAAAAAAAAABAgMABAURUf/EABUBAQEAAAAAAAAAAAAAAAAAAAMF/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAECEf/aAAwDAQACEQMRAD8Anz9voy1dCI2mectSE5ioFCqia+KCwJ8HzGMZPqJb1oPEf//Z"
/>
),
});

export const decorators = [
(Story) => {
return (
Expand All @@ -19,11 +33,11 @@ export const decorators = [
];

export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
};
24 changes: 24 additions & 0 deletions src/components/atoms/Avatar/Avatar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Image from 'next/image';
import PropTypes from 'prop-types';
import { frame, image } from './styles';

export function Avatar({ className, width, height, ...props }) {
return (
<div data-testid="avatar" className={className} css={frame}>
<Image {...props} layout="fill" css={image} />
</div>
);
}

Avatar.propTypes = {
...Image.propTypes,
src: PropTypes.oneOfType([PropTypes.string, PropTypes.shape]).isRequired,
className: PropTypes.string,
};

Avatar.defaultProps = {
...Image.defaultProps,
className: '',
};

export default Avatar;
16 changes: 16 additions & 0 deletions src/components/atoms/Avatar/Avatar.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Avatar } from './Avatar';

export default {
component: Avatar,
title: 'Components/Atoms/Avatar',
};

const Template = (args) => <Avatar {...args} />;

export const Default = Template.bind({});

Default.args = {
src: 'https://picsum.photos/200',
alt: 'Example Name',
width: 100,
};
19 changes: 19 additions & 0 deletions src/components/atoms/Avatar/Avatar.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { render, screen } from '@testing-library/react';
import { Avatar } from './Avatar';

describe('<Avatar />', () => {
it('should render an img', () => {
expect.assertions(3);

const src = 'https://example.com/images/profile.png';
const alt = 'Example Name';

render(<Avatar src={src} alt={alt} className="test-class" />);

expect(screen.getByRole('img')).toHaveAttribute('src');

expect(screen.getByRole('img')).toHaveAttribute('alt', 'Example Name');

expect(screen.getByTestId('avatar')).toHaveClass('test-class');
});
});
5 changes: 5 additions & 0 deletions src/components/atoms/Avatar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Avatar } from './Avatar';

export * from './Avatar';

export default Avatar;
15 changes: 15 additions & 0 deletions src/components/atoms/Avatar/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { css } from '@emotion/react';

export const frame = css`
position: relative;
overflow: hidden;
border-radius: 50%;
box-shadow: var(--elevation-medium);
width: 100%;
aspect-ratio: 1 / 1;
`;

export const image = css`
overflow: hidden;
border-radius: 50%;
`;

0 comments on commit 9ef1172

Please sign in to comment.