JSFiddle - React, Tailwind, and code Playground

by Felipe Alfaro

HTML

<textarea id="test" cols="30" rows="1"></textarea>

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

CSS

textarea {
    padding: 7px 12px 7px 12px;
    margin: 0;
    line-height: 17px;
    box-sizing: border-box;
    resize: none;
    min-height: 30px;
    height: 30px;
    border: none;
    outline: none;
    overflow: hidden;
}

JavaScript

(function(doc){

	var textArea = doc.getElementById('test');
    
    var rowHeight = 17;
    textArea.oninput = function(){
    	var height = textArea.clientHeight,
        	scrollHeight = textArea.scrollHeight;
        if(height===scrollHeight){
            while(height===scrollHeight){
            	this.style.height = (height-rowHeight) + 'px';
                height = textArea.clientHeight;
                scrollHeight = textArea.scrollHeight;
            }
        }else if(height+1<scrollHeight){
            var newHeight = 30 + (Math.floor(scrollHeight/rowHeight)-1)*rowHeight;
            this.style.height = newHeight + 'px'
        }
        
            
         
    };
    

})(document);