Skip to content

Commit a0ad9e5

Browse files
authored
docs(material/form-field): update documentation about the form-field appearances (#22817)
1 parent 298fed2 commit a0ad9e5

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/components-examples/material/form-field/form-field-harness/form-field-harness-example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<mat-form-field id="with-errors">
1+
<mat-form-field id="with-errors" appearance="fill">
22
<span class="custom-control">Custom control harness</span>
33
<input matInput [formControl]="requiredControl">
44

src/material/form-field/form-field.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,49 @@ The following Angular Material components are designed to work inside a `<mat-fo
1616

1717
### Form field appearance variants
1818
The `mat-form-field` supports 4 different appearance variants which can be set via the `appearance`
19-
input. The `legacy` appearance is the default style that the `mat-form-field` has traditionally had.
20-
It shows the input box with an underline underneath it. The `standard` appearance is a slightly
21-
updated version of the `legacy` appearance that has spacing that is more consistent with the `fill`
22-
and `outline` appearances. The `fill` appearance displays the form field with a filled background
23-
box in addition to the underline. Finally the `outline` appearance shows the form field with a
24-
border all the way around, not just an underline.
25-
26-
There are a couple differences to be aware of between the `legacy` appearance and the newer
27-
`standard`, `fill`, and `outline` appearances. The `matPrefix` and `matSuffix` elements are center
28-
aligned by default for the newer appearances. The Material Design spec shows this as being the
29-
standard way to align prefix and suffix icons in the newer appearance variants. We do not recommend
30-
using text prefix and suffixes in the `fill` and `outline` variants because the label and input do not have the same
31-
alignment. It is therefore impossible to align the prefix or suffix in a way that looks good when
32-
compared with both the label and input text.
19+
input. The `fill` an `outline` appearances implement the current version of the spec. The `fill`
20+
appearance displays the form field with a filled background box and an underline, while the
21+
`outline` appearance shows the form field with a border all the way around.
22+
23+
The `legacy` and `standard` appearances implement an older version of the spec where
24+
`mat-form-field` was shown with an underline beneath it. We recommend not using these appearances as
25+
they do not match the current spec.
26+
27+
There are a couple differences in behavior to be aware of between the different appearances.
28+
29+
We recommend that text prefix and suffixes in the `fill` and `outline` appearances only be used in
30+
conjunction with the `floatLabel="always"` option. This is because the resting label and the input
31+
value do not have the same alignment, and it is therefore impossible to align the prefix or suffix
32+
in a way that looks good when compared with both. In the `standard` and `legacy` appearances, the
33+
resting label and input value align, so this isn't an issue. We plan to improve support for text
34+
prefix and suffixes in the future so they can be used without `floatLabel="always"`.
3335

3436
The second important difference is that the `standard`, `fill`, and `outline` appearances do not
3537
promote placeholders to labels. For the `legacy` appearance specifying
3638
`<input placeholder="placeholder">` will result in a floating label being added to the
3739
`mat-form-field`. For the newer variants it will just add a normal placeholder to the input. If you
38-
want a floating label, add a `<mat-label>` to the `mat-form-field`.
40+
want a floating label, add a `<mat-label>` to the `mat-form-field` instead.
41+
42+
Out of the box, if you do not specify an `appearance` for the `<mat-form-field>` it will default to
43+
`legacy`. However, this can be configured using a global provider to choose a different default
44+
appearance for your app.
45+
46+
```ts
47+
@NgModule({
48+
providers: [
49+
{provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: {appearance: 'fill'}}
50+
]
51+
})
52+
```
3953

4054
<!-- example(form-field-appearance) -->
4155

4256
### Floating label
4357

4458
The floating label is a text label displayed on top of the form field control when
45-
the control does not contain any text or when `<select matNativeControl>` does not show any option text.
46-
By default, when text is present the floating label
47-
floats above the form field control. The label for a form field can be specified by adding a
48-
`mat-label` element.
59+
the control does not contain any text or when `<select matNativeControl>` does not show any option
60+
text. By default, when text is present the floating label floats above the form field control. The
61+
label for a form field can be specified by adding a `mat-label` element.
4962

5063
In the legacy version of the `<mat-form-field>` (one that has no `appearance` attribute or has
5164
`appearance="legacy"`) if a label is not specified, the `placeholder` attribute on the form control

0 commit comments

Comments
 (0)