18

Is it possible to change CSS :root variable in ReactJS ? looking for solution to change --basecolor code based on what user selected from .change-me input color

Demo: https://codepen.io/anon/pen/RgXBEK

CSS

:root {
  --base: $primary;
}

React

changeTheme(e){
    console.log(e.target.value);
}

class App extends React.Component {
    render() {
        return (
            <div className="row">
                <div className="col-xs-12 text-center">
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam voluptates ut eaque, voluptatum veniam nostrum sequi numquam sint, excepturi amet unde quis, ipsum ducimus reprehenderit eligendi pariatur animi esse sed.</p>
                    <input 
                      className="" 
                      type="color" 
                      onChange={this.changeTheme.bind(this)}
                      />
                    <br/><br/>
                </div>
            </div>
        );
    }
}

ReactDOM.render(<App />, window.document.getElementById('myDiv'));
6
  • i guess you have to exclude it from scss Commented Jul 21, 2017 at 7:07
  • @Muhaimin Looking for solution something like document.documentElement.style.setProperty(--${this.id}, this.value + suffix); Commented Jul 21, 2017 at 8:39
  • perhaps you can use ref on componentDidMount Commented Jul 21, 2017 at 12:29
  • @Muhaimin I found something. What you think about the answer given below ? Commented Jul 21, 2017 at 12:34
  • fine by me. how's the result Commented Jul 21, 2017 at 12:47

3 Answers 3

26

Finally found solution

constructor(props){
    super(props);
    this.state = {color: '#3C454B'};
}
changeTheme(e){
    this.setState({color: event.target.value});
    document.documentElement.style.setProperty('--base',this.state.color);
}

class App extends React.Component {
    render() {
        return (
            <div className="row">
                <div className="col-xs-12 text-center">
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam voluptates ut eaque, voluptatum veniam nostrum sequi numquam sint, excepturi amet unde quis, ipsum ducimus reprehenderit eligendi pariatur animi esse sed.</p>
                    <input 
                      className="" 
                      type="color" 
                      defaultValue={this.state.color}
                      onChange={(e) => this.handleColor(e)}
                      />
                    <br/><br/>
                </div>
            </div>
        );
    }
}

ReactDOM.render(<App />, window.document.getElementById('myDiv'));
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to set the color in App.css, and then call it across the app?This way, I won't have to do a document.setProperty for every element (that is the hope, anyway)
just a note: setState might not finish setting your state before you call the next line. so either use event.target.value in the document.documentElement... line or setState's second parameter is a callback that is called when the state is set.
@marcellsimon good point
8

I found out that you can change the root variable from any element. By wrapping the variable in double quotes "--base" and using it as the key in your style object.

<input 
    className="" 
    type="color" 
    onChange={this.changeTheme.bind(this)}
    style={{"--base":this.state.color}}
/>

4 Comments

This one saved me! it's really useful for setting up styles for individual elements
nice solution, but will not work for typescript
this one is best and cleaner solution, thanks
How can we make this work for TypeScript?
3

I found this answer for you: https://stackoverflow.com/a/37802204 document.documentElement.style.setProperty('--your-variable', '#YOURCOLOR');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.