Skip to content

Commit

Permalink
[Alert] Add support for CSS vars (#32624)
Browse files Browse the repository at this point in the history
  • Loading branch information
haneenmahd committed Jun 22, 2022
1 parent 67ec02d commit 76e2ef6
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 17 deletions.
98 changes: 98 additions & 0 deletions docs/pages/experiments/material-ui/alert.tsx
@@ -0,0 +1,98 @@
import * as React from 'react';
import {
Experimental_CssVarsProvider as CssVarsProvider,
useColorScheme,
} from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Container from '@mui/material/Container';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import { Alert } from '@mui/material';

const ColorSchemePicker = () => {
const { mode, setMode } = useColorScheme();
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return null;
}

return (
<Button
variant="outlined"
onClick={() => {
if (mode === 'light') {
setMode('dark');
} else {
setMode('light');
}
}}
>
{mode === 'light' ? <Moon /> : <Sun />}
</Button>
);
};

export default function CssVarsTemplate() {
return (
<CssVarsProvider>
<CssBaseline />
<Container sx={{ my: 5 }}>
<Box sx={{ pb: 2 }}>
<ColorSchemePicker />
</Box>
<Box
sx={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(256px, 1fr))',
gap: 2,
'& > div': {
placeSelf: 'center',
},
}}
>
<Alert variant="filled" severity="error">
This is an error alert — check it out!
</Alert>
<Alert variant="filled" severity="warning">
This is a warning alert — check it out!
</Alert>
<Alert variant="filled" severity="info">
This is an info alert — check it out!
</Alert>
<Alert variant="filled" severity="success">
This is a success alert — check it out!
</Alert>
<Alert variant="standard" severity="error">
This is an error alert — check it out!
</Alert>
<Alert variant="standard" severity="warning">
This is a warning alert — check it out!
</Alert>
<Alert variant="standard" severity="info">
This is an info alert — check it out!
</Alert>
<Alert variant="standard" severity="success">
This is a success alert — check it out!
</Alert>
<Alert variant="outlined" severity="error">
This is an error alert — check it out!
</Alert>
<Alert variant="outlined" severity="warning">
This is a warning alert — check it out!
</Alert>
<Alert variant="outlined" severity="info">
This is an info alert — check it out!
</Alert>
<Alert variant="outlined" severity="success">
This is a success alert — check it out!
</Alert>
</Box>
</Container>
</CssVarsProvider>
);
}
59 changes: 42 additions & 17 deletions packages/mui-material/src/Alert/Alert.js
Expand Up @@ -52,30 +52,55 @@ const AlertRoot = styled(Paper, {
padding: '6px 16px',
...(color &&
ownerState.variant === 'standard' && {
color: getColor(theme.palette[color].light, 0.6),
backgroundColor: getBackgroundColor(theme.palette[color].light, 0.9),
[`& .${alertClasses.icon}`]: {
color:
theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light,
},
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
backgroundColor: theme.vars
? theme.vars.palette.Alert[`${color}StandardBg`]
: getBackgroundColor(theme.palette[color].light, 0.9),
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color:
theme.palette.mode === 'dark'
? theme.palette[color].main
: theme.palette[color].light,
},
}),
...(color &&
ownerState.variant === 'outlined' && {
color: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${theme.palette[color].light}`,
[`& .${alertClasses.icon}`]: {
color:
theme.palette.mode === 'dark' ? theme.palette[color].main : theme.palette[color].light,
},
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${(theme.vars || theme).palette[color].light}`,
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color:
theme.palette.mode === 'dark'
? theme.palette[color].main
: theme.palette[color].light,
},
}),
...(color &&
ownerState.variant === 'filled' && {
color: theme.palette.getContrastText(
theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main,
),
fontWeight: theme.typography.fontWeightMedium,
backgroundColor:
theme.palette.mode === 'dark' ? theme.palette[color].dark : theme.palette[color].main,
...(theme.vars
? {
color: theme.vars.palette.Alert[`${color}FilledColor`],
backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`],
}
: {
backgroundColor:
theme.palette.mode === 'dark'
? theme.palette[color].dark
: theme.palette[color].main,
color: theme.palette.getContrastText(
theme.palette.mode === 'dark'
? theme.palette[color].dark
: theme.palette[color].main,
),
}),
}),
};
});
Expand Down
18 changes: 18 additions & 0 deletions packages/mui-material/src/styles/createPalette.d.ts
Expand Up @@ -130,6 +130,24 @@ export interface PaletteWithChannels {
getContrastText: (background: string) => string;
augmentColor: (options: PaletteAugmentColorOptions) => PaletteColor;
// component tokens
Alert: {
errorColor: string;
infoColor: string;
successColor: string;
warningColor: string;
errorFilledBg: string;
infoFilledBg: string;
successFilledBg: string;
warningFilledBg: string;
errorStandardBg: string;
infoStandardBg: string;
successStandardBg: string;
warningStandardBg: string;
errorIconColor: string;
infoIconColor: string;
successIconColor: string;
warningIconColor: string;
};
AppBar: {
defaultBg: string;
darkBg: string;
Expand Down
57 changes: 57 additions & 0 deletions packages/mui-material/src/styles/experimental_extendTheme.js
Expand Up @@ -79,6 +79,7 @@ export default function extendTheme(options = {}, ...args) {

// assign component variables
assignNode(palette, [
'Alert',
'AppBar',
'Avatar',
'Chip',
Expand All @@ -95,6 +96,34 @@ export default function extendTheme(options = {}, ...args) {
'Tooltip',
]);
if (key === 'light') {
setColor(palette.Alert, 'errorColor', darken(palette.error.light, 0.6));
setColor(palette.Alert, 'infoColor', darken(palette.info.light, 0.6));
setColor(palette.Alert, 'successColor', darken(palette.success.light, 0.6));
setColor(palette.Alert, 'warningColor', darken(palette.warning.light, 0.6));
setColor(palette.Alert, 'errorFilledBg', 'var(--mui-palette-error-main)');
setColor(palette.Alert, 'infoFilledBg', 'var(--mui-palette-info-main)');
setColor(palette.Alert, 'successFilledBg', 'var(--mui-palette-success-main)');
setColor(palette.Alert, 'warningFilledBg', 'var(--mui-palette-warning-main)');
setColor(palette.Alert, 'errorFilledColor', lightPalette.getContrastText(palette.error.main));
setColor(palette.Alert, 'infoFilledColor', lightPalette.getContrastText(palette.info.main));
setColor(
palette.Alert,
'successFilledColor',
lightPalette.getContrastText(palette.success.main),
);
setColor(
palette.Alert,
'warningFilledColor',
lightPalette.getContrastText(palette.warning.main),
);
setColor(palette.Alert, 'errorStandardBg', lighten(palette.error.light, 0.9));
setColor(palette.Alert, 'infoStandardBg', lighten(palette.info.light, 0.9));
setColor(palette.Alert, 'successStandardBg', lighten(palette.success.light, 0.9));
setColor(palette.Alert, 'warningStandardBg', lighten(palette.warning.light, 0.9));
setColor(palette.Alert, 'errorIconColor', 'var(--mui-palette-error-light)');
setColor(palette.Alert, 'infoIconColor', 'var(--mui-palette-info-light)');
setColor(palette.Alert, 'successIconColor', 'var(--mui-palette-success-light)');
setColor(palette.Alert, 'warningIconColor', 'var(--mui-palette-warning-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)');
Expand Down Expand Up @@ -131,6 +160,34 @@ export default function extendTheme(options = {}, ...args) {
setColor(palette.TableCell, 'border', lighten(alpha(palette.divider, 1), 0.88));
setColor(palette.Tooltip, 'bg', alpha(palette.grey[700], 0.92));
} else {
setColor(palette.Alert, 'errorColor', lighten(palette.error.light, 0.6));
setColor(palette.Alert, 'infoColor', lighten(palette.info.light, 0.6));
setColor(palette.Alert, 'successColor', lighten(palette.success.light, 0.6));
setColor(palette.Alert, 'warningColor', lighten(palette.warning.light, 0.6));
setColor(palette.Alert, 'errorFilledBg', 'var(--mui-palette-error-dark)');
setColor(palette.Alert, 'infoFilledBg', 'var(--mui-palette-info-dark)');
setColor(palette.Alert, 'successFilledBg', 'var(--mui-palette-success-dark)');
setColor(palette.Alert, 'warningFilledBg', 'var(--mui-palette-warning-dark)');
setColor(palette.Alert, 'errorFilledColor', darkPalette.getContrastText(palette.error.dark));
setColor(palette.Alert, 'infoFilledColor', darkPalette.getContrastText(palette.info.dark));
setColor(
palette.Alert,
'successFilledColor',
darkPalette.getContrastText(palette.success.dark),
);
setColor(
palette.Alert,
'warningFilledColor',
darkPalette.getContrastText(palette.warning.dark),
);
setColor(palette.Alert, 'errorStandardBg', darken(palette.error.light, 0.9));
setColor(palette.Alert, 'infoStandardBg', darken(palette.info.light, 0.9));
setColor(palette.Alert, 'successStandardBg', darken(palette.success.light, 0.9));
setColor(palette.Alert, 'warningStandardBg', darken(palette.warning.light, 0.9));
setColor(palette.Alert, 'errorIconColor', 'var(--mui-palette-error-main)');
setColor(palette.Alert, 'infoIconColor', 'var(--mui-palette-info-main)');
setColor(palette.Alert, 'successIconColor', 'var(--mui-palette-success-main)');
setColor(palette.Alert, 'warningIconColor', 'var(--mui-palette-warning-main)');
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
Expand Down

0 comments on commit 76e2ef6

Please sign in to comment.