<mat-horizontal-stepper [selectedIndex]="selectedStepIndex" #stepper>
@ViewChild('stepper', { static: false }) stepper: MatStepper;
// I personally do this in ngAfterViewInit() method
setTimeout(() => {
this.stepper.steps.forEach((step, idx) => {
step.select = () => {
// Your custom code here
// if you want to change step do execute code below
this.selectedStepIndex = idx;
};
});
});