1
1
// This is the only place MuiAvatar can be used
2
2
// eslint-disable-next-line no-restricted-imports -- Read above
3
3
import MuiAvatar , { AvatarProps as MuiAvatarProps } from "@mui/material/Avatar" ;
4
- import { makeStyles } from "@mui/styles" ;
5
4
import { FC } from "react" ;
6
- import { combineClasses } from "utils/combineClasses " ;
5
+ import { css , type Theme } from "@emotion/react " ;
7
6
8
7
export type AvatarProps = MuiAvatarProps & {
9
8
size ?: "sm" | "md" | "xl" ;
10
9
colorScheme ?: "light" | "darken" ;
11
10
fitImage ?: boolean ;
12
11
} ;
13
12
13
+ const sizeStyles = {
14
+ sm : ( theme : Theme ) => ( {
15
+ width : theme . spacing ( 3 ) ,
16
+ height : theme . spacing ( 3 ) ,
17
+ fontSize : theme . spacing ( 1.5 ) ,
18
+ } ) ,
19
+ md : { } ,
20
+ xl : ( theme : Theme ) => ( {
21
+ width : theme . spacing ( 6 ) ,
22
+ height : theme . spacing ( 6 ) ,
23
+ fontSize : theme . spacing ( 3 ) ,
24
+ } ) ,
25
+ } ;
26
+
27
+ const colorStyles = {
28
+ light : { } ,
29
+ darken : ( theme : Theme ) => ( {
30
+ background : theme . palette . divider ,
31
+ color : theme . palette . text . primary ,
32
+ } ) ,
33
+ } ;
34
+
35
+ const fitImageStyles = css `
36
+ & .MuiAvatar-img {
37
+ object-fit : contain;
38
+ }
39
+ ` ;
40
+
14
41
export const Avatar : FC < AvatarProps > = ( {
15
42
size = "md" ,
16
43
colorScheme = "light" ,
17
44
fitImage,
18
- className,
19
45
children,
20
46
...muiProps
21
47
} ) => {
22
- const styles = useStyles ( ) ;
23
-
24
48
return (
25
49
< MuiAvatar
26
50
{ ...muiProps }
27
- className = { combineClasses ( [
28
- className ,
29
- styles [ size ] ,
30
- styles [ colorScheme ] ,
31
- fitImage && styles . fitImage ,
32
- ] ) }
51
+ css = { [
52
+ sizeStyles [ size ] ,
53
+ colorStyles [ colorScheme ] ,
54
+ fitImage && fitImageStyles ,
55
+ ] }
33
56
>
34
57
{ typeof children === "string" ? firstLetter ( children ) : children }
35
58
</ MuiAvatar >
@@ -40,8 +63,15 @@ export const Avatar: FC<AvatarProps> = ({
40
63
* Use it to make an img element behaves like a MaterialUI Icon component
41
64
*/
42
65
export const AvatarIcon : FC < { src : string } > = ( { src } ) => {
43
- const styles = useStyles ( ) ;
44
- return < img src = { src } alt = "" className = { styles . avatarIcon } /> ;
66
+ return (
67
+ < img
68
+ src = { src }
69
+ alt = ""
70
+ css = { {
71
+ maxWidth : "50%" ,
72
+ } }
73
+ />
74
+ ) ;
45
75
} ;
46
76
47
77
const firstLetter = ( str : string ) : string => {
@@ -51,36 +81,3 @@ const firstLetter = (str: string): string => {
51
81
52
82
return "" ;
53
83
} ;
54
-
55
- const useStyles = makeStyles ( ( theme ) => ( {
56
- // Size styles
57
- sm : {
58
- width : theme . spacing ( 3 ) ,
59
- height : theme . spacing ( 3 ) ,
60
- fontSize : theme . spacing ( 1.5 ) ,
61
- } ,
62
- // Just use the default value from theme
63
- md : { } ,
64
- xl : {
65
- width : theme . spacing ( 6 ) ,
66
- height : theme . spacing ( 6 ) ,
67
- fontSize : theme . spacing ( 3 ) ,
68
- } ,
69
- // Colors
70
- // Just use the default value from theme
71
- light : { } ,
72
- darken : {
73
- background : theme . palette . divider ,
74
- color : theme . palette . text . primary ,
75
- } ,
76
- // Avatar icon
77
- avatarIcon : {
78
- maxWidth : "50%" ,
79
- } ,
80
- // Fit image
81
- fitImage : {
82
- "& .MuiAvatar-img" : {
83
- objectFit : "contain" ,
84
- } ,
85
- } ,
86
- } ) ) ;
0 commit comments