Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify a custom pallete name? #10615

Closed
Vishal1419 opened this issue Mar 11, 2018 · 5 comments
Closed

How to specify a custom pallete name? #10615

Vishal1419 opened this issue Mar 11, 2018 · 5 comments
Labels
duplicate This issue or pull request already exists
Milestone

Comments

@Vishal1419
Copy link

I want to declare a custom pallete named excel, which I will use on for example a button to show it green. So, I tried that on code sandbox and I was unsuccessful. Here is the link to sandbox: https://codesandbox.io/s/13rzn53xx7

I don't know that if this is a feature or not? If its a feature then that should be a bug. Else can you please suggest me a workaround.

Expected Behavior

As per my sandbox code, color of secondary button should look green.

Current Behavior

Color of secondary button looks gray

Tech Version
Material-UI next
React > 16
browser chrome
@oliviertassinari oliviertassinari added support: question Community support but can be turned into an improvement new feature New feature or request and removed support: question Community support but can be turned into an improvement labels Mar 11, 2018
@oliviertassinari oliviertassinari added this to the post v1.0.0 milestone Mar 11, 2018
@oliviertassinari
Copy link
Member

@Vishal1419 You can't. The color property has static supported values. You need to write a wrapping component that implements this behavior.

I want to work on this story in the future. I was thinking of introducing a Color component for this use case.

@gustavopch
Copy link

@Vishal1419 Any success?

@Vishal1419
Copy link
Author

Vishal1419 commented Mar 13, 2018

@oliviertassinari Thanks for giving me the idea, I have created the wrapping component and it works.

@gustavopch Yes, I implemented it. Take a look at the code below:

import React, { Component } from 'react';
import { withTheme } from 'material-ui/styles'
import PropTypes from 'prop-types';

class Color extends Component {
    constructor(props) {
        super(props);
        this.state = {
            hover: false,
        }
        this.toggleHover = this.toggleHover.bind(this);
    }

    toggleHover() {
        this.setState({
            hover: !this.state.hover,
        });
    }

    render() {
        const { theme, color, children } = this.props;
        return (
            React.cloneElement(children, {
                style: {
                    ...children.props.style,
                    color: !children.props.disabled && 
                           theme.palette[color].contrastText,
                    background: !children.props.disabled &&
                                (this.state.hover 
                                ? theme.palette[color].dark
                                : theme.palette[color].main),
                },
                onMouseEnter: this.toggleHover,
                onMouseLeave: this.toggleHover,
            })
        );
    }
}

Color.propTypes = {
    color: PropTypes.string.isRequired,
    children: PropTypes.element.isRequired,
    theme: PropTypes.object.isRequired,
}

export default withTheme()(Color);

You can use it as follows:

<Color color="excel">
    <Button 
        color="primary" 
        variant="raised" 
        style={{ margin: 10, marginTop: 6 }}
        disabled={props.selectedContacts.length <= 0}
        onClick={props.deleteSelectedContacts}
    >
        <DeleteIcon style={{ marginRight: 10 }} />
        Export To Excel
    </Button>
</Color>

In theme I am doing this:

import { createMuiTheme } from 'material-ui/styles';

const theme = createMuiTheme({
  palette: {
    primary: {
      light: '#e3f2fd',
      main: '#2196f3',
      dark: '#1976d2',
      contrastText: '#fff',
    },
    secondary: {
      light: '#fbbcd0',
      main: '#f50057',
      dark: '#c51162',
      contrastText: '#fff',
    },
    excel: {
      light: '#E8F5E9',
      main: '#4CAF50',
      dark: '#2E7D32',
      contrastText: '#fff',      
    }
  },
});

export default theme;

And in App.js, I wrap parent div inside MuiThemeProvider as follows:

      <div className="App">
        <Provider store={store}>
          <MuiThemeProvider theme={LightTheme}>
            <Contacts />
          </MuiThemeProvider>
        </Provider>
      </div>

@Vishal1419
Copy link
Author

@oliviertassinari Any in-built support for this in newer versions?

@Vishal1419 Vishal1419 reopened this Apr 3, 2019
@oliviertassinari oliviertassinari added duplicate This issue or pull request already exists and removed new feature New feature or request labels Apr 3, 2019
@oliviertassinari
Copy link
Member

I'm closing as a duplicate of #14185. #13875 covers the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants