Skip to content

Commit 501d73e

Browse files
committed
fix(material/chips): styling not cleared on basic chips in listbox and grid (#26771)
Fixes several issues that prevented the basic chips from appearing basic in `mat-chip-listbox` and `mat-chip-grid`: * `basicChipAttrName` was overwritten to the correct value, but because we were checking it in the constructor, the override value wasn't picked up. * The proper host bindings weren't in place to clear the styling. * The user agent styles typography styles of the `button` inside the `mat-basic-chip-option` weren't cleared. * The logic that checks if a click originates from a chip was excluding basic chips. (cherry picked from commit d444c58)
1 parent e676125 commit 501d73e

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

src/dev-app/chips/chips-demo.html

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ <h4>Unstyled</h4>
1919
<mat-basic-chip>Basic Chip 3</mat-basic-chip>
2020
</mat-chip-set>
2121

22+
<mat-chip-listbox>
23+
<mat-basic-chip-option>Basic Chip Option 1</mat-basic-chip-option>
24+
<mat-basic-chip-option>Basic Chip Option 2</mat-basic-chip-option>
25+
<mat-basic-chip-option>Basic Chip Option 3</mat-basic-chip-option>
26+
</mat-chip-listbox>
27+
28+
<mat-chip-grid #basicGrid>
29+
<mat-basic-chip-row>Basic Chip Row 1</mat-basic-chip-row>
30+
<mat-basic-chip-row>Basic Chip Row 2</mat-basic-chip-row>
31+
<mat-basic-chip-row>Basic Chip Row 3</mat-basic-chip-row>
32+
<input [matChipInputFor]="basicGrid" readonly>
33+
</mat-chip-grid>
34+
2235
<h4>With avatar, icons, and color</h4>
2336

2437
<mat-chip-set>

src/dev-app/chips/chips-demo.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
}
1919

