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(cdk/text-field): auto sizing broken if user styles stretch the element #30412

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/cdk/text-field/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,30 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
}

// Use a clone element because we have to override some styles.
let textareaClone = this._textareaElement.cloneNode(false) as HTMLTextAreaElement;
const textareaClone = this._textareaElement.cloneNode(false) as HTMLTextAreaElement;
const cloneStyles = textareaClone.style;
textareaClone.rows = 1;

// Use `position: absolute` so that this doesn't cause a browser layout and use
// `visibility: hidden` so that nothing is rendered. Clear any other styles that
// would affect the height.
textareaClone.style.position = 'absolute';
textareaClone.style.visibility = 'hidden';
textareaClone.style.border = 'none';
textareaClone.style.padding = '0';
textareaClone.style.height = '';
textareaClone.style.minHeight = '';
textareaClone.style.maxHeight = '';
cloneStyles.position = 'absolute';
cloneStyles.visibility = 'hidden';
cloneStyles.border = 'none';
cloneStyles.padding = '0';
cloneStyles.height = '';
cloneStyles.minHeight = '';
cloneStyles.maxHeight = '';

// App styles might be messing with the height through the positioning properties.
cloneStyles.top = cloneStyles.bottom = cloneStyles.left = cloneStyles.right = 'auto';

// In Firefox it happens that textarea elements are always bigger than the specified amount
// of rows. This is because Firefox tries to add extra space for the horizontal scrollbar.
// As a workaround that removes the extra space for the scrollbar, we can just set overflow
// to hidden. This ensures that there is no invalid calculation of the line height.
// See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
textareaClone.style.overflow = 'hidden';
cloneStyles.overflow = 'hidden';

this._textareaElement.parentNode!.appendChild(textareaClone);
this._cachedLineHeight = textareaClone.clientHeight;
Expand Down
Loading