123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id='container'>Loading...</div>

            
          
!
            
              body {
  background: #d5d5d5; 
  font-family: arial;
  color: #FAFAFA;
  text-transform: uppercase;
}

.scoreboard {
  background: #333;
  width: 700px;
  margin: 70px auto;
  box-shadow: 0 4px 0px #333;
  border-radius: 15px;

}

.header {
  padding: 5px 10px;
  text-align: center;
  display: flex;
  align-items: center;
  background-color: #222;
  border-radius: 15px 15px 0 0;
  border-bottom: solid 2px #444;
}

.header h1 {
  flex-grow: 1;
  font-size: 1.5em;
  letter-spacing: 3px;
  font-weight: normal;
}

.header .stats,
.header .stopwatch {
  width: 170px;
}

.stats {
  margin-top: 0;
  font-weight: normal;
}

  .stats td:first-child {
    text-align: right;
    font-weight: normal;
    letter-spacing: 2px;
    color: #666;
    font-size: .7em;
  }

  .stats td:last-child {
    text-align: left;
  }

.stopwatch {
  padding: 15px 10px 5px 10px;
  margin: -5px -10px -5px 10px;
  background: #2f2f2f;
  border-radius: 0 15px 0 0;
}

  .stopwatch-time {
    font-family: monospace;
    font-size: 2em;
  }

  .stopwatch button {
    margin: 8px 5px;
    background-color: #222;
    border-radius: 5px;
    padding: 7px 8px;
    border: none;
    color: #999;
    letter-spacing: 2px;
    font-weight: bold;
    text-shadow: none;
    text-transform: uppercase;
  }

  .stopwatch button:hover {
    background: #4b71b5;
    color: #fafafa;
    cursor: pointer;
  }

  .stopwatch h2 {
    font-size: .6em;
    margin: 0;
    font-weight: normal;
    letter-spacing: 2px;
    color: #666;
  }

