@@ -105,13 +105,6 @@ export const SELECT_MULTIPLE_PANEL_PADDING_X = SELECT_PANEL_PADDING_X * 1.5 + 20
105
105
*/
106
106
export const SELECT_PANEL_VIEWPORT_PADDING = 8 ;
107
107
108
- /**
109
- * Default minimum width of the trigger based on the CSS.
110
- * Used as a fallback for server-side rendering.
111
- * @docs -private
112
- */
113
- const SELECT_TRIGGER_MIN_WIDTH = 112 ;
114
-
115
108
/** Injection token that determines the scroll handling while a select is open. */
116
109
export const MD_SELECT_SCROLL_STRATEGY =
117
110
new InjectionToken < ( ) => ScrollStrategy > ( 'md-select-scroll-strategy' ) ;
@@ -219,8 +212,8 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
219
212
/** Unique id for this input. */
220
213
private _uid = `mat-select-${ nextUniqueId ++ } ` ;
221
214
222
- /** The cached height of the trigger element . */
223
- private _triggerHeight : number ;
215
+ /** The last measured value for the trigger's client bounding rect . */
216
+ _triggerRect : ClientRect ;
224
217
225
218
/** The aria-describedby attribute on the select for improved a11y. */
226
219
_ariaDescribedby : string ;
@@ -231,12 +224,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
231
224
/** Deals with the selection logic. */
232
225
_selectionModel : SelectionModel < MdOption > ;
233
226
234
- /**
235
- * The width of the trigger. Must be saved to set the min width of the overlay panel
236
- * and the width of the selected value.
237
- */
238
- _triggerWidth : number ;
239
-
240
227
/** Manages keyboard events for options in the panel. */
241
228
_keyManager : FocusKeyManager < MdOption > ;
242
229
@@ -301,9 +288,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
301
288
/** Trigger that opens the select. */
302
289
@ViewChild ( 'trigger' ) trigger : ElementRef ;
303
290
304
- /** Element used to measure the font-size of the trigger element. */
305
- @ViewChild ( 'measureFontSize' ) _measureFontSizeEl : ElementRef ;
306
-
307
291
/** Overlay pane containing the options. */
308
292
@ViewChild ( ConnectedOverlayDirective ) overlayDir : ConnectedOverlayDirective ;
309
293
@@ -325,9 +309,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
325
309
set placeholder ( value : string ) {
326
310
this . _placeholder = value ;
327
311
this . stateChanges . next ( ) ;
328
-
329
- // Must wait to record the trigger width to ensure placeholder width is included.
330
- Promise . resolve ( null ) . then ( ( ) => this . _setTriggerWidth ( ) ) ;
331
312
}
332
313
333
314
/** Whether the component is required. */
@@ -424,7 +405,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
424
405
constructor (
425
406
private _viewportRuler : ViewportRuler ,
426
407
private _changeDetectorRef : ChangeDetectorRef ,
427
- private _platform : Platform ,
428
408
private _ngZone : NgZone ,
429
409
renderer : Renderer2 ,
430
410
elementRef : ElementRef ,
@@ -478,9 +458,10 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
478
458
return ;
479
459
}
480
460
481
- if ( ! this . _triggerWidth ) {
482
- this . _setTriggerWidth ( ) ;
483
- }
461
+ this . _triggerRect = this . trigger . nativeElement . getBoundingClientRect ( ) ;
462
+ // Note: The computed font-size will be a string pixel value (e.g. "16px").
463
+ // `parseInt` ignores the trailing 'px' and converts this to a number.
464
+ this . _triggerFontSize = parseInt ( getComputedStyle ( this . trigger . nativeElement ) [ 'font-size' ] ) ;
484
465
485
466
this . _calculateOverlayPosition ( ) ;
486
467
this . _panelOpen = true ;
@@ -585,17 +566,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
585
566
return this . _dir ? this . _dir . value === 'rtl' : false ;
586
567
}
587
568
588
- /**
589
- * Sets the width of the trigger element. This is necessary to match
590
- * the overlay width to the trigger width.
591
- */
592
- private _setTriggerWidth ( ) : void {
593
- this . _triggerWidth = this . _platform . isBrowser ? this . _getTriggerRect ( ) . width :
594
- SELECT_TRIGGER_MIN_WIDTH ;
595
-
596
- this . _changeDetectorRef . markForCheck ( ) ;
597
- }
598
-
599
569
/** Handles the keyboard interactions of a closed select. */
600
570
_handleClosedKeydown ( event : KeyboardEvent ) : void {
601
571
if ( ! this . disabled ) {
@@ -777,10 +747,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
777
747
this . stateChanges . next ( ) ;
778
748
}
779
749
780
- private _getTriggerRect ( ) : ClientRect {
781
- return this . trigger . nativeElement . getBoundingClientRect ( ) ;
782
- }
783
-
784
750
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
785
751
private _initKeyManager ( ) {
786
752
this . _keyManager = new FocusKeyManager < MdOption > ( this . options ) . withTypeAhead ( ) ;
@@ -923,9 +889,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
923
889
924
890
/** Calculates the scroll position and x- and y-offsets of the overlay panel. */
925
891
private _calculateOverlayPosition ( ) : void {
926
- this . _triggerHeight = this . trigger . nativeElement . getBoundingClientRect ( ) . height ;
927
- this . _triggerFontSize = this . _measureFontSizeEl . nativeElement . getBoundingClientRect ( ) . height ;
928
-
929
892
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
930
893
931
894
const items = this . _getItemCount ( ) ;
@@ -1033,7 +996,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1033
996
private _calculateOverlayOffsetY ( selectedIndex : number , scrollBuffer : number ,
1034
997
maxScroll : number ) : number {
1035
998
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1036
- const optionHeightAdjustment = ( itemHeight - this . _triggerHeight ) / 2 ;
999
+ const optionHeightAdjustment = ( itemHeight - this . _triggerRect . height ) / 2 ;
1037
1000
const maxOptionsDisplayed = Math . floor ( SELECT_PANEL_MAX_HEIGHT / itemHeight ) ;
1038
1001
let optionOffsetFromPanelTop : number ;
1039
1002
@@ -1075,16 +1038,15 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1075
1038
private _checkOverlayWithinViewport ( maxScroll : number ) : void {
1076
1039
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1077
1040
const viewportRect = this . _viewportRuler . getViewportRect ( ) ;
1078
- const triggerRect = this . _getTriggerRect ( ) ;
1079
1041
1080
- const topSpaceAvailable = triggerRect . top - SELECT_PANEL_VIEWPORT_PADDING ;
1042
+ const topSpaceAvailable = this . _triggerRect . top - SELECT_PANEL_VIEWPORT_PADDING ;
1081
1043
const bottomSpaceAvailable =
1082
- viewportRect . height - triggerRect . bottom - SELECT_PANEL_VIEWPORT_PADDING ;
1044
+ viewportRect . height - this . _triggerRect . bottom - SELECT_PANEL_VIEWPORT_PADDING ;
1083
1045
1084
1046
const panelHeightTop = Math . abs ( this . _offsetY ) ;
1085
1047
const totalPanelHeight =
1086
1048
Math . min ( this . _getItemCount ( ) * itemHeight , SELECT_PANEL_MAX_HEIGHT ) ;
1087
- const panelHeightBottom = totalPanelHeight - panelHeightTop - triggerRect . height ;
1049
+ const panelHeightBottom = totalPanelHeight - panelHeightTop - this . _triggerRect . height ;
1088
1050
1089
1051
if ( panelHeightBottom > bottomSpaceAvailable ) {
1090
1052
this . _adjustPanelUp ( panelHeightBottom , bottomSpaceAvailable ) ;
@@ -1142,7 +1104,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1142
1104
/** Sets the transform origin point based on the selected option. */
1143
1105
private _getOriginBasedOnOption ( ) : string {
1144
1106
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1145
- const optionHeightAdjustment = ( itemHeight - this . _triggerHeight ) / 2 ;
1107
+ const optionHeightAdjustment = ( itemHeight - this . _triggerRect . height ) / 2 ;
1146
1108
const originY = Math . abs ( this . _offsetY ) - optionHeightAdjustment + itemHeight / 2 ;
1147
1109
return `50% ${ originY } px 0px` ;
1148
1110
}
0 commit comments