Skip to content

Commit

Permalink
check the width of the textarea element on window.resize, rather than…
Browse files Browse the repository at this point in the history
… document.
  • Loading branch information
jackmoore committed Sep 26, 2015
1 parent fb23c56 commit 7b64e33
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {

let heightOffset = null;
let overflowY = 'hidden';
let clientWidth = ta.clientWidth;

function init() {
const style = window.getComputedStyle(ta, null);
Expand Down Expand Up @@ -82,14 +83,17 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {

ta.style.height = endHeight+'px';

// used to check if an update is actually necessary on window.resize
clientWidth = ta.clientWidth;

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
}

function update() {
const startHeight = ta.style.height;

resize();

const style = window.getComputedStyle(ta, null);
Expand All @@ -110,14 +114,12 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
ta.dispatchEvent(evt);
}
}

var lastPageWidth = document.documentElement.clientWidth;
function pageResize() {
if (document.documentElement.clientWidth != lastPageWidth) {
lastPageWidth = document.documentElement.clientWidth;

const pageResize = () => {
if (ta.clientWidth !== clientWidth) {
update();
}
}
};

const destroy = style => {
window.removeEventListener('resize', pageResize);
Expand Down

0 comments on commit 7b64e33

Please sign in to comment.