1 | import { alpha, Theme } from '@mui/material/styles';
|
---|
2 | import { AvatarProps } from '@mui/material/Avatar';
|
---|
3 | import { avatarGroupClasses, AvatarGroupProps } from '@mui/material/AvatarGroup';
|
---|
4 |
|
---|
5 | // ----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | const COLORS = ['default', 'primary', 'secondary', 'info', 'success', 'warning', 'error'] as const;
|
---|
8 |
|
---|
9 | const colorByName = (name: string) => {
|
---|
10 | const charAt = name.charAt(0).toLowerCase();
|
---|
11 |
|
---|
12 | if (['a', 'c', 'f'].includes(charAt)) return 'primary';
|
---|
13 | if (['e', 'd', 'h'].includes(charAt)) return 'secondary';
|
---|
14 | if (['i', 'k', 'l'].includes(charAt)) return 'info';
|
---|
15 | if (['m', 'n', 'p'].includes(charAt)) return 'success';
|
---|
16 | if (['q', 's', 't'].includes(charAt)) return 'warning';
|
---|
17 | if (['v', 'x', 'y'].includes(charAt)) return 'error';
|
---|
18 | return 'default';
|
---|
19 | };
|
---|
20 |
|
---|
21 | // NEW VARIANT
|
---|
22 | declare module '@mui/material/AvatarGroup' {
|
---|
23 | interface AvatarGroupPropsVariantOverrides {
|
---|
24 | compact: true;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | // ----------------------------------------------------------------------
|
---|
29 |
|
---|
30 | export function avatar(theme: Theme) {
|
---|
31 | return {
|
---|
32 | MuiAvatar: {
|
---|
33 | variants: COLORS.map((color) =>
|
---|
34 | color === 'default'
|
---|
35 | ? {
|
---|
36 | props: { color: 'default' },
|
---|
37 | style: {
|
---|
38 | color: theme.palette.text.secondary,
|
---|
39 | backgroundColor: alpha(theme.palette.grey[500], 0.24),
|
---|
40 | },
|
---|
41 | }
|
---|
42 | : {
|
---|
43 | props: { color },
|
---|
44 | style: {
|
---|
45 | color: theme.palette[color].contrastText,
|
---|
46 | backgroundColor: theme.palette[color].main,
|
---|
47 | },
|
---|
48 | }
|
---|
49 | ),
|
---|
50 |
|
---|
51 | styleOverrides: {
|
---|
52 | rounded: {
|
---|
53 | borderRadius: theme.shape.borderRadius * 1.5,
|
---|
54 | },
|
---|
55 | colorDefault: ({ ownerState }: { ownerState: AvatarProps }) => {
|
---|
56 | const color = colorByName(`${ownerState.alt}`);
|
---|
57 |
|
---|
58 | return {
|
---|
59 | ...(!!ownerState.alt && {
|
---|
60 | ...(color !== 'default'
|
---|
61 | ? {
|
---|
62 | color: theme.palette[color].contrastText,
|
---|
63 | backgroundColor: theme.palette[color].main,
|
---|
64 | }
|
---|
65 | : {
|
---|
66 | color: theme.palette.text.secondary,
|
---|
67 | backgroundColor: alpha(theme.palette.grey[500], 0.24),
|
---|
68 | }),
|
---|
69 | }),
|
---|
70 | };
|
---|
71 | },
|
---|
72 | },
|
---|
73 | },
|
---|
74 | MuiAvatarGroup: {
|
---|
75 | styleOverrides: {
|
---|
76 | root: ({ ownerState }: { ownerState: AvatarGroupProps }) => ({
|
---|
77 | justifyContent: 'flex-end',
|
---|
78 | ...(ownerState.variant === 'compact' && {
|
---|
79 | width: 40,
|
---|
80 | height: 40,
|
---|
81 | position: 'relative',
|
---|
82 | [`& .${avatarGroupClasses.avatar}`]: {
|
---|
83 | margin: 0,
|
---|
84 | width: 28,
|
---|
85 | height: 28,
|
---|
86 | position: 'absolute',
|
---|
87 | '&:first-of-type': {
|
---|
88 | left: 0,
|
---|
89 | bottom: 0,
|
---|
90 | zIndex: 9,
|
---|
91 | },
|
---|
92 | '&:last-of-type': {
|
---|
93 | top: 0,
|
---|
94 | right: 0,
|
---|
95 | },
|
---|
96 | },
|
---|
97 | }),
|
---|
98 | }),
|
---|
99 | avatar: {
|
---|
100 | fontSize: 16,
|
---|
101 | fontWeight: theme.typography.fontWeightSemiBold,
|
---|
102 | '&:first-of-type': {
|
---|
103 | fontSize: 12,
|
---|
104 | color: theme.palette.primary.dark,
|
---|
105 | backgroundColor: theme.palette.primary.lighter,
|
---|
106 | },
|
---|
107 | },
|
---|
108 | },
|
---|
109 | },
|
---|
110 | };
|
---|
111 | }
|
---|