One possible method to achieve this is:
HTML:
<ng-template #clearRef>
@if (value) {
<svg
class="text-danger"
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
(click)="$event.stopPropagation(); control.reset()"
>
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
}
</ng-template>
<ng-template #chevrondownRef>
<svg class="chevron-down" [class.animate]="isPanelOpen" xmlns="http://www.w3.org/2000/svg" width="20" height="20">
<path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" />
</svg>
</ng-template>
CSS:
.mat-mdc-form-field-type-mat-select {
.mat-mdc-form-field-infix {
--mat-form-field-container-vertical-padding: 0;
.mat-mdc-select-arrow-wrapper {
flex-direction: row-reverse;
gap: 0.5rem;
}
}
}
.chevron-down {
transition: all 0.25s ease;
transform: translate(-50%, -50%) rotate(0deg);
&.animate {
transform: translate(-50%, -50%) rotate(180deg);
}
}
TS:
private vcr = inject(ViewContainerRef);
@ViewChild(MatSelect) matSelect!: MatSelect;
@ViewChild("clearRef") clearRef!: TemplateRef<HTMLElement>;
@ViewChild("chevrondownRef") chevrondownRef!: TemplateRef<HTMLElement>;
ngAfterViewInit(): void {
//Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
//Add 'implements AfterViewInit' to the class.
const mdcSelectArrowWrapper = document.querySelector(".mat-mdc-select-arrow-wrapper");
const mdcSelectArrow = this.matSelect._elementRef.nativeElement.querySelector(
".mat-mdc-select-arrow-wrapper .mat-mdc-select-arrow",
) as HTMLElement;
mdcSelectArrowWrapper?.appendChild(this.vcr.createEmbeddedView(this.clearRef).rootNodes[0]);
mdcSelectArrow.replaceChildren(this.vcr.createEmbeddedView(this.chevrondownRef).rootNodes[0]);
}
Outcome:
https://i.sstatic.net/LrQ4IMdr.png