Skip to content

feat(chips): add ripple to chips #9761

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
merged 1 commit into from
Feb 2, 2018
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
59 changes: 53 additions & 6 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,33 @@
import {FocusableOption} from '@angular/cdk/a11y';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {BACKSPACE, DELETE, SPACE} from '@angular/cdk/keycodes';
import {Platform} from '@angular/cdk/platform';
import {
ContentChild,
Directive,
ElementRef,
EventEmitter,
forwardRef,
Inject,
Input,
NgZone,
OnDestroy,
Optional,
Output,
} from '@angular/core';
import {CanColor, CanDisable, mixinColor, mixinDisabled} from '@angular/material/core';
import {
CanColor,
CanDisable,
CanDisableRipple,
MAT_RIPPLE_GLOBAL_OPTIONS,
mixinColor,
mixinDisabled,
mixinDisableRipple,
RippleConfig,
RippleGlobalOptions,
RippleRenderer,
RippleTarget
} from '@angular/material/core';
import {Subject} from 'rxjs/Subject';


Expand Down Expand Up @@ -47,7 +63,7 @@ export class MatChipBase {
constructor(public _elementRef: ElementRef) {}
}

export const _MatChipMixinBase = mixinColor(mixinDisabled(MatChipBase), 'primary');
export const _MatChipMixinBase = mixinColor(mixinDisableRipple(mixinDisabled(MatChipBase)), 'primary');

const CHIP_ATTRIBUTE_NAMES = ['mat-basic-chip'];

Expand Down Expand Up @@ -76,7 +92,7 @@ export class MatChipTrailingIcon {}
*/
@Directive({
selector: `mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]`,
inputs: ['color', 'disabled'],
inputs: ['color', 'disabled', 'disableRipple'],
exportAs: 'matChip',
host: {
'class': 'mat-chip',
Expand All @@ -85,6 +101,7 @@ export class MatChipTrailingIcon {}
'[class.mat-chip-selected]': 'selected',
'[class.mat-chip-with-avatar]': 'avatar',
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
'[class.mat-chip-disabled]': 'disabled',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-selected]': 'ariaSelected',
Expand All @@ -93,10 +110,26 @@ export class MatChipTrailingIcon {}
'(focus)': '_hasFocus = true',
'(blur)': '_blur()',
},

})
export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDestroy, CanColor,
CanDisable {
CanDisable, CanDisableRipple, RippleTarget {
/**
* Ripple configuration for ripples that are launched on pointer down.
* @docs-private
*/
rippleConfig: RippleConfig = {};

/** Reference to the RippleRenderer for the chip. */
private _chipRipple: RippleRenderer;

/**
* Whether ripples are disabled on interaction
* @docs-private
*/
get rippleDisabled(): boolean {
return this.disabled || this.disableRipple;
}

/** Whether the chip has focus. */
_hasFocus: boolean = false;

Expand Down Expand Up @@ -188,10 +221,23 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
return this.selectable ? this.selected.toString() : null;
}

constructor(public _elementRef: ElementRef) {
constructor(public _elementRef: ElementRef,
ngZone: NgZone,
platform: Platform,
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions) {
super(_elementRef);

this._addHostClassName();

this._chipRipple = new RippleRenderer(this, ngZone, _elementRef, platform);
this._chipRipple.setupTriggerEvents(_elementRef.nativeElement);

if (globalOptions) {
this.rippleConfig = {
speedFactor: globalOptions.baseSpeedFactor,
animation: globalOptions.animation,
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about that again, it's weird that we need to repeat that so often, and even need to keep track of the global options. Just wanted to add that here, nothing to fix in this PR actually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tinayuangao Just saw, that this does not include terminateOnPointerUp yet. This would be important to add here as well.

}

_addHostClassName() {
Expand All @@ -208,6 +254,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes

ngOnDestroy() {
this.destroyed.emit({chip: this});
this._chipRipple._removeTriggerEvents();
}

/** Selects the chip. */
Expand Down
5 changes: 5 additions & 0 deletions src/lib/chips/chips.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ $mat-chip-input-margin: 3px;
$mat-chip-avatar-size: 32px;
$mat-chip-remove-size: 18px;

.mat-chip {
position: relative;
overflow: hidden;
}

.mat-standard-chip {
@include mat-elevation-transition;
display: inline-flex;
Expand Down