What is the proper way to apply :hover on the last child element using material-ui?

Currently, I am working with React and material-ui to create a menu component. This menu has a list of items, with the last item being unique in terms of styling. I have successfully used the :last-child selector for this purpose, but I am facing an issue when trying to apply a hover effect to this last element.

Here is the styling I am using:

const useStyles = makeStyles((theme) => ({
menu: {
  textDecoration: 'none',
  color: theme.palette.navy.default,
  cursor: 'pointer',
  fontWeight: 'bold',
  textTransform: 'uppercase',
  '&:last-child': {
    background: theme.palette.orange.default,
    borderRadius: '5em',
    color: theme.palette.white.default,
    // hover effect not working on the last child item
    '&:hover': {
      background: theme.palette.green.default,
    },
  },
 },
}));

And here is how the component is implemented:

...
const classes = useStyles();

<MenuList>
    {menu.map((item, index) => (
        <MenuItem selected={false} key={index} className={classes.menu}>
            {item.title}
        </MenuItem>
    ))}
</MenuList>

Any tips on how I can make the hover effect work on the last child item?

Answer №1

Here is one way to achieve it:

menu: {
  textDecoration: 'none',
  color: theme.palette.navy.default,
  cursor: 'pointer',
  fontWeight: 'bold',
  textTransform: 'uppercase',
  '&:last-child': {
    background: theme.palette.orange.default,
    borderRadius: '5em',
    color: theme.palette.white.default,
    
  },
   // apply hover effect to last child element
    '&:last-child:hover': {
      background: theme.palette.green.default,
    },
 },
}));

Answer №2

After thorough testing, I developed a new component that successfully resolved the issues I was experiencing. Incorporating @Sanish Joseph's suggestion, which had previously failed, I was able to achieve the desired outcome with this new component.

In the specific scenario, I created a menu structure that involved mapping objects arrays, each containing submenus. Through a process of elimination, I identified the root cause of the problem. It turned out that within the Popper component used to display these submenus, there was a property named "disablePortal" that was preventing the proper functionality of the "hover" on the last child element.

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

The react decorator for maintaining type safety fails to identify the appropriate ReturnType of the supplied function

I want to enhance the redux connect feature by using it as a decorator for a specific reducer/state. Although I know that redux connect can already be used as a decorator, I am curious about why my custom implementation does not work the way I expect. Her ...

Transitioning with an ellipsis

Currently, I am working on a project where I am trying to utilize the CSS property text-overflow: ellipsis to achieve a specific animation. However, despite applying this property along with white-space: nowrap and overflow: hidden to .card-dish__info, I a ...

The React Owl Carousel disappears after I refresh the page

After implementing the react-owl-carousel package, I noticed that upon refreshing the page, the carousel disappears. Any suggestions on how to fix this issue? <OwlCarousel items={3} className="owl-theme" ...

Is it possible to convert MUI components into strings on the client side?

I have incorporated the Material UI (MUI) library into my React application and am currently attempting to display certain components as PDF files directly in the browser. The approach I am taking involves: Creating a React element Rendering the React el ...

Prevent the collapse functionality in a Material-UI Stepper component

Currently in the development phase of a React website using Material-UI, with a focus on incorporating the Stepper component. Is there a method available to prevent the collapse and expansion of each individual StepContent in a vertical Stepper? The goal ...

What is the process for inserting images into a vertical timeline format?

I am currently working on building a timeline and incorporating images into it. I came across a solution on W3 that I'm trying to implement, but I'm facing some challenges. I've been experimenting with the code but I feel like there are part ...

What could be causing my CSS transitions to fail when using jQuery to add classes?

I'm currently working on a website and I'm facing an issue with the transition not functioning as expected. The problem persists even when the 7.css stylesheet is removed and interestingly, the transition works fine when using window:hover. My a ...

Solve TypeScript React error TS(2339) by resolving issues with extending a React.FC and using static property of type JSX.Element for uninitialized components

Currently, in VSCode, the typescript compiler is at TypeScript React 4.4.2 and it's pointing to a TS(2339) error: Property 'Col' does not exist on type 'FC<GridProps>'. I have attempted to add this prop to GridProps: export ...

Using React and Typescript: Passing functions as props to other components

In this setup, we have three main components: Toggle, ToggleMenu, and Wrapper. The Toggle component is designed to be universal and used for various functions. The Wrapper component, on the other hand, is meant to change the background color only when the ...

how to choose the :after pseudo-element with jQuery

Below are the codes I have tried. When this popup appears, I want to be able to close the entire popbox using the close button. CSS code .bigdiv{ display:none; background-color:#efefef; box-shadow: 10px 10px 10px 100000px rgba(0, 0, 0, 0.4); ...

The function is not being called as expected when using `onclick` or `eventListener('click')`

I'm in the process of developing a login form for my website that will offer 2 options - "login" and "signup". The concept is similar to mini tabs and iframe windows. Essentially, I have two divs side by side for "login" and "signup". When the user cl ...

Exploring domain routing in Next.js with sub-domains

I am currently working on translating my Next app according to the specific subdomain. For instance, I want en.hello.com to display content in English, while it.hello.com should show content in Italian. I have been attempting to accomplish this using Next ...

A design featuring a two-column layout, with one column remaining static while the other

I just finished putting together a two-column layout. <div class="layout"> <div class="col1"></div> <div class="col2"></div> </div> Check it out here There are a couple of things I'm wondering about: Is t ...

What is the reason behind the default port number 8080 for my React application running locally?

Why is the default URL for a local React application set to http://localhost:8088/ Shouldn't it be on port 80 instead, since that's the standard port for web servers handling HTTP requests? ...

Create a list item that includes an image with a corresponding label positioned beneath it

Currently attempting to convert my design into HTML and CSS for the first time, I've hit a roadblock with this particular task: I'm trying to create a series of white boxes, with each item having a white border. However, I'm struggling to c ...

Distribute progress bar evenly around a circular path

I am working on a component in ReactJS that involves a circle in the center displaying the average (rendered as a div element), with 12 progress bars aligned around it at equal angles. To achieve this, I am utilizing React Bootstrap ProgressBar. <Progr ...

I am struggling to make my Bootstrap 5 Navbar transparent. Can anyone help me figure this out?

I've been struggling with my Bootstrap Navbar, which appears as white no matter what I do. I've tried numerous solutions from Stack Overflow to make it transparent without any luck. Even after setting the background to transparent and ensuring t ...

Exploring ways to intercept Firebase API requests using msw - mock service worker for testing React applications

I've been facing challenges while testing my demo React app. Using msw to intercept Firebase API requests, I keep encountering errors in my terminal. It's important to mention that I have Firebase Authentication set up so only authorized users ...

Experience the power of ReactJS as you utilize the componentDidMount lifecycle method to fetch data

Currently, I am in the process of learning how to utilize ReactJS, Spotify API, and Promises. My goal is to retrieve top albums from musicians on Spotify and play a 30-second snippet of their tracks. I have decided to work with a Spotify package known as ...

Resolving duplicate NPM dependencies problem: two packages depend on a common peerDependency but with varying version requirements in CRA React

So, here's the dilemma: I seem to be facing a duplication issue in my React project that I just can't seem to figure out. Packages A and B both depend on package C as a peer dependency. When I build the project with only package A and C installe ...