Ways to customize the border of the ListItemButton when it's in a selected state using Material UI?

I'm currently working on a feature that involves highlighting a ListItemButton with a specific background color when selected. However, I now want to take it a step further and add an outline or border color around the ListItemButton when it is selected. Unfortunately, my attempt using the borderColor attribute is not yielding the desired result.

https://i.stack.imgur.com/1stsM.png

See the source code snippet below:

 /* eslint-disable react/react-in-jsx-scope -- Unaware of jsxImportSource */
/** @jsxImportSource @emotion/react */
import { useState } from "react";
import ListItemButton from "@mui/material/ListItemButton";
import { css } from "@emotion/react";


export default function TestSample() {
  

  const [selected2, setSelected2] = useState(false);

  return (
    <div className="App">
      <ListItemButton
        sx={{
          "&.Mui-selected": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
          "&.Mui-focusVisible": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
          ":hover": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
        }}
        selected={selected2}
        onClick={() => setSelected2((prev) => !prev)}
      >
       ListItemButton
      </ListItemButton>

      
    </div>
  );
}

I would greatly appreciate any insights or feedback you can provide, especially since I am currently delving into MUI and trying to grasp its functionality and implementation. Thank you for your assistance!

Answer №1

The borderWidth and borderStyle properties are necessary for your current red border to be visible. Right now, the border is there but it's 0 pixels thick.

Alternatively, you can use a shorthand approach by simply specifying the border property like this:

sx={{
    border: '1px solid red'
}}

This is equivalent to:

sx={{
    borderStyle: 'solid',
    borderWidth: '1px',
    borderColor: 'red'
}}

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

I assigned a key prop to the child component, but I am still encountering the error message: "Each item in a list must have a distinct 'key' prop"

I am currently utilizing MongoDB and Express for the backend of my project. Here is part of my component: return ( <> <Addnote /> <div className="row my-3"> {notes.map((note) => { ...

A guide on adjusting the height of UI elements in Semantic: steps for modifying

One of my current challenges involves using Semantic UI and embedding it within an iFrame tag to display a video. However, I am struggling to adjust the height of the Semantic UI element. <div className="video_box ui embed"> ...

Use Material UI Grid to create a horizontal line design instead of focusing on making the

Is there a way to turn off responsiveness for the material UI grid similar to how it can be done with a basic HTML table? While an HTML table scales down and adds a horizontal scroll bar, the material UI grid remains responsive as shown in the example belo ...

Turn off slider trace animation?

Check out the slider component in MUI here: https://mui.com/material-ui/react-slider/ I'm currently exploring how to disable the animation on the nub so it moves instantly to the new position. Any advice on how to achieve this? ...

Data import customization feature

I have implemented a simple code that fetches data from a local json server when the page is loaded. However, I have duplicated this code in multiple places and I am looking to consolidate it into a custom hook. Can you guide me on how to correctly write ...

Implementing a feature in React/NextJS to selectively load an external script for users who are not authenticated

Our goal is to dynamically load an ad script only if a user is not authenticated. Please note that our project is built with Next.js. Here is a simplified example of how we are currently implementing the ad script loading functionality: const AdContext = ...

The JQuery CSS function is unable to alter the height of the element

I am working with a grid that contains various elements, each with the class item. When I click on one of these elements, I toggle the class expand to resize the element. <body> <div class="grid"> <a href="#"> < ...

Combining Multiple Middlewares in Next.js

Currently, I am using Next.js version 13.4.7 and have successfully implemented a middleware that manages redirection based on a token. Now, my goal is to introduce another middleware for internationalization (i18n) utilizing next-intl. However, I am facing ...

Tool for controlling the layout of the viewport with Javascript

I have experience using ExtJS in the past to create dashboards, and one of my favorite features is the full-screen viewport with a border layout. This allows for easy splitting of a dashboard into panels on different sides without creating excessive scroll ...

Creating a CSS section that expands with a minimum height of 100% and perfectly centers its content vertically

Seeking an elegant solution for having sections with a minimum height of 100% (window height). The div should expand to fit the content if it exceeds 100%, and vertically center the content within the div if it is smaller. I have managed to find a simple ...

Tips for aligning a div containing an image in the center of another div

Trying to perfectly center an image inside a div both vertically and horizontally using the display inline-block property. HTML <div class="center-wrapper"> <div class="image-holder"> <img src="picture.jpeg" alt="Centered Image"> ...

How can I deploy a react-express application to Azure cloud platform?

Struggling to deploy my react-express application on Azure. The code is divided into client and server directories. Attempted deployment using Azure Static Web application but encountered failure. https://i.stack.imgur.com/ailA0.png https://i.stack.imgur.c ...

Having trouble with your YouTube Live Stream not playing in the React Player version 2.9.0?

I successfully integrated react-player into my react.js website and it was working perfectly. However, after a few days, it suddenly stopped functioning. Even updating the plugin to version 2.9.0 did not resolve the issue. Strangely enough, standard YouTub ...

Align button text vertically in the middle using Flexbox with Bootstrap 4

Why is the text in the button displaying at the top instead of the center when using internet explorer? button { height: 100px; } <div class="container"> <button class="w-100 f-default-f btn btn-primary d-flex justify-content-between px-3"& ...

I recently added Tailwindcss to my project and set up the configuration after initialization. However, I am facing issues as my styles are not being applied to the page

This is my current tailwind configuration file. I have attempted different suggestions found on Stack Overflow, but none seem to be resolving the issue. Does anyone have any insights or catch any mistakes that may not be related to the content? Thank you ...

Where did the match parameter go in the props?

I'm in the process of developing my first react application and I am looking to implement a Switch element to display components based on different routes. One of the routes includes a parameter. However, I have encountered an issue where the match at ...

Launch the ReactJS application without specifying a port

I am currently developing a ReactJS application using create-react-app. While the npm start and npm run build commands are functioning properly, the issue I'm facing is related to hosting the build application without a port number in a public URL su ...

Is there a way to customize CKEditor to prevent it from continuously adding <p></p> tags within the textarea automatically?

As I was going through the CKEditor tutorial, I implemented the following: $( '#editor' ).ckeditor(function(){}); //it's working great!! However, after submitting the form, I noticed that by default, the textarea displays <p></p ...

Concealing a child component when hovering over its parent element with styled-components

I have a react component structured like this - const MyComponent = () => ( <ContainerSection> <DeleteButtonContainer> <Button theme="plain" autoWidth onClick={() = ...

Updating state array within reducer leads to duplicate execution of calculations

const updateCartReducer = (state, action)=>{ console.log('running the updater function') const existingItemIndex = state.items.findIndex( (item) => item.restaurant === action.item.restaurant && ...