Skip to content

Commit

Permalink
Updating AvatarBase to use Text component instead of Box and adding f…
Browse files Browse the repository at this point in the history
…ont size logic based on avatar size
  • Loading branch information
georgewrmarshall committed Mar 16, 2023
1 parent d45c4ed commit 2a70217
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 54 deletions.
2 changes: 2 additions & 0 deletions ui/components/component-library/avatar-base/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Possible sizes include:

Defaults to `md`

The font-size of the `AvatarBase` is based on the size prop to override the font-size use the `variant` prop.

<Canvas>
<Story id="components-componentlibrary-avatarbase--size" />
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarBase should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md box--flex-direction-row box--color-text-default box--background-color-background-alternative box--border-color-border-default box--border-style-solid box--border-width-1"
class="box mm-text mm-avatar-base mm-avatar-base--size-md mm-text--body-sm mm-text--text-transform-uppercase mm-text--color-text-default box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--background-color-background-alternative box--rounded-full box--border-color-border-default box--border-style-solid box--border-width-1"
data-testid="avatar-base"
/>
</div>
Expand Down
56 changes: 40 additions & 16 deletions ui/components/component-library/avatar-base/avatar-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';

import Box from '../../ui/box/box';
import {
BackgroundColor,
BorderColor,
TextColor,
DISPLAY,
JustifyContent,
AlignItems,
BorderRadius,
TextVariant,
TEXT_TRANSFORM,
} from '../../../helpers/constants/design-system';

import { Text } from '../text';

import { AVATAR_BASE_SIZES } from './avatar-base.constants';

export const AvatarBase = ({
Expand All @@ -18,19 +26,36 @@ export const AvatarBase = ({
color = TextColor.textDefault,
className,
...props
}) => (
<Box
className={classnames(
'mm-avatar-base',
`mm-avatar-base--size-${size}`,
className,
)}
{...{ backgroundColor, borderColor, color, ...props }}
>
{children}
</Box>
);
}) => {
let fallbackTextVariant;

if (size === AVATAR_BASE_SIZES.LG || size === AVATAR_BASE_SIZES.XL) {
fallbackTextVariant = TextVariant.bodyLgMedium;
} else if (size === AVATAR_BASE_SIZES.SM || size === AVATAR_BASE_SIZES.MD) {
fallbackTextVariant = TextVariant.bodySm;
} else {
fallbackTextVariant = TextVariant.bodyXs;
}
return (
<Text
className={classnames(
'mm-avatar-base',
`mm-avatar-base--size-${size}`,
className,
)}
as="div"
display={DISPLAY.FLEX}
justifyContent={JustifyContent.center}
alignItems={AlignItems.center}
borderRadius={BorderRadius.full}
variant={fallbackTextVariant}
textTransform={TEXT_TRANSFORM.UPPERCASE}
{...{ backgroundColor, borderColor, color, ...props }}
>
{children}
</Text>
);
};
AvatarBase.propTypes = {
/**
* The size of the AvatarBase.
Expand Down Expand Up @@ -62,8 +87,7 @@ AvatarBase.propTypes = {
*/
className: PropTypes.string,
/**
* AvatarBase also accepts all Box props including but not limited to
* className, as(change root element of HTML element) and margin props
* AvatarBase also accepts all Text props including variant and all Box props
*/
...Box.propTypes,
...Text.propTypes,
};
5 changes: 0 additions & 5 deletions ui/components/component-library/avatar-base/avatar-base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@
width: var(--avatar-size);
max-width: var(--avatar-size);
flex: 0 0 var(--avatar-size);
border-radius: 100%;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
text-transform: uppercase;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ export default {
color: TextColor.textDefault,
backgroundColor: BackgroundColor.backgroundAlternative,
borderColor: BorderColor.borderDefault,
children: 'B',
},
};

export const DefaultStory = (args) => <AvatarBase {...args}>B</AvatarBase>;
export const DefaultStory = (args) => <AvatarBase {...args} />;

DefaultStory.storyName = 'Default';

Expand Down
32 changes: 16 additions & 16 deletions ui/components/component-library/avatar-base/avatar-base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { Color } from '../../../helpers/constants/design-system';
import { Color, TextColor } from '../../../helpers/constants/design-system';

import { AvatarBase } from './avatar-base';

