What is the best way to personalize Material UI elements, such as getting rid of the blue outline on the Select component?

Embarking on my journey of developing a React app, I made the decision to incorporate Material UI for its array of pre-built components.

However, delving into the customization of these components and their styles has proven to be quite challenging for me.

Here is a snippet of my code:

import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';

const Dropdown = ({ value, label, options, handleChange }) => {

    return (
        <FormControl
            className="dropdown-container"
            variant='outlined'
            sx={{
                minWidth: 120,
                backgroundColor: "#eb6060",
                borderRadius: "5px",
                border: "1px solid #eb6060",
            }}
        >
            <InputLabel shrink={false}>{value === "" ? label : ""}</InputLabel>
            <Select

                onChange={handleChange}
                label={value === "" ? label : ""}
                value={value}
                notched={false}
            >
                {options.map((option) => (
                    <MenuItem
                        key={option.code}
                        value={option.code}
                    >
                        {option.alias}
                    </MenuItem>
                ))}
            </Select>
        </FormControl>
    );
};

export default Dropdown;

This represents my endeavor in customizing MUI's Select component. Despite my efforts in eliminating the shrinking label and notch within the Select border, challenges persist.

  1. Am I approaching the customization process correctly?

  2. In what ways can I further enhance the customization by eliminating the blue focus outline displayed upon selecting the dropdown component, along with the keyboard focus highlight from the first MenuItem?

UPDATE: By implementing the code provided by Prem Chaudhary, the blue outline has effectively disappeared. The current appearance is showcased below:

How can I independently devise solutions like this? Specifically, I aim to make the subsequent modifications to this component:

  1. Eliminate the blue highlight from the select component label
  2. Omit the grey highlight on the initial menu item during menu expansion
  3. Slightly intensify the select button's background when opening the menu

Ideally, I would like to resolve each issue autonomously without continuously resorting to external assistance. How do I determine the classes necessitating style overrides? Although I attempted leveraging CSS overview for the first point, achieving the desired adjustment remained elusive:

Answer №1

To implement the desired customizations, consider making the following adjustments:

Modify the styles of MuiOutlinedInput-notchedOutline within the FormControl:

sx={{
  minWidth: 120,
  backgroundColor: "#eb6060",
  borderRadius: "5px",
  border: "1px solid #eb6060",
  '& .MuiOutlinedInput-notchedOutline': {
       border: 'none',
   },
   '&:hover .MuiOutlinedInput-notchedOutline': {
       border: 'none',
   },
   '&.Mui-focused .MuiOutlinedInput-notchedOutline': {
       border: 'none',
   },
}}

In this snippet, the MuiOutlinedInput-notchedOutline styles for the FormControl have been adjusted to eliminate the outlined border in various states.

UPDATE: In cases of issues or specific requirements, you can explore these strategies:

Strategy 1: Documentation Investigation Thoroughly examining the provided documentation from the library or framework is a dependable method for problem-solving. Even though it may require time and effort, relying on official documentation ensures that our code aligns with future updates and support from the library maintainers.

Strategy 2: Browser Inspector Tool Usage A quick approach involves using browser inspector tools to inspect element structures and applied CSS. By inspecting elements, you can identify if your CSS is functioning as intended or being overridden by other styles.

For instance, you can utilize the browser inspector to address your initial issue.

Resolutions for subsequent concerns:

Adjust the styling of the FormControl:

sx={{ minWidth: 120,
    backgroundColor: "#eb6060",
    borderRadius: "5px",
    border: "1px solid #eb6060",
    '& .MuiOutlinedInput-notchedOutline': {
        border: 'none',
    },
    '&:hover .MuiOutlinedInput-notchedOutline': {
        border: 'none',
    },
    '&.Mui-focused .MuiOutlinedInput-notchedOutline': {
        border: 'none',
    },
    '& .MuiSelect-select': {
        color: '#FFF' // remove the blue highlight on select component label
    },
    '& .Mui-focused .MuiSelect-select': {
        backgroundColor: '#e84545' // Slightly darken the select button's background when menu is opened
    }
}}

Adjust the styling of the MenuItem:

sx={{ 
    '&.Mui-selected.Mui-focusVisible': {
        backgroundColor: '#FFF' //Set the highlight color to white on the first menu item when the menu opens
    }
}}

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

Integrating Facebook Videos

My website has a collection of video links that open in a fancybox when clicked using jQuery. However, there is an issue where the videos used to play with sound automatically, but now they require manual unmuting. I am unsure of why this behavior changed. ...

Utilizing Node and Electron to dynamically adjust CSS style properties

Having a dilemma here: I need to access the CSS properties from styles.css within Electron. Trying to use document.getElementsByClassName() won't work because Node doesn't have document. The goal is to change the color of a specific div when the ...

