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

Conversation

Matsuuu
Copy link

@Matsuuu Matsuuu commented Mar 30, 2022

Heya, I'm bad at writing clear explanations on the issue, so I provided a youtube video to show what's up and how it fixes it:
https://www.youtube.com/watch?v=gO07e-Kxe18

Comment on lines 111 to 115
ta.style.height = '';
changeOverflow("scroll");
ta.style.height = (ta.scrollHeight+heightOffset)+'px';
changeOverflow("hidden");

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants