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 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.

            
              @import url('https://fonts.googleapis.com/css?family=Open+Sans|Slabo+27px');

body{
  font-family: 'Open Sans', sans-serif;
  background: linear-gradient(45deg, #32ff92 0%,#2bc3ff 83%);
  min-height:100vh;
}

.todo{
  display:flex;
  background: linear-gradient(to right, #e3f2fd 68%,#cce7f9 100%);
  padding:0.5em;
  // border-bottom:1px solid #90CAF9;
  align-items:flex-start;
  margin-bottom: 0.5em;
  box-shadow:2px 2px 2px rgba(0,0,0,0.3);
  max-height:100px;
  overflow:hidden;
  transition: all 300ms ease;
  
  button{
    margin-right:1em;
  }
  
  &.deleted{
    max-height:0px;
    padding:0;
    margin-bottom:0;
    opacity:0;
  }
}

button{
  border:0;
  color:#fff;
  outline:none;
}

.remove{
  color: #F44336;
  background:none;
}

.todo-list{

  max-width:400px;
  margin:3em auto 0;
  background: linear-gradient(to right, #f7f7f7 54%,#e8f4fc 100%);
  padding:1em;
  box-shadow:5px 5px 10px rgba(0,0,0,0.3);

  h1{
    margin-top:0;
    color:#212121;
    font-family: 'Slabo 27px', serif;
    text-align:center;
    
  }

  .controls{
    display:flex;
    margin-top:1em;
    input{
      flex:1;
      padding:0.5em;
      border: 1px solid rgba(#009688,0.5);
      border-right:0px;
      outline:none;
    }
    button{
      background: linear-gradient(to right, #009688 1%,#005e6b 100%);
      color:#fff;
      padding:0.5em;
    }
  }

}
            
          
!
            
              const {React, ReactDOM} = window;

class TodoList extends React.Component{

  constructor(props){
    super(props);
    this.state = JSON.parse(localStorage.getItem('todoapp')) || {
      input: '',
      todos: [{
        value: 'This is a sample TODO!',
        id: this.guid()
      }]
    };
    this.addTodo = this.addTodo.bind(this);
    this.handleInput = this.handleInput.bind(this);
    this.removeTodo = this.removeTodo.bind(this);
  }

  componentDidMount(){
    this.todoInput.focus();
  }

  componentDidUpdate(){
    localStorage.setItem('todoapp', JSON.stringify(this.state));
  }

  addTodo(){
    const newTodo = {
      value: this.state.input,
      id: this.guid()
    };

    this.setState(state => ({
      todos: [ ...state.todos, newTodo],
      input: ''
    }));
  }

  handleInput(evt){
    if(evt.nativeEvent.key === "Enter"){
      this.addTodo();
    }else{
      this.setState({
        input: evt.target.value
      });
    }
  }

  removeTodo(id){

    this.setState(state => {
      return{
        todos: state.todos.map(todo => {
          if(todo.id !== id){
            return todo;
          } else {
            return { ...todo, deleted: true }
          }
        })
      };
    });

    setTimeout(() => {
      this.setState(state => {
        return{
          todos: state.todos.filter(t => t.id !== id)
        }});
    }, 1000);
  }

  guid(){
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
      return v.toString(16);
    });
  }

  render(){
    return(
      <div className="todo-list">
        <h1>Reactjs Todo List</h1>

        { this.state.todos.map(t => <Todo key={t.id} {...t} onClick={()=>this.removeTodo(t.id)}/>)}

        <div className="controls">
          <input type="text" 
            value={this.state.input} 
            onChange={this.handleInput} 
            onKeyDown={this.handleInput}  
            ref={(input) => { this.todoInput = input; }} />
          <button onClick={this.addTodo}>Add</button>
        </div>
      </div>
    )
  }  
}

const Todo = ({value, onClick, deleted}) => (
  <div className={`todo ${deleted? 'deleted' : ''}`} >
    <button className="remove" onClick={onClick}>×</button>
    <div>{value}</div>

  </div>
);

ReactDOM.render(
  <TodoList />,
  document.querySelector('#app')
);
            
          
!
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.
Loading ..................

Console