Skip to content

Commit 17a7bcb

Browse files
crisbetommalerba
authored andcommitted
fix(material-experimental/mdc-chips): avoid potential server-s… (#18044)
Fixes a few places in the MDC basec `mat-chip` that can throw if they're hit during server-side rendering. Also sets up the server-side rendering check.
1 parent 22d7f77 commit 17a7bcb

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/material-experimental/mdc-chips/chip.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,15 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
263263
},
264264
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
265265
notifyRemoval: () => this.removed.emit({chip: this}),
266-
getComputedStyleValue: propertyName =>
267-
window.getComputedStyle(this._elementRef.nativeElement).getPropertyValue(propertyName),
266+
getComputedStyleValue: propertyName => {
267+
// This function is run when a chip is removed so it might be
268+
// invoked during server-side rendering. Add some extra checks just in case.
269+
if (typeof window !== 'undefined' && window) {
270+
const getComputedStyle = window.getComputedStyle(this._elementRef.nativeElement);
271+
return getComputedStyle.getPropertyValue(propertyName);
272+
}
273+
return '';
274+
},
268275
setStyleProperty: (propertyName: string, value: string) => {
269276
this._elementRef.nativeElement.style.setProperty(propertyName, value);
270277
},
@@ -339,12 +346,13 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
339346
_listenToRemoveIconInteraction() {
340347
this.removeIcon.interaction
341348
.pipe(takeUntil(this._destroyed))
342-
.subscribe((event) => {
349+
.subscribe(event => {
343350
// The MDC chip foundation calls stopPropagation() for any trailing icon interaction
344351
// event, even ones it doesn't handle, so we want to avoid passing it keyboard events
345-
// for which we have a custom handler.
346-
if (this.disabled || (event instanceof KeyboardEvent &&
347-
this.HANDLED_KEYS.indexOf(event.keyCode) !== -1)) {
352+
// for which we have a custom handler. Note that we assert the type of the event using
353+
// the `type`, because `instanceof KeyboardEvent` can throw during server-side rendering.
354+
if (this.disabled || (event.type.startsWith('key') &&
355+
this.HANDLED_KEYS.indexOf((event as KeyboardEvent).keyCode) !== -1)) {
348356
return;
349357
}
350358
this._chipFoundation.handleTrailingIconInteraction(event);

src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@ <h2>MDC checkbox</h2>
2727

2828
<h2>MDC chips</h2>
2929

30-
<!-- TODO: Copy kitchen sink examples for mat-chip-list here -->
31-
Not yet implemented.
30+
<mat-chip-set>
31+
<mat-basic-chip>Basic Chip 1</mat-basic-chip>
32+
<mat-basic-chip>Basic Chip 2</mat-basic-chip>
33+
<mat-basic-chip>Basic Chip 3</mat-basic-chip>
34+
</mat-chip-set>
35+
36+
<mat-chip-listbox>
37+
<mat-chip-option>Extra Small</mat-chip-option>
38+
<mat-chip-option>Small</mat-chip-option>
39+
<mat-chip-option>Medium</mat-chip-option>
40+
<mat-chip-option>Large</mat-chip-option>
41+
</mat-chip-listbox>
3242

3343
<h2>MDC menu</h2>
3444
<button mat-button [matMenuTriggerFor]="menu">Open</button>

0 commit comments

Comments
 (0)