diff --git a/ui/components/app/add-network/add-network.js b/ui/components/app/add-network/add-network.js index 834db14cc..a44c5258d 100644 --- a/ui/components/app/add-network/add-network.js +++ b/ui/components/app/add-network/add-network.js @@ -14,7 +14,6 @@ import { BackgroundColor, TextColor, IconColor, - Size, } from '../../../helpers/constants/design-system'; import Button from '../../ui/button'; import Tooltip from '../../ui/tooltip'; @@ -40,6 +39,7 @@ import { IconName, IconSize, AvatarNetwork, + AvatarNetworkSize, Text, } from '../../component-library'; import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics'; @@ -199,7 +199,7 @@ const AddNetwork = () => { > diff --git a/ui/components/app/transaction-list-item/smart-transaction-list-item.component.js b/ui/components/app/transaction-list-item/smart-transaction-list-item.component.js index 7ea134bb9..206fa0ec3 100644 --- a/ui/components/app/transaction-list-item/smart-transaction-list-item.component.js +++ b/ui/components/app/transaction-list-item/smart-transaction-list-item.component.js @@ -18,6 +18,7 @@ import TransactionListItemDetails from '../transaction-list-item-details'; import { ActivityListItem } from '../../multichain'; import { AvatarNetwork, + AvatarNetworkSize, BadgeWrapper, BadgeWrapperAnchorElementShape, Box, @@ -25,7 +26,6 @@ import { import { BackgroundColor, Display, - Size, } from '../../../helpers/constants/design-system'; import { getCurrentNetwork } from '../../../selectors'; @@ -77,7 +77,7 @@ export default function SmartTransactionListItem({ )} > {children} diff --git a/ui/components/component-library/avatar-base/avatar-base.types.ts b/ui/components/component-library/avatar-base/avatar-base.types.ts index 5c5aeef9b..63ab72601 100644 --- a/ui/components/component-library/avatar-base/avatar-base.types.ts +++ b/ui/components/component-library/avatar-base/avatar-base.types.ts @@ -14,7 +14,7 @@ export enum AvatarBaseSize { Xl = 'xl', } -export interface Props extends TextStyleUtilityProps { +export interface AvatarBaseStyleUtilityProps extends TextStyleUtilityProps { /** * The size of the AvatarBase. * Possible values could be 'AvatarBaseSize.Xs'(16px), 'AvatarBaseSize.Sm'(24px), @@ -48,7 +48,7 @@ export interface Props extends TextStyleUtilityProps { } export type AvatarBaseProps = - PolymorphicComponentPropWithRef; + PolymorphicComponentPropWithRef; export type AvatarBaseComponent = ( props: AvatarBaseProps, diff --git a/ui/components/component-library/avatar-network/README.mdx b/ui/components/component-library/avatar-network/README.mdx index 81a32c49e..beddfd9c9 100644 --- a/ui/components/component-library/avatar-network/README.mdx +++ b/ui/components/component-library/avatar-network/README.mdx @@ -12,7 +12,7 @@ The `AvatarNetwork` is a component responsible for display of the image of a giv ## Props -The `AvatarNetwork` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props +The `AvatarNetwork` accepts all props below as well as all [Box](/docs/components-componentlibrary-box--docs#props) component props @@ -37,13 +37,13 @@ The fallback display of the `AvatarNetwork` is a circle with the initial letter ```jsx -import { Size } from '../../../helpers/constants/design-system'; -import { AvatarNetwork } from '../../component-library'; - - - - - +import { AvatarNetwork, AvatarNetworkSize } from '../../component-library'; + + + + + + ``` ### Name diff --git a/ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.js.snap b/ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.tsx.snap similarity index 81% rename from ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.js.snap rename to ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.tsx.snap index 36aa6b484..b186d4aa0 100644 --- a/ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.js.snap +++ b/ui/components/component-library/avatar-network/__snapshots__/avatar-network.test.tsx.snap @@ -6,7 +6,11 @@ exports[`AvatarNetwork should render correctly 1`] = ` class="mm-box mm-text mm-avatar-base mm-avatar-base--size-md mm-avatar-network mm-text--body-sm mm-text--text-transform-uppercase mm-box--display-flex mm-box--justify-content-center mm-box--align-items-center mm-box--color-text-default mm-box--background-color-background-alternative mm-box--rounded-full mm-box--border-color-transparent box--border-style-solid box--border-width-1" data-testid="avatar-network" > - ? + ethereum logo `; diff --git a/ui/components/component-library/avatar-network/avatar-network.constants.js b/ui/components/component-library/avatar-network/avatar-network.constants.js deleted file mode 100644 index 50c261eab..000000000 --- a/ui/components/component-library/avatar-network/avatar-network.constants.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Size } from '../../../helpers/constants/design-system'; - -export const AVATAR_NETWORK_SIZES = { - XS: Size.XS, - SM: Size.SM, - MD: Size.MD, - LG: Size.LG, - XL: Size.XL, -}; diff --git a/ui/components/component-library/avatar-network/avatar-network.js b/ui/components/component-library/avatar-network/avatar-network.js deleted file mode 100644 index cdc18025d..000000000 --- a/ui/components/component-library/avatar-network/avatar-network.js +++ /dev/null @@ -1,133 +0,0 @@ -import React, { useState, useEffect } from 'react'; -import PropTypes from 'prop-types'; -import classnames from 'classnames'; -import { AvatarBase } from '../avatar-base'; -import Box from '../../ui/box/box'; -import { - Size, - DISPLAY, - AlignItems, - JustifyContent, - TextColor, - BackgroundColor, - BorderColor, -} from '../../../helpers/constants/design-system'; -import { AVATAR_NETWORK_SIZES } from './avatar-network.constants'; - -export const AvatarNetwork = React.forwardRef( - ( - { - size = Size.MD, - name, - src, - showHalo, - color = TextColor.textDefault, - backgroundColor = BackgroundColor.backgroundAlternative, - borderColor = BorderColor.transparent, - className, - ...props - }, - ref, - ) => { - const [showFallback, setShowFallback] = useState(false); - - useEffect(() => { - setShowFallback(!src); - }, [src]); - - const fallbackString = name && name[0] ? name[0] : '?'; - - const handleOnError = () => { - setShowFallback(true); - }; - - return ( - - {showFallback ? ( - fallbackString - ) : ( - <> - {showHalo && ( - - )} - {`${name} - - )} - - ); - }, -); - -AvatarNetwork.propTypes = { - /** - * The name accepts the string to render the first alphabet of the Avatar Name - */ - name: PropTypes.string, - /** - * The src accepts the string of the image to be rendered - */ - src: PropTypes.string, - /** - * The showHalo accepts a boolean prop to render the image with halo effect - */ - showHalo: PropTypes.bool, - /** - * The size of the AvatarNetwork - * Possible values could be Size.XS(16px), Size.SM(24px), Size.MD(32px), Size.LG(40px), Size.XL(48px) - * Defaults to Size.MD - */ - size: PropTypes.oneOf(Object.values(AVATAR_NETWORK_SIZES)), - /** - * The background color of the AvatarNetwork - * Defaults to BackgroundColor.backgroundAlternative - */ - backgroundColor: PropTypes.oneOf(Object.values(BackgroundColor)), - /** - * The background color of the AvatarNetwork - * Defaults to BorderColor.borderDefault - */ - borderColor: PropTypes.oneOf(Object.values(BorderColor)), - /** - * The color of the text inside the AvatarNetwork - * Defaults to TextColor.textDefault - */ - color: PropTypes.oneOf(Object.values(TextColor)), - /** - * Additional classNames to be added to the AvatarNetwork - */ - className: PropTypes.string, - /** - * AvatarNetwork also accepts all Box props including but not limited to - * className, as(change root element of HTML element) and margin props - */ - ...Box.propTypes, -}; - -AvatarNetwork.displayName = 'AvatarNetwork'; diff --git a/ui/components/component-library/avatar-network/avatar-network.stories.js b/ui/components/component-library/avatar-network/avatar-network.stories.tsx similarity index 61% rename from ui/components/component-library/avatar-network/avatar-network.stories.js rename to ui/components/component-library/avatar-network/avatar-network.stories.tsx index 811864f69..277409ab7 100644 --- a/ui/components/component-library/avatar-network/avatar-network.stories.js +++ b/ui/components/component-library/avatar-network/avatar-network.stories.tsx @@ -1,20 +1,17 @@ import React from 'react'; +import { StoryFn, Meta } from '@storybook/react'; import { - Size, - DISPLAY, + Display, TextColor, BackgroundColor, BorderColor, - Color, AlignItems, } from '../../../helpers/constants/design-system'; - -import Box from '../../ui/box/box'; - +import { Box } from '..'; +import { AvatarNetworkSize } from './avatar-network.types'; import README from './README.mdx'; import { AvatarNetwork } from './avatar-network'; -import { AVATAR_NETWORK_SIZES } from './avatar-network.constants'; export default { title: 'Components/ComponentLibrary/AvatarNetwork', @@ -27,7 +24,7 @@ export default { argTypes: { size: { control: 'select', - options: Object.values(AVATAR_NETWORK_SIZES), + options: Object.values(AvatarNetworkSize), }, color: { options: Object.values(TextColor), @@ -54,38 +51,38 @@ export default { args: { name: 'Arbitrum One', src: './images/arbitrum.svg', - size: Size.MD, + size: AvatarNetworkSize.Md, showHalo: false, }, -}; +} as Meta; -const Template = (args) => { +const Template: StoryFn = (args) => { return ; }; export const DefaultStory = Template.bind({}); DefaultStory.storyName = 'Default'; -export const SizeStory = (args) => ( +export const SizeStory: StoryFn = (args) => ( <> - - - - - + + + + + - - - - - - + + + + + + ); @@ -96,8 +93,8 @@ Name.args = { src: '', }; -export const Src = (args) => ( - +export const Src: StoryFn = (args) => ( + @@ -115,21 +112,23 @@ ShowHalo.args = { showHalo: true, }; -export const ColorBackgroundColorAndBorderColor = (args) => ( - +export const ColorBackgroundColorAndBorderColor: StoryFn< + typeof AvatarNetwork +> = (args) => ( + ); diff --git a/ui/components/component-library/avatar-network/avatar-network.test.js b/ui/components/component-library/avatar-network/avatar-network.test.tsx similarity index 67% rename from ui/components/component-library/avatar-network/avatar-network.test.js rename to ui/components/component-library/avatar-network/avatar-network.test.tsx index 4eb5ed202..27efe0948 100644 --- a/ui/components/component-library/avatar-network/avatar-network.test.js +++ b/ui/components/component-library/avatar-network/avatar-network.test.tsx @@ -8,7 +8,7 @@ import { TextColor, } from '../../../helpers/constants/design-system'; -import { AvatarNetwork } from './avatar-network'; +import { AvatarNetwork, AvatarNetworkSize } from '.'; describe('AvatarNetwork', () => { const args = { @@ -19,7 +19,7 @@ describe('AvatarNetwork', () => { it('should render correctly', () => { const { getByTestId, container } = render( - , + , ); expect(getByTestId('avatar-network')).toBeDefined(); expect(container).toMatchSnapshot(); @@ -56,7 +56,11 @@ describe('AvatarNetwork', () => { // className it('should render with custom className', () => { const { getByTestId } = render( - , + , ); expect(getByTestId('avatar-network')).toHaveClass('test-class'); }); @@ -65,10 +69,12 @@ describe('AvatarNetwork', () => { const { getByTestId } = render( <> @@ -86,10 +92,12 @@ describe('AvatarNetwork', () => { const { getByTestId } = render( <> @@ -107,10 +115,12 @@ describe('AvatarNetwork', () => { const { getByTestId } = render( <> @@ -123,10 +133,59 @@ describe('AvatarNetwork', () => { `mm-box--border-color-${BorderColor.errorDefault}`, ); }); + // AvatarNetworkSize + it('should render with different AvatarNetworkSize', () => { + const { getByTestId } = render( + <> + + + + + + , + ); + expect(getByTestId(AvatarNetworkSize.Xs)).toHaveClass( + `mm-avatar-base--size-${AvatarNetworkSize.Xs}`, + ); + expect(getByTestId(AvatarNetworkSize.Sm)).toHaveClass( + `mm-avatar-base--size-${AvatarNetworkSize.Sm}`, + ); + expect(getByTestId(AvatarNetworkSize.Md)).toHaveClass( + `mm-avatar-base--size-${AvatarNetworkSize.Md}`, + ); + expect(getByTestId(AvatarNetworkSize.Lg)).toHaveClass( + `mm-avatar-base--size-${AvatarNetworkSize.Lg}`, + ); + expect(getByTestId(AvatarNetworkSize.Xl)).toHaveClass( + `mm-avatar-base--size-${AvatarNetworkSize.Xl}`, + ); + }); it('should forward a ref to the root html element', () => { - const ref = React.createRef(); - render(); + const ref = React.createRef(); + render(); expect(ref.current).not.toBeNull(); - expect(ref.current.nodeName).toBe('DIV'); + if (ref.current) { + expect(ref.current.nodeName).toBe('DIV'); + } }); }); diff --git a/ui/components/component-library/avatar-network/avatar-network.tsx b/ui/components/component-library/avatar-network/avatar-network.tsx new file mode 100644 index 000000000..676b674aa --- /dev/null +++ b/ui/components/component-library/avatar-network/avatar-network.tsx @@ -0,0 +1,90 @@ +import React, { useState, useEffect } from 'react'; +import classnames from 'classnames'; +import { + Display, + AlignItems, + JustifyContent, + TextColor, + BackgroundColor, + BorderColor, +} from '../../../helpers/constants/design-system'; +import type { PolymorphicRef } from '../box'; +import { AvatarBase, AvatarBaseProps } from '../avatar-base'; +import type { AvatarNetworkComponent } from './avatar-network.types'; +import { AvatarNetworkProps, AvatarNetworkSize } from './avatar-network.types'; + +export const AvatarNetwork: AvatarNetworkComponent = React.forwardRef( + ( + { + size = AvatarNetworkSize.Md, + name, + src, + showHalo, + color = TextColor.textDefault, + backgroundColor = BackgroundColor.backgroundAlternative, + borderColor = BorderColor.transparent, + className = '', + ...props + }: AvatarNetworkProps, + ref?: PolymorphicRef, + ) => { + const [showFallback, setShowFallback] = useState(false); + + useEffect(() => { + setShowFallback(!src); + }, [src]); + + const fallbackString = name?.[0] ?? '?'; + + const handleOnError = () => { + setShowFallback(true); + }; + + return ( + ), + }} + > + {showFallback ? ( + fallbackString + ) : ( + <> + {showHalo && ( + + )} + {`${name} + + )} + + ); + }, +); diff --git a/ui/components/component-library/avatar-network/avatar-network.types.ts b/ui/components/component-library/avatar-network/avatar-network.types.ts new file mode 100644 index 000000000..d0d049143 --- /dev/null +++ b/ui/components/component-library/avatar-network/avatar-network.types.ts @@ -0,0 +1,42 @@ +import type { PolymorphicComponentPropWithRef } from '../box'; +import type { AvatarBaseStyleUtilityProps } from '../avatar-base/avatar-base.types'; + +export enum AvatarNetworkSize { + Xs = 'xs', + Sm = 'sm', + Md = 'md', + Lg = 'lg', + Xl = 'xl', +} + +/** + * Props for the AvatarNetwork component + */ +export interface AvatarNetworkStyleUtilityProps + extends Omit { + /** + * The name accepts the string to render the first alphabet of the Avatar Name + */ + name: string; + /** + * The src accepts the string of the image to be rendered + */ + src?: string; + /** + * The showHalo accepts a boolean prop to render the image with halo effect + */ + showHalo?: boolean; + /** + * The size of the AvatarNetwork + * Possible values could be AvatarNetworkSize.Xs(16px), AvatarNetworkSize.Sm(24px), AvatarNetworkSize.Md(32px), AvatarNetworkSize.Lg(40px), AvatarNetworkSize.Xl(48px) + * Defaults to AvatarNetworkSize.Md + */ + size?: AvatarNetworkSize; +} + +export type AvatarNetworkProps = + PolymorphicComponentPropWithRef; + +export type AvatarNetworkComponent = ( + props: AvatarNetworkProps, +) => React.ReactElement | null; diff --git a/ui/components/component-library/avatar-network/index.js b/ui/components/component-library/avatar-network/index.js deleted file mode 100644 index 538d36758..000000000 --- a/ui/components/component-library/avatar-network/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export { AvatarNetwork } from './avatar-network'; -export { AVATAR_NETWORK_SIZES } from './avatar-network.constants'; diff --git a/ui/components/component-library/avatar-network/index.ts b/ui/components/component-library/avatar-network/index.ts new file mode 100644 index 000000000..93d20bceb --- /dev/null +++ b/ui/components/component-library/avatar-network/index.ts @@ -0,0 +1,3 @@ +export { AvatarNetwork } from './avatar-network'; +export type { AvatarNetworkProps } from './avatar-network.types'; +export { AvatarNetworkSize } from './avatar-network.types'; diff --git a/ui/components/component-library/avatar-token/avatar-token.stories.js b/ui/components/component-library/avatar-token/avatar-token.stories.js index 4262f30d1..ed8cfc53b 100644 --- a/ui/components/component-library/avatar-token/avatar-token.stories.js +++ b/ui/components/component-library/avatar-token/avatar-token.stories.js @@ -12,6 +12,7 @@ import Box from '../../ui/box/box'; import { AvatarNetwork, + AvatarNetworkSize, BUTTON_LINK_SIZES, BadgeWrapper, ButtonLink, @@ -128,7 +129,7 @@ export const SizeStory = (args) => ( @@ -141,7 +142,7 @@ export const SizeStory = (args) => ( @@ -154,7 +155,7 @@ export const SizeStory = (args) => ( @@ -167,7 +168,7 @@ export const SizeStory = (args) => ( @@ -180,7 +181,7 @@ export const SizeStory = (args) => ( @@ -194,7 +195,7 @@ export const SizeStory = (args) => ( badge={ @@ -213,7 +214,7 @@ export const SizeStory = (args) => ( badge={ @@ -232,7 +233,7 @@ export const SizeStory = (args) => ( badge={ @@ -251,7 +252,7 @@ export const SizeStory = (args) => ( badge={ @@ -270,7 +271,7 @@ export const SizeStory = (args) => ( badge={ diff --git a/ui/components/component-library/badge-wrapper/README.mdx b/ui/components/component-library/badge-wrapper/README.mdx index 88432f582..5ca0851ce 100644 --- a/ui/components/component-library/badge-wrapper/README.mdx +++ b/ui/components/component-library/badge-wrapper/README.mdx @@ -30,6 +30,7 @@ import { Color, Size } from '../../../helpers/constants/design-system'; import { AvatarAccount, AvatarNetwork, + AvatarNetworkSize, AvatarToken, BadgeWrapper, BadgeWrapperAnchorElementShape, @@ -39,7 +40,7 @@ import { = (args) => ( = () => ( = () => ( = () => ( = () => ( = () => ( position={BadgeWrapperPosition.topLeft} badge={ = () => ( = () => ( position={BadgeWrapperPosition.bottomLeft} badge={ = () => ( position={BadgeWrapperPosition.bottomRight} badge={ = () => ( diff --git a/ui/components/multichain/activity-list-item/activity-list-item.stories.js b/ui/components/multichain/activity-list-item/activity-list-item.stories.js index 5927657fa..09ea2d49c 100644 --- a/ui/components/multichain/activity-list-item/activity-list-item.stories.js +++ b/ui/components/multichain/activity-list-item/activity-list-item.stories.js @@ -7,12 +7,12 @@ import { Color, Display, FontWeight, - Size, TextAlign, TextVariant, } from '../../../helpers/constants/design-system'; import { AvatarNetwork, + AvatarNetworkSize, BadgeWrapper, BadgeWrapperAnchorElementShape, Box, @@ -44,7 +44,7 @@ DefaultStory.args = {