Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #root
              
            
!

CSS

              
                $green: rgb(67, 239, 169);
$blue: rgb(78, 179, 241);

$theme1: $green;
$theme2: $blue;

html, body, #root, #app{
  height: 100%;
  margin: 0px;
  padding: 0px;
  width: 100%;
}

body{
  overflow-x: hidden;
}

.scroll-bar{
  &::-webkit-scrollbar {
    background-color: white;
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background-color: $gray200;
  }
}

#app{
  overflow: auto;
  div, h1, h2, h3, h4, h5, h6, input, button, li, textarea{
    font-family: 'Roboto', sans-serif;
  }
  
  #hints{
    background-color: white;
    border-radius: 2px;
    box-shadow: $shadow1;
    bottom: 0px;
    left: 0px;
    margin: 20px;
    padding: 15px 20px;
    position: fixed;
    z-index: 1;
    
    #hint-title{
      border-bottom: 1px solid $gray230;
      margin-bottom: 10px;
      padding-bottom: 5px;
      h1{
        color: $gray60;
        font-size: 0.8em;
        font-weight: 400;
        margin: 0px;
      }
    }
    
    .hint{
      &:nth-child(n + 2){
        margin-top: 5px;
      }
      
      svg, h1{
        display: inline-block;
        height: 12px;
        vertical-align: top;
      }
      
      svg{
        width: 12px;
        
        path{
          fill: $theme2;
        }
      }
      
      h1{
        color: $gray60;
        font-size: 0.8em;
        font-weight: 400;
        line-height: 12px;
        margin: 0px;
        margin-left: 10px;
      }
    }
  }
  
  #app__background-accent{
    background-color: $theme2;
    height: 90vh;
    left: 50%;
    position: fixed; 
    top: 50%;
    transform: translateX(-50%) rotate(5deg);
    width: 150%;
  }
  
  #items-outer-container{
    animation: fadeIn 0.5s ease-in;
    background-color: white;
    border-radius: 10px;
    box-shadow: $shadow2;
    left: 50%;
    padding: 20px;
    position: absolute;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    z-index: 10;
    
    #items-container{
      max-height: 600px;
      overflow: auto;
      width: 460px;

      #add-item-input{
        padding: 20px;
        padding-left: 40px;

        svg, input{        
          display: inline-block;
          vertical-align: top;
        }

        svg{
          height: 20px;
          margin-right: 20px;
          width: 20px;

          path{
            fill: $gray200;
          }
        }

        input{
          border: 0px;
          color: $gray120;
          font-size: 1em;
          font-weight: 400;
          height: 20px;
          outline: 0px;
          padding: 0px;
        }
      }

      #items{
        margin: 20px;
        margin-top: 0px;
        overflow: hidden;
        position: relative;
        
        .item{
          left: 0px;
          padding: 20px;
          position: absolute;
          transition: all 0.5s, background-color 0s;
          width: calc(100% - 40px);

          &:hover{
            background-color: rgba($gray230, 0.4);
                    
            .item-name-input{
              textarea{
                background-color: transparent;
              }
            }
            
            .item-edit, .item-delete{
              opacity: 1; 
            }
          }

          &.completed{
            .item-icon{
              &:hover{
                .completed{
                  path{
                    fill: $gray60;
                  }
                }
              }
                
              .completed{
                opacity: 1;
                transform: translateX(-50%) translateY(-50%) scale(1);           
              } 

              .uncompleted{
                opacity: 0;
                transform: translateX(-50%) translateY(-50%) scale(0.25);
              } 
            }
            
            .item-name{
              h1{
                color: $gray200;
                text-decoration: line-through;
              }
            }
            
            .item-edit{
              opacity: 0;
              pointer-events: none;
            }
          }
          
          &.editing{
            .item-edit{
              opacity: 0;
              pointer-events: none;
            }
          }

          .item-name, .item-icon, .item-edit, .item-name-input, .item-delete{
            display: inline-block;
            vertical-align: top;
          }

          .item-name{
            margin-left: 20px;
            width: calc(100% - 120px);
            h1{
              color: $gray60;
              font-size: 1em;
              font-weight: 400;
              line-height: 20px;
              margin: 0px;
            }
          }

          .item-icon{
            cursor: pointer;
            height: 20px;
            position: relative;
            width: 20px;
            z-index: 2;
            
            &:hover{
              svg{
                &.uncompleted{
                  path{
                    fill: $theme1 !important;
                  }
                }
              }
            }
                
            svg{          
              height: 18px;
              left: 50%;
              line-height: 18px;
              position: absolute;
              top: 50%;
              transform: translateX(-50%) translateY(-50%) scale(1);
              transition: all 0.5s;
              width: 18px;

              &.completed{
                opacity: 0;
                transform: translateX(-50%) translateY(-50%) scale(0.25);
                path{
                  fill: $theme1;
                } 
              }

              &.uncompleted{
                path{
                  fill: $gray60;
                } 
              }
            }
          }
          
          .item-edit, .item-delete{
            cursor: pointer;
            height: 18px;
            line-height: 18px;
            margin-left: 20px;
            opacity: 0;
            position: relative;
            text-align: center;
            width: 18px;
            z-index: 2;
            
            &:hover{
              svg{
                path{
                  fill: $theme1;
                }
              }
            }

            svg{
              height: 18px;
              line-height: 18px;
              position: relative;
              text-align: center;
              width: 18px;
              z-index: 1;
              path{
                fill: $gray60;
              }
            }
          }
          
          .item-name-input{
            margin-left: 20px;
            width: calc(100% - 120px);
            
            textarea{
              border: none;
              font-size: 1em;
              font-weight: 400;
              line-height: 20px;
              outline: none;
              padding: 0px;
              resize: none;
              width: 100%;
            }
          }
          
          .item-delete{
            
            &:hover{
              svg{
                path{
                  fill: $red;
                }
              }
            }
          }
        }

        #items-completed__header{
          height: 60px;
          margin-top: 20px;
          position: relative;

          &:after{
            background-color: $gray230;
            content: '';
            height: 1px;
            left: 0px;
            position: absolute;
            top: 0px;
            width: 100%;
          }

          h1{
            color: $gray200;
            font-size: 1em;
            font-weight: 400;
            height: 19px;
            margin: 0px;
            padding: 20px;
            padding-left: 60px;
          }
        }

        #items-uncompleted__spacer, #items-completed__spacer{
          transition: all 0.5s;
          width: 100%;
        }

        #items-uncompleted__spacer{
          top: 0px;
        }

        #items-completed__spacer{

        }
      }
    }
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

              
            
