admin管理员组文章数量:1189389
Is there any way I can blur the background content when an MUI dialog appears? (the default action is darkening the background, but I need to blur it). Here's what I am looking for:
Is there any way I can blur the background content when an MUI dialog appears? (the default action is darkening the background, but I need to blur it). Here's what I am looking for:
Share Improve this question asked Jun 25, 2020 at 14:46 Ali TouraniAli Tourani 1,2473 gold badges22 silver badges33 bronze badges 2 |2 Answers
Reset to default 22One way to solve this is to use a backdropFilter on the dialog backdrop, which can blur the background. The backdropFilter css property is a relatively new css feature but it works well on Chrome, Edge, Samsung Internet and Safari. It doesn't work on Firefox at this time. But maybe that's suitable for you? Here's a code example:
import React from "react";
import { Dialog, DialogContent, makeStyles, Theme, Typography } from "@material-ui/core";
const useStyles = makeStyles((theme: Theme) => ({
backDrop: {
backdropFilter: "blur(3px)",
backgroundColor:'rgba(0,0,30,0.4)'
},
}));
export default function ExampleDialogComponent() {
const classes = useStyles();
return (
<Dialog
open={true}
BackdropProps={{
classes: {
root: classes.backDrop,
},
}}
onClose={() => {}}
>
<DialogContent style={{ textAlign: "center" }}>
<Typography variant="body1">Example Dialog</Typography>
</DialogContent>
</Dialog>
);
}
Update for Material UI 5
In the upcoming version of Material UI 5, you could use a styled component like this:
import Box from "@material-ui/core/Box";
import { experimentalStyled as styled } from "@material-ui/core/styles";
import Dialog, { DialogProps } from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogActions from "@material-ui/core/DialogActions";
import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline";
const BlurryDialog = styled(Dialog)<DialogProps>(({ theme }) => ({
backdropFilter: "blur(5px)",
// other styles here...
}));
export default function ExampleNextDialog() {
return (
<>
<CssBaseline />
<BlurryDialog fullWidth onClose={() => {}} open={true} maxWidth="xs">
<DialogTitle>Example Dialog</DialogTitle>
<DialogContent>Example Content Here</DialogContent>
<DialogActions>
<Button>ooooh.</Button>
</DialogActions>
</BlurryDialog>
<Box
sx={{
minHeight: "100vh",
background:
"url(https://source.unsplash.com/random) no-repeat center center",
backgroundSize: "cover",
}}
></Box>
</>
);
}
Or use the sx prop like this:
import Box from "@material-ui/core/Box";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogActions from "@material-ui/core/DialogActions";
import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline";
export default function ExampleNextDialog() {
return (
<>
<CssBaseline />
<Dialog
fullWidth
onClose={() => {}}
open={true}
maxWidth="xs"
sx={{
backdropFilter: "blur(5px)",
//other styles here
}}
>
<DialogTitle>Example Dialog</DialogTitle>
<DialogContent>Example Content Here</DialogContent>
<DialogActions>
<Button>ooooh.</Button>
</DialogActions>
</Dialog>
<Box
sx={{
minHeight: "100vh",
background:
"url(https://source.unsplash.com/random) no-repeat center center",
backgroundSize: "cover",
}}
></Box>
</>
);
}
You need to add some class to your content root block after dialog opens and add filter styles for that class: filter: blur(3px);
本文标签: javascriptReact Blur background when MaterialUI Dialog appearedStack Overflow
版权声明:本文标题:javascript - React: Blur background when Material-UI Dialog appeared - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738401805a2084820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
filter: blur(3px);
– demkovych Commented Jun 25, 2020 at 14:50style={{ filter: showDIalog ? "blur(3px)" : "none" }}
to the root container solved it :) – Ali Tourani Commented Jun 25, 2020 at 14:59