Skip to content

Commit 385f96d

Browse files
mmalerbajosephperrott
authored andcommitted
fix(form-field): scrollbars appear on autosize textarea in chrome (#10811)
1 parent 8c30cee commit 385f96d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/cdk/text-field/_text-field.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
textarea.cdk-textarea-autosize {
2020
resize: none;
2121
}
22+
23+
// This class is temporarily applied to the textarea when it is being measured. It is immediately
24+
// removed when measuring is complete. We use `!important` rules here to make sure user-specified
25+
// rules do not interfere with the measurement.
26+
textarea.cdk-textarea-autosize-measuring {
27+
height: auto !important;
28+
overflow: hidden !important;
29+
// Having 2px top and bottom padding seems to fix a bug where Chrome gets an incorrect
30+
// measurement. We just have to account for it later and subtract it off the final result.
31+
padding: 2px 0 !important;
32+
box-sizing: content-box !important;
33+
}
2234
}
2335

2436
// Used to generate UIDs for keyframes used to change the text field autofill styles.

src/cdk/text-field/autosize.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,16 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
187187
// Long placeholders that are wider than the textarea width may lead to a bigger scrollHeight
188188
// value. To ensure that the scrollHeight is not bigger than the content, the placeholders
189189
// need to be removed temporarily.
190-
textarea.style.height = 'auto';
191-
textarea.style.overflow = 'hidden';
190+
textarea.classList.add('cdk-textarea-autosize-measuring');
192191
textarea.placeholder = '';
193192

194-
const height = textarea.scrollHeight;
193+
// The cdk-textarea-autosize-measuring class includes a 2px padding to workaround an issue with
194+
// Chrome, so we account for that extra space here by subtracting 4 (2px top + 2px bottom).
195+
const height = textarea.scrollHeight - 4;
195196

196197
// Use the scrollHeight to know how large the textarea *would* be if fit its entire value.
197198
textarea.style.height = `${height}px`;
198-
textarea.style.overflow = '';
199+
textarea.classList.remove('cdk-textarea-autosize-measuring');
199200
textarea.placeholder = placeholderText;
200201

201202
// On Firefox resizing the textarea will prevent it from scrolling to the caret position.

src/lib/input/input.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ textarea.mat-input-element {
9292
resize: none;
9393
}
9494
}
95+
96+
textarea.mat-input-element {
97+
// The 2px padding prevents scrollbars from appearing on Chrome even when they aren't needed.
98+
// We also add a negative margin to negate the effect of the padding on the layout.
99+
padding: 2px 0;
100+
margin: -2px 0;
101+
}

0 commit comments

Comments
 (0)