I want to customize my alerts in Ionic 2. I know that I can do it globally in the variables.scss, but I want to modify a specific one, in a specific page.
I tried cssClass in the alert code, I tried other different things, that work, but globally, not for a specific one.
Is there any way to do it?
Edit all your AlertController.create methods to look like this:
const alert = this.alertCtrl.create({
title: title,
subTitle: msg,
buttons: ['OK'],
cssClass: 'alertCustomCss' // <- added this
});
And add this to app.scss:
.alertCustomCss {
// text color for basic alerts
color: white;
// alert modal page bg color. Warning this will remove the alert transparency
background-color: color($colors, dark, base);
button {
color: white !important; // button text color
background-color: color($colors, secondary, base) !important;
//button bg color
}
.alert-message {
color: white; // text color for confirm alerts
}
.alert-wrapper {
background-color: color($colors, dark, base); // bg color of alert content
}
}
Adding the styles in app.css and calling it in page.ts
showAlert() {
let alert = this.alertCtrl.create({
title: 'New Friend!',
subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
buttons: ['OK'],
cssClass: 'alertCustomCss'
});
alert.present();
}
In App.css
.alertCustomCss{
background-color: white;
color: blue;
button{
color: blue;
}
}
If you created your specific page (let's call it Sample1) with the ionic CLI command ionic g page Sample1, you will find in your project a directory called Sample1 with 3 files: Sample1.html, Sample1.ts and Sample1.scss.
In Sample1.scss you will find:
sample1-page {
}
In that place you must define your custom css class or redefine the ionic element style and all your styles will have scope only onto the Sample1 page.
Hope this could help you
UPDATE
As Duannx mentions the alert components are not child of your page so if you put the css class into the specific page .scss file it will not be applied to the alert but if you put it into app.scss it will be applied. So this is an example:
app.scss
.alertCustomCss{
background-color: white;
color: blue;
button{
color: blue;
}
}
Sample1.html
<button ion-button block outline (click)="showAlert()">Alert</button>
<button ion-button block outline (click)="showAlert2()">Alert2</button>
Sample1.ts
showAlert() {
let alert = this.alertCtrl.create({
title: 'New Friend!',
subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
buttons: ['OK'],
cssClass: 'alertCustomCss'
});
alert.present();
}
showAlert2() {
let alert = this.alertCtrl.create({
title: 'New Friend!',
subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
buttons: ['OK']
});
alert.present();
}
Now you will see that the button "Alert" will show a customized alert while the button "Alert2" will show the alter with the default css style
Just add any name you want in cssClass of alert as i named alertCustomCss
logOut() {
this.alrtCtrl.create({
title: "Log out ?",
message: "Are you sure to logout?",
buttons: [
{
text: "Cancel",
role: 'cancel',
handler: () => {
//something to do
}
},
{
text: "Confirm",
handler: () => {
//log Out
}
}
],
cssClass: 'alertCustomCss'
}).present();
Adding Styles to alertCustomCss class
.alertCustomCss{
background-color: red;
color: white;
button{
color: green!important;
}
.alert-wrapper{
background: yellow;
}
.alert-message {
color: skyblue;
}
.alert-title{
color: black;
}
}
View Image after applying above style => Image Link
Though this is coming from Ionic 3, but you could always put the style outside of the page-something block from your something.scss file if you still want to keep local changes in your local files instead of the app.scss.
.alert-style{
padding-left: 16px;
}
page-something{
}
Related
I have an action button in a data grid (pro):
<GridActionsCellItem icon={ <EditIcon/> } />
how can I put color="inhereted" or color="#123" to all my action icon buttons using a theme?
I want to make all my action icon buttons to default to the same color.
If you want to set the change in your theme the code below should work for you.
MuiDataGrid: {
root: {
'& .MuiDataGrid-actionsCell': {
'& .MuiSvgIcon-root': {
color: 'red'
}
}
}
}
Or you can use styled components like this
const GridActionsCellItemStyled = styled(GridActionsCellItem)(({ theme }) => ({
"& .MuiSvgIcon-root": {
color: "red"
}
}));
<GridActionsCellItemStyled
icon={<EditIcon />}
label="Edit"
/>,
I want to give alert controller style in ionic 4.these is my demo code,
async presentalert() {
const alert = await this.alertCtrl.create({
header: ' DO YOU WANT TO CANCEL',
message: 'DO YOU WANT TO CANCEL',
cssClass: 'alertCancel',
mode: 'ios',
buttons: [
{
text: 'NO',
role: 'cancel',
cssClass: 'alertButton',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'YES',
cssClass: 'alertButton',
handler: () => {
console.log('Confirm Okay');
}
}
]
})
await alert.present();
}
and i tried to apply scss in global.scss
.alertCancel{
--background: red;
}
.alertButton {
background-color: white !important;
}
I have tried every possible way to give css in alert controller but it s not working so please help me I am stuck here.
--background is a css variable of the component ion-alert, therefore do the following in the variables.scss
ion-alert
{
--background: red !important;
}
For reference:
https://ionicframework.com/docs/theming/css-variables#ionic-variables
If you wish to style your alert using defined cssClass instead of the component ion-alert then add the following code to your variables.scss
.alertCancel{
--background: red;
button.alertButton{
color: white;
}
}
Result
Since, ion-alert do not provide CSS Custom Properties to style alert buttons. I suggest you to use defined cssClass to style your alert rather than ion-alert when you want to style your alert buttons too.
I was having the same issue and this worked for me without needing to use "!important"
in the variables.scss I added this:
#media (prefers-color-scheme: light){
body{
--ion-background-color: rgb(240, 248, 240);
}
ion-alert{
--ion-background-color: #ffffff;
}
}
This allows having a general color for the background of the app but changes it for every alert (on this case, on the light theme) :)
I have an alert where I have some text and one ionicon and I want to change its color and adjust its size and position (center) and I created a css class tried to apply it but it doesn't work, even thought HTML seems to work, I used the <b> tag just fine.
My code:
async presentAlertError(data) {
const alert = await this.alertController.create({
header: 'Confirmation',
message: '<ion-icon name="close" class="custom-icon-notfound" > </ion-icon>' + "Registration " + data.statusText,
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (aux) => {
console.log('Confirm Cancel: aux');
}
}, {
text: 'Scan Next',
handler: () => {
this.scanCode();
console.log('Confirm ');
}
}
]
});
await alert.present()
Should I do this in a different way?
The ion-alert isnt displayed in the home page or (whatever page/component you have the alert on) shadow DOM so styling wont affect it if you add it there, you have to add the styles to an element which is in the same scope as the alert. I added the class to variables.scss as that has the highest scope:
variables.scss-
.secondary {
color: #A0A !important;
font-size: 2em !important;
}
.custom-icon-notfound {color: red}
also need added !important so it ignores its previous styling.
I want to include the primary, secondary and tertiary colors I used in the app I am working on and I haven't found a way to do it. but after looking on here and after trying to find an answer on google but to no luck so I am asking here.
I tried looking in the official docs and didn't find anything related to the alert that it uses.
<ion-select formControlName="currency">
<ion-select-option value="btc" checked="true">Bitcoin</ion-select-option>
<ion-select-option value="ethereum">Ethereum</ion-select-option>
</ion-select>
It currently looks like this (I can't post images yet so I have to include the link directly)
https://i.imgur.com/aMGFhHZ.png
while the ionic colors are used for primary, secondary and tertiary are used like this
https://i.imgur.com/TlUP79g.png
Is there a way to do this without creating a custom alert?
that goes in your .ts file
async presentAlert(){
const alert = await this.alertController.create({
header: "My Header",
message:
"this is my message.",
cssClass: "CUSTOMCSS", <<<< place the class name there
buttons: [
{
text: "Cancel",
role: "cancel",
cssClass: "secondary",
handler: blah => {
// Do something
}
},
{
text: "Sync",
handler: () => {
// Do something
}
}
]
});
await alert.present();
}
then in your global.scss
.CUSTOMCSS{
*{
ion-item {
--background: var(--ion-color-primary);
color: var(--ion-color-dark);
}
}
}
I'm trying to make the default Ionic Alerts larger. I'm developing an app that needs to have easy touch points and the default alerts are too small for what I'm needing.
I've tried enlarging the font as well as expanding the width of the alerts but nothing seems to actually make the alerts larger.
Any easy/best ways to do this?
AlertController supports custom classes which could be placed in your component's scss file and there you can do necessary alterations.
For example in your component's ts file you can have this method that creates alert with reference to custom class "scaledAlert":
delete() {
let confirm = this.alertCtrl.create({
title: "Are You Sure?",
cssClass: "scaledAlert",
message: "this will remove image from your image gallery",
buttons: [
{
text: "Cancel",
handler: () => {
console.log("Canceled delete");
}
},
{
text: "Confirm",
handler: () => {
console.log("deleting...");
this.deleteImageFromGallery(this.image)
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
this.viewCtrl.dismiss();
}
}
]
});
confirm.present();
}
Now in the scss file you add class to style as you need to scale the controller, such class goes after your page or component:
home-page {
.item {
min-height: 2rem; /* <- this can be whatever you need */
}
ion-label {
margin: 0px 0px 0px 0;
}
.item-content {
padding-top: 0px;
padding-bottom: 0px;
margin-top: -12px;
margin-bottom: -12px;
height: 50px;
}
}
.scaledAlert {
transform: scale(1.5);
}
Here I used just naive "scale" function which may require you to add some cross browser compatible versions of it. But you should achieve what you want with it (it worked in my app without issues).
Alternatively you can override default styles using saas variables: https://ionicframework.com/docs/api/components/alert/AlertController/#sass-variables
You will have to alter them in theme\variables.scss" which is located in your project's folder
See more here: https://ionicframework.com/docs/theming/overriding-ionic-variables/
And third option is indeed to check elements' style via devtool and attempt to override those classes. But I don't like that way, feels a bit more hacky.
Some of the styles for alert are not getting updated if written in component SCSS file. The styles need to be written in the global scss file.