Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dontFocus to resize #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions autogrow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;(function($){
;(function($){
//pass in just the context as a $(obj) or a settings JS object
$.fn.autogrow = function(opts) {
var that = $(this).css({overflow: 'hidden', resize: 'none'}) //prevent scrollies
Expand All @@ -18,7 +18,7 @@
var min, clone;
elem = $(elem);
//if the element is "invisible", we get an incorrect height value
//to get correct value, clone and append to the body.
//to get correct value, clone and append to the body.
if (elem.is(':visible') || parseInt(elem.css('height'), 10) > 0) {
min = parseInt(elem.css('height'), 10) || elem.innerHeight();
} else {
Expand All @@ -36,19 +36,19 @@
clone.remove();
}
if (opts.fixMinHeight) {
elem.data('autogrow-start-height', min); //set min height
elem.data('autogrow-start-height', min); //set min height
}
elem.css('height', min);

if (opts.onInitialize && elem.length) {
resize.call(elem[0]);
resize.call(elem[0], true);
}
});
opts.context
.on('keyup paste', selector, resize)
;
function resize (e){

function resize (e, dontFocus){
var box = $(this)
, oldHeight = box.innerHeight()
, newHeight = this.scrollHeight
Expand All @@ -69,27 +69,27 @@
//add clone class for extra css rules
.addClass(opts.cloneClass)
//make "invisible", remove height restriction potentially imposed by existing CSS
.css({position: 'absolute', zIndex:-10, height: ''})
.css({position: 'absolute', zIndex:-10, height: ''})
//populate with content for consistent measuring
.val(box.val())
.val(box.val())
;
box.after(clone); //append as close to the box as possible for best CSS matching for clone
do { //reduce height until they don't match
newHeight = clone[0].scrollHeight - 1;
clone.innerHeight(newHeight);
} while (newHeight === clone[0].scrollHeight);
newHeight++; //adding one back eliminates a wiggle on deletion
newHeight++; //adding one back eliminates a wiggle on deletion
clone.remove();
box.focus(); // Fix issue with Chrome losing focus from the textarea.
if (!!dontFocus) box.focus(); // Fix issue with Chrome losing focus from the textarea.

//if user selects all and deletes or holds down delete til beginning
//user could get here and shrink whole box
newHeight < minHeight && (newHeight = minHeight);
oldHeight > newHeight && opts.animate ? box.stop().animate({height: newHeight}, opts.speed) : box.innerHeight(newHeight);
} else { //just set to the minHeight
box.innerHeight(minHeight);
}
}
}
}
return that;
}
Expand Down