Search code examples
reactjsmaterial-ui

How to target backdrop color property of Material UI Dialog using styled components?


I'm looking to change the color of the backdrop of the Dialog component of Material UI using styled-components.

I found this thread on how to do exactly that but I'm not sure how to apply this to styled-components.

I currently haved a StyledDialog as such:

const StyledDialog = styled(Dialog).attrs({

  classes: { paper: 'container' },
  keepMounted: true,
  'aria-labelledby': 'alert-dialog-slide-title',
  'aria-describedby': 'alert-dialog-slide-description'
})`
  .container {
    border-radius: 0;
  }
`;

Solution

  • You can reference the backdrop via its global class ("MuiBackdrop-root") in the following manner:

    const StyledDialog = styled(Dialog)`
      .MuiBackdrop-root {
        background-color: lightgreen;
      }
    `;
    

    Edit styled-components Dialog backdrop

    Relevant Styled Components documentation: https://www.styled-components.com/docs/basics#pseudoelements-pseudoselectors-and-nesting