Git Product home page Git Product logo

angularjs-autogrow's Introduction

angularjs-autogrow

AngularJS directive for auto-grow / auto-resize of textarea elements on typing.

  • Works in all cases: expands on line breaks and word breaks.
  • Great Performance: minimal DOM manipulation and no watchers.
  • Allows limitation of auto-growing so a scrollbar will appear after X lines of content.

Installation via NPM

npm install angularjs-autogrow --save

Usage:

Include angular-autogrow.min.js file in <head> section of the HTML:

<script type="text/javascript" src="angularjs-autogrow.min.js"></script>

Include angularjs-autogrow dependency in your angular module:

var app = angular.module("app", ["angularjs-autogrow"]);

It's also recommended to add those two CSS properties to make things stable:

textarea {
	resize: none;
	box-sizing: content-box;
}

Add the directive to the textarea element:

<textarea autogrow></textarea>

More Options:

Limit Autogrow Lines

You can limit the auto-growing of the textarea element by using max-lines attribute:

<textarea autogrow max-lines="3"></textarea>

Set Initial Rows

You can set the initial line number using rows attribute:

<textarea autogrow rows="1"></textarea>

Autogrow on css properties change

You can define which CSS properties have to be watched in order to trigger the auto-growing:

<textarea autogrow="font-family,font-size"></textarea>

angularjs-autogrow's People

Contributors

codingaspect avatar evyros avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

angularjs-autogrow's Issues

Doesn't update on directive load with pre-existing text

Hi,
I was having an issue with the directive (very good, by the way!) when put inside another directive and then having initial text on page load.

I fixed this using a $timeout in the autogrowFn call and changing the event listener.

I hope it may help other users :)

(function(){
    'use strict';
    angular.module('angular-autogrow', []).directive('autogrow', ['$window', '$timeout', function($window, $timeout){
        return {
            link: function($scope, $element, $attrs){

                /**
                 * Default settings
                 */
                $scope.attrs = {
                    rows: 1,
                    maxLines: 999
                };

                /**
                 * Merge defaults with user preferences
                 */
                for(var i in $scope.attrs){
                    if($attrs[i]){
                        $scope.attrs[i] = parseInt($attrs[i]);
                    }
                }

                /**
                 * Calculates the vertical padding of the element
                 * @returns {number}
                 */
                $scope.getOffset = function(){
                    var style = $window.getComputedStyle($element[0], null),
                        props = ['paddingTop', 'paddingBottom'],
                        offset = 0;

                    for(var i=0; i<props.length; i++){
                        offset += parseInt(style[props[i]]);
                    }
                    return offset;
                };

                /**
                 * Sets textarea height as exact height of content
                 * @returns {boolean}
                 */
                $scope.autogrowFn = function(){
                    $timeout(function() {
                        var newHeight = 0, hasGrown = false;
                        if (($element[0].scrollHeight - $scope.offset) > $scope.maxAllowedHeight) {
                            $element[0].style.overflowY = 'scroll';
                            newHeight = $scope.maxAllowedHeight;
                        }
                        else {
                            $element[0].style.overflowY = 'hidden';
                            $element[0].style.height = 'auto';
                            newHeight = $element[0].scrollHeight - $scope.offset;
                            hasGrown = true;
                        }
                        $element[0].style.height = newHeight + 'px';
                        return hasGrown;
                    });
                };

                $scope.offset = $scope.getOffset();
                $scope.lineHeight = ($element[0].scrollHeight / $scope.attrs.rows) - ($scope.offset / $scope.attrs.rows);
                $scope.maxAllowedHeight = ($scope.lineHeight * $scope.attrs.maxLines) - $scope.offset;

                $scope.$watch($attrs.ngModel, $scope.autogrowFn);
                /**
                 * Auto-resize when there's content on page load
                 */
                if($element[0].value !== ''){
                    $scope.autogrowFn();
                }
            }
        }
    }]);
})();

Please note that I know it should be a Pull Request, but I didn't have the possibility to do this when I found the fix at work ;)

Auto-resize on load?

Is there a way to possibly have the textarea boxes autogrow to fit the value already in them when the page/view loads with them in it?

Very slow when typing and holding specific key

The rendering of the text in the textarea is very slow when you are trying to type and holds a specific key only (example: try to type aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)

I found out that there is an issue in this part:

setting the height of the textarea to auto then setting it again to new height.

image

Grow Up? (Catchy Title :-)

Hi,

Cool component. I am able to use it on a "regular" scenario but - I have a chat page, similar to how WhatsApp looks in which the text area is at the bottom and I want it to expand upwards and not downwards.

Is there a way to do it?

I've seen this example that does so but I am not able to get it to work within my angular app :-(

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.