Skip to content

fix(material/core): icons not aligned inside mat-option #26054

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
Nov 30, 2022
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
4 changes: 3 additions & 1 deletion src/material/core/option/option.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<mat-pseudo-checkbox *ngIf="multiple" class="mat-mdc-option-pseudo-checkbox"
[state]="selected ? 'checked' : 'unchecked'" [disabled]="disabled"></mat-pseudo-checkbox>

<span class="mdc-list-item__primary-text"><ng-content></ng-content></span>
<ng-content select="mat-icon"></ng-content>

<span class="mdc-list-item__primary-text" #text><ng-content></ng-content></span>

<!-- See a11y notes inside optgroup.ts for context behind this element. -->
<span class="cdk-visually-hidden" *ngIf="group && group._inert">({{ group.label }})</span>
Expand Down
2 changes: 2 additions & 0 deletions src/material/core/option/option.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
}
}

.mat-icon,
.mat-pseudo-checkbox {
margin-right: mdc-list-variables.$side-padding;
flex-shrink: 0;

[dir='rtl'] & {
margin-right: 0;
Expand Down
6 changes: 5 additions & 1 deletion src/material/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Output,
EventEmitter,
QueryList,
ViewChild,
} from '@angular/core';
import {Subject} from 'rxjs';
import {MatOptgroup, MAT_OPTGROUP, _MatOptgroupBase} from './optgroup';
Expand Down Expand Up @@ -86,6 +87,9 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
// tslint:disable-next-line:no-output-on-prefix
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange<T>>();

/** Element containing the option's text. */
@ViewChild('text', {static: true}) _text: ElementRef<HTMLElement> | undefined;

/** Emits when the state of the option changes and any parents have to be notified. */
readonly _stateChanges = new Subject<void>();

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

/** Selects the option. */
Expand Down
2 changes: 1 addition & 1 deletion src/material/legacy-core/option/option.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<mat-pseudo-checkbox *ngIf="multiple" class="mat-option-pseudo-checkbox"
[state]="selected ? 'checked' : 'unchecked'" [disabled]="disabled"></mat-pseudo-checkbox>

<span class="mat-option-text"><ng-content></ng-content></span>
<span class="mat-option-text" #text><ng-content></ng-content></span>

<!-- See a11y notes inside optgroup.ts for context behind this element. -->
<span class="cdk-visually-hidden" *ngIf="group && group._inert">({{ group.label }})</span>
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class _MatOptgroupBase extends _MatOptgroupMixinBase implements CanDisabl
export class MatOption<T = any> extends _MatOptionBase<T> {
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"], false, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["mat-icon", "*"], false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
}
Expand Down Expand Up @@ -297,6 +297,7 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
setActiveStyles(): void;
setInactiveStyles(): void;
readonly _stateChanges: Subject<void>;
_text: ElementRef<HTMLElement> | undefined;
value: T;
get viewValue(): string;
// (undocumented)
Expand Down