JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="autogrowWrapper">
<div class="autogrow"></div>
<div contenteditable="true"></div>
</div>
CSS
.autogrowWrapper {
position: relative;
}
.autogrow {
border: 1px solid rgb(0, 0, 0);
height: 40px; /* line-height + 2 * padding */
}
div[contenteditable] {
outline: none;
line-height : 20px;
position: absolute;
top: 10px; /* padding */
left: 10px; /* padding */
right: 10px; /* padding */
}
JavaScript
var kAnimationSpeed = 250;
var kPadding = 10;
var keyCodeDelete = 8;
var keyCodeReturn = 13;
$('div[contenteditable]').on('blur keyup paste', function(e) {
var el = $(this);
var styleElement = el.prev();
var editorHeight = el.height();
var styleElementHeight = styleElement.height();
var divCount = parseInt(el.data('divcount') || 0 );
var newDivCount = el.children('div').not(':contains("br")').length;
var newLineDeleted = (divCount - 1 == newDivCount && e.which == keyCodeDelete);
var heightCheck = (editorHeight !== styleElementHeight - kPadding * 2);
var keyCodeCheck = (e.which === keyCodeReturn || e.which === keyCodeDelete);
if (heightCheck && keyCodeCheck) {
if (newLineDeleted) {
$('body').append('<p>nl removed!</p>');
}
styleElement.stop().animate({ height: editorHeight + kPadding * 2 }, kAnimationSpeed);
}
el.data('divcount', newDivCount);
});