question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autocomplete custom styles get overwritten by MUI styles (v5)

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

After upgrading to MUI5, the autocomplete options custom styles are loaded after the MUI styles which makes me use !important to override the styles.

image

Expected behavior 🤔

My styles should be loaded first like in other components in order to avoid the use of !important.

Your environment 🌎

`npx @mui/envinfo`
  System:
    OS: macOS 11.6.2
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Edge: Not Found
    Firefox: 93.0
    Safari: 15.2
  npmPackages:
    @emotion/react: ^11.5.0 => 11.5.0 
    @emotion/styled: ^11.3.0 => 11.3.0 
    @mui/core:  5.0.0-alpha.54 
    @mui/lab: ^5.0.0-alpha.54 => 5.0.0-alpha.54 
    @mui/material: ^5.1.0 => 5.1.0 
    @mui/private-theming:  5.1.0 
    @mui/styled-engine:  5.1.0 
    @mui/styles: ^5.1.0 => 5.1.0 
    @mui/system:  5.1.0 
    @mui/types:  7.1.0 
    @mui/utils:  5.1.0 
    @types/react: ^17.0.22 => 17.0.24 
    react: 17.0.2 => 17.0.2 
    react-dom: 17.0.2 => 17.0.2 
    typescript: ^4.4.3 => 4.4.3 

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mnajdovacommented, Dec 21, 2021

I am already using that selector.

You are not based on the code. You should use something like:

styles.css

 .listbox {
+  // Need to bump the specificity on the options
+  & .MuiAutocomplete-option {
+    // the menuItem styles
+ }
}
  <Autocomplete
    id={name}
    classes={{
-     option: styles.menuItem,
      listbox: styles.menuList,
      noOptions: styles.noOptions,
      groupLabel: styles.headerItem,
    }} />

If there are problems please create a codesandbox, otherwise, it’s hard to iterate and verify that it works. cc @hbjORbj probably these are more opportunities for improving the migration guide.

1reaction
tihuancommented, Apr 26, 2022

We’re probably missing something, but Dialog doesn’t seem to append the custom styles at the end?

In this codesanbox, we create a custom Paper component and use it in Dialog:

const StyledPaper = styled(Paper)`
  background-color: pink;
  max-width: revert; // <-- doesn't work
  width: 1200px;
`;

<Dialog ... PaperComponent={StyledPaper}>
  ...
</Dialog>

But somehow our custom style rules are not appended at the end of the Paper class, so we can’t override max-width:

Screen Shot 2022-04-13 at 10 47 29 AM

However, if we wrap our custom style with & { ... }, then the custom styles are in their own inline style tag, and thus works:

codesandbox

const StyledPaper = styled(Paper)`
  & { // <-- net change
      background-color: pink;
      max-width: revert; // <-- works now!
      width: 1200px;
  }
`;
Screen Shot 2022-04-13 at 11 00 47 AM

Question:

Is it normal that sometimes custom styles are appended at the end of the rules and sometimes it’s not?

Our suspicion is that perhaps our custom styles are in the middle of the MuiDialog-paper css class, because Dialog adds more default MUI styles to its PaperComponent, thus overriding our custom StyledPaper. And if this is the intended behavior, is & { ... } the recommended solution?

Thank you!

CC: @mnajdova please kindly help take a look at this if possible, since resolving/clarifying this will help our company’s design system MUIv5 roll out 😄 🙏

UPDATE: Created a separate issue for SEO!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change styles of Material-UI Autocomplete list?
One way of doing this is by adjusting the elevation of the Paper component used by Autocomplete . The default elevation is 1....
Read more >
MUI v5 -> How to style options inside Autocomplete ...
I'm trying to apply some basic styles to the options inside the Autocomplete component from MUI v5. I'm just trying to change the...
Read more >
Autocomplete API - Material UI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
Custom React Component styles being overwritten by ...
Coding example for the question Custom React Component styles being overwritten by Material-UI style-Reactjs.
Read more >
Global Styling with Material-UI Theme Overrides and Props
Learn how to use global CSS overrides and default props in a theme to customize all instances of a Material-UI component in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found