Skip to content

Commit

Permalink
[Avatar] Add support for CSS variables (#32499)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicasas authored Jun 17, 2022
1 parent d14be1c commit b9cfab2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/pages/experiments/material-ui/css-variables-custom-theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@mui/material/styles';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import Avatar from '@mui/material/Avatar';
import Button from '@mui/material/Button';
import Chip from '@mui/material/Chip';
import Box from '@mui/material/Box';
Expand Down Expand Up @@ -121,6 +122,18 @@ export default function Page() {
))}
<Input sx={overrideCssVariables} placeholder="custom" />
</Box>
<Box sx={{ py: 5, maxWidth: { md: 1152, xl: 1536 }, mx: 'auto' }}>
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 4, mb: 1 }}>
<Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar variant="rounded" alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar variant="square" alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
</Box>
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 4, mb: 1 }}>
<Avatar>H</Avatar>
<Avatar variant="rounded">H</Avatar>
<Avatar variant="square">H</Avatar>
</Box>
</Box>
</Box>
</CssVarsProvider>
);
Expand Down
14 changes: 14 additions & 0 deletions docs/pages/experiments/material-ui/css-variables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@mui/material/styles';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import Avatar from '@mui/material/Avatar';
import Button from '@mui/material/Button';
import Box from '@mui/material/Box';
import Input from '@mui/material/Input';
Expand Down Expand Up @@ -73,6 +74,19 @@ export default function Page() {
))}
</Box>
</Box>

<div>
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 4, mb: 1 }}>
<Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar variant="rounded" alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar variant="square" alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
</Box>
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 4, mb: 1 }}>
<Avatar>H</Avatar>
<Avatar variant="rounded">H</Avatar>
<Avatar variant="square">H</Avatar>
</Box>
</div>
</CssVarsProvider>
);
}
14 changes: 10 additions & 4 deletions packages/mui-material/src/Avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ const AvatarRoot = styled('div', {
overflow: 'hidden',
userSelect: 'none',
...(ownerState.variant === 'rounded' && {
borderRadius: theme.shape.borderRadius,
borderRadius: (theme.vars || theme).shape.borderRadius,
}),
...(ownerState.variant === 'square' && {
borderRadius: 0,
}),
...(ownerState.colorDefault && {
color: theme.palette.background.default,
backgroundColor:
theme.palette.mode === 'light' ? theme.palette.grey[400] : theme.palette.grey[600],
color: (theme.vars || theme).palette.background.default,
...(theme.vars
? {
backgroundColor: theme.vars.palette.Avatar.defaultBg,
}
: {
backgroundColor:
theme.palette.mode === 'light' ? theme.palette.grey[400] : theme.palette.grey[600],
}),
}),
}));

Expand Down
3 changes: 3 additions & 0 deletions packages/mui-material/src/styles/createPalette.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ export interface PaletteWithChannels {
darkBg: string;
darkColor: string;
};
Avatar: {
defaultBg: string;
};
Chip: {
defaultBorder: string;
defaultAvatarColor: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/mui-material/src/styles/experimental_extendTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function extendTheme(options = {}, ...args) {
// assign component variables
assignNode(palette, [
'AppBar',
'Avatar',
'Chip',
'FilledInput',
'LinearProgress',
Expand All @@ -94,6 +95,7 @@ export default function extendTheme(options = {}, ...args) {
]);
if (key === 'light') {
setColor(palette.AppBar, 'defaultBg', 'var(--mui-palette-grey-100)');
setColor(palette.Avatar, 'defaultBg', 'var(--mui-palette-grey-400)');
setColor(palette.Chip, 'defaultBorder', 'var(--mui-palette-grey-400)');
setColor(palette.Chip, 'defaultAvatarColor', 'var(--mui-palette-grey-700)');
setColor(palette.Chip, 'defaultIconColor', 'var(--mui-palette-grey-700)');
Expand Down Expand Up @@ -130,6 +132,7 @@ export default function extendTheme(options = {}, ...args) {
setColor(palette.AppBar, 'defaultBg', 'var(--mui-palette-grey-900)');
setColor(palette.AppBar, 'darkBg', 'var(--mui-palette-background-paper)'); // specific for dark mode
setColor(palette.AppBar, 'darkColor', 'var(--mui-palette-text-primary)'); // specific for dark mode
setColor(palette.Avatar, 'defaultBg', 'var(--mui-palette-grey-600)');
setColor(palette.Chip, 'defaultBorder', 'var(--mui-palette-grey-700)');
setColor(palette.Chip, 'defaultAvatarColor', 'var(--mui-palette-grey-300)');
setColor(palette.Chip, 'defaultIconColor', 'var(--mui-palette-grey-300)');
Expand Down

0 comments on commit b9cfab2

Please sign in to comment.