shadow
Toast(消息提示)是一种现代应用中常用的小型通知。它可以用于提供有关操作的反馈或显示系统消息。Toast 显示在应用内容的上方,应用可以将其关闭以恢复用户与应用的交互。
ion-toast 可以直接在模板中编写组件来使用。这减少了你需要连接以呈现 toast 的处理程序数量。
ion-toast 上的 isOpen 属性允许开发者从应用状态控制 toast 的呈现状态。这意味着当 isOpen 设置为 true 时,toast 将显示;当 isOpen 设置为 false 时,toast 将关闭。
isOpen 使用单向数据绑定,这意味着当 toast 关闭时,它不会自动设置为 false。开发者应监听 ionToastDidDismiss 或 didDismiss 事件,并将 isOpen 设置为 false。这样做的原因是防止 ion-toast 的内部实现与应用状态紧密耦合。使用单向数据绑定时,toast 只需要关心响应式变量提供的布尔值。而使用双向数据绑定时,toast 需要同时关心布尔值和响应式变量本身的存在性,这可能导致不确定的行为并使应用更难调试。
Toast 旨在作为小型的通知,不应中断用户。因此,关闭 toast 不应需要用户交互。
可以通过在 toast 选项的 duration 中传递要显示的毫秒数,使 toast 在特定时间后自动关闭。如果添加了带有 "cancel" 角色的按钮,则该按钮将关闭 toast。要在创建后关闭 toast,请调用实例上的 dismiss() 方法。
按下硬件返回按钮不会关闭 toast,因为它们不应中断用户。
以下示例演示如何使用 buttons 属性添加一个在点击时自动关闭 toast 的按钮,以及如何收集关闭事件的 role。
控制台消息将在上方示例中调用 console.log 时显示在此处。
Toast 可以定位在视口的顶部、底部或中间。位置可以在创建时传递。可能的值是 top、bottom 和 middle。如果未指定位置,toast 将显示在视口底部。
如果 toast 与导航元素(如头部、底部或 FAB)一起呈现,则默认情况下 toast 可能会与这些元素重叠。这可以使用 positionAnchor 属性来修复,该属性接受元素引用或 ID。toast 将相对于所选元素定位,在使用 position="top" 时出现在其下方,在使用 position="bottom" 时出现在其上方。使用 position="middle" 时,positionAnchor 属性将被忽略。
Toast 中的按钮容器可以显示在与消息相同的行上,也可以使用 layout 属性堆叠在不同的行上。堆叠布局应与具有长文本值的按钮一起使用。此外,堆叠 toast 布局中的按钮可以使用 start 或 end 的 side 值,但不能同时使用两者。
可以在 toast 内部的内容旁边添加图标。通常情况下,toast 中的图标应用于添加额外的样式或上下文,而不是吸引用户注意或提升 toast 的优先级。如果你希望向用户传达更高优先级的消息或保证回复,我们建议使用警告框(Alert)。
Toast 旨在作为小型的通知,不打算中断用户。关闭 toast 不应需要用户交互。因此,焦点在 toast 呈现时不会自动移动到 toast。
Toast 设置了 aria 属性以确保屏幕阅读器的无障碍访问,但如果这些属性描述不够充分或与应用中 toast 的使用方式不一致,可以覆盖它们。
ion-toast 在内部的 .toast-content 元素上设置了 role="status" 和 aria-live="polite"。这导致屏幕阅读器只宣布 toast 消息和头部。按钮和图标在 toast 呈现时不会被宣布。
aria-live 使屏幕阅读器在内容更新时宣布 toast 的内容。但是,由于该属性设置为 'polite',屏幕阅读器不应中断当前任务。
由于 toast 旨在作为小型通知,aria-live 绝不应设置为 "assertive"。如果开发者需要用重要消息中断用户,我们建议使用警告框(alert)。
包含文本的按钮在与用户交互时将被屏幕阅读器读取。如果按钮只包含图标,或希望提供现有文本之外的其他描述,可以通过在按钮的 htmlAttributes 属性中传递 aria-label 来为按钮分配标签。
- Angular
- Javascript
- React
- Vue
const toast = await this.toastController.create({
header: 'Header',
buttons: [
{
icon: 'close',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
const toast = await this.toastController.create({
header: 'Header',
buttons: [
{
icon: 'close',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
useIonToast({
header: 'Header',
buttons: [
{
icon: 'close',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
const toast = await toastController.create({
header: 'Header',
buttons: [
{
icon: 'close',
htmlAttributes: {
'aria-label': 'close',
},
},
],
});
虽然这不是完整的列表,但以下是一些使用 toast 时应遵循的指南。
interface ToastButton {
text?: string;
icon?: string;
side?: 'start' | 'end';
role?: 'cancel' | string;
cssClass?: string | string[];
htmlAttributes?: { [key: string]: any };
handler?: () => boolean | void | Promise<boolean | void>;
}
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;
}
| 说明 | If true, the toast will animate. |
| 属性 | animated |
| 类型 | boolean |
| 默认值 | true |
| 说明 | An array of buttons for the toast. |
| 属性 | undefined |
| 类型 | (string | ToastButton)[] | undefined |
| 默认值 | undefined |
| 说明 | The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming. |
| 属性 | color |
| 类型 | "danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined |
| 默认值 | undefined |
| 说明 | Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces. |
| 属性 | css-class |
| 类型 | string | string[] | undefined |
| 默认值 | undefined |
| 说明 | How many milliseconds to wait before hiding the toast. By default, it will show until dismiss() is called. |
| 属性 | duration |
| 类型 | number |
| 默认值 | config.getNumber('toastDuration', 0) |
| 说明 | Animation to use when the toast is presented. |
| 属性 | undefined |
| 类型 | ((baseEl: any, opts?: any) => Animation) | undefined |
| 默认值 | undefined |
| 说明 | Header to be shown in the toast. |
| 属性 | header |
| 类型 | string | undefined |
| 默认值 | undefined |
| 说明 | Additional attributes to pass to the toast. |
| 属性 | undefined |
| 类型 | undefined | { [key: string]: any; } |
| 默认值 | undefined |
| 说明 | The name of the icon to display, or the path to a valid SVG file. See ion-icon. https://ionic.io/ionicons |
| 属性 | icon |
| 类型 | string | undefined |
| 默认值 | undefined |
| 说明 | If true, the toast will open. If false, the toast will close. Use this if you need finer grained control over presentation, otherwise just use the toastController or the trigger property. Note: isOpen will not automatically be set back to false when the toast dismisses. You will need to do that in your code. |
| 属性 | is-open |
| 类型 | boolean |
| 默认值 | false |
| 说明 | If true, the keyboard will be automatically dismissed when the overlay is presented. |
| 属性 | keyboard-close |
| 类型 | boolean |
| 默认值 | false |
| 说明 | Defines how the message and buttons are laid out in the toast. 'baseline': The message and the buttons will appear on the same line. Message text may wrap within the message container. 'stacked': The buttons containers and message will stack on top of each other. Use this if you have long text in your buttons. |
| 属性 | layout |
| 类型 | "baseline" | "stacked" |
| 默认值 | 'baseline' |
| 说明 | Animation to use when the toast is dismissed. |
| 属性 | undefined |
| 类型 | ((baseEl: any, opts?: any) => Animation) | undefined |
| 默认值 | undefined |
| 说明 | Message to be shown in the toast. This property accepts custom HTML as a string. Content is parsed as plaintext by default. innerHTMLTemplatesEnabled must be set to true in the Ionic config before custom HTML can be used. |
| 属性 | message |
| 类型 | IonicSafeString | string | undefined |
| 默认值 | undefined |
| 说明 | The mode determines which platform styles to use.
这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。 |
| 属性 | mode |
| 类型 | "ios" | "md" |
| 默认值 | undefined |
| 说明 | The starting position of the toast on the screen. Can be tweaked further using the positionAnchor property. |
| 属性 | position |
| 类型 | "bottom" | "middle" | "top" |
| 默认值 | 'bottom' |
| 说明 | The element to anchor the toast's position to. Can be set as a direct reference or the ID of the element. With position="bottom", the toast will sit above the chosen element. With position="top", the toast will sit below the chosen element. With position="middle", the value of positionAnchor is ignored. |
| 属性 | position-anchor |
| 类型 | HTMLElement | string | undefined |
| 默认值 | undefined |
| 说明 | If set to 'vertical', the Toast can be dismissed with a swipe gesture. The swipe direction is determined by the value of the position property: top: The Toast can be swiped up to dismiss. bottom: The Toast can be swiped down to dismiss. middle: The Toast can be swiped up or down to dismiss. |
| 属性 | swipe-gesture |
| 类型 | "vertical" | undefined |
| 默认值 | undefined |
| 说明 | If true, the toast will be translucent. Only applies when the mode is "ios" and the device supports backdrop-filter. |
| 属性 | translucent |
| 类型 | boolean |
| 默认值 | false |
| 说明 | An ID corresponding to the trigger element that causes the toast to open when clicked. |
| 属性 | trigger |
| 类型 | string | undefined |
| 默认值 | undefined |
| Name | 说明 | 冒泡 |
|---|
didDismiss | Emitted after the toast has dismissed. Shorthand for ionToastDidDismiss. | true |
didPresent | Emitted after the toast has presented. Shorthand for ionToastWillDismiss. | true |
ionToastDidDismiss | Emitted after the toast has dismissed. | true |
ionToastDidPresent | Emitted after the toast has presented. | true |
ionToastWillDismiss | Emitted before the toast has dismissed. | true |
ionToastWillPresent | Emitted before the toast has presented. | true |
willDismiss | Emitted before the toast has dismissed. Shorthand for ionToastWillDismiss. | true |
willPresent | Emitted before the toast has presented. Shorthand for ionToastWillPresent. | true |
| 说明 | Dismiss the toast overlay after it has been presented. |
| 签名 | dismiss(data?: any, role?: string) => Promise<boolean> |
| 参数 | data: Any data to emit in the dismiss events. role: The role of the element that is dismissing the toast. This can be useful in a button handler for determining which button was clicked to dismiss the toast. Some examples include: ``"cancel", "destructive", "selected", and "backdrop".
This is a no-op if the overlay has not been presented yet. If you want to remove an overlay from the DOM that was never presented, use the remove method. |
| 说明 | Returns a promise that resolves when the toast did dismiss. |
| 签名 | onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
| 说明 | Returns a promise that resolves when the toast will dismiss. |
| 签名 | onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
| 说明 | Present the toast overlay after it has been created. |
| 签名 | present() => Promise<void> |
| Name | 说明 |
|---|
button | Any button element that is displayed inside of the toast. |
button cancel | Any button element with role "cancel" that is displayed inside of the toast. |
container | The element that wraps all child elements. |
header | The header text of the toast. |
icon | The icon that appears next to the toast content. |
message | The body text of the toast. |
| Name | 说明 |
|---|
--background | Background of the toast |
--border-color | Border color of the toast |
--border-radius | Border radius of the toast |
--border-style | Border style of the toast |
--border-width | Border width of the toast |
--box-shadow | Box shadow of the toast |
--button-color | Color of the button text |
--color | Color of the toast text |
--end | Position from the right if direction is left-to-right, and from the left if direction is right-to-left |
--height | Height of the toast |
--max-height | Maximum height of the toast |
--max-width | Maximum width of the toast |
--min-height | Minimum height of the toast |
--min-width | Minimum width of the toast |
--start | Position from the left if direction is left-to-right, and from the right if direction is right-to-left |
--white-space | White space of the toast message |
--width | Width of the toast |
该组件没有可用的插槽。