!

JS

              
                class App extends React.Component{
  constructor(props){
    super(props)
    this.getMeta = this.getMeta.bind(this)
    this.toggleItem = this.toggleItem.bind(this)
    this.addItem = this.addItem.bind(this)
    this.editItem = this.editItem.bind(this)
    this.deleteItem = this.deleteItem.bind(this)
    this.setItemHeight = this.setItemHeight.bind(this)
    this.getItemCountText = this.getItemCountText.bind(this)
    this.state = {
      item: {
        height: 60
      },
      items: [
        {id: 1, name: 'Go to the store', isCompleted: false, height: 0},
        {id: 2, name: 'Walk the dog', isCompleted: false, height: 0},
        {id: 3, name: 'Hit the gym', isCompleted: false, height: 0}
      ]
    }
  }
  componentDidMount(){
    setTimeout(() => this.toggleItem(1), 700)
  }
  getMeta(){
    const {items} = this.state,
          completed = items.filter(item => item.isCompleted),
          uncompleted = items.filter(item => !item.isCompleted)
    
    return {
      completed: {
        items: completed,
        height: completed.length > 0 ? _.sumBy(completed, 'height') : 0
      },
      uncompleted: {
        items: uncompleted,
        height: uncompleted.length > 0 ? _.sumBy(uncompleted, 'height') : 0
      }
    }
  }
  toggleItem(id){
    const updatedItems = this.state.items
      .map(item => ({...item, isCompleted: item.id === id ? !item.isCompleted : item.isCompleted}))
    this.setState({items: updatedItems})
  }
  addItem(name){
    let updatedItems = [...this.state.items]
    updatedItems.push({id: 0, name, isCompleted: false})
    updatedItems = updatedItems.map((item, index) => {
      const newItem = {...item,  id: index + 1}
      return newItem
    })
    this.setState({items: updatedItems})
  }
  editItem(id, name){
    const updatedItems = this.state.items
      .map(item => ({...item, name: item.id === id ? name : item.name}))
    this.setState({items: updatedItems})
  }
  deleteItem(id){
    let updatedItems = [...this.state.items].filter(item => item.id !== id)
    this.setState({items: updatedItems})
  }
  setItemHeight(id, height){
    const updatedItems = this.state.items
      .map(item => {
        if(item.id === id){
          item.height = height
        }
        return item
      })
    this.setState({items: updatedItems})
  }
  getItemCountText(){
    const meta = this.getMeta()
    let itemCountText = ''
    if(meta.completed.items.length === 0){
      itemCountText = 'No completed items'
    }
    else if(meta.completed.items.length >= 1){
      const pluralText = meta.completed.items.length === 1 ? 'item' : 'items'
      itemCountText = `${meta.completed.items.length} completed ${pluralText}`
    }
    return itemCountText
  }
  render(){
    const meta = this.getMeta()
    
    let uInd = 0, cInd = 0
    
    const items = this.state.items
      .map((item, index) => (
        <Item 
          key={item.id} 
          id={item.id}
          index={item.isCompleted ? cInd++ : uInd++}
          height={item.height}
          name={item.name} 
          meta={meta}
          isCompleted={item.isCompleted}
          toggleItem={() => this.toggleItem(item.id)}
          editItem={this.editItem}
          deleteItem={() => this.deleteItem(item.id)}          
          setItemHeight={this.setItemHeight}
        />
      ))
    
    const itemCountText = this.getItemCountText()
                                  
    return(
      <div id="app">
        <div id="items-outer-container">
          <div id="items-container" className="scroll-bar">
            <AddItemInput addItem={this.addItem}/>
            <div id="items">
              <div 
                id="items-uncompleted__spacer"
                style={{height: `${meta.uncompleted.height}px`}}
              />
              {items}
              <div id="items-completed__header">
                <h1>{itemCountText}</h1>
              </div>
              <div 
                id="items-completed__spacer" 
                style={{height: `${meta.completed.height}px`}}
              />
            </div>
          </div>
        </div>
        <div id="app__background-accent"/>
        <div id="hints">
          <div id="hint-title">
            <h1>Hints</h1>
          </div>
          <div className="hint">
            <i className="fas fa-arrow-right"/>
            <h1>Hit enter to add new todo</h1>
          </div>
          <div className="hint">
            <i className="fas fa-arrow-right"/>
            <h1>Double click todo text to edit</h1>
          </div>
        </div>
      </div>
    )
  }
}

