Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ion-backdrop opacity?

I'm trying to change the opacity of my ion-backdrop from 0.08 to 0.33.

I've tried:

ion-backdrop {
  opacity: 0.33 !important;
}

and setting $popover-ios-background: rgba(0, 0, 0, 0.33);.

Setting the value on ion-backdrop does work but since it's important, it doesn't animate the fade out.

How can I change the opacity of the backdrop?

like image 796
Joe Scotto Avatar asked Mar 15 '18 20:03

Joe Scotto


3 Answers

I know I am a bit late to this party, but now with Ionic 5, you have a CSS selector that will do the job for you. That is mentioned in their documentation as well.

So basically all you could do is, initialize the modal and style it in your SCSS file.

This is my component.ts file:

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
// ModalComponent is just a normal angular component, your path may vary
import { ModalComponent } from '../../modals/modal.component';

@Component({
  selector: 'some-component',
  templateUrl: './some-component.component.html',
  styleUrls: ['./some-component.component.scss']
})
export class SomeComponentComponent {
  constructor(
    private modalController: ModalController,
  ) { }

  async presentModal() {
    const modal = await this.modalController.create({
      component: ModalComponent,
      cssClass: 'modal-class'
    });

    return await modal.present();
  }
}

and my component.scss file:

.modal-class {
  ion-backdrop {
    --backdrop-opacity: 0.33;
  }
}
like image 100
Valer Varga Avatar answered Oct 08 '22 03:10

Valer Varga


I’ve do it using the cssClass property in alertController (Ionic 4)

  async alertError(message: string) {
    const alert = await this.alertController.create({
      cssClass: 'alertClass',
      animated: true,
      header: 'Error',
      message,
      buttons: ['OK']
    });
    await alert.present();
  }

 ion-alert {
   &.alertClass{
     background: rgb(0,0,0,.8);
   }
 }
like image 35
Sergio Suárez Avatar answered Oct 08 '22 01:10

Sergio Suárez


I am guessing that this ion-backdrop question it's related with the Ionic Alert Controller. If that is the case than you need to apply CSS inside the global.scss (Ionic 3) file or theme\variable.scss (Ionic 4/5). This is required because ion-backdrop lives in the app as an Ionic Global Component.

Therefore find the mentioned file inside your Ionic project. It's usually in this directory app > src > global.scss.

Now let's suppose that we have this Alert Controller instanciated in some page class.

...
async serviceErrorAlert() {

    const alert = await this.alertController.create({
      cssClass: 'try-again-alert',
      ...
    });

    await alert.present();
}
...

As you can see this Alert Controller haves a CSS class of try-again-alert. So to add all custom CSS that you want just go the style file and add your own style.

global.scss (Ionic 3):

.try-again-alert {
    --background: rgba(55, 67, 77, 0.9);
}

theme\variable.scss (Ionic 4/5):

I strongly recommend you to use CSS background attribute and rgba() property. With this approach you can now choose the color that you want (first three numbers) and the opacity of the color (fourth number).

like image 33
Grinnex. Avatar answered Oct 08 '22 02:10

Grinnex.