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 100% 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 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 50c261eab9c7..000000000000 --- 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 cdc18025df1c..000000000000 --- 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 62% 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 811864f69bb2..36b5eee12d28 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,19 @@ 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 { 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 +26,7 @@ export default { argTypes: { size: { control: 'select', - options: Object.values(AVATAR_NETWORK_SIZES), + options: Object.values(AvatarNetworkSize), }, color: { options: Object.values(TextColor), @@ -54,38 +53,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 +95,8 @@ Name.args = { src: '', }; -export const Src = (args) => ( - +export const Src: StoryFn = (args) => ( + @@ -115,21 +114,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 89% 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 4eb5ed202424..d458fcf12606 100644 --- a/ui/components/component-library/avatar-network/avatar-network.test.js +++ b/ui/components/component-library/avatar-network/avatar-network.test.tsx @@ -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( <> @@ -124,9 +134,11 @@ describe('AvatarNetwork', () => { ); }); 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 000000000000..3ef25abfb4e9 --- /dev/null +++ b/ui/components/component-library/avatar-network/avatar-network.tsx @@ -0,0 +1,86 @@ +import React, { useState, useEffect, Ref } from 'react'; +import classnames from 'classnames'; +import { + Display, + AlignItems, + JustifyContent, + TextColor, + BackgroundColor, + BorderColor, +} from '../../../helpers/constants/design-system'; +import { AvatarBase } from '../avatar-base'; + +import { AvatarNetworkProps, AvatarNetworkSize } from './avatar-network.types'; + +export const AvatarNetwork = React.forwardRef( + ( + { + size = AvatarNetworkSize.Md, + name, + src, + showHalo, + color = TextColor.textDefault, + backgroundColor = BackgroundColor.backgroundAlternative, + borderColor = BorderColor.transparent, + className = '', + ...props + }: AvatarNetworkProps, + ref: Ref, + ) => { + const [showFallback, setShowFallback] = useState(false); + + useEffect(() => { + setShowFallback(!src); + }, [src]); + + const fallbackString = name?.[0] ?? '?'; + + const handleOnError = () => { + setShowFallback(true); + }; + + return ( + + {showFallback ? ( + fallbackString + ) : ( + <> + {showHalo && ( + + )} + {`${name} + + )} + + ); + }, +); + +AvatarNetwork.displayName = 'AvatarNetwork'; 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 000000000000..25b57d23eab1 --- /dev/null +++ b/ui/components/component-library/avatar-network/avatar-network.types.ts @@ -0,0 +1,33 @@ +import { AvatarBaseProps } 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 AvatarNetworkProps 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; +} 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 538d367582fc..000000000000 --- 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 000000000000..93d20bceb659 --- /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/index.js b/ui/components/component-library/index.js index 564643063982..e6427dd481a5 100644 --- a/ui/components/component-library/index.js +++ b/ui/components/component-library/index.js @@ -6,7 +6,7 @@ export { } from './avatar-account'; export { AvatarFavicon, AVATAR_FAVICON_SIZES } from './avatar-favicon'; export { AvatarIcon, AVATAR_ICON_SIZES } from './avatar-icon'; -export { AvatarNetwork, AVATAR_NETWORK_SIZES } from './avatar-network'; +export { AvatarNetwork, AvatarNetworkSize } from './avatar-network'; export { AvatarToken } from './avatar-token'; export { AvatarBase } from './avatar-base'; export {