-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
feat(slide-toggle): add ripple focus indicator #3739
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,20 @@ import { | |
AfterContentInit, | ||
ViewChild, | ||
ViewEncapsulation, | ||
OnDestroy, | ||
} from '@angular/core'; | ||
import { | ||
applyCssTransform, | ||
coerceBooleanProperty, | ||
HammerInput, | ||
FocusOriginMonitor, | ||
FocusOrigin, | ||
MdRipple, | ||
RippleRef | ||
} from '../core'; | ||
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; | ||
import {applyCssTransform, coerceBooleanProperty, HammerInput} from '../core'; | ||
import {Observable} from 'rxjs/Observable'; | ||
|
||
import {Subscription} from 'rxjs/Subscription'; | ||
|
||
export const MD_SLIDE_TOGGLE_VALUE_ACCESSOR: any = { | ||
provide: NG_VALUE_ACCESSOR, | ||
|
@@ -41,8 +50,6 @@ let nextId = 0; | |
'[class.mat-slide-toggle]': 'true', | ||
'[class.mat-checked]': 'checked', | ||
'[class.mat-disabled]': 'disabled', | ||
// This mat-slide-toggle prefix will change, once the temporary ripple is removed. | ||
'[class.mat-slide-toggle-focused]': '_hasFocus', | ||
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"', | ||
'(mousedown)': '_setMousedown()' | ||
}, | ||
|
@@ -52,7 +59,7 @@ let nextId = 0; | |
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | ||
export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueAccessor { | ||
|
||
private onChange = (_: any) => {}; | ||
private onTouched = () => {}; | ||
|
@@ -67,8 +74,11 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
private _required: boolean = false; | ||
private _disableRipple: boolean = false; | ||
|
||
// Needs to be public to support AOT compilation (as host binding). | ||
_hasFocus: boolean = false; | ||
/** Reference to the focus state ripple. */ | ||
private _focusRipple: RippleRef; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way is fine; my thinking is that the "Ripple" part of the identity here is captured by the type. |
||
|
||
/** Reference to the focus origin monitor subscription. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /** Subscription to focus-origin changes */ ? |
||
private _focusOriginSubscription: Subscription; | ||
|
||
/** Name value will be applied to the input element if present */ | ||
@Input() name: string = null; | ||
|
@@ -110,12 +120,31 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
/** Returns the unique id for the visual hidden input. */ | ||
get inputId(): string { return `${this.id || this._uniqueId}-input`; } | ||
|
||
/** Reference to the underlying input element. */ | ||
@ViewChild('input') _inputElement: ElementRef; | ||
|
||
constructor(private _elementRef: ElementRef, private _renderer: Renderer) {} | ||
/** Reference to the ripple directive on the thumb container. */ | ||
@ViewChild(MdRipple) _ripple: MdRipple; | ||
|
||
constructor(private _elementRef: ElementRef, | ||
private _renderer: Renderer, | ||
private _focusOriginMonitor: FocusOriginMonitor) {} | ||
|
||
ngAfterContentInit() { | ||
this._slideRenderer = new SlideToggleRenderer(this._elementRef); | ||
|
||
this._focusOriginSubscription = this._focusOriginMonitor | ||
.monitor(this._inputElement.nativeElement, this._renderer, false) | ||
.subscribe(focusOrigin => this._onInputFocusChange(focusOrigin)); | ||
} | ||
|
||
ngOnDestroy() { | ||
this._focusOriginMonitor.unmonitor(this._inputElement.nativeElement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, maybe in another PR we can rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I will note it down. |
||
|
||
if (this._focusOriginSubscription) { | ||
this._focusOriginSubscription.unsubscribe(); | ||
this._focusOriginSubscription = null; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -162,19 +191,6 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
setTimeout(() => this._isMousedown = false, 100); | ||
} | ||
|
||
_onInputFocus() { | ||
// Only show the focus / ripple indicator when the focus was not triggered by a mouse | ||
// interaction on the component. | ||
if (!this._isMousedown) { | ||
this._hasFocus = true; | ||
} | ||
} | ||
|
||
_onInputBlur() { | ||
this._hasFocus = false; | ||
this.onTouched(); | ||
} | ||
|
||
/** Implemented as part of ControlValueAccessor. */ | ||
writeValue(value: any): void { | ||
this.checked = value; | ||
|
@@ -195,10 +211,9 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
this.disabled = isDisabled; | ||
} | ||
|
||
/** Focuses the slide-toggle. */ | ||
/** Focuses the slide-toggle programmatically. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the "programmatically" is necessary |
||
focus() { | ||
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus'); | ||
this._onInputFocus(); | ||
this._focusOriginMonitor.focusVia(this._inputElement.nativeElement, this._renderer, 'program'); | ||
} | ||
|
||
/** Whether the slide-toggle is checked. */ | ||
|
@@ -223,6 +238,22 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
this.checked = !this.checked; | ||
} | ||
|
||
/** Function is called whenever the focus changes for the input element. */ | ||
private _onInputFocusChange(focusOrigin: FocusOrigin) { | ||
if (!this._focusRipple && focusOrigin === 'keyboard') { | ||
// For keyboard focus show a persistent ripple as focus indicator. | ||
this._focusRipple = this._ripple.launch(0, 0, { persistent: true, centered: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else if (!focusOrigin) { | ||
this.onTouched(); | ||
|
||
// Fade out and clear the focus ripple if one is currently present. | ||
if (this._focusRipple) { | ||
this._focusRipple.fadeOut(); | ||
this._focusRipple = null; | ||
} | ||
} | ||
} | ||
|
||
private _updateColor(newColor: string) { | ||
this._setElementColor(this._color, false); | ||
this._setElementColor(newColor, true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
FOCUS_ORIGIN_MONITOR_PROVIDER