1
1
import {
2
- Component ,
3
- ViewEncapsulation ,
4
- Input ,
5
- HostBinding ,
6
2
ChangeDetectionStrategy ,
3
+ Component ,
4
+ Directive ,
7
5
ElementRef ,
6
+ HostBinding ,
7
+ Input ,
8
+ OnDestroy ,
8
9
Renderer ,
9
- Directive ,
10
+ ViewEncapsulation
10
11
} from '@angular/core' ;
11
- import { coerceBooleanProperty } from '../core' ;
12
+ import { coerceBooleanProperty , FocusOriginMonitor } from '../core' ;
12
13
13
14
14
- // TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
15
15
// TODO(kara): Convert attribute selectors to classes when attr maps become available
16
16
17
17
@@ -90,25 +90,15 @@ export class MdMiniFabCssMatStyler {}
90
90
'button[mat-fab], button[mat-mini-fab]' ,
91
91
host : {
92
92
'[disabled]' : 'disabled' ,
93
- '[class.mat-button-focus]' : '_isKeyboardFocused' ,
94
- '(mousedown)' : '_setMousedown()' ,
95
- '(focus)' : '_setKeyboardFocus()' ,
96
- '(blur)' : '_removeKeyboardFocus()' ,
97
93
} ,
98
94
templateUrl : 'button.html' ,
99
95
styleUrls : [ 'button.css' ] ,
100
96
encapsulation : ViewEncapsulation . None ,
101
97
changeDetection : ChangeDetectionStrategy . OnPush ,
102
98
} )
103
- export class MdButton {
99
+ export class MdButton implements OnDestroy {
104
100
private _color : string ;
105
101
106
- /** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
107
- _isKeyboardFocused : boolean = false ;
108
-
109
- /** Whether a mousedown has occurred on this element in the last 100ms. */
110
- _isMouseDown : boolean = false ;
111
-
112
102
/** Whether the button is round. */
113
103
_isRoundButton : boolean = [ 'icon-button' , 'fab' , 'mini-fab' ] . some ( suffix => {
114
104
let el = this . _getHostElement ( ) ;
@@ -129,22 +119,20 @@ export class MdButton {
129
119
get disabled ( ) { return this . _disabled ; }
130
120
set disabled ( value : boolean ) { this . _disabled = coerceBooleanProperty ( value ) ? true : null ; }
131
121
132
- constructor ( private _elementRef : ElementRef , private _renderer : Renderer ) { }
122
+ constructor ( private _elementRef : ElementRef , private _renderer : Renderer ,
123
+ private _focusOriginMonitor : FocusOriginMonitor ) {
124
+ this . _focusOriginMonitor . monitor ( this . _elementRef . nativeElement , this . _renderer , true ) ;
125
+ }
126
+
127
+ ngOnDestroy ( ) {
128
+ this . _focusOriginMonitor . unmonitor ( this . _elementRef . nativeElement ) ;
129
+ }
133
130
134
131
/** The color of the button. Can be `primary`, `accent`, or `warn`. */
135
132
@Input ( )
136
133
get color ( ) : string { return this . _color ; }
137
134
set color ( value : string ) { this . _updateColor ( value ) ; }
138
135
139
- _setMousedown ( ) {
140
- // We only *show* the focus style when focus has come to the button via the keyboard.
141
- // The Material Design spec is silent on this topic, and without doing this, the
142
- // button continues to look :active after clicking.
143
- // @see http://marcysutton.com/button-focus-hell/
144
- this . _isMouseDown = true ;
145
- setTimeout ( ( ) => { this . _isMouseDown = false ; } , 100 ) ;
146
- }
147
-
148
136
_updateColor ( newColor : string ) {
149
137
this . _setElementColor ( this . _color , false ) ;
150
138
this . _setElementColor ( newColor , true ) ;
@@ -157,14 +145,6 @@ export class MdButton {
157
145
}
158
146
}
159
147
160
- _setKeyboardFocus ( ) {
161
- this . _isKeyboardFocused = ! this . _isMouseDown ;
162
- }
163
-
164
- _removeKeyboardFocus ( ) {
165
- this . _isKeyboardFocused = false ;
166
- }
167
-
168
148
/** Focuses the button. */
169
149
focus ( ) : void {
170
150
this . _renderer . invokeElementMethod ( this . _getHostElement ( ) , 'focus' ) ;
@@ -189,19 +169,15 @@ export class MdButton {
189
169
host : {
190
170
'[attr.disabled]' : 'disabled' ,
191
171
'[attr.aria-disabled]' : '_isAriaDisabled' ,
192
- '[class.mat-button-focus]' : '_isKeyboardFocused' ,
193
- '(mousedown)' : '_setMousedown()' ,
194
- '(focus)' : '_setKeyboardFocus()' ,
195
- '(blur)' : '_removeKeyboardFocus()' ,
196
172
'(click)' : '_haltDisabledEvents($event)' ,
197
173
} ,
198
174
templateUrl : 'button.html' ,
199
175
styleUrls : [ 'button.css' ] ,
200
176
encapsulation : ViewEncapsulation . None
201
177
} )
202
178
export class MdAnchor extends MdButton {
203
- constructor ( elementRef : ElementRef , renderer : Renderer ) {
204
- super ( elementRef , renderer ) ;
179
+ constructor ( elementRef : ElementRef , renderer : Renderer , focusOriginMonitor : FocusOriginMonitor ) {
180
+ super ( elementRef , renderer , focusOriginMonitor ) ;
205
181
}
206
182
207
183
/** @docs -private */
0 commit comments