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