Trouble arises when trying to deploy React application on Github pages

After running npm run deploy and following all the steps, it shows that the project is published successfully. However, upon checking the Github hosted link, only my navbar component is visible. Despite setting up the routes correctly in app.js to display ...

CSS Sprite Animation Technique

Hello there, I've been attempting to create a simple animation using CSS and a sprite but for some reason it's not working. Can anyone help me figure out what I'm missing? I have created a sample on JS Fiddle. Could someone please explain w ...

An effective method for converting a string into a two-dimensional array using JavaScript

One challenge I am facing is checking from a string if a specific fruit has the correct amount on a given date. I've managed to achieve this by converting the string into a 2D array and iterating over the columns. While this method works, I can't ...

Retrieving Specific Text Data from Formik Dropdown<option>

I have implemented a reusable Material-UI select component in my project based on the code snippet provided in a tutorial. The SelectWrapper component is used to create a select dropdown with various options: import React from 'react'; import { T ...

I encountered an error of "Unexpected token '>'" while working with an

My code includes an ajax call and utilizes promises in the function: element.on("keypress", ".keyEvents", function(event) { if (event.which == 13) { // create the url and json object var putUrl = ...

I am interested in extracting specific data from the JSON response

I am trying to extract the value of the message parameter under the messages array where the parameter name is equal to documentId (highlighted in bold below). However, the code I have tried so far does not achieve this as needed. dynamic obj = JsonConver ...

In my demo, when I try to set up the Monaco Editor, I encounter the following error: "Unexpected usage at EditorSimpleWorker.loadForeignModule"

While setting up the monaco-editor in my demo located at https://github.com/liaodalin19903/react-test-02 I encountered the following error: Unexpected usage at EditorSimpleWorker.loadForeignModule (http://localhost:3000/static/js/bundle.js:115802:27) ...

The requested 'Pagination' component (imported as 'Pagination') could not be located within the 'swiper' library. Possible exports include Swiper and default

I was trying to implement pagination using swiper. I included the Pagination module with this import statement: import { Pagination } from "swiper"; Here's my configuration: The error that I encountered is : I have noticed that it w ...

Image-switching button

I'm new to JavaScript and struggling with my first code. I've been staring at it for two days now, but can't figure out what's wrong. The goal is to create an HTML page where the user can change an image by clicking on a button, and th ...

What could be the reason for my image not loading properly in Vue.js 3?

I'm struggling to load an image using a relative path and value binding with v-for in my template code. Despite following the correct syntax, the website is displaying an error indicating that it can't retrieve the image. Here's a snippet of ...

What is the most straightforward method to convert a current Express node app into a static site?

After primarily working with React and create-react-app, I've grown accustomed to the convenience of having a build utility for npm that simplifies deploying projects to static web hosting platforms like github pages or surge. This process ultimately ...

Vue 3 does not currently support the usage of Bootstrap components

Currently, I am diving into the world of Vuejs and experimenting with integrating bootstrap/vue-bootstrap components like Card or b-table. However, I encountered some issues along the way. [Vue warn]: Failed to resolve component: b-table If this is a nat ...

Creating TypeScript models from a JSON response in React components

My Angular 2 application retrieves a JSON string, and I am looking to map its values to a model. According to my understanding, a TypeScript model file is used to assist in mapping an HTTP Get response to an object - in this case, a class named 'Custo ...

Tips for sending two data elements from the frontend to the backend using an axios HTTP request

axios.delete(http://localhost:3001/api/delete/${movie}/${review}) The issue is that I am trying to pass 2 pieces of information, but it is not working. On the backend, my code looks like this: app.delete("/api/delete/:movie/:review", (req, res) => { co ...

Using React to iterate through the child components of the parent

I have created a component that can accept either a single child or multiple children. Here is an example with multiple children: <SideDataGridItem> <div id='top'> <div>A1</div> <div>B1</div> ...

Can the state stored in vuex be accessed by nested components?

After reviewing the documentation at: https://vuex.vuejs.org/guide/mutations.html#committing-mutations-in-components and watching the video tutorial here: I'm unsure whether the store is accessible in nested or child components within the parent co ...

Tips on adjusting the Leaflet Map's zoom level to display all markers in React Leaflet

I am currently working on a project with React Leaflet map that requires changing the center and zoom based on a set of markers. The goal is to adjust the zoom level so that all the markers are visible on the map. To achieve this change in view, I am util ...

The use of Ajax post results in the retrieval of multiple arrays containing objects that possess various values

I have a PHP file (ajax.php) that retrieves messages from a database and a JavaScript file (main.js) that sends an AJAX request to this PHP file. My goal is to create a table row in the JS file for each message returned by the PHP file. Main.js: functio ...