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 incorrect height calculation on stylized textareas when pasting #398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion dist/autosize.esm.js

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

8 changes: 5 additions & 3 deletions dist/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.autosize = factory());
}(this, (function () {
})(this, (function () {
var map = typeof Map === "function" ? new Map() : function () {
var keys = [];
var values = [];
Expand Down Expand Up @@ -117,7 +117,9 @@
var docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)

ta.style.height = '';
ta.style.height = ta.scrollHeight + heightOffset + 'px'; // used to check if an update is actually necessary on window.resize
changeOverflow("scroll");
ta.style.height = ta.scrollHeight + heightOffset + 'px';
changeOverflow("hidden"); // used to check if an update is actually necessary on window.resize

clientWidth = ta.clientWidth; // prevents scroll-position jumping

Expand Down Expand Up @@ -271,4 +273,4 @@

return autosize$1;

})));
}));
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.

7 changes: 6 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
<meta charset='utf-8'/>
<title>Simple Autosize for textareas</title>
<style>
body {
width: 50%;
margin: auto;
max-height: 80vh;
}
textarea {
padding: 10px;
vertical-align: top;
width: 200px;
width: 100%;
}
textarea:focus {
outline-style: solid;
Expand Down
2 changes: 2 additions & 0 deletions src/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function assign(ta) {
const docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)

ta.style.height = '';
changeOverflow("scroll");
ta.style.height = (ta.scrollHeight+heightOffset)+'px';
changeOverflow("hidden");

Comment on lines 111 to 115
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you can capture the scrollHeight before resetting the height of the textarea.

The issue is that we reset the height so it may remove any scrollbar we have from the parent, which will cause the textarea to be a little bit wider (around 17px wider I think, value to be changed based on the browser).

When we set the height, we calculate the scrollHeight a second time but we are basing it on the fact that there is no scrollbar so the textarea is a little bit wider.

By capturing the scrollHeight before resetting the height, we get the proper value as the parent scrollbar is included.

Suggested change
ta.style.height = '';
changeOverflow("scroll");
ta.style.height = (ta.scrollHeight+heightOffset)+'px';
changeOverflow("hidden");
// we capture the scroll height based on the parent (with the parent scrollbar if required)
const scrollHeight = ta.scrollHeight;
// we reset the textarea's height and potentially hide the parent scrollbar
ta.style.height = '';
// we set the new height
// the initial code was fetching the scrollHeight value again but was incorrect because of the previous reset
ta.style.height = (scrollHeight+heightOffset)+'px';

The reason why I think this is a more robust solution is that we take the parent's scrollbar into account and not the textarea's scrollbar.

Both solutions will work in most cases but we can now style scrollbars. However, your solution may fail if the parent scrollbar is different than the textarea scrollbar, the computation would be incorrect again.

cf: https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-width and https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar

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