Skip to content

Commit

Permalink
AvatarBase font-size logic (#18203)
Browse files Browse the repository at this point in the history
* Updating AvatarBase to use Text component instead of Box and adding font size logic based on avatar size

* Updating snaps
  • Loading branch information
georgewrmarshall authored Mar 17, 2023
1 parent c21c2bd commit 3117890
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarAccount should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md mm-avatar-account 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-avatar-account 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-account"
>
<div
Expand Down
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 text styles of the `AvatarBase` is based on the `size` prop. To override this use the `Text` component's `variant` prop. `AvatarBase` also accepts all `Text` component props.

<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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarFavicon should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md mm-avatar-favicon box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--color-text-default box--background-color-background-alternative box--border-color-transparent box--border-style-solid box--border-width-1"
class="box mm-text mm-avatar-base mm-avatar-base--size-md mm-avatar-favicon 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-transparent box--border-style-solid box--border-width-1"
data-testid="avatar-favicon"
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarIcon should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md mm-avatar-icon box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--color-primary-default box--background-color-primary-muted box--border-color-transparent box--border-style-solid box--border-width-1"
class="box mm-text mm-avatar-base mm-avatar-base--size-md mm-avatar-icon mm-text--body-sm mm-text--text-transform-uppercase mm-text--color-primary-default box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--background-color-primary-muted box--rounded-full box--border-color-transparent box--border-style-solid box--border-width-1"
data-testid="avatar-icon"
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ describe('AvatarIcon', () => {
/>,
);

expect(getByTestId('success')).toHaveClass('box--color-success-default');
expect(getByTestId('success')).toHaveClass(
'mm-text--color-success-default',
);
expect(getByTestId('success')).toHaveClass(
'box--background-color-success-muted',
);
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 `Text` component props to change the `variant`, `font-weight`, `font-family`, etc.

<Canvas>
<Story id="components-componentlibrary-avatarnetwork--name" />
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarNetwork should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md mm-avatar-network box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--color-text-default box--background-color-background-alternative box--border-color-transparent box--border-style-solid box--border-width-1"
class="box mm-text mm-avatar-base mm-avatar-base--size-md mm-avatar-network 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-transparent box--border-style-solid box--border-width-1"
data-testid="avatar-network"
>
?
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ describe('AvatarNetwork', () => {
</>,
);
expect(getByTestId(TextColor.successDefault)).toHaveClass(
`box--color-${TextColor.successDefault}`,
`mm-text--color-${TextColor.successDefault}`,
);
expect(getByTestId(TextColor.errorDefault)).toHaveClass(
`box--color-${TextColor.errorDefault}`,
`mm-text--color-${TextColor.errorDefault}`,
);
});
// background color
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 `Text` component props to change the `variant`, `font-weight`, `font-family`, etc.

<Canvas>
<Story id="components-componentlibrary-avatartoken--name" />
</Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`AvatarToken should render correctly 1`] = `
<div>
<div
class="box mm-avatar-base mm-avatar-base--size-md mm-avatar-token box--display-flex box--flex-direction-row box--justify-content-center box--align-items-center box--color-text-default box--background-color-background-alternative box--border-color-transparent box--border-style-solid box--border-width-1"
class="box mm-text mm-avatar-base mm-avatar-base--size-md mm-avatar-token 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-transparent box--border-style-solid box--border-width-1"
data-testid="avatar-token"
>
<img
Expand Down
Loading

0 comments on commit 3117890

Please sign in to comment.