Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/@next/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';
import { BaseContainer } from '../../Layout/GlintsContainer/GlintsContainer';
import { Avatar, AvatarProps } from './Avatar';

export default {
title: '@next/Avatar',
component: Avatar,
decorators: [Story => <BaseContainer>{Story()}</BaseContainer>],
argTypes: {},
} as Meta;

const Template: Story<AvatarProps> = args => <Avatar {...args} />;

export const Interactive = Template.bind({});
Interactive.args = {
initials: 'HE',
};
44 changes: 44 additions & 0 deletions src/@next/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { Typography } from '../Typography';
import { Neutral } from '../utilities/colors';
import { AvatarStyle } from './AvatarStyle';

const avatarBackgroundColorVariant = [
'supplementary',
'warning',
'danger',
'success',
'outstanding',
] as const;

const avatarSizeVariant = ['large', 'medium'] as const;

export type AvatarBackgroundColorVariant =
typeof avatarBackgroundColorVariant[number];

export type AvatarSizeVariant = typeof avatarSizeVariant[number];

export interface AvatarProps extends React.HTMLAttributes<HTMLDivElement> {
variant?: AvatarBackgroundColorVariant;
size?: AvatarSizeVariant;
initials?: string;
}

export const Avatar = ({
variant = 'supplementary',
size = 'medium',
initials,
...props
}: AvatarProps) => {
return (
<AvatarStyle variant={variant} size={size} {...props}>
<Typography
Comment thread
ninariccimarie marked this conversation as resolved.
variant={size === 'medium' ? 'body1' : 'headline6'}
color={Neutral.B18}
as={'span'}
>
{initials.slice(0, 2)}
</Typography>
</AvatarStyle>
);
};
83 changes: 83 additions & 0 deletions src/@next/Avatar/AvatarStyle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import styled from 'styled-components';
import * as Breakpoints from '../utilities/breakpoints';
import { Blue, Green, Neutral, Orange, Red } from '../utilities/colors';
import {
AvatarBackgroundColorVariant,
AvatarProps,
AvatarSizeVariant,
} from './Avatar';

const mediumAvatarSizeStyle = `
width: 40px;
height: 40px;

@media (max-width: ${Breakpoints.large}) {
width: 32px;
height: 32px;
}
`;

const largeAvatarSizeStyle = `
width: 60px;
height: 60px;

@media (max-width: ${Breakpoints.large}) {
width: 40px;
height: 40px;
}
`;

const avatarBackgroundColor: {
[variant in AvatarBackgroundColorVariant]: string;
} = {
['supplementary']: Neutral.B99,
['warning']: Orange.S42,
['danger']: Red.B100,
['success']: Green.B89,
['outstanding']: Blue.S08,
};

const avatarSizeVariant: {
[sizeVariant in AvatarSizeVariant]: string;
} = {
['large']: largeAvatarSizeStyle,
['medium']: mediumAvatarSizeStyle,
};

const getAvatarBackgroundColor = (variant: AvatarBackgroundColorVariant) => {
if (!(variant in avatarBackgroundColor)) {
console.warn(
`${variant} is not a valid variant for the background color, default will be used`
);
return avatarBackgroundColor['supplementary'];
}

return avatarBackgroundColor[variant];
};

const getAvatarSizeStyle = (sizeVariant: AvatarSizeVariant) => {
if (!(sizeVariant in avatarSizeVariant)) {
console.warn(
`${sizeVariant} is not a valid variant for the size, default will be used`
);

return avatarSizeVariant['medium'];
}

return avatarSizeVariant[sizeVariant];
};

export const AvatarStyle = styled.div<AvatarProps>`
${({ size }: AvatarProps) => {
return getAvatarSizeStyle(size);
}}}

${({ variant }: AvatarProps) => ({
'background-color': getAvatarBackgroundColor(variant),
})}}

display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
`;
1 change: 1 addition & 0 deletions src/@next/Avatar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Avatar';
1 change: 1 addition & 0 deletions src/@next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { ButtonGroup } from './ButtonGroup';
export { Icon } from './Icon';
export { Tab, TabProps, Tabs, TabsProps } from './Tabs';
export { Typography } from './Typography';
export { Avatar } from './Avatar';

// Utilities
import * as BorderRadius from './utilities/borderRadius';
Expand Down
90 changes: 90 additions & 0 deletions test/e2e/avatar/avatar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expect, Page, test } from '@playwright/test';
import { StoryBookPage } from '../storybookPage';

const getPage = (page: Page) =>
new StoryBookPage(page, '?path=/story/next-avatar--interactive');

const testData = [
{
testName: 'Avatar',
args: '',
screenshotResult: 'avatar',
},
{
testName: 'Avatar - supplementary',
args: 'variant:supplementary',
screenshotResult: 'avatar-supplementary',
},
{
testName: 'Avatar - warning',
args: 'variant:warning',
screenshotResult: 'avatar-warning',
},
{
testName: 'Avatar - danger',
args: 'variant:danger',
screenshotResult: 'avatar-danger',
},
{
testName: 'Avatar - success',
args: 'variant:success',
screenshotResult: 'avatar-success',
},
{
testName: 'Avatar - outstanding',
args: 'variant:outstanding',
screenshotResult: 'avatar-outstanding',
},
];

for (const [, testRecord] of testData.entries()) {
test(`${testRecord.testName} - large`, async ({ page }) => {
const avatarPage = getPage(page);
await avatarPage.goto(
`args=${testRecord.args};size:large` as `args=${string}`
);
await expect(avatarPage.container).toHaveScreenshot(
`${testRecord.screenshotResult}-large.png`
);
});
}

for (const [, testRecord] of testData.entries()) {
test(`${testRecord.testName} - medium`, async ({ page }) => {
const avatarPage = getPage(page);
await avatarPage.goto(
`args=${testRecord.args};size:medium` as `args=${string}`
);
await expect(avatarPage.container).toHaveScreenshot(
`${testRecord.screenshotResult}-medium.png`
);
});
}

test.describe('small viewport', () => {
for (const [, testRecord] of testData.entries()) {
test(`${testRecord.testName} - large`, async ({ page }) => {
page.setViewportSize({ width: 768, height: 600 });
const avatarPage = getPage(page);
await avatarPage.goto(
`args=${testRecord.args};size:large` as `args=${string}`
);
await expect(avatarPage.container).toHaveScreenshot(
`${testRecord.screenshotResult}-small-viewport-large.png`
);
});
}

for (const [, testRecord] of testData.entries()) {
test(`${testRecord.testName} - medium`, async ({ page }) => {
page.setViewportSize({ width: 768, height: 600 });
const avatarPage = getPage(page);
await avatarPage.goto(
`args=${testRecord.args};size:medium` as `args=${string}`
);
await expect(avatarPage.container).toHaveScreenshot(
`${testRecord.screenshotResult}-small-viewport-medium.png`
);
});
}
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.