cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              @import url(https://fonts.googleapis.com/css?family=Roboto:100);

h2 {
  font-family: 'Roboto', sans-serif;
  font-weight: 100;
  margin: 10px;
}

button {
	background-color:transparent;
	-moz-border-radius:5px;
	-webkit-border-radius:5px;
	border-radius:5px;
	border:1px solid #bdbdbd;
	display:inline-block;
	cursor:pointer;
	color:#bdbdbd;
	font-family:Arial;
	font-size:16px;
	padding:10px 20px;
  margin: 10px;
	text-decoration:none;
  transition: all 0.4s ease;
}
button:hover {
	color: #76daff;
  border: 1px solid #76daff;
}
button:focus {
  outline: 0;
}

.usercard {
  height: 170px;
  width: 300px;
  border: 1px solid #bdbdbd;
  border-radius: 15px;
}

.circle {
  height: 100px;
  width: 100px;
  border-radius: 50%;
  margin: 10px;
  transition: background-color 1s ease;
}
            
          
!
            
              class User extends React.Component {
  constructor(props){
    super(props);
    
    // Move the currentColor to top-level state
    this.state={
      currentColor: '#f8c483'
    }
  }
  
  // Hook context favColor to this.state.currentColor
  getChildContext() {
    return {
      favColor: this.state.currentColor,
      userName: 'James Ipsum'
    }
  }
  
  // Swap between colors on click
  swapColor () {
    if(this.state.currentColor === '#f8c483'){
      this.setState({
        currentColor: '#76daff'
      })
    } else {
      this.setState({
        currentColor: '#f8c483'
      })
    }
  }
  
  render() {
    return(
      <div>
        <Usercard />
        <button onClick={this.swapColor.bind(this)}>Swap Color</button>
      </div>
    );
  }
}

User.childContextTypes = {
  favColor: React.PropTypes.string,
  userName: React.PropTypes.string
};

class Usercard extends React.Component {
  // Usercard again makes no use of context
  render() {
    return(
      <div className='usercard'>
        <UserIcon />
        <UserInfo />
      </div>
    );
  }
}

class UserInfo extends React.Component {
  render() {
    return(
      <h2>{this.context.userName}</h2>
    );
  }
}

UserInfo.contextTypes = {
  userName: React.PropTypes.string
};

class UserIcon extends React.Component {
  // The context value for favColor now correctly corresponds to the currentColor value in the User component state
  render() {
    return(
      <div 
        className='circle' 
        style={{
          backgroundColor: this.context.favColor
        }}></div>
    );
  }
}

UserIcon.contextTypes = {
  favColor: React.PropTypes.string
};

ReactDOM.render(<User />, document.getElementById('app'));
            
          
!
999px
Loading ..................

Console