I'm currently utilizing the react mui package, specifically @mui/x-date-pickers. Could someone kindly provide guidance on how to customize the color of the

I am currently working with MUI in React and using the DatePicker component from the @mui/x-date-pickers library. The version I am using is 6.0.3. I have encountered an issue where, upon selecting the first day, the specified color changes as shown in the image below. When reopening the calendar window, the default color is displayed until clicking on another area, which then changes the color back to the specified one. Is there a solution to this problem that anyone can provide? I have tried searching for a fix but haven't had any luck, so I'm reaching out here for help. Please advise if my search approach is incorrect.

https://i.stack.imgur.com/bIEPe.png https://i.stack.imgur.com/0Ixb0.png https://i.stack.imgur.com/efVCM.png

Below is the code snippet I'm using:

import { LGTheme } from '@libs/color';
import { DatePicker } from '@mui/x-date-pickers';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { koKR } from '@mui/x-date-pickers/locales';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

const DatePickerTest = () => {
    return (
        <LocalizationProvider
            dateAdapter={AdapterDayjs}
            adapterLocale={'ko'}
            localeText={koKR.components.MuiLocalizationProvider.defaultProps.localeText}
            dateFormats={{ monthAndYear: 'YYYY년 MM월' }}
        >
            <DatePicker
                label=""
                format="YYYY-MM-DD"
                slotProps={{
                    textField: {
                        size: 'small',
                    },
                    day: {
                        sx: {
                            ['&[data-mui-date="true"] .Mui-selected']: {
                                // Reset the background color of the selected date
                                backgroundColor: 'blue',
                            },
                            ':not(.Mui-selected)': {
                                backgroundColor: '#fff',
                                borderColor: LGTheme[0],
                            },
                            '&.Mui-selected': {
                                color: '#fff',
                                backgroundColor: LGTheme[0],
                                borderColor: LGTheme[0],
                                ':hover': {
                                    color: '#fff',
                                    backgroundColor: LGTheme[0],
                                    borderColor: LGTheme[0],
                                },
                            },
                            ':hover': {
                                color: '#fff',
                                backgroundColor: LGTheme[0],
                                borderColor: LGTheme[0],
                            },
                        },
                    },
                }}
            />
        </LocalizationProvider>
    );
};

export default DatePickerTest;


Answer №1

If you are encountering an issue with the styling of your date picker, it could be due to the use of the .Mui-selected class which only affects the selected date and not the current date. The current date is highlighted when you open the calendar and might not match the selected date. To target the current date, you should utilize the .MuiPickersDay-root class in your sx prop.

To resolve this problem, include both the .MuiPickersDay-root and .Mui-selected classes in your sx prop like this:

<DatePicker
  slotProps={{
    day: {
      sx: {
        "&.MuiPickersDay-root.Mui-selected": {
          backgroundColor: "red",
        },
      },
    },
  }}
/>

You can view a complete example here.

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

What is the reason for incorporating the footer within the container?

To see the issue in question, please visit: The footer needs to span the full width. Here is the code snippet for the page: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="container"> <div c ...

ReactJS: The object's value is not defined

I am working on developing an application to display Covid 19 statistics. Initially, I aim to showcase global figures when the component is loaded using useEffect hooks. However, upon attempting to render global.confirmed, it returns an object. When I furt ...

Having Trouble with jQuery toggleClass

I'm currently working on a project where I have implemented a button that displays a menu when clicked. I am attempting to change the color of the button when it is active, however, the toggleClass function is not behaving as expected. Below is the co ...

The npx command to create a new react-app doesn't seem to be functioning as expected

Encountered errors while running create react-app in the terminal. npx create react-app sample Error: EPERM: operation not permitted, mkdir 'C:\Users\Yasiru' TypeError: Cannot read property 'loaded' of undefined at exit ...

Group Hover by StyleX

I recently experimented with the innovative StyleX library and encountered a particular challenge. Can a group hover effect be achieved for a component solely using this library? For instance, let's assume we have the following component in Tailwind ...

Utilize the atan2() function to rotate a div so that it follows the movement of the mouse

