Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(button): use FocusOriginMonitor for focus styles #3294

Merged
merged 8 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/button/_button-base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $mat-mini-fab-padding: 8px !default;
cursor: default;
}

&.mat-button-focus {
&.cdk-keyboard-focused {
.mat-button-focus-overlay {
opacity: 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/button/_button-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
$foreground: map-get($theme, foreground);

.mat-button, .mat-icon-button, .mat-raised-button, .mat-fab, .mat-mini-fab {
&.mat-button-focus {
&.cdk-keyboard-focused {
@include _mat-button-focus-color($theme);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/button/button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {async, TestBed, ComponentFixture} from '@angular/core/testing';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdButtonModule} from './index';
Expand Down
60 changes: 18 additions & 42 deletions src/lib/button/button.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
Component,
ViewEncapsulation,
Input,
HostBinding,
ChangeDetectionStrategy,
Component,
Directive,
ElementRef,
HostBinding,
Input,
OnDestroy,
Renderer,
Directive,
ViewEncapsulation
} from '@angular/core';
import {coerceBooleanProperty} from '../core';
import {coerceBooleanProperty, FocusOriginMonitor} from '../core';


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


Expand Down Expand Up @@ -85,25 +85,15 @@ export class MdMiniFabCssMatStyler {}
'button[mat-fab], button[mat-mini-fab]',
host: {
'[disabled]': 'disabled',
'[class.mat-button-focus]': '_isKeyboardFocused',
'(mousedown)': '_setMousedown()',
'(focus)': '_setKeyboardFocus()',
'(blur)': '_removeKeyboardFocus()',
},
templateUrl: 'button.html',
styleUrls: ['button.css'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdButton {
export class MdButton implements OnDestroy {
private _color: string;

/** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
_isKeyboardFocused: boolean = false;

/** Whether a mousedown has occurred on this element in the last 100ms. */
_isMouseDown: boolean = false;

/** Whether the button is round. */
_isRoundButton: boolean = ['icon-button', 'fab', 'mini-fab'].some(suffix => {
let el = this._getHostElement();
Expand All @@ -124,22 +114,20 @@ export class MdButton {
get disabled() { return this._disabled; }
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value) ? true : null; }

constructor(private _elementRef: ElementRef, private _renderer: Renderer) { }
constructor(private _elementRef: ElementRef, private _renderer: Renderer,
private _focusOriginMonitor: FocusOriginMonitor) {
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
}

ngOnDestroy() {
this._focusOriginMonitor.unmonitor(this._elementRef.nativeElement);
}

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

_setMousedown() {
// We only *show* the focus style when focus has come to the button via the keyboard.
// The Material Design spec is silent on this topic, and without doing this, the
// button continues to look :active after clicking.
// @see http://marcysutton.com/button-focus-hell/
this._isMouseDown = true;
setTimeout(() => { this._isMouseDown = false; }, 100);
}

_updateColor(newColor: string) {
this._setElementColor(this._color, false);
this._setElementColor(newColor, true);
Expand All @@ -152,14 +140,6 @@ export class MdButton {
}
}

_setKeyboardFocus() {
this._isKeyboardFocused = !this._isMouseDown;
}

_removeKeyboardFocus() {
this._isKeyboardFocused = false;
}

/** Focuses the button. */
focus(): void {
this._renderer.invokeElementMethod(this._getHostElement(), 'focus');
Expand All @@ -184,19 +164,15 @@ export class MdButton {
host: {
'[attr.disabled]': 'disabled',
'[attr.aria-disabled]': '_isAriaDisabled',
'[class.mat-button-focus]': '_isKeyboardFocused',
'(mousedown)': '_setMousedown()',
'(focus)': '_setKeyboardFocus()',
'(blur)': '_removeKeyboardFocus()',
'(click)': '_haltDisabledEvents($event)',
},
templateUrl: 'button.html',
styleUrls: ['button.css'],
encapsulation: ViewEncapsulation.None
})
export class MdAnchor extends MdButton {
constructor(elementRef: ElementRef, renderer: Renderer) {
super(elementRef, renderer);
constructor(elementRef: ElementRef, renderer: Renderer, focusOriginMonitor: FocusOriginMonitor) {
super(elementRef, renderer, focusOriginMonitor);
}

/** @docs-private */
Expand Down
27 changes: 16 additions & 11 deletions src/lib/button/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import {NgModule, ModuleWithProviders} from '@angular/core';
import {ModuleWithProviders, NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MdRippleModule, CompatibilityModule} from '../core';
import {CompatibilityModule, MdRippleModule, StyleModule} from '../core';
import {
MdButton,
MdAnchor,
MdButton,
MdButtonCssMatStyler,
MdRaisedButtonCssMatStyler,
MdIconButtonCssMatStyler,
MdFabCssMatStyler,
MdIconButtonCssMatStyler,
MdMiniFabCssMatStyler,
MdRaisedButtonCssMatStyler
} from './button';


export * from './button';


@NgModule({
imports: [CommonModule, MdRippleModule, CompatibilityModule],
imports: [
CommonModule,
MdRippleModule,
CompatibilityModule,
StyleModule,
],
exports: [
MdButton,
MdAnchor,
Expand All @@ -22,7 +30,7 @@ import {
MdRaisedButtonCssMatStyler,
MdIconButtonCssMatStyler,
MdFabCssMatStyler,
MdMiniFabCssMatStyler
MdMiniFabCssMatStyler,
],
declarations: [
MdButton,
Expand All @@ -31,7 +39,7 @@ import {
MdRaisedButtonCssMatStyler,
MdIconButtonCssMatStyler,
MdFabCssMatStyler,
MdMiniFabCssMatStyler
MdMiniFabCssMatStyler,
],
})
export class MdButtonModule {
Expand All @@ -43,6 +51,3 @@ export class MdButtonModule {
};
}
}


export * from './button';