@@ -13,25 +13,38 @@ export const avatarTranslations: AvatarTranslations = {
13
13
label : 'Avatar' ,
14
14
} ;
15
15
16
- export type LocalAvatarProps = ThemeProps & {
17
- actionable ?: boolean ;
18
- className ?: string ;
19
- color ?: 'neutral' | 'primary' ;
20
- imgSrc ?: string ;
21
- initials ?: string ;
22
- size ?: 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | '2xlarge' | '3xlarge' | '4xlarge' | '5xlarge' | 'custom' ;
23
- style ?: React . CSSProperties ;
24
- translations ?: Partial < AvatarTranslations > ;
25
- } ;
16
+ export type AvatarElementType = Extract < keyof HTMLElementTagNameMap , 'a' | 'button' | 'div' | 'figure' > ;
17
+
18
+ export type LocalAvatarProps < T extends AvatarElementType = 'div' > = ThemeProps &
19
+ React . HTMLAttributes < HTMLElementTagNameMap [ T ] > & {
20
+ actionable ?: boolean ;
21
+ className ?: string ;
22
+ color ?: 'neutral' | 'primary' ;
23
+ imgSrc ?: string ;
24
+ initials ?: string ;
25
+ size ?:
26
+ | 'xsmall'
27
+ | 'small'
28
+ | 'medium'
29
+ | 'large'
30
+ | 'xlarge'
31
+ | '2xlarge'
32
+ | '3xlarge'
33
+ | '4xlarge'
34
+ | '5xlarge'
35
+ | 'custom' ;
36
+ style ?: React . CSSProperties ;
37
+ translations ?: Partial < AvatarTranslations > ;
38
+ } ;
26
39
27
- export type AvatarProps < T extends React . ElementType = 'div' > = AsReactType < T > & MergeElementProps < T , LocalAvatarProps > ;
40
+ export type AvatarProps < T extends AvatarElementType = 'div' > = AsReactType < T > & MergeElementProps < T , LocalAvatarProps > ;
28
41
29
42
/**
30
43
* An avatar represents a user as a picture or initials.
31
44
* It can be rendered as a standard read-only div, an
32
45
* actionable button, or a custom element.
33
46
*/
34
- export function Avatar < T extends React . ElementType = 'div' > ( {
47
+ export function Avatar < T extends AvatarElementType = 'div' > ( {
35
48
actionable = false ,
36
49
as,
37
50
className,
@@ -45,35 +58,35 @@ export function Avatar<T extends React.ElementType = 'div'>({
45
58
translations : customTranslations ,
46
59
unthemed = false ,
47
60
...props
48
- } : AvatarProps < T > ) : React . ReactElement < AvatarProps < T > , T > {
61
+ } : AvatarProps < T > ) : JSX . Element {
49
62
const themeId = useThemeId ( initThemeId ) ;
50
63
const color = contrast ? 'contrast' : initColor ;
51
- const Element = as || ( actionable ? 'button' : 'div' ) ;
64
+ const element = as || ( actionable ? 'button' : 'div' ) ;
52
65
const translations = useTranslations < AvatarTranslations > ( {
53
66
customTranslations,
54
67
fallbackTranslations : avatarTranslations ,
55
68
} ) ;
56
69
const { label } = translations ;
57
70
58
- return (
59
- < Element
60
- aria-label = { `${ label } : ${ initials } ` }
61
- className = { cx (
71
+ return React . createElement (
72
+ element ,
73
+ {
74
+ 'aria-label' : `${ label } : ${ initials } ` ,
75
+ className : cx (
62
76
styles . avatar ,
63
77
color && styles [ `avatar--${ color } ` ] ,
64
78
themeId && ! unthemed && styles [ `avatar--${ themeId } ` ] ,
65
79
imgSrc && styles [ 'avatar--image' ] ,
66
80
size && styles [ `avatar--${ size } ` ] ,
67
81
actionable && styles [ 'avatar--actionable' ] ,
68
82
className ,
69
- ) }
70
- role = "img"
71
- style = { { backgroundImage : imgSrc && `url(${ imgSrc } )` , ...style } }
72
- tabIndex = { actionable ? 0 : undefined }
73
- { ...props }
74
- >
75
- < div className = { styles . avatar__content } > { initials } </ div >
76
- </ Element >
83
+ ) ,
84
+ role : 'img' ,
85
+ style : { backgroundImage : imgSrc && `url(${ imgSrc } )` , ...style } ,
86
+ tabIndex : actionable ? 0 : undefined ,
87
+ ...props ,
88
+ } ,
89
+ < div className = { styles . avatar__content } > { initials } </ div > ,
77
90
) ;
78
91
}
79
92
0 commit comments