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

fix scroll issues for textarea on divs with overflow #298

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions dist/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
})(this, function (exports, module) {
'use strict';

var set = typeof Set === 'function' ? new Set() : (function () {
var set = typeof Set === "function" ? new Set() : (function () {
var list = [];

return {
Expand All @@ -30,7 +30,8 @@
},
'delete': function _delete(key) {
list.splice(list.indexOf(key), 1);
} };
}
};
})();

var createEvent = function createEvent(name) {
Expand All @@ -48,7 +49,7 @@
}

function assign(ta) {
var _ref = arguments[1] === undefined ? {} : arguments[1];
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var _ref$setOverflowX = _ref.setOverflowX;
var setOverflowX = _ref$setOverflowX === undefined ? true : _ref$setOverflowX;
Expand All @@ -60,6 +61,7 @@
var heightOffset = null;
var overflowY = null;
var clientWidth = ta.clientWidth;
var overflowParent = null;

function init() {
var style = window.getComputedStyle(ta, null);
Expand All @@ -82,9 +84,23 @@
heightOffset = 0;
}

overflowParent = findOverflowParent(ta);

update();
}

function findOverflowParent(element) {
while (element.parentNode && element.parentNode !== document) {
var _parent = element.parentNode;
var parentStyle = window.getComputedStyle(_parent, null);
var overflowValue = parentStyle.getPropertyValue('overflow-y');
if (overflowValue === 'auto') {
return _parent;
}
element = _parent;
}
}

function changeOverflow(value) {
{
// Chrome/Safari-specific fix:
Expand Down Expand Up @@ -112,6 +128,7 @@
var htmlTop = window.pageYOffset;
var bodyTop = document.body.scrollTop;
var originalHeight = ta.style.height;
var parentTop = overflowParent ? overflowParent.scrollTop : undefined;

ta.style.height = 'auto';

Expand All @@ -131,6 +148,9 @@
// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
if (overflowParent) {
overflowParent.scrollTop = parentTop;
}
}

function update() {
Expand Down Expand Up @@ -178,7 +198,8 @@
resize: ta.style.resize,
overflowY: ta.style.overflowY,
overflowX: ta.style.overflowX,
wordWrap: ta.style.wordWrap });
wordWrap: ta.style.wordWrap
});

ta.addEventListener('autosize:destroy', destroy, false);

Expand Down
2 changes: 1 addition & 1 deletion dist/autosize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
let heightOffset = null;
let overflowY = null;
let clientWidth = ta.clientWidth;
let overflowParent = null;

function init() {
const style = window.getComputedStyle(ta, null);
Expand All @@ -54,9 +55,23 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
heightOffset = 0;
}

overflowParent = findOverflowParent(ta);

update();
}

function findOverflowParent(element) {
while(element.parentNode && element.parentNode !== document) {
const parent = element.parentNode;
const parentStyle = window.getComputedStyle(parent, null);
const overflowValue = parentStyle.getPropertyValue('overflow-y');
if (overflowValue === 'auto') {
return parent;
}
element = parent;
}
}

function changeOverflow(value) {
{
// Chrome/Safari-specific fix:
Expand Down Expand Up @@ -84,6 +99,7 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
const htmlTop = window.pageYOffset;
const bodyTop = document.body.scrollTop;
const originalHeight = ta.style.height;
const parentTop = overflowParent ? overflowParent.scrollTop : undefined;

ta.style.height = 'auto';

Expand All @@ -103,6 +119,9 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
if (overflowParent) {
overflowParent.scrollTop = parentTop;
}
}

function update() {
Expand Down