JSFiddle - React, Tailwind, and code Playground

by oscard

HTML

<!-- Resize Div -->
<div id="wrapper">
    <div id="drag_wrap">
        <div id="draggable-element">Resize me!</div>
        <div class="text-field" contenteditable="true" id="text-field">         
        </div>
        <div id="resize-handler" class="resize-handler"></div>
    </div>
</div>

CSS

body {padding:10px}
#wrapper{
    margin:5px;
    padding:10px;
    border:0px solid #ccc;
    position:absolute;
    bottom:0;
    left:0;
    right:0;
    top:0;
}
#drag_wrap {
  width:250px;
  border:1px solid #ccc;
  position:absolute;
}
#draggable-element {
  min-height:20px;
  color:white;
  padding:10px 12px;
  cursor:move;
  background:#ccc;
}
.text-field{
    min-height:50px;
    padding:5px;
    outline:none;
    border:1px dashed #ccc;
    margin:5px;
}
.resize-handler{
    height:20px;
    width:20px;
    right:-12px;
    bottom:-12px;
    border-radius:15px;
    position:absolute;
    border:1px solid #ccc;
    cursor:se-resize;
    background:#fff;
}

JavaScript

/*********************************
****   Resize Element first   ****
*********************************/

var rSelected = null, textFieldH, textFieldW, // Object of the element to be moved
    x_p = 0, y_p = 0, // Stores x & y coordinates of the mouse pointer
    x_elm = 0, y_elm = 0; // Stores top, left values (edge) of the element

    var textField = document.getElementById("text-field");

// Will be called when user starts dragging an element
function rDrag_init(elm) {
    // Store the object of the element which needs to be moved
    rSelected = elm;
    x_elm = x_p - rSelected.parentNode.offsetWidth;
    y_elm = y_p - rSelected.parentNode.offsetHeight;
     
    textFieldW = x_p - textField.offsetWidth;
    textFieldH = y_p - textField.offsetHeight;
}

// Will be called when user dragging an element
function rMove_elem(e) {
    x_p = document.all ? window.event.clientX : e.pageX;
    y_p = document.all ? window.event.clientY : e.pageY;
    if (rSelected !== null) {
        rSelected.parentNode.style.width = (x_p - x_elm) + 'px';
        rSelected.parentNode.style.height = (y_p - y_elm) + 'px';
        
        textField.style.width = (x_p - textFieldW - 10) + 'px';
        textField.style.height = (y_p - textFieldH - 10) + 'px';
    }
}

// Destroy the object when we are done
function rDestroy() {
    rSelected = null;
    return false;
}

// Bind the functions...
document.getElementById('resize-handler').onmousedown = function () {
    rDrag_init(this);
    return false;
};
document.onmousemove = rMove_elem;
document.onmouseup = rDestroy;