Skip to content

Commit 43f8fa2

Browse files
committed
Use FocusOriginMonitor
1 parent f2c9a60 commit 43f8fa2

File tree

3 files changed

+5
-33
lines changed

3 files changed

+5
-33
lines changed

src/lib/button-toggle/button-toggle.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
[checked]="checked"
66
[disabled]="disabled"
77
[name]="name"
8-
(blur)="_onInputBlur()"
9-
(focus)="_onInputFocus()"
108
(change)="_onInputChange($event)"
119
(click)="_onInputClick($event)">
1210

src/lib/button-toggle/button-toggle.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $mat-button-toggle-border-radius: 2px !default;
3838
position: relative;
3939
}
4040

41-
.mat-button-toggle.mat-button-toggle-focus .mat-button-toggle-focus-overlay {
41+
.mat-button-toggle.cdk-focused .mat-button-toggle-focus-overlay {
4242
opacity: 1;
4343
}
4444

src/lib/button-toggle/button-toggle.ts

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormsModule} from '@angular/forms';
2222
import {Observable} from 'rxjs/Observable';
2323
import {
24+
FocusOriginMonitor,
2425
UniqueSelectionDispatcher,
2526
coerceBooleanProperty,
2627
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
@@ -286,21 +287,13 @@ export class MdButtonToggleGroupMultiple {
286287
styleUrls: ['button-toggle.css'],
287288
encapsulation: ViewEncapsulation.None,
288289
host: {
289-
'[class.mat-button-toggle-focus]': '_hasFocus',
290290
'[class.mat-button-toggle]': 'true',
291-
'(mousedown)': '_setMousedown()',
292291
}
293292
})
294293
export class MdButtonToggle implements OnInit {
295294
/** Whether or not this button toggle is checked. */
296295
private _checked: boolean = false;
297296

298-
/** Whether the button has focus. Used for class binding. */
299-
_hasFocus: boolean = false;
300-
301-
/** Whether a mousedown has occurred on this element in the last 100ms. */
302-
_isMouseDown: boolean = false;
303-
304297
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
305298
_type: ToggleType;
306299

@@ -339,7 +332,8 @@ export class MdButtonToggle implements OnInit {
339332
constructor(@Optional() toggleGroup: MdButtonToggleGroup,
340333
@Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple,
341334
public buttonToggleDispatcher: UniqueSelectionDispatcher,
342-
private _renderer: Renderer) {
335+
private _renderer: Renderer,
336+
private _focusOriginMonitor: FocusOriginMonitor) {
343337
this.buttonToggleGroup = toggleGroup;
344338

345339
this.buttonToggleGroupMultiple = toggleGroupMultiple;
@@ -370,6 +364,7 @@ export class MdButtonToggle implements OnInit {
370364
if (this.buttonToggleGroup && this._value == this.buttonToggleGroup.value) {
371365
this._checked = true;
372366
}
367+
this._focusOriginMonitor.registerElementForFocusClasses(this._inputElement.nativeElement, this._renderer);
373368
}
374369

375370
/** Unique ID for the underlying `input` element. */
@@ -468,31 +463,10 @@ export class MdButtonToggle implements OnInit {
468463
event.stopPropagation();
469464
}
470465

471-
_onInputFocus() {
472-
// Only show the focus / ripple indicator when the focus was not triggered by a mouse
473-
// interaction on the component.
474-
if (!this._isMouseDown) {
475-
this._hasFocus = true;
476-
}
477-
}
478-
479-
_onInputBlur() {
480-
this._hasFocus = false;
481-
}
482-
483466
/** Focuses the button. */
484467
focus() {
485468
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
486469
}
487-
488-
_setMousedown() {
489-
// We only *show* the focus style when focus has come to the button via the keyboard.
490-
// The Material Design spec is silent on this topic, and without doing this, the
491-
// button continues to look :active after clicking.
492-
// @see http://marcysutton.com/button-focus-hell/
493-
this._isMouseDown = true;
494-
setTimeout(() => { this._isMouseDown = false; }, 100);
495-
}
496470
}
497471

498472

0 commit comments

Comments
 (0)