- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.8k
feat(slide-toggle): add ripple focus indicator #3739
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
          
     Merged
      
      
            tinayuangao
  merged 2 commits into
  angular:master
from
devversion:feat/slide-toggle-focus
  
      
      
   
  Mar 29, 2017 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
|  | ||
| /** 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; | ||
|  | @@ -197,8 +213,7 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor { | |
|  | ||
| /** Focuses the slide-toggle. */ | ||
| 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}); | ||
| } 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); | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
_focusIndicator?Uh oh!
There was an error while loading. Please reload this page.
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.
I really prefer
_focusRipplehere because it's just indicates that it's a ripple. On the checkbox we have it similar. JustfocusedRipple.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.
Either way is fine; my thinking is that the "Ripple" part of the identity here is captured by the type.