scoped
アラートは、ユーザーに情報を提示したり、Inputを使ってユーザーから情報を収集したりするダイアログである。アラートはアプリのコンテンツの上に表示され、アプリとの対話を再開する前に、ユーザーが手動で解除する必要があります。また、オプションで header、subHeader、message を持つことができます。
ion-alert は、テンプレートに直接コンポーネントを記述して使用することができます。これにより、アラートを表示するために必要なハンドラの数を減らすことができます。
ion-alert の isOpen プロパティは、開発者がアプリケーションの状態からアラートの表示状態を制御することを可能にします。つまり、isOpen を true に設定するとアラートが表示され、isOpen を false に設定するとアラートは解除されます。
isOpen は一方向のデータバインディングを使うため、Alert を閉じても自動的に false には戻りません。ionAlertDidDismiss または didDismiss イベントを受け取ったときに、isOpen を false に設定してください。これにより、ion-alert の内部実装がアプリの状態に強く依存するのを防げます。一方向のバインディングなら、Alert は reactive 変数が返す boolean 値だけを扱います。双方向の場合は、その値に加えて変数自体の有無も扱う必要があり、動作が予測しづらく、デバッグが難しくなることがあります。
Alert を開くタイミングや閉じるタイミングを細かく制御したい場合は、alertController を使えます。
buttons 配列の各ボタンには text を指定し、必要に応じて handler を追加できます。handler が false を返すと、そのボタンをクリックしても Alert は自動で閉じません。ボタンは buttons 配列に追加された順に左から右へ並びます。一番右のボタン(配列の最後のボタン)がメインのボタンです。
オプションで、cancelのような role プロパティをボタンに追加することができます。もし cancel ロールがボタンのいずれかに設定されている場合、バックドロップをタップしてアラートが解除されると、キャンセルロールを持つボタンから handler が起動されます。
Console messages will appear here when logged from the example above.
Alert には複数の入力欄を設けてユーザーに情報の入力を求め、その値をアプリに返すことができます。ラジオボタン、チェックボックス、テキスト入力が使えますが、これらを同じ Alert 内で混在させることはできません。たとえば、すべてをラジオボタンにするか、すべてをチェックボックスにします。ただし、テキスト入力同士なら、url、email、text、textarea などの種類を組み合わせられます。Alert のガイドラインに収まらない複雑なフォームには Modal を使うことをおすすめします。
Alertは scoped encapsulation を使用しており、実行時に各スタイルにクラスを追加することで自動的にCSSをスコープします。CSSでscoped selector をオーバーライドするには、higher specificity selector が必要です。
create メソッドで cssClass にカスタムクラスを渡し、それを使ってホストと内部要素にカスタムスタイルを追加することをお勧めします。このプロパティは、スペースで区切られた複数のクラスを受け付けることもできます。
.alert-wrapper {
background: #e5e5e5;
}
.my-custom-class .alert-wrapper {
background: #e5e5e5;
}
CSS Custom Properties は、個々の要素をターゲットにすることなく、Alert のスタイルに使用することができます。
.my-custom-class {
--background: #e5e5e5;
}
IonicのAngularアプリを構築する場合、スタイルはグローバルなスタイルシートファイルに追加する必要があります。
アラートは、スクリーンリーダーにとってアクセシブルであるために、ariaプロパティを設定しますが、これらのプロパティは、十分な説明がない場合、またはアラートがアプリでどのように使用されているかと一致しない場合は、オーバーライドすることができます。
Ionicは自動的にアラートのroleを、入力やボタンがある場合はalertdialogに、何もない場合はalertに設定します。
Alert に header プロパティが定義されている場合、aria-labelledby 属性は自動的にヘッダーの ID に設定されます。header が定義されていない場合、subHeader 要素がフォールバックとして使用されます。同様に、message プロパティが定義されている場合は、その要素の ID が aria-describedby 属性に自動設定されます。
ARIAの仕様に合わせるために、アラートには message と header または subHeader を含めることを強くお勧めします。もし header や subHeader を含めない場合は、htmlAttributes プロパティを使用して、説明的な aria-label を指定することができます。
- Angular
- Javascript
- React
- Vue
const alert = await this.alertController.create({
message: 'This is an alert with custom aria attributes.',
htmlAttributes: {
'aria-label': 'alert dialog',
},
});
const alert = await this.alertController.create({
message: 'This is an alert with custom aria attributes.',
htmlAttributes: {
'aria-label': 'alert dialog',
},
});
useIonAlert({
message: 'This is an alert with custom aria attributes.',
htmlAttributes: {
'aria-label': 'alert dialog',
},
});
const alert = await alertController.create({
message: 'This is an alert with custom aria attributes.',
htmlAttributes: {
'aria-label': 'alert dialog',
},
});
すべてのARIA属性は、Alert のhtmlAttributesプロパティにカスタム値を定義することで、手動で上書きすることができます。
テキストを含むボタンはスクリーンリーダーによって読み取られます。既存のテキスト以外の説明が必要な場合は、ボタンのhtmlAttributesプロパティにaria-labelを渡して、ボタンにラベルを設定できます。
- Angular
- Javascript
- React
- Vue
const alert = await this.alertController.create({
header: 'Header',
buttons: [
{
text: 'Exit',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
const alert = await this.alertController.create({
header: 'Header',
buttons: [
{
text: 'Exit',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
useIonAlert({
header: 'Header',
buttons: [
{
text: 'Exit',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
const alert = await alertController.create({
header: 'Header',
buttons: [
{
text: 'Exit',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
type AlertButtonOverlayHandler = boolean | void | { [key: string]: any };
interface AlertButton {
text: string;
role?: 'cancel' | 'destructive' | string;
cssClass?: string | string[];
id?: string;
htmlAttributes?: { [key: string]: any };
handler?: (value: any) => AlertButtonOverlayHandler | Promise<AlertButtonOverlayHandler>;
}
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 | カスタムCSSに適用する追加のクラス。複数のクラスを指定する場合は、スペースで区切る必要があります。 |
| 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の場合、アラートは開く。もし false ならば、アラートは閉じます。alertControllerやtriggerプロパティを使用してください。注意: アラートが終了しても isOpen は自動的に false に戻りません。あなたのコードでそれを行う必要があります。 |
| Attribute | is-open |
| Type | boolean |
| Default | false |
| Description | trueの場合、オーバーレイが表示されたときにキーボードが自動的に解除されます。 |
| Attribute | keyboard-close |
| Type | boolean |
| Default | true |
| Description | アラートが解除されたときに使用するアニメーション。 |
| Attribute | undefined |
| Type | ((baseEl: any, opts?: any) => Animation) | undefined |
| Default | undefined |
| Description | アラートに表示されるメインメッセージ。messageには、文字列としてプレーンテキストまたはHTMLのいずれかを指定することができます。通常HTML用に予約されている文字を表示するには、エスケープする必要があります。例えば、<Ionic>は <Ionic> になります:セキュリティ・ドキュメント このプロパティは、カスタムHTMLを文字列として受け付けます。デフォルトでは、コンテンツはプレーンテキストとしてパースされます。カスタムHTMLを使用するには、Ionicの設定で innerHTMLTemplatesEnabled を true に設定する必要があります。 |
| Attribute | message |
| Type | IonicSafeString | string | undefined |
| Default | undefined |
| Description | modeは、どのプラットフォームのスタイルを使用するかを決定します。
This is a virtual property that is set once during initialization and will not update if you change its value after the initial render. |
| Attribute | mode |
| Type | "ios" | "md" |
| Default | undefined |
| Description | アラートの見出しにあるサブタイトルです。タイトルの下に表示されます。 |
| Attribute | sub-header |
| Type | string | undefined |
| Default | undefined |
| Description | trueの場合、アラートは半透明になります。modeが "ios" で、デバイスが backdrop-filter をサポートしている場合にのみ適用されます。 |
| Attribute | translucent |
| Type | boolean |
| Default | false |
| Description | クリックされるとアラートが開くトリガー要素に対応するID。 |
| Attribute | trigger |
| Type | string | undefined |
| Default | undefined |
| Name | Description | Bubbles |
|---|
didDismiss | アラートが解除された後に発行されます。ionAlertDidDismissの略記。 | true |
didPresent | アラートが提示された後に発行されます。ionAlertWillDismissの略記。 | true |
ionAlertDidDismiss | アラートが解除された後に発行されます。 | true |
ionAlertDidPresent | アラートが提示された後に発行されます。 | true |
ionAlertWillDismiss | アラートが解除される前に発行されます。 | true |
ionAlertWillPresent | アラートが提示される前に発行されます。 | true |
willDismiss | アラートが解除される前に発行されます。ionAlertWillDismissの略記。 | true |
willPresent | アラートが提示される前に発行されます。ionAlertWillPresentの略記。 | true |
| Description | アラートのオーバーレイが表示された後に閉じます。オーバーレイがまだ表示されていない場合は何もしません。表示されなかったオーバーレイを DOM から削除するには、remove メソッドを使用してください。 |
| Signature | dismiss(data?: any, role?: string) => Promise<boolean> |
| Parameters | data: Any data to emit in the dismiss events. role: The role of the element that is dismissing the alert. This can be useful in a button handler for determining which button was clicked to dismiss the alert. Some examples include: "cancel", "destructive", "selected", and "backdrop". |
| 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 | アラートの幅 |
| Name | Description |
|---|
--backdrop-opacity | 背景の不透明度 |
--background | 注意喚起の背景 |
--height | アラートの高さ |
--max-height | アラートの最大の高さ |
--max-width | アラートの最大幅 |
--min-height | アラートの最小の高さ |
--min-width | アラートの最小幅 |
--width | アラートの幅 |
No slots available for this component.