Expand All @@ -25,19 +25,19 @@ describe('AvatarBase', () => {
</>,
);
expect(getByTestId('avatar-base-xs')).toHaveClass(
'mm-avatar-base--size-xs',
'mm-avatar-base--size-xs mm-text--body-xs',
);
expect(getByTestId('avatar-base-sm')).toHaveClass(
'mm-avatar-base--size-sm',
'mm-avatar-base--size-sm mm-text--body-sm',
);
expect(getByTestId('avatar-base-md')).toHaveClass(
'mm-avatar-base--size-md',
'mm-avatar-base--size-md mm-text--body-sm',
);
expect(getByTestId('avatar-base-lg')).toHaveClass(
'mm-avatar-base--size-lg',
'mm-avatar-base--size-lg mm-text--body-lg-medium',
);
expect(getByTestId('avatar-base-xl')).toHaveClass(
'mm-avatar-base--size-xl',
'mm-avatar-base--size-xl mm-text--body-lg-medium',
);
});
// className
Expand All @@ -63,32 +63,32 @@ describe('AvatarBase', () => {
const { getByTestId } = render(
<>
<AvatarBase
color={Color.successDefault}
data-testid={Color.successDefault}
color={TextColor.successDefault}
data-testid={TextColor.successDefault}
/>
<AvatarBase
color={Color.errorDefault}
data-testid={Color.errorDefault}
color={TextColor.errorDefault}
data-testid={TextColor.errorDefault}
/>
</>,
);
expect(getByTestId(Color.successDefault)).toHaveClass(
`box--color-${Color.successDefault}`,
expect(getByTestId(TextColor.successDefault)).toHaveClass(
`mm-text--color-${TextColor.successDefault}`,
);
expect(getByTestId(Color.errorDefault)).toHaveClass(
`box--color-${Color.errorDefault}`,
expect(getByTestId(TextColor.errorDefault)).toHaveClass(
`mm-text--color-${TextColor.errorDefault}`,
);
});
// background color
it('should render with different background colors', () => {
const { getByTestId } = render(
<>
<AvatarBase
backgroundColor={Color.successDefault}
backgroundColor={TextColor.successDefault}
data-testid={Color.successDefault}
/>
<AvatarBase
backgroundColor={Color.errorDefault}
backgroundColor={TextColor.errorDefault}
data-testid={Color.errorDefault}
/>
</>,
Expand Down
4 changes: 4 additions & 0 deletions ui/components/component-library/avatar-network/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Possible sizes include:

Defaults to `md`

The fallback display of the `AvatarNetwork` is a circle with the initial letter of the network name. The size of the initial letter is calculated based on the size of the `AvatarNetwork` component.

<Canvas>
<Story id="components-componentlibrary-avatarnetwork--size-story" />
</Canvas>
Expand All @@ -48,6 +50,8 @@ import { AvatarNetwork } from '../../component-library';

Use the `name` prop to set the initial letter of the `AvatarNetwork`. This will be used as the fallback display if no image url is passed to the `src` prop.

Use the `fallbackTextProps` to access the `Text` component props of the fallback text.

<Canvas>
<Story id="components-componentlibrary-avatarnetwork--name" />
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
import Box from '../../ui/box/box';

import README from './README.mdx';

import { AvatarNetwork } from './avatar-network';
import { AVATAR_NETWORK_SIZES } from './avatar-network.constants';

export default {
title: 'Components/ComponentLibrary/AvatarNetwork',

component: AvatarNetwork,
parameters: {
docs: {
Expand Down Expand Up @@ -67,13 +67,27 @@ export const DefaultStory = Template.bind({});
DefaultStory.storyName = 'Default';

export const SizeStory = (args) => (
<Box display={DISPLAY.FLEX} alignItems={AlignItems.baseline} gap={1}>
<AvatarNetwork {...args} size={Size.XS} />
<AvatarNetwork {...args} size={Size.SM} />
<AvatarNetwork {...args} size={Size.MD} />
<AvatarNetwork {...args} size={Size.LG} />
<AvatarNetwork {...args} size={Size.XL} />
</Box>
<>
<Box
display={DISPLAY.FLEX}
alignItems={AlignItems.flexEnd}
gap={1}
marginBottom={4}
>
<AvatarNetwork {...args} size={Size.XS} />
<AvatarNetwork {...args} size={Size.SM} />
<AvatarNetwork {...args} size={Size.MD} />
<AvatarNetwork {...args} size={Size.LG} />
<AvatarNetwork {...args} size={Size.XL} />
</Box>
<Box display={DISPLAY.FLEX} alignItems={AlignItems.flexEnd} gap={1}>
<AvatarNetwork {...args} src="" size={Size.XS} />
<AvatarNetwork {...args} src="" size={Size.SM} />
<AvatarNetwork {...args} src="" size={Size.MD} />
<AvatarNetwork {...args} src="" size={Size.LG} />
<AvatarNetwork {...args} src="" size={Size.XL} />
</Box>
</>
);
SizeStory.storyName = 'Size';

Expand Down
4 changes: 4 additions & 0 deletions ui/components/component-library/avatar-token/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Possible sizes include:

Defaults to `md`

The fallback display of the `AvatarToken` is a circle with the initial letter of the network name. The size of the initial letter is calculated based on the size of the `AvatarToken` component.

<Canvas>
<Story id="components-componentlibrary-avatartoken--size-story" />
</Canvas>
Expand All @@ -48,6 +50,8 @@ import { AvatarToken } from '../../component-library';

Use the `name` prop to set the initial letter of the `AvatarToken`. This will be used as the fallback display if no image url is passed to the `src` prop.

Use the `fallbackTextProps` to access the `Text` component props of the fallback text.

<Canvas>
<Story id="components-componentlibrary-avatartoken--name" />
</Canvas>
Expand Down
Loading

0 comments on commit 2a70217

Please sign in to comment.