class AddItemInput extends React.Component {
  constructor(props){
    super(props)
    this.handleKeyDown = this.handleKeyDown.bind(this)
  }
  handleKeyDown(e){
    const {value} = e.target
    if(value && e.key === 'Enter'){
      this.props.addItem(value)
      this.refs.addItemInput.value = ''
    }
  }
  render(){
    const {handleKeyDown} = this.props
    return(
      <div id="add-item-input">
        <i className="fas fa-plus"/>
        <input 
          ref="addItemInput"
          type="text" 
          placeholder="Add item" 
          onKeyDown={this.handleKeyDown}
        />
      </div>
    )
  }
}

class Item extends React.Component{
  constructor(props){
    super(props)
    this.getTop = this.getTop.bind(this)
    this.toggleEdit = this.toggleEdit.bind(this)
    this.state = {
      isEditing: false
    }
  }
  componentDidMount(){
    const height = this.refs.item.getBoundingClientRect().height
    this.props.setItemHeight(this.props.id, height)
  }
  componentDidUpdate(){
    const height = this.refs.item.getBoundingClientRect().height
    if(height !== this.props.height){
      console.log(height, this.props.height)
      this.props.setItemHeight(this.props.id, height)
    }
  }
  getTop(){
    const {meta, id, index, height, isCompleted} = this.props
    
    const prevHeight = isCompleted ? 
            _.sumBy(meta.completed.items.slice(0, index), 'height') :
            _.sumBy(meta.uncompleted.items.slice(0, index), 'height'),
          top = isCompleted ? 
            meta.uncompleted.height + prevHeight + 80 :
            prevHeight
    
    return top
  }
  toggleEdit(){
    this.setState({isEditing: !this.state.isEditing})
  }
  render(){
    const {name, id, height, index, isCompleted, toggleItem, deleteItem, editItem} = this.props
    const {isEditing} = this.state
    let classes = isCompleted ? "item completed" : "item",
          top = this.getTop()
    
    classes = isEditing ? `${classes} editing` : classes

    const itemName = isEditing ? (
      <ItemNameInput 
        id={id} 
        name={name} 
        height={height} 
        editItem={editItem}
        toggleEdit={this.toggleEdit}
      />
    ) : (
      <div className="item-name" onDoubleClick={!isCompleted ? this.toggleEdit : null}>
        <h1>{name}</h1>
      </div>
    )
    
    return(
      <div 
        id={`item-${id}`}
        data-height={height}
        data-top={top}
        ref="item"
        className={classes} 
        style={{top: `${top}px`}}
      >
        <div className="item-icon" onClick={toggleItem}>
          <i className="far fa-circle uncompleted"/>
          <i className="fas fa-check completed"/>
        </div>
        {itemName} 
        <div className="item-edit" onClick={this.toggleEdit}>
          <i className="fas fa-pen"/>
        </div>
        <div className="item-delete" onClick={deleteItem}>
          <i className="fas fa-times-circle"/>
        </div>
      </div>
    )
  }
}

class ItemNameInput extends React.Component{
  constructor(props){
    super(props)
    this.handleKeyDown = this.handleKeyDown.bind(this)
    this.toggleEdit = this.toggleEdit.bind(this)
  }
  componentDidMount(){
    this.refs.itemNameTextArea.focus()
  }
  handleKeyDown(e){
    const {value} = e.target
    if(value && e.key === 'Enter'){
      this.props.toggleEdit()
      this.props.editItem(this.props.id, value)
    }
  }
  toggleEdit(e){
    const {value} = e.target
    this.props.toggleEdit()
    this.props.editItem(this.props.id, value)
  }
  render(){
    const {name, height} = this.props
    return(
      <div className="item-name-input" style={{height: `${height - 40}px`}} >
        <textarea 
          ref="itemNameTextArea" 
          defaultValue={name} 
          style={{height: `${height - 40}px`}}
          onKeyDown={this.handleKeyDown}
          onBlur={this.toggleEdit}
        />
      </div>
    )
  } 
}

ReactDOM.render(<App/>, document.getElementById('root'))
              
            
!
999px

Console