1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/components/component-library/avatar-network/avatar-network.tsx
Binij Shrestha ceadfacb21
Fix/18884 migrate avatar network (#19079)
* Migrate AvatarNetwokr

fixing error

fix textAlign

added AvatarNetworkSize

NetworkProps extends BaseProps instead of Boxprops

omitted children from base, made name required

replace deprecated and fix lint

* update AvatarNetwork TS

* add AvatarNetworkSize test

* remove unused size import

* Update ui/components/component-library/avatar-network/avatar-network.types.ts

Co-authored-by: George Marshall <georgewrmarshall@gmail.com>

* fix readme

* update to latest box component

---------

Co-authored-by: garrettbear <gwhisten@gmail.com>
Co-authored-by: George Marshall <georgewrmarshall@gmail.com>
2023-07-24 11:15:33 -07:00

91 lines
2.4 KiB
TypeScript

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(
<C extends React.ElementType = 'div'>(
{
size = AvatarNetworkSize.Md,
name,
src,
showHalo,
color = TextColor.textDefault,
backgroundColor = BackgroundColor.backgroundAlternative,
borderColor = BorderColor.transparent,
className = '',
...props
}: AvatarNetworkProps<C>,
ref?: PolymorphicRef<C>,
) => {
const [showFallback, setShowFallback] = useState(false);
useEffect(() => {
setShowFallback(!src);
}, [src]);
const fallbackString = name?.[0] ?? '?';
const handleOnError = () => {
setShowFallback(true);
};
return (
<AvatarBase
ref={ref}
size={size}
display={Display.Flex}
alignItems={AlignItems.center}
justifyContent={JustifyContent.center}
className={classnames(
'mm-avatar-network',
showHalo ? 'mm-avatar-network--with-halo' : '',
className,
)}
{...{
backgroundColor,
borderColor,
color,
...(props as AvatarBaseProps<C>),
}}
>
{showFallback ? (
fallbackString
) : (
<>
{showHalo && (
<img
src={src}
className={
showHalo ? 'mm-avatar-network__network-image--blurred' : ''
}
aria-hidden="true"
/>
)}
<img
className={
showHalo
? 'mm-avatar-network__network-image--size-reduced'
: 'mm-avatar-network__network-image'
}
onError={handleOnError}
src={src}
alt={`${name} logo` || 'network logo'}
/>
</>
)}
</AvatarBase>
);
},
);