I am trying to create an effect where the mouse aligns with the top of a div and the div rotates as the mouse moves. The rotation should happen when the top part of the div is in line with the mouse cursor. My goal is to achieve this using the atan2 functi ...

The X-Frame-Options header with the DENY directive is specifically designed to restrict framing on backend port

While working on spring security, I have configured the headers.frameOptions to DENY. When testing this by embedding my backend endpoint in an iframe with localhost:8080 here, everything functions correctly. However, when attempting to embed the frontend l ...

Creating dynamic components in ReactJS allows for versatile and customizable user interfaces. By passing

Within the DataGridCell component, I am trying to implement a feature that allows me to specify which component should be used to render the content of the cell. Additionally, I need to pass props into this component. Below is my simplified attempt at achi ...

Expo-app-auth encountering network error: Broken request in React Native

I've been working on implementing oAuth with the Spotify API in my React-Native app, but I keep encountering an error message that says ExpoAppAuth.get Auth: Network Error. I'm having trouble figuring out what's causing this issue and where ...

Reactjs throwing an Unsupported Media Type error with code 415

I am currently working on implementing a delete function to remove banner images. Here is the code snippet for my delete function: const [del, setDel] = useState([]); const DeleteBanner = async (banner) => { setDel(banner); ...

Enhancing Security Measures in a ReactJS Application on Heroku by Implementing Content Security

I recently encountered a frustrating issue with my CSP policy that I just can't seem to resolve. The setup involves a reactjs application running on Heroku within an expressjs environment. Here's a glimpse of my folder structure: ExpressApp ...

I am experiencing an issue where the posts in my .map() function are not showing up on

I've gone over my code multiple times, but I can't seem to pinpoint the issue. I'm working on a blog build tutorial using Next.js 13.4 with Sanity.io. Within my BlogList component, when I console log my posts, all posts show up in the termi ...

problem with custom scroll bars on Chrome and Internet Explorer

Below is the URL for the designated folder The vertical scroll bar is functioning properly across all browsers on my personal computer, however, it appears to be malfunctioning on Chrome and IE of a select few other devices. I conducted a test on a machi ...

Encountered a glitch while trying to install React JS using npx create-react-app xyz

After entering the command in the terminal, I encountered an error stating: npm Err! code-ENOENT npm Err! syscall lstat npm Err! path Interestingly, this same command worked perfectly on my instructor's laptops. For reference, I have attached a snaps ...

What is the best way to showcase an SVG image (stored on Cloudinary) using the Next.js Image tag?

Currently, I am creating a project using Next.js with Strapi as the CMS and hosting images on Cloudinary. One issue I have encountered is that SVG images do not display when using the Image tag from Next.js. I came across a thread discussing this topic h ...

Is it possible for me to convert the contents of a <div> into a rasterized image when printing?

I have been using jquery.printElement.js to print a custom calendar table that I created. The functionality inside a div is mostly working as expected, but I am facing some challenges with the CSS. Despite passing along all necessary .css files, my cells a ...

Display HTML content within a Material-UI Card component using React

Currently, I am utilizing the < CardHeader /> component nested within a < Card />. <CardHeader title={card.title} subheader={`${moment(card.createdAt).startOf('minute').fromNow()}` + ' by ' + <div>ABC</div>}/ ...

Tap on the image placeholder to replace it with your own image

Just starting out, I have the following HTML code within a <form>: <div class="image-upload"> <img id="send_photo_img1" src="assets/images/index2.png"/> <input id="send_photo_input1" type="file"/> <img id="send_photo_img2" sr ...

Dynamic color changing AppBar in Material-UI while scrolling in a React application

I have been struggling to change the color of the material-ui Appbar component when scrolling. I am not sure if there is a built-in functionality for this or if 'useScrollTrigger' can be used in this case. Any guidance on how to achieve this woul ...

Create an animation effect where a div increases in height, causing the divs beneath it to shift downward in a

My aim is to create columns of divs where the user can click on a div to see more content as the height expands. I've managed to set this up, but it seems there's an issue with the document flow. When I click on a div in the first column, the div ...