JSFiddle - React, Tailwind, and code Playground
HTML
<div class="wrapper">
<div class="main-content">
<div class="header">
<button class="add-row" onclick="addRow()">Add Row +</button>
<button class="add-column" onclick="addCol()">Add Column +</button>
</div>
<div class="content-box" id="contentBox">
<p>
Click add row to add row, to add column - > click add column and select any added row
</p>
</div>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
.wrapper {
float: left;
width: 100%;
height: 100vh;
}
body {
padding: 0;
margin: 0;
position: relative;
}
.sidebar {
float: left;
width: 300px;
background: #03A9F4;
height: 100%;
padding: 10px;
}
.main-content {
float: left;
width: 100%;
height: 100%;
background: #fafafa;
}
.each-draggable-item {
width: 100%;
height: 40px;
padding: 0 10px;
display: flex;
align-items: center;
border: 1px solid #ccc;
background: white;
margin-bottom: 10px;
border-radius: 6px;
}
.header {
height: 60px;
display: flex;
align-items: center;
justify-content: flex-end;
padding: 0 20px;
}
.content-box {
width: 100%;
height: calc(100% - 60px);
padding: 15px;
}
button {
background: #000;
border: 0;
padding: 0 20px;
height: 40px;
margin-left: 10px;
font-weight: 600;
color: white;
cursor: pointer;
}
.row-block {
width: 100%;
border: 2px dashed #848484;
padding: 20px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-bottom: 20px;
}
.row-block:hover {
border-color: #2654d1;
}
.column-block {
position: relative;
width: 100%;
min-height: 1px;
padding-right: 10px;
padding-left: 10px;
margin: 0 10px;
-ms-flex-preferred-size: 0;
flex-basis: 0;
-webkit-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
max-width: 100%;
border: 2px dashed #848484;
background-color: #dedede;
padding: 20px;
}
/* .column-inner {
border: 2px dashed #848484;
padding: 20px;
min-height: 200px;
} */
.column-block:first-child {
margin-left: 0;
}
.column-block:last-child {
margin-right: 0;
}
.row-block .each-draggable-item {
position: relative;
width: 100%;
min-height: 1px;
padding-right: 10px;
padding-left: 10px;
-ms-flex-preferred-size: 0;
flex-basis: 0;
-webkit-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
max-width: 100%;
margin-bottom: 0;
}
.gu-mirror {
cursor: grabbing;
}
.container .ex-moved {
background-color:...
JavaScript
// Row Addition
var i = 0;
var j = 0;
let newColNode = null;
function addRow() {
const row = document.createElement('div');
row.className = 'row-block';
row.setAttribute('draggable', true);
++i;
row.textContent = `Row-${i}`;
row.id = "row" + i;
document.getElementById('contentBox').appendChild(row);
row.addEventListener('click', function(event) {
if (newColNode != null)
row.appendChild(newColNode);
newColNode = null
});
row.addEventListener('dragstart', dragStart);
row.addEventListener('dragend', dragEnd);
row.addEventListener('dragover', dragOver);
row.addEventListener('dragenter', dragEnter);
row.addEventListener('dragleave', dragLeave);
row.addEventListener('drop', Drop);
}
// Row Addition
function addCol() {
const col = document.createElement('p');
col.className = 'column-block';
col.setAttribute('draggable', true);
++j;
col.textContent = `Column-${j}`;
col.id = "col" + j;
newColNode = col;
col.addEventListener('dragstart', dragStart);
col.addEventListener('dragend', dragEnd);
col.addEventListener('dragover', dragOver);
col.addEventListener('dragenter', dragEnter);
col.addEventListener('dragleave', dragLeave);
col.addEventListener('drop', Drop);
}
// drag and drop
var dragItem = null;
var dropItem = null;
function dragStart() {
if (dragItem !== null) return;
console.log("from: ", this.id);
dragItem = this;
setTimeout = (() => this.style.display = "none", 0)
}
function dragEnd() {
setTimeout = (() => this.style.display = "none", 0)
dragItem = null;
dropItem = null;
}
function Drop() {
if (dropItem !== null) return;
console.log("to: ", this.id);
dropItem = this;
if (dragItem !== null)
this.append(dragItem);
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
}
function dragLeave() {
// this.style.borderColor = "none";
}