Z-index bring to front last dragged window

Author: lifeofjerCreated May 9, 2019Updated Jul 27, 2025

Wanted to share this snippet of code I am using to bring the latest dragged window to the front. Any feedback or improvements are welcome for others looking for the same functionality.

Note: This is definitely a clunky implementation and involves a flash due to timing on when the onStop is triggered

<Draggable 
  onStop={onDraggableStop}
/>
bash
export function onDraggableStop(element) {

  var prev_window = document.getElementsByClassName('react-draggable-last');
  var new_window = document.getElementById(element.path[1].id);

  setTimeout(function(){

    var i;
    for (i = 0; i < prev_window.length; i++) {
      prev_window[i].classList.remove('react-draggable-last');
    }
    new_window.classList.add('react-draggable-last');


  }, 0);

}

Source: react-grid-layout/react-draggable