Skip to main content
Version: v6

ion-toast

shadow

A Toast is a subtle notification commonly used in modern applications. It can be used to provide feedback about an operation or to display a system message. The toast appears on top of the app's content, and can be dismissed by the app to resume user interaction with the app.

Presenting

Positioning

Toasts can be positioned at the top, bottom or middle of the viewport. The position can be passed upon creation. The possible values are top, bottom and middle. If the position is not specified, the toast will be displayed at the bottom of the viewport.

Controller

Inline

When using Ionic with React or Vue, ion-toast 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 toast is dismissed; it will not be updated automatically.

import React, { useState } from 'react';
import { IonButton, IonToast } from '@ionic/react';

function Example() {
const [showToast, setShowToast] = useState(false);

return (
<>
<IonButton onClick={() => setShowToast(true)}>Show Toast</IonButton>
<IonToast isOpen={showToast} onDidDismiss={() => setShowToast(false)} message="Hello World!" duration={1500} />
</>
);
}

Dismissing

Toasts are intended to be subtle notifications and should not interrupt the user. As a result, user interaction should not be required to dismiss the toast.

The toast can be dismissed automatically after a specific amount of time by passing the number of milliseconds to display it in the duration of the toast options. If a button with a role of "cancel" is added, then that button will dismiss the toast. To dismiss the toast after creation, call the dismiss() method on the instance.

Pressing the hardware back button does not dismiss toasts since they are not supposed to interrupt the user.

The following example demonstrates how to use the buttons property to add a button that automatically dismisses the toast when clicked, as well as how to collect the role of the dismiss event.

Layout

Button containers within the toast can be displayed either on the same line as the message or stacked on separate lines using the layout property. The stacked layout should be used with buttons that have long text values. Additionally, buttons in a stacked toast layout can use a side value of either start or end, but not both.

Icons

An icon can be added next to the content inside of the toast. In general, icons in toasts should be used to add additional style or context, not to grab the user's attention or elevate the priority of the toast. If you wish to convey a higher priority message to the user or guarantee a response, we recommend using an Alert instead.

Theming

Interfaces

ToastButton

interface ToastButton {
text?: string;
icon?: string;
side?: 'start' | 'end';
role?: 'cancel' | string;
cssClass?: string | string[];
handler?: () => boolean | void | Promise<boolean | void>;
}

ToastOptions

interface ToastOptions {
header?: string;
message?: string | IonicSafeString;
cssClass?: string | string[];
duration?: number;
buttons?: (ToastButton | string)[];
position?: 'top' | 'bottom' | 'middle';
translucent?: boolean;
animated?: boolean;
icon?: string;
htmlAttributes?: { [key: string]: any };

color?: Color;
mode?: Mode;
keyboardClose?: boolean;
id?: string;

enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
}

Accessibility

Focus Management

Toasts are intended to be subtle notifications and are not intended to interrupt the user. User interaction should not be required to dismiss the toast. As a result, focus is not automatically moved to a toast when one is presented.

Screen Readers

ion-toast has aria-live="polite" and aria-atomic="true" set by default.

aria-live causes screen readers to announce the content of the toast when it is updated. However, since the attribute is set to 'polite', screen readers generally do not interrupt the current task. Developers can customize this behavior by using the htmlAttributes property to set aria-live to 'assertive'. This will cause screen readers to immediately notify the user when a toast is updated, potentially interrupting any previous updates.

aria-atomic="true" is set to ensure that the entire toast is announced as a single unit. This is useful when dynamically updating the content of the toast as it prevents screen readers from announcing only the content that has changed.

Tips

While this is not a complete list, here are some guidelines to follow when using toasts.

  • Do not require user interaction to dismiss toasts. For example, having a "Dismiss" button in the toast is fine, but the toast should also automatically dismiss on its own after a timeout period. If you need user interaction for a notification, consider using ion-alert instead.

  • Avoid opening multiple toasts in quick succession. If aria-live is set to 'assertive', screen readers may interrupt the reading of the current task to announce the new toast, causing the context of the previous toast to be lost.

  • For toasts with long messages, consider adjusting the duration property to allow users enough time to read the content of the toast.

Properties

animated

Descriptiontrueの場合、トーストはアニメーションします。
Attributeanimated
Typeboolean
Defaulttrue

buttons

Descriptionトースト用のボタンがずらり。
Attributeundefined
Type(string | ToastButton)[] | undefined
Defaultundefined

