Create a custom ionic alert with progress bar using ionic 7 angular

I have create a custom ionic alert with a progress bar using Ionic 7 Angular. but not opening the custom alert

Custom Alert Component Template (custom-alert.component.html):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div class="custom-alert">
<ion-icon name="close-circle-outline" class="icon-error"></ion-icon>
<h2 class="title">Wrong OTP</h2>
<p class="message">Please enter valid 4 digit verification code</p>
<div class="progress-bar">
<div class="progress"></div>
</div>
</div>
</code>
<code><div class="custom-alert"> <ion-icon name="close-circle-outline" class="icon-error"></ion-icon> <h2 class="title">Wrong OTP</h2> <p class="message">Please enter valid 4 digit verification code</p> <div class="progress-bar"> <div class="progress"></div> </div> </div> </code>
<div class="custom-alert">
  <ion-icon name="close-circle-outline" class="icon-error"></ion-icon>
  <h2 class="title">Wrong OTP</h2>
  <p class="message">Please enter valid 4 digit verification code</p>
  <div class="progress-bar">
    <div class="progress"></div>
  </div>
</div>

Custom Alert Component Styles (custom-alert.component.scss):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.custom-alert {
display: flex;
flex-direction: column;
align-items: center;
padding: 16px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
.icon-error {
font-size: 24px;
color: #ff5252;
}
.title {
margin: 8px 0;
font-size: 18px;
font-weight: bold;
color: #00bcd4;
}
.message {
margin-bottom: 16px;
font-size: 14px;
color: #757575;
text-align: center;
}
.progress-bar {
width: 100%;
height: 4px;
background-color: #e0e0e0;
border-radius: 2px;
overflow: hidden;
.progress {
width: 50%; /* Set the progress value as needed */
height: 100%;
background-color: #00bcd4;
animation: progressAnimation 2s linear infinite;
}
@keyframes progressAnimation {
0% {
width: 0;
}
100% {
width: 100%;
}
}
}
}
</code>
<code>.custom-alert { display: flex; flex-direction: column; align-items: center; padding: 16px; background-color: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); .icon-error { font-size: 24px; color: #ff5252; } .title { margin: 8px 0; font-size: 18px; font-weight: bold; color: #00bcd4; } .message { margin-bottom: 16px; font-size: 14px; color: #757575; text-align: center; } .progress-bar { width: 100%; height: 4px; background-color: #e0e0e0; border-radius: 2px; overflow: hidden; .progress { width: 50%; /* Set the progress value as needed */ height: 100%; background-color: #00bcd4; animation: progressAnimation 2s linear infinite; } @keyframes progressAnimation { 0% { width: 0; } 100% { width: 100%; } } } } </code>
.custom-alert {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

  .icon-error {
    font-size: 24px;
    color: #ff5252;
  }

  .title {
    margin: 8px 0;
    font-size: 18px;
    font-weight: bold;
    color: #00bcd4;
  }

  .message {
    margin-bottom: 16px;
    font-size: 14px;
    color: #757575;
    text-align: center;
  }

  .progress-bar {
    width: 100%;
    height: 4px;
    background-color: #e0e0e0;
    border-radius: 2px;
    overflow: hidden;

    .progress {
      width: 50%; /* Set the progress value as needed */
      height: 100%;
      background-color: #00bcd4;
      animation: progressAnimation 2s linear infinite;
    }

    @keyframes progressAnimation {
      0% {
        width: 0;
      }
      100% {
        width: 100%;
      }
    }
  }
}

Custom Alert Component Logic (custom-alert.component.ts):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-custom-alert',
templateUrl: './custom-alert.component.html',
styleUrls: ['./custom-alert.component.scss'],
})
export class CustomAlertComponent implements OnInit {
constructor() {}
ngOnInit() {}
}
</code>
<code>import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-custom-alert', templateUrl: './custom-alert.component.html', styleUrls: ['./custom-alert.component.scss'], }) export class CustomAlertComponent implements OnInit { constructor() {} ngOnInit() {} } </code>
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-custom-alert',
  templateUrl: './custom-alert.component.html',
  styleUrls: ['./custom-alert.component.scss'],
})
export class CustomAlertComponent implements OnInit {
  constructor() {}

  ngOnInit() {}
}

Using the custom alert in your page (example in home.page.ts):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { CustomAlertComponent } from '../components/custom-alert/custom-alert.component';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(private alertController: AlertController) {}
async presentCustomAlert() {
const alert = await this.alertController.create({
header: 'Alert',
cssClass: 'custom-alert-class',
message: '',
buttons: ['OK'],
backdropDismiss: false,
});
await alert.present();
const alertElement = document.querySelector('.custom-alert-class .alert-message');
if (alertElement) {
alertElement.innerHTML = '<app-custom-alert></app-custom-alert>';
}
}
}
</code>
<code>import { Component } from '@angular/core'; import { AlertController } from '@ionic/angular'; import { CustomAlertComponent } from '../components/custom-alert/custom-alert.component'; @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage { constructor(private alertController: AlertController) {} async presentCustomAlert() { const alert = await this.alertController.create({ header: 'Alert', cssClass: 'custom-alert-class', message: '', buttons: ['OK'], backdropDismiss: false, }); await alert.present(); const alertElement = document.querySelector('.custom-alert-class .alert-message'); if (alertElement) { alertElement.innerHTML = '<app-custom-alert></app-custom-alert>'; } } } </code>
import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { CustomAlertComponent } from '../components/custom-alert/custom-alert.component';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  constructor(private alertController: AlertController) {}

  async presentCustomAlert() {
    const alert = await this.alertController.create({
      header: 'Alert',
      cssClass: 'custom-alert-class',
      message: '',
      buttons: ['OK'],
      backdropDismiss: false,
    });

    await alert.present();

    const alertElement = document.querySelector('.custom-alert-class .alert-message');
    if (alertElement) {
      alertElement.innerHTML = '<app-custom-alert></app-custom-alert>';
    }
  }
}

Add CSS to global styles (global.scss):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.custom-alert-class .alert-wrapper {
background: transparent;
box-shadow: none;
}
</code>
<code>.custom-alert-class .alert-wrapper { background: transparent; box-shadow: none; } </code>
.custom-alert-class .alert-wrapper {
  background: transparent;
  box-shadow: none;
}

Update the module (app.module.ts):

Ensure your CustomAlertComponent is declared in your AppModule.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { CustomAlertComponent } from './components/custom-alert/custom-alert.component';
@NgModule({
declarations: [AppComponent, CustomAlertComponent],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
bootstrap: [AppComponent],
})
export class AppModule {}
</code>
<code>import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouteReuseStrategy } from '@angular/router'; import { IonicModule, IonicRouteStrategy } from '@ionic/angular'; import { AppComponent } from './app.component'; import { AppRoutingModule } from './app-routing.module'; import { CustomAlertComponent } from './components/custom-alert/custom-alert.component'; @NgModule({ declarations: [AppComponent, CustomAlertComponent], imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule], providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }], bootstrap: [AppComponent], }) export class AppModule {} </code>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { CustomAlertComponent } from './components/custom-alert/custom-alert.component';

@NgModule({
  declarations: [AppComponent, CustomAlertComponent],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
  bootstrap: [AppComponent],
})
export class AppModule {}

Now you have a custom Ionic alert with a progress bar that matches the design in the image. You can trigger this alert from any part of your app using the presentCustomAlert method.

New contributor

keshav godani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật