9

In MUI v4, it's very easy to inspect the DOM and isolate the exact component file and style block within that file:

MUI v4 way of finding a specific element/style class

However, in MUI v5, this is not possible, which makes it much harder to debug and find elements in styling buried in your code (my screenshot is trivial, but imagine having a large app with hundreds of components):

MUI v5 way of doing styling with DOM inspection

Is there a better debugging MUI v5 to allow for faster finding/isolation of a specific component/styling block in the code?

Also, here are the various ways I've found of doing (and not doing) styling in MUI v5, however, none of them show good debugging info when inspecting the DOM (see the github source for the difference between classes, styles, and sty):

      {/* OK - these work */}
      <Box sx={{ ...classes.text }}>HELLO</Box>
      <Box style={classes.text}>HELLO</Box>
      <Box css={styles.text}>HELLO</Box>
      <Box sx={sty.text}>HELLO</Box>

      {/* FAIL - these do not work - the v4 way, for completeness */}
      <Box classes={{ root: classes.text }}>HELLO</Box>
      <Box className={classes.text}>HELLO</Box>
      <Box class={classes.text}>HELLO</Box>

Source: https://github.com/masaok/react-css-google-demo/blob/master/src/archive/GoogleTest.jsx#L72-L81

7
  • Use the select inspector tool (the first button in the upper-left of the dev tools, it looks like a square with a mouse cursor in it) and then select the UI element in the page to select and focus on it in the "Elements" tab. No need for fancy CSS class names, etc...
    – Drew Reese
    Commented Feb 17, 2022 at 6:01
  • Hi Drew, yes, I understand how to use the inspector tool, however, if you look closely at the the screenshots in my question, the MUI v4 version shows the file (component) and style block in the element class in the DOM (very helpful in locating the code), whereas, in the MUI v5 version, the DOM only says MuiBox-root for every Box element (not helpful).
    – ReactDev
    Commented Feb 17, 2022 at 6:34
  • That is a view in the dev tool, nothing from React is driving that.
    – Drew Reese
    Commented Feb 17, 2022 at 6:45
  • Are you referring to my MUI v4 or v5 screenshot? or both? Notice that in the v4 version, the HelloWorld-root and HelloWorld-main match the name of the component and the styling "class" name. In the v5 version, this does not exist, making it much harder to know exactly which component is rendering the element hovered in the dev tool.
    – ReactDev
    Commented Feb 17, 2022 at 7:09
  • Oh, sorry, I thought you were referring to the "styles" panel the shows the classnames and applied CSS rules. I'm still not sure what the issue is other than that you are asking how to use custom class names instead of the auto-generated names commonly used in styling libraries. Is that all you are really asking about?
    – Drew Reese
    Commented Feb 17, 2022 at 7:14

1 Answer 1

-1

You can add a label attribute to your styles and it will show up in the inspector. E.g.:

main: { color: 'blue', fontWeight: 'bold', label: 'someLabel' }

And this will show up in the inspector as after the css-hash:

E.g:

<div class="MuiBox-root css-h8tuqk-someLabel">...</div>

Elements tab

Not the answer you're looking for? Browse other questions tagged or ask your own question.