Skip to content

Commit

Permalink
[Snackbar] Add support for CSS variables (#32603)
Browse files Browse the repository at this point in the history
  • Loading branch information
gin1314 authored May 5, 2022
1 parent 27a09c8 commit 50160e5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions docs/pages/experiments/material-ui/snackbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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 Container from '@mui/material/Container';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
import MuiAlert, { AlertProps } from '@mui/material/Alert';

const Alert = React.forwardRef<HTMLDivElement, AlertProps>(function Alert(props, ref) {
return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />;
});

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 SnackbarCssVars() {
const [open, setOpen] = React.useState(false);
const handleClick = () => {
setOpen(true);
};

const handleClose = (event?: React.SyntheticEvent | Event, reason?: string) => {
if (reason === 'clickaway') {
return;
}

setOpen(false);
};

return (
<CssVarsProvider>
<CssBaseline />
<Container sx={{ my: 5 }}>
<Box sx={{ pb: 2 }}>
<ColorSchemePicker />
</Box>
<Box
sx={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(256px, 1fr))',
gridAutoRows: 'minmax(160px, auto)',
gap: 2,
'& > div': {
placeSelf: 'center',
},
}}
>
<Box>
<Button variant="outlined" onClick={handleClick}>
Open success snackbar
</Button>
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
<Alert onClose={handleClose} severity="success" sx={{ width: '100%' }}>
This is a success message!
</Alert>
</Snackbar>
</Box>
</Box>
</Container>
</CssVarsProvider>
);
}
2 changes: 1 addition & 1 deletion packages/mui-material/src/Snackbar/Snackbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const SnackbarRoot = styled('div', {
};

return {
zIndex: theme.zIndex.snackbar,
zIndex: (theme.vars || theme).zIndex.snackbar,
position: 'fixed',
display: 'flex',
left: 8,
Expand Down

0 comments on commit 50160e5

Please sign in to comment.