Skip to content

Instantly share code, notes, and snippets.

@guidobouman
Last active June 14, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidobouman/9550274 to your computer and use it in GitHub Desktop.
Save guidobouman/9550274 to your computer and use it in GitHub Desktop.
Angular Fluid Textarea Directive
// Use like: <textarea fluid-textarea rows="1"></textarea>
angular.module('q42.fluidTextarea', [])
.directive('fluidTextarea', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.css({
resize: 'none'
});
element.attr('rows', 1);
function update() {
element.height('auto');
var scrollHeight = element.get(0).scrollHeight;
var paddingTop = parseInt(element.css('padding-top'))
var paddingBottom = parseInt(element.css('padding-bottom'));
var targetHeight = scrollHeight - (paddingTop + paddingBottom);
element.height(targetHeight);
}
element.on('input', update);
update();
}
};
});
@guidobouman
Copy link
Author

Bug: the update function creates a tiny textarea when it's not actually visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment