Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom color to Badge component not working

I need to add a custom color to my Badge component and it does not seem to work.

I have tried these:

<Badge className="badge" badgeStyle={{backgroundColor: '#00AFD7'}} variant="dot" />

<Badge className="badge" color='#00AFD7' variant="dot" />

These do not work. How can I pass a custom color to my Badge component

like image 531
Kimaya Avatar asked Apr 19 '19 19:04

Kimaya


People also ask

How do you use the badge in MUI?

Badge visibilityThe visibility of badges can be controlled using the invisible prop. The badge hides automatically when badgeContent is zero. You can override this with the showZero prop.

How do I add a badge in react JS?

Initialize the Badge Component import * as React from 'react'; import * as ReactDOM from "react-dom"; import './App. css'; function App() { return ( <span>Badge Component <span className="e-badge">New</span></span> ); } export default App; ReactDOM.

What is badge in material UI?

A badge is a small descriptor for UI elements. It typically sits on or near an element and indicates the status of that element by displaying a number, icon, or other short set of characters. The BadgeUnstyled component creates a badge that is applied to its child element.


2 Answers

You can leverage withStyles and use the badge css class to customize this.

Here's an example:

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Badge from "@material-ui/core/Badge";
import MailIcon from "@material-ui/icons/Mail";

const styles = theme => ({
  margin: {
    margin: theme.spacing.unit * 2
  },
  customBadge: {
    backgroundColor: "#00AFD7",
    color: "white"
  }
});

function SimpleBadge(props) {
  const { classes } = props;
  return (
    <div>
      <Badge
        classes={{ badge: classes.customBadge }}
        className={classes.margin}
        badgeContent={10}
      >
        <MailIcon />
      </Badge>
    </div>
  );
}

SimpleBadge.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(SimpleBadge);

Edit Badge custom color

In v4, you can use functions within the styles that leverage props.

Documentation here: https://material-ui.com/styles/basics/#adapting-the-higher-order-component-api

const styles = theme => ({
  margin: {
    margin: theme.spacing.unit * 2
  },
  customBadge: {
    backgroundColor: props => props.color,
    color: "white"
  }
});

Edit Badge custom color

like image 142
Ryan Cogswell Avatar answered Sep 21 '22 16:09

Ryan Cogswell


In MUI v5, you can use either the sx prop:

<Badge
  badgeContent={130}
  sx={{
    "& .MuiBadge-badge": {
      color: "lightgreen",
      backgroundColor: "green"
    }
  }}
>
  <MailIcon />
</Badge>

Or styled() function:

const StyledBadge = styled(Badge)({
  "& .MuiBadge-badge": {
    color: "red",
    backgroundColor: "pink"
  }
});

Codesandbox Demo

like image 34
NearHuscarl Avatar answered Sep 21 '22 16:09

NearHuscarl