简体   繁体   English

MUI 对话框背景色

[英]MUI Dialog Background Color

I have a mui dialog and would like to set its background to black.我有一个 mui 对话框,想将其背景设置为黑色。 (no color), but I don't succeed. (没有颜色),但我没有成功。 And I don't understand why it doesn't work.而且我不明白为什么它不起作用。

My Dialog:我的对话:

export const MyDialog = (props: any) => {
        return (
            <ThemeProvider theme={TestTheme}>
                <CssBaseline />
            <Dialog open={true}>
                <DialogTitle>Title</DialogTitle>
                <DialogContentText>Text</DialogContentText>
            </Dialog>
            </ThemeProvider>
        );
};

My TestTheme looks like this:我的 TestTheme 看起来像这样:

export const TestTheme = createTheme(
    {palette: {
            mode: 'dark',
            background: {
                paper: '#000000',
                // paper: 'red',
                default: '#000000',
            },
        }
    }
)

and it looks like this:它看起来像这样:

在此处输入图像描述

As we can see the dialog color has not the same black as the background even I have set the colors for both correctly.正如我们所看到的,即使我已经为两者正确设置了 colors,对话框颜色也与背景不同。

If I set the colors:如果我设置 colors:

            paper: 'red',
            // paper: '#000000',
            // default: '#000000',
            default: 'blue',

在此处输入图像描述

How can I set the background color of the dialog to black (no color)?如何将对话框的背景颜色设置为黑色(无颜色)? (Regardless if it make sence or not, I would like to understand) (不管有没有道理,我都想明白)

You are doing everything right with the way you set the background color.您正在按照设置背景颜色的方式正确地做所有事情。 The reason you see two different black colours is because when the Dialog is open is getting a class of MuiBackdrop-root which has a background-colour of background-color: rgba(0, 0, 0, 0.5);你看到两种不同的黑色的原因是因为当Dialog打开时得到一个 class 的MuiBackdrop-root有一个背景颜色的background-color: rgba(0, 0, 0, 0.5); so this additional semi transparent layer is causing this colour difference.所以这个额外的半透明层导致了这种色差。

Here is a codesandbox with a solution to your issue.这是一个代码框,可以解决您的问题。

Code:代码:

    <Dialog open={true} 
      sx={{ //You can copy the code below in your theme
        background: '#000',
        '& .MuiPaper-root': {
          background: '#000'
        },
        '& .MuiBackdrop-root': {
          backgroundColor: 'transparent' // Try to remove this to see the result
        }
      }}>
        <DialogTitle sx={{ color: "white" }}>Title</DialogTitle>
        <DialogContentText sx={{ color: "white" }}>Text</DialogContentText>
      </Dialog>

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM