Skip to content

Commit d260206

Browse files
committed
fix(material/core): icons not aligned inside mat-option
Fixes that icons projected inside an MDC-based `mat-option` weren't centered and didn't have the correct margin like the non-MDC version. Fixes #26024.
1 parent 57676e4 commit d260206

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/material/core/option/option.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<mat-pseudo-checkbox *ngIf="multiple" class="mat-mdc-option-pseudo-checkbox"
22
[state]="selected ? 'checked' : 'unchecked'" [disabled]="disabled"></mat-pseudo-checkbox>
33

4-
<span class="mdc-list-item__primary-text"><ng-content></ng-content></span>
4+
<ng-content select="mat-icon"></ng-content>
5+
6+
<span class="mdc-list-item__primary-text" #text><ng-content></ng-content></span>
57

68
<!-- See a11y notes inside optgroup.ts for context behind this element. -->
79
<span class="cdk-visually-hidden" *ngIf="group && group._inert">({{ group.label }})</span>

src/material/core/option/option.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
}
4747
}
4848

49+
.mat-icon,
4950
.mat-pseudo-checkbox {
5051
margin-right: mdc-list-variables.$side-padding;
52+
flex-shrink: 0;
5153

5254
[dir='rtl'] & {
5355
margin-right: 0;

src/material/core/option/option.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Output,
2525
EventEmitter,
2626
QueryList,
27+
ViewChild,
2728
} from '@angular/core';
2829
import {Subject} from 'rxjs';
2930
import {MatOptgroup, MAT_OPTGROUP, _MatOptgroupBase} from './optgroup';
@@ -86,6 +87,9 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
8687
// tslint:disable-next-line:no-output-on-prefix
8788
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange<T>>();
8889

90+
/** Element containing the option's text. */
91+
@ViewChild('text', {static: true}) _text: ElementRef<HTMLElement> | undefined;
92+
8993
/** Emits when the state of the option changes and any parents have to be notified. */
9094
readonly _stateChanges = new Subject<void>();
9195

@@ -112,7 +116,7 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
112116
*/
113117
get viewValue(): string {
114118
// TODO(kara): Add input property alternative for node envs.
115-
return (this._getHostElement().textContent || '').trim();
119+
return (this._text?.nativeElement.textContent || '').trim();
116120
}
117121

118122
/** Selects the option. */

tools/public_api_guard/material/core.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class _MatOptgroupBase extends _MatOptgroupMixinBase implements CanDisabl
263263
export class MatOption<T = any> extends _MatOptionBase<T> {
264264
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
265265
// (undocumented)
266-
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"], false, never>;
266+
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["mat-icon", "*"], false, never>;
267267
// (undocumented)
268268
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
269269
}
@@ -297,6 +297,7 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
297297
setActiveStyles(): void;
298298
setInactiveStyles(): void;
299299
readonly _stateChanges: Subject<void>;
300+
_text: ElementRef<HTMLElement> | undefined;
300301
value: T;
301302
get viewValue(): string;
302303
// (undocumented)

0 commit comments

Comments
 (0)