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

Display a text over a full-screen HTML5 video

Is there a way to display text on a fullscreen video in HTML? I have tried using absolute positioning for the text and relative/fixed/none positioning for the video, but it does not work when the video is in fullscreen mode. I also attempted to call the ...

Using the React context without explicitly setting the contextType or defining a default context

Is it possible to access the closest context from a provider without having to explicitly define contextType in a component by using this.context? Alternatively, is there a way to establish a default context so that when I use this.context, I automaticall ...

After updating the file path, the Next.Js module couldn't be located: Module not found – Unable to

After relocating the EmptyTable.tsx file from its original directory at views/forms-tables/tables/react-table/EmptyTable to a new location at components/Tables/EmptyTable, I encountered a persistent issue. Despite updating the import path in my code to mat ...

A different approach for dynamically displaying React components sourced from an API

As I develop a website using Next.js/React that pulls in content from Strapi CMS, my goal is to create a dynamic page template for news articles. The aim is to empower content editors by giving them the flexibility to choose the type of content they wish t ...

React Sticky sidebar implementation throughout the various components

My goal is to implement a sticky sidebar that moves smoothly across specific components on my webpage. The challenge I am facing is that the CSS 'sticky' property only allows movement within the component it was created in. So, my question is - w ...

ERESOLVE encountered difficulties resolving dependency tree within React

I am facing an issue when trying to add dependencies react as I cannot install any packages. I recently deleted a package-lock.json file in order to resolve an ELOCKVERIFY error. Even after running 'npm audit fix' command, the ERESOLVE error stil ...

If the value is not already in the array, React Native will add it to the state

Can someone please guide me on the correct way to add a value to an array in my state variable if the value does not already exist? I have written some code that seems to work, but as a beginner, I am unsure if this is the best approach. This function is ...

Optimizing Static File Caching in Yii

Having a frustrating issue with Yii where my local development environment caches CSS and JS files. Despite making changes to the file, the edits do not reflect in the output and sometimes causes corruption leading to broken functionality. This problem see ...

Guide to animating the height of a div using jQuery:

I'm hoping to create a cool animation effect where a certain part of a div expands to 100% width when clicked, and then reverts back to its original height on a second click. I've tried writing some code for this, but it's not working as exp ...

The issue of Next.js redux useSelector causing HTML inconsistency

Currently, I am utilizing Next.js for the development of a React application. In order to manage the state, I have integrated redux along with redux-toolkit. A peculiar error has surfaced in the console with the message: Warning: Did not expect server H ...

React: Updating State with the Spread Operator and Array Method to Add Elements to an Array

Link to my jsfiddle code snippet: https://jsfiddle.net/ilikeflex/r2uws1ez/30/ I've been attempting to update the state in React by accessing the previous state. There are three different cases where I'm trying to achieve this, but I'm havin ...

The element residing within the body exceeds the body's dimensions

HTML : <body> <header> <div> </div> </header> </body> CSS : body { width : 1000px ; } header { width : 100% ; } If these codes are implemented, The header's width should be the same as the body's ...

Tips on receiving feedback from an express server regarding an error by utilizing axios within a react application

Currently, I am working with react and express. In my react application, I am trying to utilize axios.post to send user credentials to the express server in hopes of receiving a response. Once I receive the response, my goal is to extract the message and s ...

Ways to disable the caching feature in Google Chrome

When I am working on CSS and JS around a page, I need to disable the cache mechanism in Google Chrome browser. A trick is to open Chrome DevTools, which automatically disables the cache mechanism (you can configure this in the settings section). Are ther ...

Ensure that the heights of various Flexbox rows are always visible within the boundaries of the browser window

Within flexbox, I've laid out three rows of boxes with varying heights. This layout ensures that the rows always align perfectly with the bottom of the browser window without any gaps. You can see the full layout on Codepen by following this link: htt ...

Troubleshooting: Next.js 13 custom 404 page not functioning as expected

Currently, I am experimenting with setting up a custom 404 page for my next 13 project. I am following this approach: https://nextjs.org/docs/app/api-reference/file-conventions/not-found. However, whenever I enter an unmatched URL, the page appears blank! ...

Obtain the breakpoint value from Bootstrap 5

We have recently updated our project from Bootstrap 4 to Bootstrap 5. I am trying to retrieve the value of a breakpoint in my TypeScript/JavaScript code, which used to work in Bootstrap 4 with: window .getComputedStyle(document.documentElement) .g ...

How do I properly deselect items from the MUI select Component when the default behavior isn't working?

Could someone please advise me on how to implement a deselect option in a Material UI select component? Specifically, I am wondering if there is a prop that automatically deselects the selected object after clicking it. This is my code snippet: <FormCo ...

What is causing the issue with CSS properties and media queries not being effective on specific elements?

I have been experimenting with React and SCSS to create a portfolio page. Although I am familiar with these tools, I recently encountered issues where certain style changes were not reflecting in the browser. One specific problem involved styling the head ...

Navigational Arrows within a JQuery Image Carousel

I've successfully developed an image slider using a combination of HTML, CSS, and JS (Jquery). The slider consists of 5 images that users can navigate between using arrow buttons. Everything is functioning correctly so far, but I'm encountering a ...