Skip to content

Commit ac9c090

Browse files
andrewseguintinayuangao
authored andcommitted
fix(select): set select value to trigger height and center text (#3021)
* fix(select): change styling to always center select value text * move border to its own div; make value ellipsis * add more room for arrow, account for margin * minor fixes
1 parent 5e038c3 commit ac9c090

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

src/lib/select/_select-theme.scss

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@
99

1010
.mat-select-trigger {
1111
color: mat-color($foreground, hint-text);
12-
border-bottom: 1px solid mat-color($foreground, divider);
1312

1413
.mat-select:focus:not(.mat-select-disabled) & {
1514
color: mat-color($primary);
16-
border-bottom: 1px solid mat-color($primary);
1715
}
1816

1917
.mat-select.ng-invalid.ng-touched:not(.mat-select-disabled) & {
2018
color: mat-color($warn);
21-
border-bottom: 1px solid mat-color($warn);
19+
}
20+
}
21+
22+
.mat-select-underline {
23+
background-color: mat-color($foreground, divider);
24+
25+
.mat-select:focus:not(.mat-select-disabled) & {
26+
background-color: mat-color($primary);
27+
}
28+
29+
.mat-select.ng-invalid.ng-touched:not(.mat-select-disabled) & {
30+
background-color: mat-color($warn);
2231
}
2332
}
2433

src/lib/select/select.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<div class="mat-select-trigger" cdk-overlay-origin (click)="toggle()" #origin="cdkOverlayOrigin" #trigger>
22
<span class="mat-select-placeholder" [class.mat-floating-placeholder]="this.selected"
33
[@transformPlaceholder]="_placeholderState" [style.width.px]="_selectedValueWidth"> {{ placeholder }} </span>
4-
<span class="mat-select-value" *ngIf="selected"> {{ selected?.viewValue }} </span>
4+
<span class="mat-select-value" *ngIf="selected">
5+
<span class="mat-select-value-text">{{ selected?.viewValue }}</span>
6+
</span>
57
<span class="mat-select-arrow"></span>
8+
<span class="mat-select-underline"></span>
69
</div>
710

811
<template cdk-connected-overlay [origin]="origin" [open]="panelOpen" hasBackdrop (backdropClick)="close()"

src/lib/select/select.scss

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ $mat-select-trigger-font-size: 16px !default;
1919

2020
.mat-select-trigger {
2121
display: flex;
22-
justify-content: space-between;
2322
align-items: center;
2423
height: $mat-select-trigger-height;
2524
min-width: $mat-select-trigger-min-width;
@@ -29,18 +28,30 @@ $mat-select-trigger-font-size: 16px !default;
2928
font-size: $mat-select-trigger-font-size;
3029

3130
[aria-disabled='true'] & {
32-
@include mat-control-disabled-underline();
33-
border-bottom: transparent;
34-
background-position: 0 bottom;
3531
cursor: default;
3632
user-select: none;
3733
}
3834
}
3935

36+
.mat-select-underline {
37+
position: absolute;
38+
bottom: 0;
39+
left: 0;
40+
right: 0;
41+
height: 1px;
42+
43+
[aria-disabled='true'] & {
44+
@include mat-control-disabled-underline();
45+
background-color: transparent;
46+
background-position: 0 bottom;
47+
}
48+
}
49+
4050
.mat-select-placeholder {
4151
position: relative;
4252
padding: 0 2px;
4353
transform-origin: left top;
54+
flex-grow: 1;
4455

4556
// These values are duplicated from animation code in order to
4657
// allow placeholders to sometimes float without animating,
@@ -69,29 +80,36 @@ $mat-select-trigger-font-size: 16px !default;
6980
}
7081

7182
.mat-select-value {
72-
@include mat-truncate-line();
7383
position: absolute;
74-
max-width: calc(100% - #{$mat-select-arrow-size * 2});
84+
max-width: calc(100% - #{($mat-select-arrow-size + $mat-select-arrow-margin) * 2});
85+
flex-grow: 1;
7586

7687
// Firefox and some versions of IE incorrectly keep absolutely
7788
// positioned children of flex containers in the flex flow when calculating
7889
// position. This has been fixed for Firefox 52, slated for early 2017.
7990
// Bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=874718
8091
//
81-
// In the meantime, we must adjust the left position to 0 to mimic where it
82-
// would be if it were correctly taken out of the flex flow. It's also necessary
83-
// to adjust the top value because absolutely positioned elements should not be
84-
// affected by the flex container's "align-items" property either. To center the text,
85-
// we must offset by 6px (6px top + 6px bottom + 18px text height = 30px total height).
92+
// In the meantime, we must adjust the position to fit the top, left, and bottom edge of the
93+
// containing trigger element. In doing so, we can use align-items: center to allow the text to
94+
// correctly position itself in the middle of the container.
95+
top: 0;
8696
left: 0;
87-
top: 6px;
97+
bottom: 0;
98+
99+
display: flex;
100+
align-items: center;
88101

89102
[dir='rtl'] & {
90103
left: auto;
91104
right: 0;
92105
}
93106
}
94107

108+
.mat-select-value-text {
109+
@include mat-truncate-line();
110+
line-height: $mat-select-trigger-height;
111+
}
112+
95113
.mat-select-arrow {
96114
width: 0;
97115
height: 0;

0 commit comments

Comments
 (0)