.player {
  display: flex;
  font-size: 1.2em;
  border-bottom: solid 2px #444;
  letter-spacing: 2px;
}

  .remove-player {
    visibility: hidden;
    margin-right: 10px;
    color: #e57373;
    cursor: pointer;
  }

  .player-name:hover .remove-player {
    visibility: visible;
  }

  .player-name {
    flex-grow: 1;
    padding: 20px 10px 10px 10px;
  }

  .player-score {
    width: 190px;
    background: blue;
  }

  .counter {
    display: flex;
  }

  .counter-action {
    border: none;
    font-weight: bold;
    color: #FAFAFA;
    display: block;
    padding: 20px 20px;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  .counter-action.increment {
    background: #66BB6A;
  }

  .counter-action.increment:hover {
    background: #549d59;
    cursor: pointer;
  }

  .counter-action.decrement {
    background: #ef5350;
  }

  .counter-action.decrement:hover {
    background: #c44442;
    cursor: pointer;
  }

  .counter-score {
    flex-grow: 1;
    background: #2b2b2b;
    color: #FAFAFA;
    text-align: center;
    font-family: monospace;
    padding: 10px;
    font-size: 2em;
  }


.add-player-form form {
  display: flex;
  background-color: #222;
  border-radius: 0 0 20px 20px
}

  .add-player-form input[type=text] {
    flex-grow: 1;
    border-width: 0 0 1px 0;
    margin: 15px 10px 15px 15px;
    padding: 10px;
    border-radius: 5px;
    background-color: #333;
    border-style: none;
    text-shadow: none;
    text-transform: uppercase;
    color: #999;
    letter-spacing: 2px;
    outline: none;
  }

  .add-player-form input[type=text]::-webkit-input-placeholder{
    color: #666;
    letter-spacing: 2px;
  }

  .add-player-form input[type=text]:focus{
    background-color: #444;
  }

  .add-player-form input[type=submit] {
    display: block;
    font-size: .6em;
    margin: 15px 15px 15px 0; 
    padding: 10px;
    background-color: #333;
    border-radius: 5px;
    border: none;
    color: #999;
    letter-spacing: 2px;
    font-weight: bold;
    text-shadow: none;
    text-transform: uppercase;
  }

  .add-player-form input[type=submit]:hover{
    background: #4b71b5;
    color: #fafafa;
    cursor: pointer;
  }
            
          
!
            
              var PLAYERS = [
  {
    name: "John",
    score: 31,
    id: 1,
  },
  {
    name: "Dale",
    score: 33,
    id: 2,
  },
  {
    name: "bill",
    score: 43,
    id: 3,
  },
];

var nextId = 4;

var Stopwatch = React.createClass({
  getInitialState: function(){
    return{
      running: false,
      elapsedTime:0,
      previousTime:0,
    }
  },
  
  componentDidMount: function(){
    this.interval = setInterval(this.onTick, 100);
  },
  componentWillUnmount: function(){
    clearInterval(this.interval);
  },
  onTick: function(){
    if(this.state.running){
       var now = Date.now();
      this.setState({
        previousTime:now,
        elapsedTime: this.state.elapsedTime + (now - this.state.previousTime),
      });
       }
  },
  
  onStart : function(){
    this.setState({
      running: true,
      previousTime: Date.now(),
    });
  },
  onStop : function(){
    this.setState({ running: false });
  },
  onReset : function(){
    this.setState({
      elapsedTime:0,
      previousTime: Date.now(),
    });
  },
  
  render: function(props){
    var seconds = Math.floor(this.state.elapsedTime / 1000);
    
    return(
      <div className="stopwatch">
        <h2>Stopwatch</h2>
        <div className="stopwatch-time">{seconds}</div>
        { this.state.running ? 
          <button onClick={this.onStop}>Stop</button> 
          : 
          <button onClick={this.onStart}>Start</button> 
        }
        <button onClick={this.onReset}>Reset</button>
      </div>
    );
  }
});

var AddPlayerForm = React.createClass({
  propTypes:{
    onAdd: React.PropTypes.func.isRequired,
  },
  
  getInitialState: function(){
    return{
      name:"",
    };
  },
  
  onNameChange: function(e){
    // console.log('onNameChange', e.target.value);
    this.setState({name:e.target.value});
  },
  
  onSubmit: function(e){
    e.preventDefault();
    
    this.props.onAdd(this.state.name);
    this.setState({name:""});
  },
  
  render: function(){
    return(
      <div className="add-player-form">
        <form onSubmit={this.onSubmit}>
          <input type="text" value={this.state.name}  onChange={this.onNameChange}/>
          <input type="submit" value="Add Player"/>
        </form>
      </div>
    )
  }
});

function Stats(props) {
  var totalPlayers = props.players.length;
  var totalPoints = props.players.reduce(function(total,player){
    return total + player.score;
  },0);
  
  return(
    <table className="stats">
      <tbody>
        <tr>
          <td>Players:</td>
          <td>{totalPlayers}</td>
        </tr>
        <tr>
          <td>Total Points:</td>
          <td>{totalPoints}</td>
        </tr>
      </tbody>
    </table>
  )
}
Stats.propTypes = {
  players: React.PropTypes.array.isRequired,  
}

function Header(props){
  return(
    <div className="header">
      <Stats players={props.players}/>
        <h1>{props.title}</h1>
      <Stopwatch />
      </div>
  );
}
Header.propTypes = {
  title: React.PropTypes.string.isRequired, //EXPECTS ONLY STRINGS IN TITLE ATTRIBUTE
  players: React.PropTypes.array.isRequired,
};

function Counter(props){
  return(
      <div className="counter">
        <button className="counter-action decrement" onClick={function(){props.onChange(-1);}}>-</button>
        <div className="counter-score">{props.score}</div>
        <button className="counter-action increment" onClick={function(){props.onChange(1);}}>+</button>
      </div>   
    );
}
Counter.propTypes = {
  score: React.PropTypes.number.isRequired,
  onChange: React.PropTypes.func.isRequired,
}

function Player(props){
  return(
    <div className="players">
        <div className="player">
          <div className="player-name">
            <a className="remove-player" onClick={props.onRemove}>x</a>
            {props.name}
          </div>
          
          <div className="player-score">
            <Counter score={props.score} onChange={props.onScoreChange}/>
          </div>
        </div>
      </div>
  );
};
Player.propTypes = {
  name: React.PropTypes.string.isRequired,
  score: React.PropTypes.number.isRequired,
  onScoreChange: React.PropTypes.func.isRequired,
  onRemove:React.PropTypes.func.isRequired,
};

var Application = React.createClass({
  
  propTypes: {
  title: React.PropTypes.string, //EXPECTS ONLY STRINGS IN TITLE ATTRIBUTE
  InitialPlayers: React.PropTypes.arrayOf(React.PropTypes.shape({
    name: React.PropTypes.string.isRequired,
    score: React.PropTypes.number.isRequired,
    id: React.PropTypes.number.isRequired,
  })).isRequired,
},
  
  getDefaultProps: function(){
    return {
      title: "Scoreboard",
    }
  },
  
  getInitialState: function(){
    return{
      players: this.props.InitialPlayers,
    };
  },
  
  onScoreChange: function(index,delta){
    // console.log('onScoreChange',index,delta);
    this.state.players[index].score += delta;
    this.setState(this.state);
  },
  
  onPlayerAdd: function(name){
    // console.log('Player Added', name);
    this.state.players.push({
      name: name,
      score: 0,
      id: nextId,
    });
    this.setState(this.state);
    nextId +=1;
  },
  
  onRemovePlayer:function(index){
    this.state.players.splice(index,1);
    this.setState(this.state);
    // console.log('remove',index);
  },
  
  render: function(){
      return (
      <div className="scoreboard">
        <Header title={this.props.title} players={this.state.players}/>

        <div className="players">
          {this.state.players.map(function(player,index){
            return(
              <Player 
                onScoreChange={function(delta) {this.onScoreChange(index,delta)}.bind(this)}
                onRemove={function(){this.onRemovePlayer(index)}.bind(this)}
                name={player.name}
                score={player.score} 
                key={player.id}
                />
            );
          }.bind(this))}
        </div>
          <AddPlayerForm onAdd={this.onPlayerAdd}/>
      </div>
    );
  }
});

ReactDOM.render(<Application InitialPlayers={PLAYERS}/>, document.getElementById('container'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console