scoped
An Alert is a dialog that presents users with information or collects information from the user using inputs. An alert appears on top of the app's content, and must be manually dismissed by the user before they can resume interaction with the app. It can also optionally have a header
, subHeader
and message
.
When using Ionic with React or Vue, ion-alert
can also be placed directly in the template through use of the isOpen
property. Note that isOpen
must be set to false
manually when the alert is dismissed; it will not be updated automatically.
import React, { useState } from 'react';
import { IonAlert, IonButton, IonContent } from '@ionic/react';
function Example() {
const [showAlert, setShowAlert] = useState(false);
return (
<IonContent>
<IonButton onClick={() => setShowAlert(true)}>Click Me</IonButton>
<IonAlert
isOpen={showAlert}
onDidDismiss={() => setShowAlert(false)}
header="Alert"
subHeader="Important message"
message="This is an alert!"
buttons={['OK']}
/>
</IonContent>
);
}
<template>
<ion-content>
<ion-button @click="setOpen(true)">Show Alert</ion-button>
<ion-alert
:is-open="isOpenRef"
header="Alert"
sub-header="Important message"
message="This is an alert!"
:buttons="['OK']"
@didDismiss="setOpen(false)"
></ion-alert>
</ion-content>
</template>
<script lang="ts">
import { IonAlert, IonButton, IonContent } from '@ionic/vue';
import { defineComponent, ref } from 'vue';
export default defineComponent({
components: { IonAlert, IonButton },
setup() {
const isOpenRef = ref(false);
const setOpen = (state: boolean) => (isOpenRef.value = state);
return { isOpenRef, setOpen };
},
});
</script>
In the array of buttons
, each button includes properties for its text
, and optionally a handler
. If a handler returns false
then the alert will not automatically be dismissed when the button is clicked. All buttons will show up in the order they have been added to the buttons
array from left to right. Note: The right most button (the last one in the array) is the main button.
Optionally, a role
property can be added to a button, such as cancel
. If a cancel
role is on one of the buttons, then if the alert is dismissed by tapping the backdrop, then it will fire the handler from the button with a cancel role.
Alerts can also include several different inputs whose data can be passed back to the app. Inputs can be used as a simple way to prompt users for information. Radios, checkboxes and text inputs are all accepted, but they cannot be mixed. For example, an alert could have all radio button inputs, or all checkbox inputs, but the same alert cannot mix radio and checkbox inputs. Do note however, different types of "text" inputs can be mixed, such as url
, email
, text
, textarea
etc. If you require a complex form UI which doesn't fit within the guidelines of an alert then we recommend building the form within a modal instead.
Alert uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector.
We recommend passing a custom class to cssClass
in the create
method and using that to add custom styles to the host and inner elements. This property can also accept multiple classes separated by spaces.
.alert-wrapper {
background: #e5e5e5;
}
.my-custom-class .alert-wrapper {
background: #e5e5e5;
}
Any of the defined CSS Custom Properties can be used to style the Alert without needing to target individual elements:
.my-custom-class {
--background: #e5e5e5;
}
If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file.
Ionic automatically sets the Alert's role
to either alertdialog
if there are any inputs or buttons included, or alert
if there are none.
If the header
property is defined for the Alert, the aria-labelledby
attribute will be automatically set to the header's ID. The subHeader
element will be used as a fallback if header
is not defined. Similarly, the aria-describedby
attribute will be automatically set to the ID of the message
element if that property is defined.
It is strongly recommended that your Alert have a message
, as well as either a header
or subHeader
, in order to align with the ARIA spec. If you choose not to include a header
or subHeader
, an alternative is to provide a descriptive aria-label
using the htmlAttributes
property.
All ARIA attributes can be manually overwritten by defining custom values in the htmlAttributes
property of the Alert.
interface AlertButton {
text: string;
role?: 'cancel' | 'destructive' | string;
cssClass?: string | string[];
handler?: (value: any) => boolean | void | { [key: string]: any };
}
interface AlertInput {
type?: TextFieldTypes | 'checkbox' | 'radio' | 'textarea';
name?: string;
placeholder?: string;
value?: any;
label?: string;
checked?: boolean;
disabled?: boolean;
id?: string;
handler?: (input: AlertInput) => void;
min?: string | number;
max?: string | number;
cssClass?: string | string[];
attributes?: { [key: string]: any };
tabindex?: number;
}
interface AlertOptions {
header?: string;
subHeader?: string;
message?: string | IonicSafeString;
cssClass?: string | string[];
inputs?: AlertInput[];
buttons?: (AlertButton | string)[];
backdropDismiss?: boolean;
translucent?: boolean;
animated?: boolean;
htmlAttributes?: { [key: string]: any };
mode?: Mode;
keyboardClose?: boolean;
id?: string;
enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
}
Description | true の場合、アラートはアニメーションで表示され ます。 |
Attribute | animated |
Type | boolean |
Default | true |
Description | true の場合、バックドロップがクリックされるとアラートが解除される。 |
Attribute | backdrop-dismiss |
Type | boolean |
Default | true |
Description | アラートに追加されるボタンの配列。 |
Attribute | undefined |
Type | (string | AlertButton)[] |
Default | [] |
Description | Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces. |
Attribute | css-class |
Type | string | string[] | undefined |
Default | undefined |
Description | アラート提示時に使用するアニメーションです。 |
Attribute | undefined |
Type | ((baseEl: any, opts?: any) => Animation) | undefined |
Default | undefined |
Description | アラートの見出しにあるメインタイトルです。 |
Attribute | header |
Type | string | undefined |
Default | undefined |
Description | アラートに渡す追加属性。 |
Attribute | undefined |
Type | undefined | { [key: string]: any; } |
Default | undefined |
Description | アラートに表示するInputの配列。 |
Attribute | undefined |
Type | AlertInput[] |
Default | [] |
Description | true の場合、オーバーレイが表示されたときにキーボードが自動的に解除されます。 |
Attribute | keyboard-close |
Type | boolean |
Default | true |
Description | アラートが解除されたときに使用するアニメーション。 |
Attribute | undefined |
Type | ((baseEl: any, opts?: any) => Animation) | undefined |
Default | undefined |
Description | The main message to be displayed in the alert. message can accept either plaintext or HTML as a string. To display characters normally reserved for HTML, they must be escaped. For example <Ionic> would become <Ionic>
For more information: Security Documentation
This property accepts custom HTML as a string. Developers who only want to pass plain text can disable the custom HTML functionality by setting innerHTMLTemplatesEnabled: false in the Ionic config. |
Attribute | message |
Type | IonicSafeString | string | undefined |
Default | undefined |
Description | modeは、どのプラットフォームのスタイルを使用するかを決定します。 |
Attribute | mode |
Type | "ios" | "md" |
Default | undefined |
Description | アラートの見出しにあるサブタイトルです。タ イトルの下に表示されます。 |
Attribute | sub-header |
Type | string | undefined |
Default | undefined |
Description | If true , the alert will be translucent. Only applies when the mode is "ios" and the device supports backdrop-filter . |
Attribute | translucent |
Type | boolean |
Default | false |
Name | Description |
---|
ionAlertDidDismiss | アラートが解除された後に発行されます。 |
ionAlertDidPresent | アラートが提示された後に発行されます。 |
ionAlertWillDismiss | アラートが解除される前に発行されます。 |
ionAlertWillPresent | アラートが提示される前に発行されます。 |
Description | アラートオーバーレイが表示された後、解除します。 |
Signature | dismiss(data?: any, role?: string) => Promise<boolean> |
Description | アラートが解除されたことを解決するPromiseを返します。 |
Signature | onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
Description | アラートが解除されるタイミングを解決するPromiseを返します。 |
Signature | onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
Description | アラートオーバーレイを作成した後に提示します。 |
Signature | present() => Promise<void> |
No CSS shadow parts available for this component.
Name | Description |
---|
--backdrop-opacity | 背景の不透明度 |
--background | 注意喚起の背景 |
--height | アラートの高さ |
--max-height | アラートの最大の高さ |
--max-width | アラートの最大幅 |
--min-height | アラートの最小の高さ |
--min-width | アラートの最小幅 |
--width | アラートの幅 |
No slots available for this component.