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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div id="root"></div>

              
            
!

CSS

              
                body {
  box-sizing: border-box;
  margin: 0 auto;
  overflow: hidden;
  font-family: Arial, Helvetica, sans-serif;
}

.page {
  position: absolute;
  background-color: black;
  padding: 0;
  width: 100%;
  height: 100%;  
}

.container {
  margin: 2rem auto;
  width: 60%;
  height: 90%; 
  border-radius: 5px;
  overflow: auto;
  padding: 15px;
}

.dnd-list {
  display: flex;
  flex-direction: row;
  height: 35px;
  margin-bottom: 10px;
}

.input-item {
  position: relative;
  height: 100%;
  flex-basis: 85%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  margin-bottom: 10px;
  font-size: calc(10px + 2vmin);
  color: black;
  border: none;
  border-radius: 5px;
  cursor: move;
  padding-left: 10px;
}

.over {
  transform: scale(1.01);
  border: 1px dashed #eee;
}

.addButton {
  display: flex;
  align-items: center;
  margin: auto;
  line-height: 26px;
  border: none;
  color: black;
  border-radius: 50%;
  font-size: 24px;
}

.delButton {
  display: flex;
  align-items: center;
  color: darkgrey;
  margin-left: 10px;
  height: 37px;
  width: 30px;
  font-size: 20px;
  font-weight: bolder;
  cursor: pointer;
}



              
            
!

JS

              
                const App = () => {
      
  let list = [1,2,3,4,5,6,7,'']
  let sourceElement = null

  const [sortedList, setSortedList] = React.useState(list)

  /* add a new entry at the end of the list.  */
  const newLine = () => {
    console.log(sortedList)
    setSortedList(sortedList.concat(''))
  }
  
  /* change opacity for the dragged item. 
  remember the source item for the drop later */
  const handleDragStart = (event) => {
    event.target.style.opacity = 0.5
    sourceElement = event.target
    event.dataTransfer.effectAllowed = 'move'
  }

  /* do not trigger default event of item while passing (e.g. a link) */
  const handleDragOver = (event) => {
    event.preventDefault()
    event.dataTransfer.dropEffect = 'move' 
  }

  /* add class .over while hovering other items */
  const handleDragEnter = (event) => {
    event.target.classList.add('over')    
  }

  /* remove class .over when not hovering over an item anymore*/
  const handleDragLeave = (event) => {
    event.target.classList.remove('over')
  }

  const handleDrop = (event) => {
    /* prevent redirect in some browsers*/
    event.stopPropagation()
    
    /* only do something if the dropped on item is 
    different to the dragged item*/
    if (sourceElement !== event.target) {

      /* remove dragged item from list */
      const list = sortedList.filter((item, i) => 
        i.toString() !== sourceElement.id)

      /* this is the removed item */
      const removed = sortedList.filter((item, i) => 
        i.toString() === sourceElement.id)[0]

      /* insert removed item after this number. */
      let insertAt = Number(event.target.id)

      console.log('list with item removed', list)
      console.log('removed:  line', removed)
      console.log('insertAt index', insertAt)

      let tempList = []

      /* if dropped at last item, don't increase target id by +1. 
         max-index is arr.length */
      if (insertAt >= list.length) {
        tempList = list.slice(0).concat(removed)
        setSortedList(tempList)
        event.target.classList.remove('over')
      } else
      if ((insertAt < list.length)) {
      /* original list without removed item until the index it was removed at */
        tempList = list.slice(0,insertAt).concat(removed)

        console.log('tempList', tempList)
        console.log('insert the rest: ', list.slice(insertAt))

        /* add the remaining items to the list */
        const newList = tempList.concat(list.slice(
          insertAt))
        console.log('newList', newList)

        /* set state to display on page */
        setSortedList(newList)
        event.target.classList.remove('over')
      }
    }
    else console.log('nothing happened')
    event.target.classList.remove('over') 
  }

  const handleDragEnd = (event) => {
    event.target.style.opacity = 1
    console.log('-------------------------------------------------------------')
  }

  /* log changes in current input field */
  const handleChange = (event) => {
    event.preventDefault()
    console.log('event.target.value', event.target.value)

    /* create new list where everything stays the same except that the current
    item replaces the existing value at this index */
    const list = sortedList.map((item, i) => {
      if (i !== Number(event.target.id)) { 
        return item }
      else return event.target.value   
    })
    setSortedList(list)
  }

  /* filter list where only items with id unequal to current id's are allowed */
  const handleDelete = (event) => {
    event.preventDefault()
    const list = sortedList.filter((item, i) => 
      i !== Number(event.target.id))
    console.log(event.target.id)
    setSortedList(list)
  }
  
  /* create list of items */
  const listItems = () => {

    return sortedList.map((item, i) => (
      <div key={i} className='dnd-list'>
        <input           
          id={i}
          type='text'
          className='input-item'  
          draggable='true' 
          onDragStart={handleDragStart} 
          onDragOver={handleDragOver} 
          onDragEnter={handleDragEnter}
          onDragLeave={handleDragLeave}
          onDrop={handleDrop}
          onDragEnd={handleDragEnd}
          onChange={handleChange}
          placeholder='Enter text here'
          value={sortedList[i]}
        />
        <div id={i} className='delButton' onClick={handleDelete}>X</div>
      </div>
    )
    )
  }

  
  console.log('sorted', sortedList)

  return (
    <div className='page'>
      <div className='container'>
        <h1 style={{ color: "white", textAlign: "center" }}>Today</h1>
        {listItems()}
        <button className='addButton' onClick={() => newLine()}>+</button>
      </div>
    </div>
  )
}


ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console