Skip to content

Commit 8ba4979

Browse files
committed
use FocusOriginMonitor for md-button focus styles
1 parent c203589 commit 8ba4979

File tree

3 files changed

+15
-40
lines changed

3 files changed

+15
-40
lines changed

src/lib/button/_button-base.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $mat-mini-fab-padding: 8px !default;
5555
cursor: default;
5656
}
5757

58-
&.mat-button-focus {
58+
&.cdk-keyboard-focused {
5959
.mat-button-focus-overlay {
6060
opacity: 1;
6161
}

src/lib/button/_button-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
$foreground: map-get($theme, foreground);
5555

5656
.mat-button, .mat-icon-button, .mat-raised-button, .mat-fab, .mat-mini-fab {
57-
&.mat-button-focus {
57+
&.cdk-keyboard-focused {
5858
@include _mat-button-focus-color($theme);
5959
}
6060
}

src/lib/button/button.ts

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {
77
ElementRef,
88
Renderer,
99
NgModule,
10-
ModuleWithProviders, Directive,
10+
ModuleWithProviders, Directive, OnDestroy,
1111
} from '@angular/core';
1212
import {CommonModule} from '@angular/common';
1313
import {MdRippleModule, coerceBooleanProperty, CompatibilityModule} from '../core';
14+
import {FocusOriginMonitor} from '../core/style/focus-origin-monitor';
1415

1516

16-
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
1717
// TODO(kara): Convert attribute selectors to classes when attr maps become available
1818

1919

@@ -87,25 +87,15 @@ export class MdMiniFabCssMatStyler {}
8787
'button[mat-fab], button[mat-mini-fab]',
8888
host: {
8989
'[disabled]': 'disabled',
90-
'[class.mat-button-focus]': '_isKeyboardFocused',
91-
'(mousedown)': '_setMousedown()',
92-
'(focus)': '_setKeyboardFocus()',
93-
'(blur)': '_removeKeyboardFocus()',
9490
},
9591
templateUrl: 'button.html',
9692
styleUrls: ['button.css'],
9793
encapsulation: ViewEncapsulation.None,
9894
changeDetection: ChangeDetectionStrategy.OnPush,
9995
})
100-
export class MdButton {
96+
export class MdButton implements OnDestroy {
10197
private _color: string;
10298

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-
10999
/** Whether the button is round. */
110100
_isRoundButton: boolean = ['icon-button', 'fab', 'mini-fab'].some(suffix => {
111101
let el = this._getHostElement();
@@ -126,22 +116,20 @@ export class MdButton {
126116
get disabled() { return this._disabled; }
127117
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value) ? true : null; }
128118

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+
}
130127

131128
/** The color of the button. Can be `primary`, `accent`, or `warn`. */
132129
@Input()
133130
get color(): string { return this._color; }
134131
set color(value: string) { this._updateColor(value); }
135132

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-
145133
_updateColor(newColor: string) {
146134
this._setElementColor(this._color, false);
147135
this._setElementColor(newColor, true);
@@ -154,14 +142,6 @@ export class MdButton {
154142
}
155143
}
156144

157-
_setKeyboardFocus() {
158-
this._isKeyboardFocused = !this._isMouseDown;
159-
}
160-
161-
_removeKeyboardFocus() {
162-
this._isKeyboardFocused = false;
163-
}
164-
165145
/** Focuses the button. */
166146
focus(): void {
167147
this._renderer.invokeElementMethod(this._getHostElement(), 'focus');
@@ -186,19 +166,15 @@ export class MdButton {
186166
host: {
187167
'[attr.disabled]': 'disabled',
188168
'[attr.aria-disabled]': '_isAriaDisabled',
189-
'[class.mat-button-focus]': '_isKeyboardFocused',
190-
'(mousedown)': '_setMousedown()',
191-
'(focus)': '_setKeyboardFocus()',
192-
'(blur)': '_removeKeyboardFocus()',
193169
'(click)': '_haltDisabledEvents($event)',
194170
},
195171
templateUrl: 'button.html',
196172
styleUrls: ['button.css'],
197173
encapsulation: ViewEncapsulation.None
198174
})
199175
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);
202178
}
203179

204180
/** @docs-private */
@@ -215,7 +191,6 @@ export class MdAnchor extends MdButton {
215191
// A disabled button shouldn't apply any actions
216192
if (this.disabled) {
217193
event.preventDefault();
218-
event.stopImmediatePropagation();
219194
}
220195
}
221196
}

0 commit comments

Comments
 (0)