Add hover effects to components inside MuiButton

I'm currently working with styled components and Material UI, but I'm struggling to add hover styles to the children of MuiButton. Despite following online resources and documentation, I can't seem to make it work. Here's how my jsx is set up:

<StyledMuiButton onClick={() => ()}>
  <Svg />
  <MuiTypography color={Color.gray} variant="caption">
    Text
   </MuiTypography>
</StyledMuiButton>

and here's the styled component implementation:

const StyledMuiButton = styled(MuiButton)`
  && {
    .MuiButton-label {
      flex-direction: column;
    }

   &:hover ${MuiTypography} {
      color: ${Color.primary};
    }
  }
`;

If anyone could provide some guidance in solving this issue, it would be greatly appreciated.

Answer №1

Below is a demonstration showcasing various methods for targeting elements within a button:

import React from "react";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
import DoneIcon from "@material-ui/icons/Done";
import styled from "styled-components";

const StyledButton = styled(Button)`
  & .MuiButton-label {
    display: flex;
    flex-direction: column;
  }
  &:hover {
    color: red;
    .MuiSvgIcon-root {
      background-color: blue;
    }
    .MuiTypography-root {
      color: green;
      &:nth-of-type(2) {
        color: purple;
      }
    }
  }
`;

export default function App() {
  return (
    <div className="App">
      <Button>Default Button</Button>
      <StyledButton>Styled Button</StyledButton>
      <StyledButton>
        <DoneIcon />
        <span>Styled Button</span>
        <Typography>Typography 1</Typography>
        <Typography>Typography 2</Typography>
      </StyledButton>
    </div>
  );
}

https://codesandbox.io/s/zen-germain-2gv22?fontsize=14&hidenavigation=1&theme=dark

This approach makes use of the predefined global class names assigned to the elements, as detailed in the CSS section of the API page for each component (e.g., the Typography documentation can be found here: https://material-ui.com/api/typography/#css). In general, the primary element within a Material-UI component can be selected using MuiComponentName-root (such as MuiTypography-root for Typography).

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

Certain rows appear to be missing even though they are present in both the webpage and its corresponding source code

I am facing a CSS issue that I am unable to resolve. I have 20 results that should be displayed on my website, but only 14 of them are appearing. The other ones are visible in the source code when inspected. When I move the position of the menu up or down, ...

What types should you use for an Axios response in React and TypeScript?

Trying to display a basic user list fetched from an API, which returns the following data: [{"UserID":2,"FirstName":"User2"},{"UserID":1,"FirstName":"User1"}] Struggling to understand how to deal ...

Best Practices for Implementing Redux Prop Types in Typescript React Components to Eliminate TypeScript Warnings

Suppose you have a React component: interface Chat { someId: string; } export const Chat = (props: Chat) => {} and someId is defined in your mapStateToProps: function mapStateToProps(state: State) { return { someId: state.someId || '' ...

React tooltip problem with vanilla Bootstrap

I have decided to stick with vanilla Bootstrap and avoid switching to react-bootstrap because of the unnecessary changes they made. However, I am facing a problem where the style of the tooltip sometimes changes unexpectedly. For example, a black tooltip ...

Organized Styled-components

Considering implementing styled-components in my application and questioning the best organizational approach. Usually, styled-components are associated with a particular component for increased reusability. However, what is the recommended method for us ...

Ensure that text is justified to the left both above and below inline elements contained within the parent div

I am attempting to align text, generated dynamically, on the left side of some images that are positioned next to each other. The image on the far left should also be aligned to the left. .inline-attachments { display:inline; vertical-align:top; } ...

Tips for avoiding the freezing of bootstrap-select scroll when a large number of options are present

I have integrated a bootstrap-select with a total of 1000 options. However, I am encountering an issue where when I attempt to scroll down the list of options, it only goes down approximately 60 options and then freezes in that position. Can anyone provi ...

I came across a small piece of CSS code that isn't functioning properly when tested on jsfiddle

I'm on the hunt for a cool "typing animation" with three dots. Stumbled upon this gem online -> https://codepen.io/mattonit/pen/vLoddq The issue is, it doesn't seem to work for me. I even tried it on jsfiddle and it just stops working. I att ...

React with TypeScript: The children prop of this JSX tag is specifically looking for a single child of type ReactNode, but it seems that multiple children were passed instead

For my class project in APIs, I am using react-typescript but running into issues. My problem arises when attempting to loop through an array of "popular" movies using .map. However, I keep getting this error message: "This JSX tag's 'children&ap ...

Utilize CSS animation classes on multiple elements throughout a single webpage

Trying to create a share button for social media with an animated button using CSS. However, encountering issues when trying to display multiple buttons on the same page for different content. The problem arises when clicking the second button, as it trigg ...

Enhancing larger elements with a combination of CSS background-image gradients and border lines

My design calls for a background image with a line at the end. The line should start right where the background size ends, and in my concept it is grey. It's important that this line remains as one cohesive element. background-image: linear-gradient( ...

Trigger action following a certain time frame

Currently, I am delving into the world of React and Redux for the very first time. My app involves utilizing a third-party API to retrieve data seamlessly, which is working out great. However, like many secure APIs, I must acquire a short-lived token to ...

Seeking assistance with TypeScript promises

Just starting out with typescript and nodejs, but I've got to tackle some issues in the typescript code. I'm looking to execute an ECS one-off task using Pulumi. I have the documentation on how to run the task from the taskDefinition, which can ...

Utilize the functionality of the acuityscheduling API to streamline your

I've experimented with various methods but haven't had any success. Hopefully, you guys can share some insight. I'm utilizing acuityscheduling's API to fetch appointments. According to their documentation, the process should look someth ...

"Utilizing the ng-class directive in AngularJS to apply multiple

I have been attempting to utilize the ng-class directive in AngularJS based on a certain condition: if a JSON value is 1, it should display as green; if it's 2, red; and if it's 0, white. However, I am unable to achieve the desired result for som ...

Struggling to integrate pdfform.js into a React application

Struggling to integrate the pdfform.js library into my React app. I attempted to use the npm package pdfform, but encountered some difficulties. If anyone has any tips or guidance, it would be greatly appreciated. Check out the pdfform.js library on GitH ...

Is there a way to silence TypeScript's complaints about a React rendered value being converted into a Date object?

In my latest project using Next.js, TypeScript, Prisma, and Radix-UI, I'm working with a loan object that has various key-value pairs: { "id": 25, "borrowerName": "Test Borrower 1", "pipelineStage": ...

Verify that a memoized component remains static and does not undergo a re-rendering process

After making a recent commit, I noticed a decrease in performance in my React app. It seems that introducing Context has caused issues with Memoization. I am interested in creating tests to confirm that a memoized component is not re-rendered after a user ...

The total size of the React JS bundle is 1.9 megabytes

Looking for ways to decrease the size of my bundle.js file, which is currently 1.9mb. Any suggestions on how I can further reduce it? I have already implemented lazy loading and managed to lower it from 2.3mb to 1.9mb. ...

Tips for organizing wells within bootstrap into separate columns

Looking for a way to organize my well list into two separate columns. I've included a link to my Plunker project here: https://plnkr.co/edit/35oC9Eochk6EPgKeI9he?p=preview. Below is the view section: <div class="well well-lg" ng-repeat="(key, d ...