Skip to content

Commit c1a3cae

Browse files
committed
fix(cdk/text-field): auto sizing broken if user styles stretch the element (#30412)
Fixes that our logic for measuring the `textarea` was being thrown off by user code that stretches it out. (cherry picked from commit 73022d8)
1 parent 0852c88 commit c1a3cae

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/cdk/text-field/autosize.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,30 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
198198
}
199199

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

204205
// Use `position: absolute` so that this doesn't cause a browser layout and use
205206
// `visibility: hidden` so that nothing is rendered. Clear any other styles that
206207
// would affect the height.
207-
textareaClone.style.position = 'absolute';
208-
textareaClone.style.visibility = 'hidden';
209-
textareaClone.style.border = 'none';
210-
textareaClone.style.padding = '0';
211-
textareaClone.style.height = '';
212-
textareaClone.style.minHeight = '';
213-
textareaClone.style.maxHeight = '';
208+
cloneStyles.position = 'absolute';
209+
cloneStyles.visibility = 'hidden';
210+
cloneStyles.border = 'none';
211+
cloneStyles.padding = '0';
212+
cloneStyles.height = '';
213+
cloneStyles.minHeight = '';
214+
cloneStyles.maxHeight = '';
215+
216+
// App styles might be messing with the height through the positioning properties.
217+
cloneStyles.top = cloneStyles.bottom = cloneStyles.left = cloneStyles.right = 'auto';
214218

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

222226
this._textareaElement.parentNode!.appendChild(textareaClone);
223227
this._cachedLineHeight = textareaClone.clientHeight;

0 commit comments

Comments
 (0)