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 change the style of components dynamically? #7408

Closed
imerkle opened this issue Jul 11, 2017 · 13 comments
Closed

How to change the style of components dynamically? #7408

imerkle opened this issue Jul 11, 2017 · 13 comments
Labels
support: question Community support but can be turned into an improvement

Comments

@imerkle
Copy link

imerkle commented Jul 11, 2017

https://www.webpackbin.com/bins/-Kon08wq7y0PT-JC4ufF

This is my current code. Since the stylesheet is static there is no way I can pass props to objects inside it. So my current approach is to use some defined classes like.

const styles = {
  bar: {
    background: '#f00',
  },
  greenBar: {
    background: 'green',
  },
};

and do this

<LinearProgress classes={{ bar: props.classes[props.color] }} />

call using

<Progress color="greenBar" />

It's not perfect, it's not even good I think but I would like to know what are the possibilities?

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 11, 2017

So my current approach is to use some defined classes like.

@dsslimshaddy This is the solution we are using for implementing our components. Here are a scope of the possibilities:

@oliviertassinari oliviertassinari added support: question Community support but can be turned into an improvement v1-alpha labels Jul 11, 2017
@imerkle
Copy link
Author

imerkle commented Jul 22, 2017

I think the JSS support rule' properties as function (background: props => props.color) is already out and is implemented by https://github.com/cssinjs/react-jss#usage

It will make customizing lot easier.

@jrmyio
Copy link

jrmyio commented Jul 28, 2017

@oliviertassinari , @dsslimshaddy is right. Material UI v1 depends on react-jss 7.0.2 which is the latest version of react-jss. It should support (background: props => props.color).

However, this doesn't seem to work?

@oliviertassinari
Copy link
Member

@ConneXNL Material-UI isn't directly using react-jss. The dynamic properties haven't been implemented by lack of time. Users can always use react-jss or any other styling solution. We will implement that feature if we need it for our own components. That can be useful.

@jrmyio
Copy link

jrmyio commented Jul 28, 2017

Users can always use react-jss or any other styling solution.

Ah I see ;)

Do you have an example of how to mix react-jss with the MuiThemes or is not that easy?

@oliviertassinari
Copy link
Member

@ConneXNL I have never tried.

@jrmyio
Copy link

jrmyio commented Jul 28, 2017

I just wrapped createMuiTheme:

const createDynamicMuiTheme = (color) => {
    return createMuiTheme({
        overrides: {
            MuiInput: {
                inkbar: {
                    '&:after': {
                        backgroundColor: color
                    }
                },
            },
        },
    });
};

But this is probably bad for performance?

@oliviertassinari
Copy link
Member

@ConneXNL Updating the theme rerender the whole tree. That's no something that should be done frequentely.

@jrmyio
Copy link

jrmyio commented Jul 28, 2017

<MuiThemeProvider theme={createDynamicMuiTheme(color)}>
  <TextField/>
</MuiThemeProvider>

I did wrap the textfield with the Provider or am i now injecting the entire css tree multiple times ? :)

Are the dynamic props scheduled for Version 1 though? I am having to do all of this just to use the removed funtionality of underlineFocusStyle

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 28, 2017

@ConneXNL Your solution shouldn't suffer from performance issue given the children tree is small.

Are the dynamic props scheduled for Version 1 though?

This won't block the release of v1. But that can always be added along the way.

@oliviertassinari oliviertassinari changed the title Best approach to change the style of components dynamically How to change the style of components dynamically? Aug 22, 2017
@JM-Mendez
Copy link

@ConneXNL I know it's a closed issue, but for anyone stumbling across this, to use react-jss with material-ui, all you have to do is replace material-ui's withStyles() method with react-jss injectSheet() , and the Themeprovider component in place of MuiThemeProvider.

@oliviertassinari
Copy link
Member

@JM-Mendez We have move the conversation to #7633

@lucas-viewup
Copy link

lucas-viewup commented May 3, 2018

@oliviertassinari, the point of @dsslimshaddy is a good way to solve that problem but another approach is implement a property that modifies the property of a inner component, in this case, you could implement an property called barProps and using this you could use the property style to modify the style of the inner element:

<LinearProgress barProps={{style: {backgroundColor: anyVariable} }} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

5 participants