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

feat(textarea): add autosize directive #1632

Closed
wants to merge 1 commit into from
Closed

Conversation

fxck
Copy link
Contributor

@fxck fxck commented Oct 27, 2016

Here's the separated PR for autosize, so you can think about the logic and what not.

@jelbourn

A row's height is calculated from line-height of the element, but unless the line-height is explicitly set, its initial value is normal, so to be able to dynamically set rows depending on the height of the textarea, I need to find the actual line-height.

So I'm creating a temp textarea element, reseting it's paddings and margins, visually hiding it, setting the rows attribute to 1(default is 2, but I'm not sure how consistent is it across browsers, so I'm setting it explicitly) and getting it's height, removing the element afterwards. When line-height is actually explicitly set(doesn't matter if it's by ems, pixels, percents, getComputedStyles always returns pixel values), I'm simply returning it as a number and doing none of above.

Now that I have actual line-height, I'm setting the min-height of the element to be

user provided rows value * line-height I found

The rest is similar to how it was before when input event happens, height is set to auto and rows to the initial value, which allows me to get the real scrollHeight even if user is removing instead of adding characters, afterwards new rows is calculated by diving the new scrollHeight by lineHeight.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 27, 2016
@jelbourn
Copy link
Member

So, I spent many hours today exploring the autosize thing. I looked at Polymer's approach, which uses a "mirror" element with the same content as the textarea. I tried playing with the idea a bit using a mirror element with white-space: pre-wrap and was happy with the results in Chrome, Safari, and Firefox. However, IE and Edge have a problem: it seems that the line wrapping behavior is different in a textarea vs. a normal element. This mainly manifests when you have a space right when the wrap would naturally occur. I spent way too long trying to get this to work. The Polymer implementation suffers from this problem, and I'd like to avoid it.

So, going back to the scrollHeight approach, I agree after messing with it that we should not work with both rows and autosize at the same time. However, we should probably support minRows and maxRows. I put together a prototype of how I think this should look. All of this can be done in a standalone md-autosize directive that is not coupled to MdInput.

@jelbourn
Copy link
Member

jelbourn commented Nov 4, 2016

@fxck let me know if you want to go ahead with the PR, otherwise I can send a PR based on my approach from above.

@fxck
Copy link
Contributor Author

fxck commented Nov 7, 2016

I'll be busy for a week or two now, so depends on how fast you want it in. As long as we can agree on this..

we should not work with both rows and autosize at the same time

..it should be easy though.

@jelbourn
Copy link
Member

jelbourn commented Nov 8, 2016

@fxck If you're busy I can try to tackle this in the next week or so.

@jelbourn
Copy link
Member

Closed in favor of #1846

@jelbourn jelbourn closed this Nov 14, 2016
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants