Optimal method for personalizing MUI 5 Drawer while avoiding CSS conflicts

Currently, I am exploring how to personalize MUI 5 components by leveraging theming and props more and minimizing the use of CSS overrides (which can negate the benefits of MUI).

To start off, I have created my own theme:

export const theme = createTheme({
  palette: {
    mode: "light",
    primary: {
      main: "#12131c",
      contrastText: "#fcfcfc",
    },
  },
});

However, this custom theme only affects the <AppBar> component and not the <Drawer> component, which remains white.

https://i.stack.imgur.com/ZpEd0.png

Adding an sx override to the <Drawer> component did not produce any changes:

<Drawer
  sx={{ bgcolor: "primary.main", color: "primary.contrastText" }}
>
  <nav>
    <List>
      <ListItem disablePadding>
        <ListItemButton>
          <ListItemIcon>
            <InfoIcon />
          </ListItemIcon>
          <ListItemText primary="Inbox" />
        </ListItemButton>
      </ListItem>
      ...

Another suggestion from a Stack Overflow answer was to add:

<Drawer          
   PaperProps={{
      sx: {
        bgcolor: "primary.main",
        color: "primary.contrastText",
      },
   }}
>

Although this solution works better, it does not apply to the <ListItemIcon>, and the color change on hover for List Items is lost.

https://i.stack.imgur.com/fsLvH.png

I could manually add sx overrides to all relevant components to achieve the desired style, but I believe this goes against the intended purpose. Is there a more effective way to utilize and benefit from the MUI system? Thank you

Answer №1

https://mui.com/material-ui/customization/theme-components/

If you are looking to customize your theme, consider something like this: (in your theme)

    palette: {...},
    components: {
        MuiDrawer: {
            styleOverrides: {
                root: ({ theme }) => ({
                    backgroundColor: theme.palette.primary.main,
                    color: theme.palette.primary.contrastText,
                }),
            },
        },
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Configuring the React Typescript router to support username-based URLs should be done in a way that does not cause conflicts with other routes

I am looking to display a user's profile on a URL format such as www.domain.com/<userName> and load the component ShowProfile. I want to ensure that terms is not mistaken for a username, so if I visit www.domain.com/terms, I do not want to load ...

The React component was not able to receive any children as input

My Typescript-written React component is called GradientText.tsx: import React, { ReactNode } from "react" import { cn } from "@/lib/utils" const typeMap = { h1: "h1", h2: "h2", p: "p", } inte ...

Webstorm showcases files with similar content in a distinct manner

In Webstorm, everything is color-coded based on whether it is a function, object, etc. I am working with JavaScript. I have noticed that in two different files, the same line of code looks differently: var Comment = React.createClass({ In file 1: "var" ...

Modify the border color for the focus state in the Select Component of Material-UI library for React

I am currently attempting to modify the appearance of the Select Component when it is in focus, specifically from the Material-UI library. To clarify, I am referring to the appearance when an element within the dropdown has been clicked (the on-focus state ...

An error message indicating that the page is currently being unloaded has appeared

While working on a NodeJS-ReactJS Isomorphic App, I encountered an issue when clicking on a Link. An error message popped up saying: Uncaught (in promise) Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by ...

The images are not displaying in the react-native-flatlist-slider component

App.js import { StyleSheet, Text, View } from 'react-native' import React from 'react' import { FlatListSlider } from 'react-native-flatlist-slider'; const images = [ { image: 'https://cdn-icons-png.flaticon. ...

Utilizing a JSDoc comment from an external interface attribute

Currently, I am in the process of developing a React application. It is common to want the props of a child component to be directly connected to the state of a parent component. To achieve this, I have detailed the following instructions in the interface ...

Is it secure to store the access token within the NextAuth session?

Utilizing a custom API built with Node.js and Express.js, I have implemented nextAuth to authenticate users in my Next.js application. Upon a successful login, the date is stored in the nextAuth session and can be accessed using the useSession hook. To acc ...

Upon refreshing the page, nested React routes may fail to display

When I navigate to the main routes and their nested routes, everything works fine. However, I encounter an issue specifically with the nested routes like /register. When I try to refresh the page, it comes up blank with no errors in the console. The main r ...

Troubleshooting a connection issue between Laravel and Next.js when calling axios

My current setup involves using Laravel for the backend and NextJs for the frontend. When I attempt to submit the form, I encounter an issue. The server responds with the following error message: error I have verified that the csrf token is correctly obt ...

What advantages could learning ReactJS first give me before diving into NextJS?

Just mastered TS and now faced with the decision of choosing a framework. I'm curious why it's recommended to learn ReactJS before NextJS. I've read countless articles advising this, but no one seems to delve into the reasons behind it. Ca ...

Toggle the presence of a string in an array with a checkbox

Currently, I am working on a user creation form for my Next.js front end project using TypeScript. The main goal is to allow an administrator to create new users by filling out a simple form which will generate a basic user object. Here is the structure of ...

Context-aware background imagery

I am facing an issue with displaying pictures in the background on my webpage. The problem I encountered is that sometimes my API does not provide images to display, so I want to set a condition to show another picture instead. However, I am unsure wheth ...

An open port could not be located on the x86_64-conda_cos6-linux-gnu system

Recently delved into learning React and encountered some challenges that I could use assistance with. I am in the midst of developing an application using this code snippet: npx create-react-app project-name Upon creating the application, I ran into issue ...

Changing all object values to true with React useState

In a certain file, I have defined an object with the following structure: export const items = { first: false, second: false, third: false } Within a component, I am using this object as shown below: import { items } from 'file'; const [el ...

Unused React import in the index.js file

Just starting to explore the world of reactJS. import React from 'react'; import ReactDOM from 'react-dom'; var App = ()=>{ return <div> Hello !!</div> } ReactDOM.render(<App />, document.getElementById(' ...

Is there a solution for the noticeable delay in loading fonts on a web page?

I've encountered an issue in my React project where a Google font I'm using seems to briefly load as a different font before switching to the correct one. How can I prevent this from happening? This is how I'm loading the font in public/ind ...

Stopping the infinite refresh issue in your React webpack application

Every time I modify the TS file, Webpack keeps refreshing the page without stopping. The console message reads: "@ebpack 5.66.0 compiled successfully" I've searched online and experimented with various plugins, but none of them seem to solve the issu ...

The term 'firestore' is not available on the specified type 'typeof import'

I'm facing some trouble setting up this app because the firebase library that has been exported from firebase/app seems to be missing the firestore() method. I have attached an image for reference. Here is my code. Could someone kindly assist me in re ...

Error: Attempting to access property 'map' of an undefined value

I am currently working on setting up a unit test for the code snippet below. I have identified where the error is occurring: populateActionDropdown = (actionList) => { let actions = actionList.map(action => { if (action.Name != 'Sig ...