Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <ul id='groceries'>
   <li draggable="true">A</li>
  <li draggable="true">B</li>
  <li draggable="true">C</li>
  <li draggable="true">D</li>
  <li draggable="true">E</li>
  <li draggable="true">F</li>
  <li draggable="true">G</li>
  <li draggable="true">H</li>
  <li draggable="true">I</li>
  <li draggable="true">J</li>
  <li draggable="true">K</li>
  <li draggable="true">L</li>
  <li draggable="true">M</li>
  <li draggable="true">N</li>
  <li draggable="true">O</li>
  <li draggable="true">P</li>
  <li draggable="true">R</li>
  <li draggable="true">S</li>
</ul>
              
            
!

CSS

              
                  body {
    font-family: "Helvetica Neue", sans-serif;
  }

  ul {
    list-style-type: none;
    padding-left : 0;
    user-select: none;
    -webkit-user-select: none;
    display: flex;
    align-items: center;
    width: 80%;
    flex-wrap: wrap;
  }

  ul li {
    border: 1px solid rgb(150,150,255);
    min-width: 72px;
    width: 72px;
    height: 72px;
    box-sizing: border-box;
    text-align: center;
    margin: 12px;
    background: rgb(240,240,240);
    padding: 3px;
    display: block;
    color: black;
  }

  ul li.insertion-marker {
    position: relative;
    height: 72px;
    min-width: 1px;
    width: 6px;
    box-sizing: border-box;
    margin: 0 -3px;
    border-radius: 2px;
    background: rgb(150,150,255);
    border: none;
    z-index: 999999;
  }

  ul li.selected {
    background: rgb(150,150,255);
    color: white;
  }

  tt {
    display: inline-block;
    font-size: 1.05rem;
    color: red;
    background: rgb(250,230,230);
    padding: 1px 2px;
    border: 1px solid;
    border-radius: 2px;
  }

  .test-good, .test-bad {
    font-family: monospace;
    border: 1px solid;
    border-left: 5px solid;
    padding: 2px 5px;
    margin: 1px 0;
  }
  .test-good {
    color: green;
    background: rgb(220,255,220);
  }
  .test-bad {
    color: red;
    background: rgb(255,220,220);
  }
              
            
!

JS

              
                  const ul = document.querySelector('ul#groceries');
  const lis = ul.querySelectorAll('li');

  // Compute the centroid of an element in _page_ coordinates
  // (from the top-left of the page, accounting for the scroll).
  // We need to account for the scroll here because it is not only possible,
  // but actually _used_ by many that with long lists you can scroll while
  // you drag - pick an item, focus over the destination drop area and then scroll
  // using the wheel to "reposition" the area for your drop. Check this out, really -
  // it works like this in native macOS controls since ages.
  //
  // Also, one of the very good indications of web-engine based apps posing as native:
  // scroll-during-drag not working correctly. We will not be like those apps.
  function computeCentroid(element) {
    const rect = element.getBoundingClientRect();
    const viewportX = (rect.left + rect.right) / 2;
    const viewportY = (rect.top + rect.bottom) / 2;
    return {x: viewportX + window.scrollX, y:  viewportY + window.scrollY};
  }

  function distanceBetweenCursorAndPoint(evt, centroid) {
    return Math.hypot(centroid.x - evt.clientX - window.scrollX, centroid.y - evt.clientY - window.scrollY);
  }

  const INTENT_BEFORE = Symbol();
  const INTENT_AFTER = Symbol();
  const DIRECTION_HORIZONTAL = Symbol();
  const DIRECTION_VERTICAL = Symbol();

  function predictDirection(a, b) {
    if (!a || !b) return DIRECTION_HORIZONTAL;
    const dx = Math.abs(b.centroid.x - a.centroid.x);
    const dy = Math.abs(b.centroid.y - a.centroid.y);
    return dx > dy ? DIRECTION_HORIZONTAL : DIRECTION_VERTICAL;
  }

  function intentFrom(direction, evt, centroid) {
    if (direction === DIRECTION_HORIZONTAL) {
     return ((evt.clientX + window.scrollX) < centroid.x) ? INTENT_BEFORE : INTENT_AFTER;
    } else {
     return ((evt.clientY + window.scrollY) < centroid.y) ? INTENT_BEFORE : INTENT_AFTER;
    }
  }

  function startReorderWithElement(el, {debug}) {
    const parent = el.parentNode;
    const orderables = Array.from(parent.children).map((element, i) => {
      return {i, element, centroid: computeCentroid(element)};
    });

    // Determine the dominant direction in the list - is it horizontal or vertical?
    const direction = predictDirection(orderables[0], orderables[1]);

    let closest = el;
    let intent = INTENT_AFTER;
    let marker = document.createElement(el.nodeName);
    marker.classList.add("insertion-marker");

    const unstyle = () => {
      orderables.forEach(({element}) => {
        element.classList.remove("reorder-accepts-before");
        element.classList.remove("reorder-accepts-after");
      });
    };

    const mouseMoveHandler = (evt) => {
      evt.preventDefault();

      const byDistance = orderables.map((orderable) => {
        return {distance: distanceBetweenCursorAndPoint(evt, orderable.centroid), ...orderable};
      }).sort((a, b) => a.distance - b.distance);

      closest = byDistance[0].element;
      intent = intentFrom(direction, evt, byDistance[0].centroid);

      unstyle();
      marker.remove();

      byDistance.forEach(({element}) => {
        if (element !== closest) return;
        if (intent === INTENT_BEFORE) {
          marker = element.insertAdjacentElement('beforebegin', marker)
          element.classList.add("reorder-accepts-before");
        } else {
          marker = element.insertAdjacentElement('afterend', marker)
          element.classList.add("reorder-accepts-after"); 
        }
      });
    };
    parent.addEventListener('dragover', mouseMoveHandler);

    const stopFn = () => {
      unstyle();
      marker.remove();
      parent.removeEventListener('dragover', mouseMoveHandler);
      return {closest, intent};
    };

    return stopFn;
  }

  lis.forEach((li) => {
    li.addEventListener("dragstart", (evt) => {
      console.warn("reorder started")
      li.classList.add("selected");
      const stop = startReorderWithElement(li, {debug: true});

      li.parentNode.addEventListener("drop", (evt) => evt.preventDefault(), {once: true});
      li.addEventListener("dragend", (evt) => {
        evt.preventDefault();

        console.warn("reorder ending");
        li.classList.remove("selected");

        const {closest, intent} = stop();
        if (intent === INTENT_BEFORE) {
          closest.insertAdjacentElement('beforebegin', li);
        } else {
          closest.insertAdjacentElement('afterend', li);
        }
      }, {once: true});
    });
  });
              
            
!
999px

Console