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(material-experimental/mdc-chips): avoid potential server-side rendering errors #18044

Merged
merged 1 commit into from
Dec 29, 2019
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
20 changes: 14 additions & 6 deletions src/material-experimental/mdc-chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,15 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
},
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
notifyRemoval: () => this.removed.emit({chip: this}),
getComputedStyleValue: propertyName =>
window.getComputedStyle(this._elementRef.nativeElement).getPropertyValue(propertyName),
getComputedStyleValue: propertyName => {
// This function is run when a chip is removed so it might be
// invoked during server-side rendering. Add some extra checks just in case.
if (typeof window !== 'undefined' && window) {
const getComputedStyle = window.getComputedStyle(this._elementRef.nativeElement);
return getComputedStyle.getPropertyValue(propertyName);
}
return '';
},
setStyleProperty: (propertyName: string, value: string) => {
this._elementRef.nativeElement.style.setProperty(propertyName, value);
},
Expand Down Expand Up @@ -339,12 +346,13 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
_listenToRemoveIconInteraction() {
this.removeIcon.interaction
.pipe(takeUntil(this._destroyed))
.subscribe((event) => {
.subscribe(event => {
// The MDC chip foundation calls stopPropagation() for any trailing icon interaction
// event, even ones it doesn't handle, so we want to avoid passing it keyboard events
// for which we have a custom handler.
if (this.disabled || (event instanceof KeyboardEvent &&
this.HANDLED_KEYS.indexOf(event.keyCode) !== -1)) {
// for which we have a custom handler. Note that we assert the type of the event using
// the `type`, because `instanceof KeyboardEvent` can throw during server-side rendering.
if (this.disabled || (event.type.startsWith('key') &&
this.HANDLED_KEYS.indexOf((event as KeyboardEvent).keyCode) !== -1)) {
return;
}
this._chipFoundation.handleTrailingIconInteraction(event);
Expand Down
14 changes: 12 additions & 2 deletions src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ <h2>MDC checkbox</h2>

<h2>MDC chips</h2>

<!-- TODO: Copy kitchen sink examples for mat-chip-list here -->
Not yet implemented.
<mat-chip-set>
<mat-basic-chip>Basic Chip 1</mat-basic-chip>
<mat-basic-chip>Basic Chip 2</mat-basic-chip>
<mat-basic-chip>Basic Chip 3</mat-basic-chip>
</mat-chip-set>

<mat-chip-listbox>
<mat-chip-option>Extra Small</mat-chip-option>
<mat-chip-option>Small</mat-chip-option>
<mat-chip-option>Medium</mat-chip-option>
<mat-chip-option>Large</mat-chip-option>
</mat-chip-listbox>

<h2>MDC menu</h2>
<button mat-button [matMenuTriggerFor]="menu">Open</button>
Expand Down