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

[Question] How to change the barColorPrimary style dynamically for LinearProgress? #11307

Closed
1 task done
franklixuefei opened this issue May 10, 2018 · 9 comments
Closed
1 task done
Labels
docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@franklixuefei
Copy link
Contributor

  • I have searched the issues of this repository and believe that this is not a duplicate.

Question

How am I able to change the bar color of LinearProgress component dynamically?
For example, I would like the color code to be a prop of my component, and in my component, I would like to set the color of the linear progress bar to be that color.

class MyComponent extends React.Component {
  render() {
    const { barColor } = this.props; // the color code, e.g, #efefef
    return (
    <LinearProgress 
      variant="determinate" 
      value={progressBar.quantity} 
      classes={{
        barColorPrimary: <how am I able to associate the barColor here?>
      }}
    />
    );
  }
}

Your Environment

Tech Version
Material-UI beta 44
React 16.2.0
@oliviertassinari oliviertassinari added the new feature New feature or request label May 10, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented May 10, 2018

@franklixuefei It's the second time I see this feedback, let's do something about it. Some options:

  1. Add a specific property for this use case.
  2. Add a styles API to mimic the classes API. Only one key should be enough to start with: styles.bar.
  3. Add a barProps property. Might be overkill.

@franklixuefei
Copy link
Contributor Author

franklixuefei commented May 10, 2018

@oliviertassinari from the options you listed out, I believe the 1st or the 2nd is better. If you think about CircularProgress, it does not expose the styles api, but just the size property so that you can control the size of it. So to make things consistent, your 1st option seems reasonable. However, as time goes by, there might be more and more similar requests like this, that require us to expose more properties. So from a scalable perspective, I think your 2nd option stands out. So I vote for your 2nd option

Add a styles API to mimic the classes API. Only one key should be enough to start with: styles.bar.

However, in doing so, we'd better support styles api for other components as well, for the sake of consistency.

BTW, I'm using cssinjs directly as a quick hack for now, and here is how it looks like:

{this.state.progressBarsInfo.map((progressBar) => {
  const barColorStyle = jss.createStyleSheet({
    bar: {
      ['background-color']: progressBar.color
    }
  }).attach();
  return (
    <LinearProgress
      variant="determinate"
      value={total ? progressBar.quantity / total * 100 : 0}
      classes={{
        root: classnames(classes.bar, classes.transparentBackground),
        barColorPrimary: barColorStyle.classes.bar
      }}
      key={progressBar.name}
    />
  );
})}

@oliviertassinari oliviertassinari added the good first issue Great for first contributions. Enable to learn the contribution process. label May 20, 2018
@oliviertassinari
Copy link
Member

@franklixuefei Yeah, I think that option 3 is overkill. I'm happy with 1. or 2.

@adeelibr
Copy link
Contributor

@oliviertassinari can i work on this?

@adeelibr
Copy link
Contributor

adeelibr commented Jun 8, 2018

I have made a PR. Now this is how you can change the primary color on Linear Progress

<LinearProgress
   styles={{
     bar: {
       backgroundColor: 'purple',
     },
  }}
/>

Kindly review the PR #11782

@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation and removed new feature New feature or request labels Jun 10, 2018
@franklixuefei
Copy link
Contributor Author

franklixuefei commented Jun 20, 2018

before #7633 is supported, there is still no easy way to resolve this issue.

@oliviertassinari
Copy link
Member

The new documentation section is live: https://material-ui.com/customization/overrides/#2-dynamic-variation-for-a-one-time-situation. Right now, people have to either use CSS variables or Theme nesting.

@fauskanger
Copy link
Contributor

I tried to use the example with theme nesting, but the example seems to crash in https://codesandbox.io/s/8n5vl4xxyl - and in my project I get a warning:

Material-UI: you are providing a theme function property to the MuiThemeProvider component:
<MuiThemeProvider theme={outerTheme => outerTheme} />
However, no outer theme is present.
Make sure a theme is already injected higher in the React tree or provide a theme object.

@fauskanger
Copy link
Contributor

I already use withTheme()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
Development

No branches or pull requests

4 participants