mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 17:33:23 +01:00
fix/migrate AvatarAccount to TS (#20208)
* migrating AvatarAccount to TS * updates * reverting changes to diameter * suggested changes * adding explicit types for AvatarAccountDiameter * updating documentation * Some small updates to types --------- Co-authored-by: georgewrmarshall <george.marshall@consensys.net>
This commit is contained in:
parent
512fdcae56
commit
9b55791af7
@ -13,15 +13,10 @@ The `AvatarAccount` is a type of avatar reserved for representing accounts.
|
||||
|
||||
## Props
|
||||
|
||||
The `AvatarAccount` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props
|
||||
The `AvatarAccount` accepts all props below as well as all [Box](/docs/components-componentlibrary-box--docs#props) component props
|
||||
|
||||
<ArgsTable of={AvatarAccount} />
|
||||
|
||||
`AvatarAccount` accepts all [AvatarBase](/docs/components-componentlibrary-avatarbase--default-story))
|
||||
component props
|
||||
|
||||
<ArgsTable of={AvatarBase} />
|
||||
|
||||
### Size
|
||||
|
||||
Use the `size` prop and the `AvatarAccountSize` enum from `../../component-library` to change the size of `AvatarAccount`
|
||||
|
@ -1,76 +0,0 @@
|
||||
import React from 'react';
|
||||
import classnames from 'classnames';
|
||||
import PropTypes from 'prop-types';
|
||||
import Jazzicon from '../../ui/jazzicon/jazzicon.component';
|
||||
import BlockieIdenticon from '../../ui/identicon/blockieIdenticon/blockieIdenticon.component';
|
||||
|
||||
import Box from '../../ui/box/box';
|
||||
|
||||
import { AvatarBase } from '../avatar-base';
|
||||
import {
|
||||
AvatarAccountDiameter,
|
||||
AvatarAccountVariant,
|
||||
AvatarAccountSize,
|
||||
} from './avatar-account.types';
|
||||
|
||||
export const AvatarAccount = React.forwardRef(
|
||||
(
|
||||
{
|
||||
size = AvatarAccountSize.Md,
|
||||
address,
|
||||
className,
|
||||
variant = AvatarAccountVariant.Jazzicon,
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) => (
|
||||
<AvatarBase
|
||||
ref={ref}
|
||||
size={size}
|
||||
className={classnames('mm-avatar-account', className)}
|
||||
{...props}
|
||||
>
|
||||
{variant === AvatarAccountVariant.Jazzicon ? (
|
||||
<Jazzicon
|
||||
className={classnames('mm-avatar-account__jazzicon')}
|
||||
address={address}
|
||||
diameter={AvatarAccountDiameter[size]}
|
||||
/>
|
||||
) : (
|
||||
<BlockieIdenticon
|
||||
address={address}
|
||||
diameter={AvatarAccountDiameter[size]}
|
||||
borderRadius="50%"
|
||||
/>
|
||||
)}
|
||||
</AvatarBase>
|
||||
),
|
||||
);
|
||||
|
||||
AvatarAccount.propTypes = {
|
||||
/**
|
||||
* The size of the AvatarAccount.
|
||||
* Possible values could be 'AvatarAccountSize.Xs', 'AvatarAccountSize.Sm', 'AvatarAccountSize.Md', 'AvatarAccountSize.Lg', 'AvatarAccountSize.Xl'
|
||||
* Defaults to AvatarAccountSize.Md
|
||||
*/
|
||||
size: PropTypes.oneOf(Object.values(AvatarAccountSize)),
|
||||
/**
|
||||
* The variant of the avatar to be rendered, it can render either a AvatarAccountVariant.Jazzicon or a AvatarAccountVariant.Blockie
|
||||
*/
|
||||
variant: PropTypes.oneOf(Object.values(AvatarAccountVariant)),
|
||||
/**
|
||||
* Address used for generating random image
|
||||
*/
|
||||
address: PropTypes.string.isRequired,
|
||||
/**
|
||||
* Add custom css class
|
||||
*/
|
||||
className: PropTypes.string,
|
||||
/**
|
||||
* AvatarAccount also accepts all Box props including but not limited to
|
||||
* className, as(change root element of HTML element) and margin props
|
||||
*/
|
||||
...Box.propTypes,
|
||||
};
|
||||
|
||||
AvatarAccount.displayName = 'AvatarAccount';
|
@ -1,6 +1,7 @@
|
||||
import React from 'react';
|
||||
import Box from '../../ui/box/box';
|
||||
import { AlignItems, DISPLAY } from '../../../helpers/constants/design-system';
|
||||
import { StoryFn, Meta } from '@storybook/react';
|
||||
import { Box } from '../box';
|
||||
import { AlignItems, Display } from '../../../helpers/constants/design-system';
|
||||
import { AvatarAccount } from './avatar-account';
|
||||
import {
|
||||
AvatarAccountVariant,
|
||||
@ -20,9 +21,7 @@ export default {
|
||||
argTypes: {
|
||||
size: {
|
||||
control: 'select',
|
||||
options: Object.values(AvatarAccountSize).map(
|
||||
(value) => value.toLowerCase(), // Removes reverse mapping from enum this is a temporary fix until we are using typescript for everything
|
||||
),
|
||||
options: Object.values(AvatarAccountSize),
|
||||
},
|
||||
address: { control: 'text' },
|
||||
variant: {
|
||||
@ -35,14 +34,14 @@ export default {
|
||||
size: AvatarAccountSize.Md,
|
||||
variant: AvatarAccountVariant.Jazzicon,
|
||||
},
|
||||
};
|
||||
} as Meta<typeof AvatarAccount>;
|
||||
|
||||
export const DefaultStory = (args) => <AvatarAccount {...args} />;
|
||||
|
||||
DefaultStory.storyName = 'Default';
|
||||
|
||||
export const Size = (args) => (
|
||||
<Box display={DISPLAY.FLEX} alignItems={AlignItems.baseline} gap={1}>
|
||||
export const Size: StoryFn<typeof AvatarAccount> = (args) => (
|
||||
<Box display={Display.Flex} alignItems={AlignItems.baseline} gap={1}>
|
||||
<AvatarAccount {...args} size={AvatarAccountSize.Xs} />
|
||||
<AvatarAccount {...args} size={AvatarAccountSize.Sm} />
|
||||
<AvatarAccount {...args} size={AvatarAccountSize.Md} />
|
||||
@ -51,15 +50,15 @@ export const Size = (args) => (
|
||||
</Box>
|
||||
);
|
||||
|
||||
export const Variant = (args) => (
|
||||
<Box display={DISPLAY.FLEX} alignItems={AlignItems.baseline} gap={1}>
|
||||
export const Variant: StoryFn<typeof AvatarAccount> = (args) => (
|
||||
<Box display={Display.Flex} alignItems={AlignItems.baseline} gap={1}>
|
||||
<AvatarAccount {...args} variant={AvatarAccountVariant.Jazzicon} />
|
||||
<AvatarAccount {...args} variant={AvatarAccountVariant.Blockies} />
|
||||
</Box>
|
||||
);
|
||||
|
||||
export const Address = (args) => (
|
||||
<Box display={DISPLAY.FLEX} alignItems={AlignItems.BASELINE} gap={1}>
|
||||
export const Address: StoryFn<typeof AvatarAccount> = (args) => (
|
||||
<Box display={Display.Flex} alignItems={AlignItems.baseline} gap={1}>
|
||||
<AvatarAccount
|
||||
{...args}
|
||||
variant={AvatarAccountVariant.Jazzicon}
|
@ -0,0 +1,49 @@
|
||||
import React from 'react';
|
||||
import classnames from 'classnames';
|
||||
import Jazzicon from '../../ui/jazzicon/jazzicon.component';
|
||||
import BlockieIdenticon from '../../ui/identicon/blockieIdenticon/blockieIdenticon.component';
|
||||
|
||||
import type { PolymorphicRef } from '../box';
|
||||
|
||||
import { AvatarBase, AvatarBaseProps } from '../avatar-base';
|
||||
import {
|
||||
AvatarAccountDiameter,
|
||||
AvatarAccountVariant,
|
||||
AvatarAccountSize,
|
||||
AvatarAccountComponent,
|
||||
AvatarAccountProps,
|
||||
} from './avatar-account.types';
|
||||
|
||||
export const AvatarAccount: AvatarAccountComponent = React.forwardRef(
|
||||
<C extends React.ElementType = 'div'>(
|
||||
{
|
||||
size = AvatarAccountSize.Md,
|
||||
address,
|
||||
className = '',
|
||||
variant = AvatarAccountVariant.Jazzicon,
|
||||
...props
|
||||
}: AvatarAccountProps<C>,
|
||||
ref?: PolymorphicRef<C>,
|
||||
) => (
|
||||
<AvatarBase
|
||||
ref={ref}
|
||||
size={size}
|
||||
className={classnames('mm-avatar-account', className)}
|
||||
{...(props as AvatarBaseProps<C>)}
|
||||
>
|
||||
{variant === AvatarAccountVariant.Jazzicon ? (
|
||||
<Jazzicon
|
||||
className={classnames('mm-avatar-account__jazzicon')}
|
||||
address={address}
|
||||
diameter={AvatarAccountDiameter[size]}
|
||||
/>
|
||||
) : (
|
||||
<BlockieIdenticon
|
||||
address={address}
|
||||
diameter={AvatarAccountDiameter[size]}
|
||||
borderRadius="50%"
|
||||
/>
|
||||
)}
|
||||
</AvatarBase>
|
||||
),
|
||||
);
|
@ -1,4 +1,5 @@
|
||||
import { Size } from '../../../helpers/constants/design-system';
|
||||
import type { PolymorphicComponentPropWithRef } from '../box';
|
||||
import { AvatarBaseStyleUtilityProps } from '../avatar-base/avatar-base.types';
|
||||
|
||||
export enum AvatarAccountVariant {
|
||||
Jazzicon = 'jazzicon',
|
||||
@ -6,17 +7,50 @@ export enum AvatarAccountVariant {
|
||||
}
|
||||
|
||||
export enum AvatarAccountSize {
|
||||
Xs = Size.XS,
|
||||
Sm = Size.SM,
|
||||
Md = Size.MD,
|
||||
Lg = Size.LG,
|
||||
Xl = Size.XL,
|
||||
Xs = 'xs',
|
||||
Sm = 'sm',
|
||||
Md = 'md',
|
||||
Lg = 'lg',
|
||||
Xl = 'xl',
|
||||
}
|
||||
|
||||
export const AvatarAccountDiameter = {
|
||||
export const AvatarAccountDiameter: Record<AvatarAccountSize, number> = {
|
||||
[AvatarAccountSize.Xs]: 16,
|
||||
[AvatarAccountSize.Sm]: 24,
|
||||
[AvatarAccountSize.Md]: 32,
|
||||
[AvatarAccountSize.Lg]: 40,
|
||||
[AvatarAccountSize.Xl]: 48,
|
||||
} as const;
|
||||
};
|
||||
|
||||
export interface AvatarAccountStyleUtilityProps
|
||||
extends Omit<AvatarBaseStyleUtilityProps, 'size' | 'variant'> {
|
||||
/**
|
||||
* The size of the AvatarAccount.
|
||||
* Possible values could be 'AvatarAccountSize.Xs', 'AvatarAccountSize.Sm', 'AvatarAccountSize.Md', 'AvatarAccountSize.Lg', 'AvatarAccountSize.Xl'
|
||||
* Defaults to AvatarAccountSize.Md
|
||||
*/
|
||||
size?: AvatarAccountSize;
|
||||
/**
|
||||
* The variant of the avatar to be rendered, it can render either a AvatarAccountVariant.Jazzicon or a AvatarAccountVariant.Blockie
|
||||
*/
|
||||
variant?: AvatarAccountVariant;
|
||||
/**
|
||||
* Address used for generating random image
|
||||
*/
|
||||
address: string;
|
||||
/**
|
||||
* Add custom css class
|
||||
*/
|
||||
className?: string;
|
||||
/**
|
||||
* AvatarAccount also accepts all Box props including but not limited to
|
||||
* className, as(change root element of HTML element) and margin props
|
||||
*/
|
||||
}
|
||||
|
||||
export type AvatarAccountProps<C extends React.ElementType> =
|
||||
PolymorphicComponentPropWithRef<C, AvatarAccountStyleUtilityProps>;
|
||||
|
||||
export type AvatarAccountComponent = <C extends React.ElementType = 'div'>(
|
||||
props: AvatarAccountProps<C>,
|
||||
) => React.ReactElement | null;
|
||||
|
@ -4,3 +4,4 @@ export {
|
||||
AvatarAccountVariant,
|
||||
AvatarAccountDiameter,
|
||||
} from './avatar-account.types';
|
||||
export type { AvatarAccountProps } from './avatar-account.types';
|
Loading…
Reference in New Issue
Block a user