In this guide, we’ll learn how to build our own custom stepper using the CDK stepper. This approach gives you complete control over the appearance and behavior of your stepper.
Create a new Angular component that extends CdkStepper:
@Component({ selector: 'app-custom-stepper', templateUrl: './custom-stepper.component.html', styleUrl: './custom-stepper.component.css', // This custom stepper provides itself as CdkStepper so that it can be recognized // by other components. providers: [{ provide: CdkStepper, useExisting: CustomStepperComponent }]})export class CustomStepperComponent extends CdkStepper { onClick(index: number): void { this.selectedIndex = index; }}
After extending CdkStepper, you can access properties like linear, selectedIndex, and steps from the base class.
2
Create the template
Design the HTML template for your custom stepper:
<section class="container"> <header><h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2></header> <div [style.display]="selected ? 'block' : 'none'"> <!-- Content from the CdkStep is projected here --> <ng-container [ngTemplateOutlet]="selected.content"></ng-container> </div> <footer class="step-navigation-bar"> <button class="nav-button" cdkStepperPrevious>←</button> @for (step of steps; track step; let i = $index) { <button class="step" [class.active]="selectedIndex === i" (click)="onClick(i)"> Step {{i + 1}} </button> } <button class="nav-button" cdkStepperNext>→</button> </footer></section>
Now you can use your custom stepper component and fill it with steps:
<app-custom-stepper> <cdk-step><p>This is any content of "Step 1"</p></cdk-step> <cdk-step><p>This is any content of "Step 2"</p></cdk-step></app-custom-stepper>
Each step needs to be wrapped inside a <cdk-step> tag.