20-
mat-basic-chip {
20+
.mat-mdc-basic-chip {
2121
margin: auto 10px;
2222
}
2323

src/material/chips/chip-option.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ export class MatChipSelectionChange {
4444
styleUrls: ['chip.css'],
4545
inputs: ['color', 'disabled', 'disableRipple', 'tabIndex'],
4646
host: {
47-
'class':
48-
'mat-mdc-chip mat-mdc-chip-option mdc-evolution-chip mdc-evolution-chip--filter mdc-evolution-chip--selectable',
47+
'class': 'mat-mdc-chip mat-mdc-chip-option',
48+
'[class.mdc-evolution-chip]': '!_isBasicChip',
49+
'[class.mdc-evolution-chip--filter]': '!_isBasicChip',
50+
'[class.mdc-evolution-chip--selectable]': '!_isBasicChip',
4951
'[class.mat-mdc-chip-selected]': 'selected',
5052
'[class.mat-mdc-chip-multiple]': '_chipListMultiple',
5153
'[class.mat-mdc-chip-disabled]': 'disabled',
@@ -141,7 +143,8 @@ export class MatChipOption extends MatChip implements OnInit {
141143
@Output() readonly selectionChange: EventEmitter<MatChipSelectionChange> =
142144
new EventEmitter<MatChipSelectionChange>();
143145

144-
ngOnInit() {
146+
override ngOnInit() {
147+
super.ngOnInit();
145148
this.role = 'presentation';
146149
}
147150

src/material/chips/chip-set.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ export class MatChipSet
222222
let currentElement = event.target as HTMLElement | null;
223223

224224
while (currentElement && currentElement !== this._elementRef.nativeElement) {
225-
// Null check the classList, because IE and Edge don't support it on all elements.
226-
if (currentElement.classList && currentElement.classList.contains('mdc-evolution-chip')) {
225+
if (currentElement.classList.contains('mat-mdc-chip')) {
227226
return true;
228227
}
229228
currentElement = currentElement.parentElement;

src/material/chips/chip.scss

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@
167167
}
168168
}
169169

170+
// Keeps basic listbox chips looking consistent with the other variations. Listbox chips don't
171+
// inherit the font size, because they wrap the label in a `button` that has user agent styles.
172+
.mat-mdc-basic-chip .mdc-evolution-chip__action--primary {
173+
font: inherit;
174+
}
175+
170176
// MDC's focus and hover indication is handled through their ripple which we currently
171177
// don't use due to size concerns so we have to re-implement it ourselves.
172178
.mat-mdc-chip-focus-overlay {

src/material/chips/chip.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
Attribute,
2929
ContentChildren,
3030
QueryList,
31+
OnInit,
3132
} from '@angular/core';
3233
import {DOCUMENT} from '@angular/common';
3334
import {
@@ -116,6 +117,7 @@ const _MatChipMixinBase = mixinTabIndex(
116117
export class MatChip
117118
extends _MatChipMixinBase
118119
implements
120+
OnInit,
119121
AfterViewInit,
120122
AfterContentInit,
121123
CanColor,
@@ -136,7 +138,7 @@ export class MatChip
136138
readonly _onBlur = new Subject<MatChipEvent>();
137139

138140
/** Whether this chip is a basic (unstyled) chip. */
139-
readonly _isBasicChip: boolean;
141+
_isBasicChip: boolean;
140142

141143
/** Role for the root of the chip. */
142144
@Input() role: string | null = null;
@@ -263,18 +265,23 @@ export class MatChip
263265
@Attribute('tabindex') tabIndex?: string,
264266
) {
265267
super(elementRef);
266-
const element = elementRef.nativeElement;
267268
this._document = _document;
268269
this._animationsDisabled = animationMode === 'NoopAnimations';
269-
this._isBasicChip =
270-
element.hasAttribute(this.basicChipAttrName) ||
271-
element.tagName.toLowerCase() === this.basicChipAttrName;
272270
if (tabIndex != null) {
273271
this.tabIndex = parseInt(tabIndex) ?? this.defaultTabIndex;
274272
}
275273
this._monitorFocus();
276274
}
277275

276+
ngOnInit() {
277+
// This check needs to happen in `ngOnInit` so the overridden value of
278+
// `basicChipAttrName` coming from base classes can be picked up.
279+
const element = this._elementRef.nativeElement;
280+
this._isBasicChip =
281+
element.hasAttribute(this.basicChipAttrName) ||
282+
element.tagName.toLowerCase() === this.basicChipAttrName;
283+
}
284+
278285
ngAfterViewInit() {
279286
this._textElement = this._elementRef.nativeElement.querySelector('.mat-mdc-chip-action-label')!;
280287

tools/public_api_guard/material/chips.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const MAT_CHIP_TRAILING_ICON: InjectionToken<unknown>;
6161
export const MAT_CHIPS_DEFAULT_OPTIONS: InjectionToken<MatChipsDefaultOptions>;
6262

6363
// @public
64-
export class MatChip extends _MatChipMixinBase implements AfterViewInit, AfterContentInit, CanColor, CanDisableRipple, CanDisable, HasTabIndex, OnDestroy {
64+
export class MatChip extends _MatChipMixinBase implements OnInit, AfterViewInit, AfterContentInit, CanColor, CanDisableRipple, CanDisable, HasTabIndex, OnDestroy {
6565
constructor(_changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef<HTMLElement>, _ngZone: NgZone, _focusMonitor: FocusMonitor, _document: any, animationMode?: string, _globalRippleOptions?: RippleGlobalOptions | undefined, tabIndex?: string);
6666
protected _allLeadingIcons: QueryList<MatChipAvatar>;
6767
protected _allRemoveIcons: QueryList<MatChipRemove>;
@@ -89,7 +89,7 @@ export class MatChip extends _MatChipMixinBase implements AfterViewInit, AfterCo
8989
// (undocumented)
9090
protected _highlighted: boolean;
9191
id: string;
92-
readonly _isBasicChip: boolean;
92+
_isBasicChip: boolean;
9393
readonly _isRippleCentered = false;
9494
_isRippleDisabled(): boolean;
9595
leadingIcon: MatChipAvatar;
@@ -100,6 +100,8 @@ export class MatChip extends _MatChipMixinBase implements AfterViewInit, AfterCo
100100
// (undocumented)
101101
ngOnDestroy(): void;
102102
// (undocumented)
103+
ngOnInit(): void;
104+
// (undocumented)
103105
protected _ngZone: NgZone;
104106
readonly _onBlur: Subject<MatChipEvent>;
105107
readonly _onFocus: Subject<MatChipEvent>;

0 commit comments

Comments
 (0)