Skip to content

Commit

Permalink
update AvatarNetwork TS
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettbear committed Jul 20, 2023
1 parent 724efad commit 36f9718
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 70 deletions.
4 changes: 2 additions & 2 deletions ui/components/app/add-network/add-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
BackgroundColor,
TextColor,
IconColor,
Size,
} from '../../../helpers/constants/design-system';
import Button from '../../ui/button';
import Tooltip from '../../ui/tooltip';
Expand All @@ -40,6 +39,7 @@ import {
IconName,
IconSize,
AvatarNetwork,
AvatarNetworkSize,
Text,
} from '../../component-library';
import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics';
Expand Down Expand Up @@ -199,7 +199,7 @@ const AddNetwork = () => {
>
<Box display={Display.Flex} alignItems={AlignItems.center}>
<AvatarNetwork
size={Size.SM}
size={AvatarNetworkSize.Sm}
src={item.rpcPrefs?.imageUrl}
name={item.nickname}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import TransactionListItemDetails from '../transaction-list-item-details';
import { ActivityListItem } from '../../multichain';
import {
AvatarNetwork,
AvatarNetworkSize,
BadgeWrapper,
BadgeWrapperAnchorElementShape,
Box,
} from '../../component-library';
import {
BackgroundColor,
Display,
Size,
} from '../../../helpers/constants/design-system';
import { getCurrentNetwork } from '../../../selectors';

Expand Down Expand Up @@ -77,7 +77,7 @@ export default function SmartTransactionListItem({
<AvatarNetwork
className="activity-tx__network-badge"
data-testid="activity-tx-network-badge"
size={Size.XS}
size={AvatarNetworkSize.Xs}
name={currentChain?.nickname}
src={currentChain?.rpcPrefs?.imageUrl}
borderWidth={1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import {
Color,
Display,
FontWeight,
Size,
TextAlign,
TextVariant,
} from '../../../helpers/constants/design-system';
import {
AvatarNetwork,
AvatarNetworkSize,
BadgeWrapper,
BadgeWrapperAnchorElementShape,
Box,
Expand Down Expand Up @@ -273,7 +273,7 @@ function TransactionListItemInner({
<AvatarNetwork
className="activity-tx__network-badge"
data-testid="activity-tx-network-badge"
size={Size.XS}
size={AvatarNetworkSize.Xs}
name={currentChain?.nickname}
src={currentChain?.rpcPrefs?.imageUrl}
borderWidth={1}
Expand Down
4 changes: 2 additions & 2 deletions ui/components/component-library/avatar-base/avatar-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type { PolymorphicRef } from '../box';
import { Text } from '../text';

import type { TextProps } from '../text';
import {
AvatarBaseComponent,
AvatarBaseProps,
Expand Down Expand Up @@ -60,7 +60,7 @@ export const AvatarBase: AvatarBaseComponent = React.forwardRef(
variant={fallbackTextVariant}
textTransform={TextTransform.Uppercase}
{...{ backgroundColor, borderColor, color }}
{...props} // TODO: There is a typing issue with spreading props to the Box component. It still works but TypeScript complains.
{...(props as TextProps<C>)}
>
{children}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface Props extends TextStyleUtilityProps {
}

export type AvatarBaseProps<C extends React.ElementType> =
PolymorphicComponentPropWithRef<C, Props>;
PolymorphicComponentPropWithRef<C, AvatarBaseStyleUtilityProps>;

export type AvatarBaseComponent = <C extends React.ElementType = 'span'>(
props: AvatarBaseProps<C>,
Expand Down
14 changes: 7 additions & 7 deletions ui/components/component-library/avatar-network/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ The fallback display of the `AvatarNetwork` is a circle with the initial letter
</Canvas>

```jsx
import { Size } from '../../../helpers/constants/design-system';
import { AvatarNetwork } from '../../component-library';
<AvatarNetwork size={Size.XS} />
<AvatarNetwork size={Size.SM} />
<AvatarNetwork size={Size.MD} />
<AvatarNetwork size={Size.LG} />
<AvatarNetwork size={Size.XL} />
import { AvatarNetwork, AvatarNetworkSize } from '../../component-library';

<AvatarNetwork size={AvatarNetworkSize.Xs} />
<AvatarNetwork size={AvatarNetworkSize.Sm} />
<AvatarNetwork size={AvatarNetworkSize.Md} />
<AvatarNetwork size={AvatarNetworkSize.Lg} />
<AvatarNetwork size={AvatarNetworkSize.Xl} />
```

### Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
?
<img
alt="ethereum logo"
class="mm-avatar-network__network-image"
src="./images/eth_logo.png"
/>
</div>
</div>
`;
19 changes: 12 additions & 7 deletions ui/components/component-library/avatar-network/avatar-network.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, Ref } from 'react';
import React, { useState, useEffect } from 'react';
import classnames from 'classnames';
import {
Display,
Expand All @@ -8,12 +8,12 @@ import {
BackgroundColor,
BorderColor,
} from '../../../helpers/constants/design-system';
import { AvatarBase } from '../avatar-base';

import type { PolymorphicRef } from '../box';
import { AvatarBase, AvatarBaseProps } from '../avatar-base';
import { AvatarNetworkProps, AvatarNetworkSize } from './avatar-network.types';

export const AvatarNetwork = React.forwardRef(
(
<C extends React.ElementType = 'div'>(
{
size = AvatarNetworkSize.Md,
name,
Expand All @@ -24,8 +24,8 @@ export const AvatarNetwork = React.forwardRef(
borderColor = BorderColor.transparent,
className = '',
...props
}: AvatarNetworkProps,
ref: Ref<HTMLElement>,
}: AvatarNetworkProps<C>,
ref?: PolymorphicRef<C>,
) => {
const [showFallback, setShowFallback] = useState(false);

Expand All @@ -51,7 +51,12 @@ export const AvatarNetwork = React.forwardRef(
showHalo ? 'mm-avatar-network--with-halo' : '',
className,
)}
{...{ backgroundColor, borderColor, color, ...props }}
{...{
backgroundColor,
borderColor,
color,
...(props as AvatarBaseProps<C>),
}}
>
{showFallback ? (
fallbackString
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { AvatarBaseProps } from '../avatar-base/avatar-base.types';
import type { PolymorphicComponentPropWithRef } from '../box';
import type { AvatarBaseStyleUtilityProps } from '../avatar-base/avatar-base.types';
import { AvatarBaseSize } from '../avatar-base/avatar-base.types';

export enum AvatarNetworkSize {
Xs = 'xs',
Sm = 'sm',
Md = 'md',
Lg = 'lg',
Xl = 'xl',
Xs = AvatarBaseSize.Xs,
Sm = AvatarBaseSize.Sm,
Md = AvatarBaseSize.Md,
Lg = AvatarBaseSize.Lg,
Xl = AvatarBaseSize.Xl,
}

/**
* Props for the AvatarNetwork component
*/
export interface AvatarNetworkProps extends Omit<AvatarBaseProps, 'children'> {
export interface AvatarNetworkStyleUtilityProps
extends Omit<AvatarBaseStyleUtilityProps, 'size'> {
/**
* The name accepts the string to render the first alphabet of the Avatar Name
*/
Expand All @@ -31,3 +34,10 @@ export interface AvatarNetworkProps extends Omit<AvatarBaseProps, 'children'> {
*/
size?: AvatarNetworkSize;
}

export type AvatarNetworkProps<C extends React.ElementType> =
PolymorphicComponentPropWithRef<C, AvatarNetworkStyleUtilityProps>;

export type AvatarNetworkComponent = <C extends React.ElementType = 'span'>(
props: AvatarNetworkProps<C>,
) => React.ReactElement | null;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Box from '../../ui/box/box';

import {
AvatarNetwork,
AvatarNetworkSize,
BUTTON_LINK_SIZES,
BadgeWrapper,
ButtonLink,
Expand Down Expand Up @@ -128,7 +129,7 @@ export const SizeStory = (args) => (
<AvatarNetwork
src="./images/eth_logo.png"
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -141,7 +142,7 @@ export const SizeStory = (args) => (
<AvatarNetwork
src="./images/eth_logo.png"
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -154,7 +155,7 @@ export const SizeStory = (args) => (
<AvatarNetwork
src="./images/eth_logo.png"
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -167,7 +168,7 @@ export const SizeStory = (args) => (
<AvatarNetwork
src="./images/eth_logo.png"
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -180,7 +181,7 @@ export const SizeStory = (args) => (
<AvatarNetwork
src="./images/eth_logo.png"
name="ETH"
size={Size.SM}
size={AvatarNetworkSize.Sm}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -194,7 +195,7 @@ export const SizeStory = (args) => (
badge={
<AvatarNetwork
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -213,7 +214,7 @@ export const SizeStory = (args) => (
badge={
<AvatarNetwork
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -232,7 +233,7 @@ export const SizeStory = (args) => (
badge={
<AvatarNetwork
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -251,7 +252,7 @@ export const SizeStory = (args) => (
badge={
<AvatarNetwork
name="ETH"
size={Size.XS}
size={AvatarNetworkSize.Xs}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand All @@ -270,7 +271,7 @@ export const SizeStory = (args) => (
badge={
<AvatarNetwork
name="ETH"
size={Size.SM}
size={AvatarNetworkSize.Sm}
borderColor={BackgroundColor.backgroundDefault}
borderWidth={2}
/>
Expand Down
Loading

0 comments on commit 36f9718

Please sign in to comment.