Skip to content

Commit 3f367d1

Browse files
authored
fix(material-experimental/mdc-form-field): missing styles for native select controls (#19140)
With the initial implementation of the MDC-based form-field we added support for `matInput`, but it looks like the styles for the native select were missing. This currently means that the select might show undesired focus indiciation or that the select is slightly inset due to the native appearance.
1 parent d129692 commit 3f367d1

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

src/material-experimental/mdc-form-field/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ sass_library(
8282
name = "form_field_partials",
8383
srcs = [
8484
"_form-field-focus-overlay.scss",
85+
"_form-field-native-select.scss",
8586
"_form-field-sizing.scss",
8687
"_form-field-subscript.scss",
8788
"_mdc-text-field-structure-overrides.scss",
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
@import '../../cdk/a11y/a11y';
2+
3+
// Width of the Material Design form-field select arrow.
4+
$mat-form-field-select-arrow-width: 10px;
5+
// Height of the Material Design form-field select arrow.
6+
$mat-form-field-select-arrow-height: 5px;
7+
// Horizontal padding that needs to be applied to the native select in an form-field so
8+
// that the absolute positioned arrow does not overlap the select content.
9+
$mat-form-field-select-horizontal-end-padding: $mat-form-field-select-arrow-width + 5px;
10+
11+
// Mixin that creates styles for native select controls in a form-field.
12+
@mixin _mat-form-field-native-select() {
13+
// Remove the native select down arrow and ensure that the native appearance
14+
// does not conflict with the form-field. e.g. Focus indication of the native
15+
// select is undesired since we handle focus as part of the form-field.
16+
select.mat-mdc-input-element {
17+
-moz-appearance: none;
18+
-webkit-appearance: none;
19+
background-color: transparent;
20+
display: inline-flex;
21+
box-sizing: border-box;
22+
23+
// Hides the default arrow native selects.
24+
&::-ms-expand {
25+
display: none;
26+
}
27+
28+
// By default the cursor does not change when hovering over a select. We want to
29+
// change this for non-disabled select elements so that it's more obvious that the
30+
// control can be clicked.
31+
&:not(:disabled) {
32+
cursor: pointer;
33+
}
34+
35+
// As a part of its user agent styling, IE11 has a blue box inside each focused `select`
36+
// element which we have to reset. Note that this needs to be in its own selector, because
37+
// having it together with another one will cause other browsers to ignore it.
38+
&::-ms-value {
39+
// We need to reset the `color` as well, because IE sets it to white.
40+
color: inherit;
41+
background: none;
42+
43+
// IE and Edge in high contrast mode reset the color for a focused select to the same color
44+
// as the background, however this causes it blend in because we've reset the `background`
45+
// above. We have to add a more specific selector in order to ensure that it gets the
46+
// `color` from our theme instead.
47+
@include cdk-high-contrast(active, off) {
48+
.mat-focused & {
49+
color: inherit;
50+
}
51+
}
52+
}
53+
}
54+
55+
// Native select elements with the `matInput` directive should have Material Design
56+
// styling. This means that we add an arrow to the form-field that is based on the
57+
// Material Design specification. We achieve this by adding a pseudo element to the
58+
// form-field infix.
59+
.mat-mdc-form-field-type-mat-native-select {
60+
.mat-mdc-form-field-infix::after {
61+
content: '';
62+
width: 0;
63+
height: 0;
64+
border-left: ($mat-form-field-select-arrow-width / 2) solid transparent;
65+
border-right: ($mat-form-field-select-arrow-width / 2) solid transparent;
66+
border-top: $mat-form-field-select-arrow-height solid;
67+
position: absolute;
68+
top: 50%;
69+
right: 0;
70+
71+
// Make the arrow non-clickable so the user can click on the form control under it.
72+
pointer-events: none;
73+
74+
[dir='rtl'] & {
75+
right: auto;
76+
left: 0;
77+
}
78+
}
79+
80+
// Add padding on the end of the native select so that the content does not
81+
// overlap with the Material Design arrow.
82+
.mat-mdc-input-element {
83+
padding-right: $mat-form-field-select-horizontal-end-padding;
84+
[dir='rtl'] & {
85+
padding-right: 0;
86+
padding-left: $mat-form-field-select-horizontal-end-padding;
87+
}
88+
}
89+
}
90+
}

src/material-experimental/mdc-form-field/form-field.scss

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
@import '@material/textfield/mixins.import';
22

3-
@import 'form-field-sizing';
4-
@import 'form-field-subscript';
5-
@import 'form-field-focus-overlay';
6-
@import 'mdc-text-field-textarea-overrides';
7-
@import 'mdc-text-field-structure-overrides';
3+
@import './form-field-sizing';
4+
@import './form-field-subscript';
5+
@import './form-field-focus-overlay';
6+
@import './form-field-native-select';
7+
@import './mdc-text-field-textarea-overrides';
8+
@import './mdc-text-field-structure-overrides';
89

910
// Base styles for MDC text-field, notched-outline, floating label and line-ripple.
1011
@include mdc-text-field-without-ripple($query: $mat-base-styles-without-animation-query);
@@ -16,9 +17,10 @@
1617
@include _mat-mdc-text-field-textarea-overrides();
1718
@include _mat-mdc-text-field-structure-overrides();
1819

19-
// Include the subscript and focus-overlay styles.
20+
// Include the subscript, focus-overlay and native select styles.
2021
@include _mat-form-field-subscript();
2122
@include _mat-form-field-focus-overlay();
23+
@include _mat-form-field-native-select();
2224

2325
// Host element of the form-field. It contains the mdc-text-field wrapper
2426
// and the subscript wrapper.

0 commit comments

Comments
 (0)