Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alert] Add support for CSS vars #32624

Merged
merged 6 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Comment on lines +134 to +149
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnajdova I could not see other ways to reduce the number of tokens if we want to keep the same behavior. 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be tricky if people add custom colors here :\ Should we maybe create these based on the PaletteOptions type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should go that far for now. There is a trade-off because some people might not want the autogenerated variables (increase in bundle size) and we need to create more APIs to let them exclude things that they don't want.

At this point, I think we can leave the new custom palette effort to the developers and make sure that the docs are clear and comprehensive.

};
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