JSFiddle - React, Tailwind, and code Playground
HTML
<div id="containing_box">
<div class="expandingArea">
<pre><span></span><br></pre>
<textarea></textarea>
</div>
</div>
CSS
#containing_box {
width: 300px;
height: 200px;
overflow: auto;
border: 1px solid;
}
textarea,
pre, p {
margin: 0;
padding: 0;
outline: 0;
border: 0;
}
.expandingArea {
position: relative;
border: 1px solid #888;
background: #fff;
}
.expandingArea > textarea,
.expandingArea > pre {
padding: 5px;
background: transparent;
white-space: pre;
}
.expandingArea > textarea {
/* The border-box box model is used to allow
* padding whilst still keeping the overall width
* at exactly that of the containing element. */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
/* This height is used when JS is disabled */
height: 100px;
width: 100px;
}
.expandingArea.active > textarea {
/* Hide any scrollbars */
overflow: hidden;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
/* Remove WebKit user-resize widget */
resize: none;
}
.expandingArea > pre {
/* display: none;*/
color: blue;
}
.expandingArea.active > pre {
display: block;
/* Hide the text; just using it for sizing */
/* visibility: hidden; */
}
JavaScript
$(document).ready(
function() {
$('div.expandingArea').each(function() {
var area = $('textarea', $(this));
var span = $('span', $(this));
area.bind('input', function() {
span.text(area.val());
});
span.text(area.val());
$(this).addClass('active');
});
}
);