color

Descriptionアプリケーションのカラーパレットから使用する色を指定します。デフォルトのオプションは以下の通りです。 "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", と "dark" です.色に関する詳しい情報は theming を参照してください。
Attributecolor
Type"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string & Record<never, never> | undefined
Defaultundefined

cssClass

DescriptionカスタムCSSに適用する追加のクラス。複数のクラスを指定する場合は、スペースで区切る必要があります。
Attributecss-class
Typestring | string[] | undefined
Defaultundefined

duration

Descriptionトーストを隠すまでに何ミリ秒待つかを指定します。デフォルトでは、dismiss()が呼ばれるまで表示されます。
Attributeduration
Typenumber
Defaultconfig.getNumber('toastDuration', 0)

enterAnimation

Description乾杯の音頭をとるときに使うアニメーションです。
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined
Descriptionトーストに表示されるヘッダー。
Attributeheader
Typestring | undefined
Defaultundefined

htmlAttributes

Descriptionトーストに渡す追加の属性。
Attributeundefined
Typeundefined | { [key: string]: any; }
Defaultundefined

icon

Description表示するアイコンの名前、または有効なSVGファイルへのパスを指定します。ion-icon`を参照。https://ionic.io/ionicons
Attributeicon
Typestring | undefined
Defaultundefined

keyboardClose

Descriptiontrueの場合、オーバーレイが表示されたときにキーボードが自動的に解除されます。
Attributekeyboard-close
Typeboolean
Defaultfalse

layout

Descriptionトーストのメッセージやボタンの配置を定義します。'baseline'を指定します。メッセージとボタンは同じ行に表示されます。メッセージテキストはメッセージコンテナ内で折り返すことができます。'stacked':ボタンコンテナとメッセージが重なるように表示されます。ボタンに長いテキストがある場合に使用します。
Attributelayout
Type"baseline" | "stacked"
Default'baseline'

leaveAnimation

Descriptionトーストの解散時に使用するアニメーションです。
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

message

Description乾杯の際に表示するメッセージ
Attributemessage
TypeIonicSafeString | string | undefined
Defaultundefined

mode

Descriptionmodeは、どのプラットフォームのスタイルを使用するかを決定します。
Attributemode
Type"ios" | "md"
Defaultundefined

position

Description画面上のトーストの位置です。
Attributeposition
Type"bottom" | "middle" | "top"
Default'bottom'

translucent

Descriptiontrueの場合、トーストは半透明になります。modeが "ios" で、デバイスが backdrop-filter をサポートしている場合にのみ適用されます。
Attributetranslucent
Typeboolean
Defaultfalse

Events

NameDescription
ionToastDidDismissトーストが解散した後に発行されます。
ionToastDidPresentトーストが提示された後に発します。
ionToastWillDismiss乾杯が解散する前に発します。
ionToastWillPresentトーストが提示される前に発行されます。

Methods

dismiss

Descriptionトーストのオーバーレイが提示された後、それを解除します。
Signaturedismiss(data?: any, role?: string) => Promise<boolean>

onDidDismiss

Descriptionトーストが解散したことを解決するPromiseを返します。
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

Descriptionトーストが解散するタイミングを解決するPromiseを返します。
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

Descriptionトーストのオーバーレイを作成した後に提示します。
Signaturepresent() => Promise<void>

CSS Shadow Parts

NameDescription
buttonトーストの内側に表示される任意のボタン要素。
containerすべての子要素を包む要素。
header乾杯のヘッダーテキストです。
iconトーストの内容の横に表示されるアイコンです。
message乾杯の音頭の本文です。

CSS Custom Properties

NameDescription
--background乾杯の背景
--border-colorトーストのボーダーカラー
--border-radiusトーストのボーダー半径
--border-styleトーストのボーダースタイル
--border-widthトーストのボーダー幅
--box-shadow乾杯の箱影
--button-colorボタンテキストの色
--colorトーストの文字色
--end方向が左から右の場合は右から、方向が右から左の場合は左から位置すること
--heightトーストの高さ
--max-heightトーストの最大の高さ
--max-widthトーストの最大幅
--min-heightトーストの最小の高さ
--min-widthトーストの最小幅
--start方向が左から右の場合は左から、方向が右から左の場合は右から位置すること
--white-space乾杯メッセージのホワイトスペース
--widthトーストの幅

Slots

No slots available for this component.