Skip to content

Commit 982b406

Browse files
committed
docs(cdk): clarify textarea autosize examples
Keep the primary autosize example minimal and move the programmatic resize scenario into a separate example so the extra logic is clearly associated with style changes.
1 parent bcb3e79 commit 982b406

7 files changed

Lines changed: 65 additions & 41 deletions

File tree

src/cdk/text-field/text-field.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ The `text-field` package provides useful utilities for working with text input f
55

66
The `cdkTextareaAutosize` directive can be applied to any `<textarea>` to make it automatically
77
resize to fit its content. The minimum and maximum number of rows to expand to can be set via the
8-
`cdkAutosizeMinRows` and `cdkAutosizeMaxRows` properties respectively.
8+
`cdkAutosizeMinRows` and `cdkAutosizeMaxRows` properties respectively. No programmatic resize logic
9+
is required when the textarea only needs to react to content changes.
910

10-
The resize logic can be triggered programmatically by calling `resizeToFitContent`. This method
11+
<!-- example(text-field-autosize-textarea) -->
12+
13+
The resize logic can also be triggered programmatically by calling `resizeToFitContent`. This method
1114
takes an optional boolean parameter `force` that defaults to `false`. Passing true will force the
12-
`<textarea>` to resize even if its text content has not changed, this can be useful if the styles
13-
affecting the `<textarea>` have changed.
15+
`<textarea>` to resize even if its text content has not changed, which is useful if styles affecting
16+
the `<textarea>` have changed. In the following example, changing the font size through the
17+
`mat-select` triggers a forced resize.
1418

15-
<!-- example(text-field-autosize-textarea) -->
19+
<!-- example(text-field-autosize-textarea-programmatic) -->
1620

1721
### Monitoring the autofill state of an `<input>`
1822

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export {TextFieldAutofillDirectiveExample} from './text-field-autofill-directive/text-field-autofill-directive-example';
22
export {TextFieldAutofillMonitorExample} from './text-field-autofill-monitor/text-field-autofill-monitor-example';
3+
export {TextFieldAutosizeTextareaProgrammaticExample} from './text-field-autosize-textarea-programmatic/text-field-autosize-textarea-programmatic-example';
34
export {TextFieldAutosizeTextareaExample} from './text-field-autosize-textarea/text-field-autosize-textarea-example';

src/components-examples/cdk/text-field/text-field-autosize-textarea/text-field-autosize-textarea-example.css renamed to src/components-examples/cdk/text-field/text-field-autosize-textarea-programmatic/text-field-autosize-textarea-programmatic-example.css

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<mat-form-field>
2+
<mat-label>Font size</mat-label>
3+
<mat-select #fontSize value="16px" (selectionChange)="triggerResize()">
4+
<mat-option value="10px">10px</mat-option>
5+
<mat-option value="12px">12px</mat-option>
6+
<mat-option value="14px">14px</mat-option>
7+
<mat-option value="16px">16px</mat-option>
8+
<mat-option value="18px">18px</mat-option>
9+
<mat-option value="20px">20px</mat-option>
10+
</mat-select>
11+
</mat-form-field>
12+
13+
<mat-form-field [style.fontSize]="fontSize.value">
14+
<mat-label>Autosize textarea</mat-label>
15+
<textarea matInput
16+
cdkTextareaAutosize
17+
#autosize="cdkTextareaAutosize"
18+
cdkAutosizeMinRows="1"
19+
cdkAutosizeMaxRows="5"></textarea>
20+
</mat-form-field>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {CdkTextareaAutosize, TextFieldModule} from '@angular/cdk/text-field';
2+
import {afterNextRender, Component, inject, Injector, ViewChild} from '@angular/core';
3+
import {MatFormFieldModule} from '@angular/material/form-field';
4+
import {MatInputModule} from '@angular/material/input';
5+
import {MatSelectModule} from '@angular/material/select';
6+
7+
/** @title Programmatically resizing an autosize textarea */
8+
@Component({
9+
selector: 'text-field-autosize-textarea-programmatic-example',
10+
templateUrl: './text-field-autosize-textarea-programmatic-example.html',
11+
styleUrl: './text-field-autosize-textarea-programmatic-example.css',
12+
imports: [MatFormFieldModule, MatSelectModule, MatInputModule, TextFieldModule],
13+
})
14+
export class TextFieldAutosizeTextareaProgrammaticExample {
15+
private _injector = inject(Injector);
16+
17+
@ViewChild('autosize') autosize!: CdkTextareaAutosize;
18+
19+
triggerResize() {
20+
// Wait for content to render, then trigger textarea resize.
21+
afterNextRender(
22+
() => {
23+
this.autosize.resizeToFitContent(true);
24+
},
25+
{
26+
injector: this._injector,
27+
},
28+
);
29+
}
30+
}
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
<mat-form-field>
2-
<mat-label>Font size</mat-label>
3-
<mat-select #fontSize value="16px" (selectionChange)="triggerResize()">
4-
<mat-option value="10px">10px</mat-option>
5-
<mat-option value="12px">12px</mat-option>
6-
<mat-option value="14px">14px</mat-option>
7-
<mat-option value="16px">16px</mat-option>
8-
<mat-option value="18px">18px</mat-option>
9-
<mat-option value="20px">20px</mat-option>
10-
</mat-select>
11-
</mat-form-field>
12-
13-
<mat-form-field [style.fontSize]="fontSize.value">
142
<mat-label>Autosize textarea</mat-label>
153
<textarea matInput
164
cdkTextareaAutosize
17-
#autosize="cdkTextareaAutosize"
185
cdkAutosizeMinRows="1"
196
cdkAutosizeMaxRows="5"></textarea>
207
</mat-form-field>
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
1-
import {CdkTextareaAutosize, TextFieldModule} from '@angular/cdk/text-field';
2-
import {afterNextRender, Component, inject, Injector, ViewChild} from '@angular/core';
3-
import {MatInputModule} from '@angular/material/input';
4-
import {MatSelectModule} from '@angular/material/select';
1+
import {TextFieldModule} from '@angular/cdk/text-field';
2+
import {Component} from '@angular/core';
53
import {MatFormFieldModule} from '@angular/material/form-field';
4+
import {MatInputModule} from '@angular/material/input';
65

76
/** @title Auto-resizing textarea */
87
@Component({
98
selector: 'text-field-autosize-textarea-example',
109
templateUrl: './text-field-autosize-textarea-example.html',
11-
styleUrl: './text-field-autosize-textarea-example.css',
12-
imports: [MatFormFieldModule, MatSelectModule, MatInputModule, TextFieldModule],
10+
imports: [MatFormFieldModule, MatInputModule, TextFieldModule],
1311
})
14-
export class TextFieldAutosizeTextareaExample {
15-
private _injector = inject(Injector);
16-
17-
@ViewChild('autosize') autosize!: CdkTextareaAutosize;
18-
19-
triggerResize() {
20-
// Wait for content to render, then trigger textarea resize.
21-
afterNextRender(
22-
() => {
23-
this.autosize.resizeToFitContent(true);
24-
},
25-
{
26-
injector: this._injector,
27-
},
28-
);
29-
}
30-
}
12+
export class TextFieldAutosizeTextareaExample {}

0 commit comments

Comments
 (0)