Exploring React's Primary Button Color Implementation

I am attempting to customize the buttons in my theme for various states such as contained, primary, and hover. However, my current approach is not yielding the desired results.

VIEW CODESANDBOX DEMO: CLICK HERE

theme/overrides/MuiButton.js

import palette from '../palette';

export default {
  contained: {
    backgroundColor: '#FFFFFF',
    '&.primary': {
      '&:hover': {
        backgroundColor: palette.primary.dark,
      },
    },
  },
};

theme/overrides/index.js

import MuiButton from "./MuiButton";

export default {
  MuiButton
};

theme/index.js

import { createMuiTheme } from "@material-ui/core";

import palette from "./palette";
import typography from "./typography";
import overrides from "./overrides";

const theme = createMuiTheme({
  palette,
  typography,
  overrides,
  zIndex: {
    appBar: 1200,
    drawer: 1100
  }
});

export default theme;

Answer №1

To effectively customize the default styles in your theme, it is essential to analyze how these styles are initially set.

Referencing the code snippet from https://github.com/mui-org/material-ui/blob/v4.11.0/packages/material-ui/src/Button/Button.js#L138:

  containedPrimary: {
    color: theme.palette.primary.contrastText,
    backgroundColor: theme.palette.primary.main,
    '&:hover': {
      backgroundColor: theme.palette.primary.dark,
      // Reset on touch devices, it doesn't add specificity
      '@media (hover: none)': {
        backgroundColor: theme.palette.primary.main,
      },
    },
  },

Implementing a similar strategy in your own code would resemble the following:

import palette from '../palette';

export default {
  containedPrimary: {
    backgroundColor: '#FFFFFF',
    '&:hover': {
      backgroundColor: palette.primary.dark,
    },
  },
};

https://codesandbox.io/s/material-demo-7v7um?fontsize=14&hidenavigation=1&theme=dark

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

Set a predefined height for an element, yet ensure it adjusts to fit within its container (with the option to overflow only if it reaches a minimum specified height)

Consider this setup: A heading text a main content block All inside a single parent container. .outer { max-height: 200px; background-color: green; } .inner { min-height: 50px; /*for illustration, default height is set higher than ou ...

In the `componentDidUpdate()` method, the function `this.props` is not

I've encountered a peculiar issue that I just can't seem to figure out. It's possible that I missed something simple, but I'm stuck trying to solve this bug. Here's what's happening: In the child component, a counter is being ...

Setting the width of a wizard modal to auto

I am facing an issue with my modal wizard. The width of the first modal is perfect, but when I navigate to the second and third modals by pressing next, they automatically take on a fixed width size that I need to modify. I have tried declaring the width ...

Adding dots between page numbers when using ReactJS/Javascript Pagination can be achieved by implementing a specific method

I have created a pagination component using React JS. Currently, it only displays all the page numbers, but I want it to show dots when there are more than 8 total pages. Here's how I envision it: c. When total is less than 9 pages: Page 1 selecte ...

Utilizing jQuery to add a class to an element when a different one is hovered above

I'm currently working on implementing effects on the edges of a webpage that will be triggered when a central div is hovered over. The main tool I'm using for this task is jQuery. Despite my efforts to diagnose the issue by using alerts in my Ja ...

Changing the properties of the admin bar when it is hovered over in

After successfully styling a button on the admin bar, I noticed a few imperfections in the implementation. The button in question is situated on the admin bar and is labeled "maintenance". Upon clicking the button, jQuery triggers the addition of the clas ...

Sending a series of filtered GET requests to my Express backend in a MERN (MongoDB, Express, React,

I have developed a MERN web application and am in the process of adding some GET methods to retrieve the same item for different scenarios. However, when I attempt to implement a second filter method and pass an existing parameter in my item schema, Postma ...

Is it possible to target elements based on a specific CSS3 property they use?

Is there a method to target all elements in the DOM with a border-radius other than 0? If anyone has suggestions, it would be greatly appreciated! ...

Issue with material-UI Dialog rendering in snapshot test using React-Testing library

function Component(props) { const { approvalValue, handleSubmit, handleDialog, handleChange, handleApprovalChange, value, characterCount, maxCharacterValue, } = props; const { handleOpen, handleClose, open } = handleD ...

Design featuring a combination of vertical columns and a fixed image placed in

I'm currently in the process of creating a website and I have a specific page layout in mind that I would like to implement: ------------------------------------------ | A catchy title | | | Here | An unconve ...

ReactJS: Alert for controlled form element

Utilizing inputs from Material-Ui, I am retrieving values from an API to populate these inputs. If the API does not return any values, the user must enter them manually. In my implementation, I am utilizing Hooks with the initial state defined as: const [ ...

What are the TypeScript types needed for a React component that accepts an array of objects as a prop?

I am currently working on a React component that includes a prop named propWhichIsArray. This prop is expected to be an array of objects. Each object in the array will contain properties such as id (an ID) and text (a string). How do I properly define this ...

Issue with Next.js: Setting the width to 100vh prevents the height from being responsive on mobile devices

While I understand that using height: 100vh on mobile is generally discouraged for various reasons, I'm curious as to why height: 100vh paired with width: 100vh in Next.js doesn't produce the expected result. Instead of a full-height square, I en ...

Executing NextJS Request on Each Route Transition

I am working on a project with NEXTJS 13 and a Pages directory. I am looking to make a request to our graphql server every time a route changes. Is it possible to do this from the server-side? Additionally, can I store this data in my Redux store after m ...

Arrange the next and previous buttons on the top right corner of the Bootstrap Carousel, ensuring they are situated closely together

I am currently utilizing the Bootstrap 4 carousel. My goal is to adjust the position of the next and previous buttons to be in the top right corner, close to each other. I have been able to achieve this, but I suspect that there may be an error in my CSS c ...

The utilization of create-react-app along with subpaths in the development process

In our extensive environment, we have multiple applications within the same domain such as: foo.org/a foo.org/b We have configured IIS to function similarly on localhost, allowing us to access the applications by typing: localhost/a localhost/b This setu ...

Preventing dragging ability in slider once a specific threshold is reached

I'm currently working on a slider with a value range of 0 to 100. Users should only be able to adjust the slider between 2 and 100, while still being able to see the space between 0 and 2 on the interface. How can I make sure dragging stops once the v ...

How can I remove the onSubmit data payload in react-hook-form once it has been triggered?

I am facing an issue with my dynamic form. The form data state changes every time I click the onSubmit button. However, the problem is that the old data from the previous form state is not being cleared. const onSubmit = (data) => { const formData = n ...

Position the left floated element at the top, with all preceding siblings floated to the right

Is there a way to rearrange the HTML structure using CSS only to move Content 3 Div (div3) to the top? The sequence of elements cannot be changed. I attempted to use negative margin, but it only works if the height of Div 1 and 2 is fixed. However, the co ...

The website is not displaying CSS styles as intended

I'm currently in the process of building a website with Bootstrap 5, but I've encountered an issue where the properties from index.css are not taking effect on index.html. Specifically, when I hover over a service card, the background is supposed ...