Skip to main content

ion-modal

shadow

模态框是出现在应用内容顶部的对话框,应用必须将其关闭才能恢复交互。当有很多选项可供选择时,或者在过滤列表中的项目以及许多其他用例时,它作为选择组件非常有用。

¥A Modal is a dialog that appears on top of the app's content, and must be dismissed by the app before interaction can resume. It is useful as a select component when there are a lot of options to choose from, or when filtering items in a list, as well as many other use cases.

¥Inline Modals (Recommended)

可以通过直接在模板中编写组件来使用 ion-modal。这减少了为了渲染模式而需要连接的处理程序的数量。

¥ion-modal can be used by writing the component directly in your template. This reduces the number of handlers you need to wire up in order to present the modal.

当将 ion-modal 与 Angular、React 或 Vue 结合使用时,你传入的组件将在模态关闭时被销毁。由于此功能是由 JavaScript 框架提供的,因此在没有 JavaScript 框架的情况下使用 ion-modal 不会破坏你传入的组件。如果这是必需的功能,我们建议使用 modalController

¥When using ion-modal with Angular, React, or Vue, the component you pass in will be destroyed when the modal is dismissed. As this functionality is provided by the JavaScript framework, using ion-modal without a JavaScript framework will not destroy the component you passed in. If this is a needed functionality, we recommend using the modalController instead.

使用 isOpen

¥Using isOpen

ion-modal 上的 isOpen 属性允许开发者从其应用状态控制模式的渲染状态。这意味着当 isOpen 设置为 true 时,将渲染模态;当 isOpen 设置为 false 时,模态将被关闭。

¥The isOpen property on ion-modal allows developers to control the presentation state of the modal from their application state. This means when isOpen is set to true the modal will be presented and when isOpen is set to false the modal will be dismissed.

isOpen 使用单向数据绑定,这意味着当模式关闭时,它不会自动设置为 false。开发者应该监听 ionModalDidDismissdidDismiss 事件并将 isOpen 设置为 false。这样做的原因是它阻止了 ion-modal 的内部与应用的状态紧密耦合。通过单向数据绑定,模态只需要关心反应变量提供的布尔值。对于双向数据绑定,模式需要关注布尔值以及反应变量本身的存在。这可能会导致不确定的行为并使应用更难以调试。

¥isOpen uses a one-way data binding, meaning it will not automatically be set to false when the modal is dismissed. Developers should listen for the ionModalDidDismiss or didDismiss event and set isOpen to false. The reason for this is it prevents the internals of ion-modal from being tightly coupled with the state of the application. With a one way data binding, the modal only needs to concern itself with the boolean value that the reactive variable provides. With a two way data binding, the modal needs to concern itself with both the boolean value as well as the existence of the reactive variable itself. This can lead to non-deterministic behaviors and make applications harder to debug.

控制器模态

¥Controller Modals

借助 modalController,开发者可以通过编程方式渲染 ion-modal。开发者将完全控制何时渲染和关闭模式。

¥With the modalController developers can present an ion-modal programmatically. Developers will have complete control over when a modal is presented and dismissed.

防止模态窗口关闭

¥Preventing a Modal from Dismissing

将数据输入模态时,通常希望有一种方法来防止意外数据丢失。ion-modal 上的 canDismiss 属性使开发者可以控制何时允许关闭模式。

¥When entering data into a modal, it is often desirable to have a way of preventing accidental data loss. The canDismiss property on ion-modal gives developers control over when a modal is allowed to dismiss.

使用 canDismiss 属性有两种不同的方式:设置布尔值或设置回调函数。

¥There are two different ways of using the canDismiss property: setting a boolean value or setting a callback function.

注意

注意:使用表单模式时,如果没有设置 0 断点,则不会在滑动时检查 canDismiss。然而,当按下 Esc 或硬件后退按钮时,它仍然会被检查。

¥Note: When using a sheet modal, canDismiss will not be checked on swipe if there is no 0 breakpoint set. However, it will still be checked when pressing Esc or the hardware back button.

设置布尔值

¥Setting a boolean value

开发者可以将 canDismiss 设置为布尔值。如果 canDismisstrue,则当用户尝试关闭模式时,模式将关闭。如果 canDismissfalse,则当用户尝试关闭模式时,模式将不会关闭。

¥Developers can set canDismiss to a boolean value. If canDismiss is true, then the modal will close when users attempt to dismiss the modal. If canDismiss is false, then the modal will not close when users attempt to dismiss the modal.

当你需要要求在关闭模式之前执行特定操作时,应使用设置布尔值。例如,如果开发者希望要求在关闭模式之前选中 "使用条款" 复选框,他们可以最初将 canDismiss 设置为 false,并在选中该复选框时将其更新为 true

¥Setting a boolean value should be used when you need to require a particular action to be taken prior to a modal being dismissed. For example, if developers want to require that a "Terms of Use" checkbox is checked prior to closing the modal, they could set canDismiss to false initially and update it to true when the checkbox is checked.

设置回调函数

¥Setting a callback function

开发者可以将 canDismiss 设置为一个函数。此函数必须返回解析为 truefalsePromise。如果 Promise 解析为 true,则模式将被驳回。如果 Promise 解析为 false,则模式不会解除。

¥Developers can set canDismiss to be a function. This function must return a Promise that resolves to either true or false. If the promise resolves to true, then the modal will dismiss. If the promise resolves to false, then the modal will not dismiss.

当你有复杂的关闭条件(例如在关闭模式之前显示确认对话框)时,应使用设置回调函数。用户在此对话框中选择的选项可用于确定模式是否应继续关闭。

¥Setting a callback function should be used when you have complex dismissing criteria such as showing a confirmation dialog prior to dismissing the modal. The option that users select in this dialog can then be used to determine whether or not the modal should proceed with dismissing.

请注意,设置回调函数将导致使用卡片或表单模态时滑动手势被中断。这是因为 Ionic 不知道你的回调函数将提前解析什么。

¥Note that setting a callback function will cause the swipe gesture to be interrupted when using a card or sheet modal. This is because Ionic does not know what your callback function will resolve to ahead of time.

防止滑动关闭

¥Prevent swipe to close

开发者可能希望阻止用户滑动以关闭卡片或工作表模式。这可以通过为 canDismiss 设置回调函数并检查 role 是否不是 gesture 来完成。

¥Developers may want to prevent users from swiping to close a card or sheet modal. This can be done by setting a callback function for canDismiss and checking if the role is not gesture.

修改子组件中的关闭行为

¥Modifying dismiss behavior in child components

在某些情况下,开发者可能需要根据所渲染模态的状态自定义 canDismiss 回调的行为。这种定制可能特别有用,例如,当开发者想要防止模式在其中的表单无效时被关闭时。

¥In certain scenarios, developers may need to customize the behavior of the canDismiss callback based on the state of a presented modal. This customization can be particularly useful, for instance, when developers want to prevent the modal from being dismissed if a form within it is invalid.

为了实现这种定制,子组件可以采用各种技术(例如函数回调、事件触发或其他反应性机制)来与父组件通信并更新控制 canDismiss 回调的条件。

¥To achieve this customization, child components can employ various techniques such as function callbacks, event emission, or other reactivity mechanisms to communicate with the parent component and update the conditions governing the canDismiss callback.

下面是一个简化的示例,说明了子组件如何与父组件交互以修改 canDismiss 回调:

¥Here's a simplified example illustrating how a child component can interact with a parent component to modify the canDismiss callback:

卡模态

¥Card Modal

开发者可以创建卡片模态效果,其中模态显示为堆叠在应用主要内容之上的卡片。要创建卡片模式,开发者需要在 ion-modal 上设置 presentingElement 属性。

¥Developers can create a card modal effect where the modal appears as a card stacked on top of your app's main content. To create a card modal, developers need to set the presentingElement property on ion-modal.

presentingElement 属性接受对应在模式下显示的元素的引用。这通常是对 ion-router-outlet 的引用。

¥The presentingElement property accepts a reference to the element that should display under your modal. This is typically a reference to ion-router-outlet.

canDismiss 属性可用于控制卡片模态是否可以滑动关闭。

¥The canDismiss property can be used to control whether or not the card modal can be swiped to close.

注意

卡片显示样式仅在 iOS 上可用。

¥The card display style is only available on iOS.

薄模态

¥Sheet Modal

信息

如果你希望模态内容可滚动,则应在工作表模态内部使用 内容

¥Content should be used inside of the sheet modal if you want your modal content to be scrollable.

开发者可以创建类似于地图应用中可用的抽屉组件的工作表模式效果。要创建表单模态,开发者需要在 ion-modal 上设置 breakpointsinitialBreakpoint 属性。

¥Developers can create a sheet modal effect similar to the drawer components available in maps applications. To create a sheet modal, developers need to set the breakpoints and initialBreakpoint properties on ion-modal.

breakpoints 属性接受一个数组,该数组声明了滑动时工作表可以捕捉到的每个断点。[0, 0.5, 1]breakpoints 属性将指示可以滑动工作表以显示模态的 0%、模态的 50% 和模态的 100%。当模态滑动到 0% 时,模态将自动关闭。请注意,如果不包含 0 断点,则无法在滑动时取消该模式,但仍可以通过按 Esc 或硬件后退按钮取消该模式。

¥The breakpoints property accepts an array which states each breakpoint that the sheet can snap to when swiped. A breakpoints property of [0, 0.5, 1] would indicate that the sheet can be swiped to show 0% of the modal, 50% of the modal, and 100% of the modal. When the modal is swiped to 0%, the modal will be automatically dismissed. Note that the modal cannot be dismissed on swipe if no 0 breakpoint is included, but it can still be dismissed by pressing Esc or the hardware back button.

initialBreakpoint 属性是必需的,以便工作表模式知道在渲染时从哪个断点开始。initialBreakpoint 值也必须存在于 breakpoints 数组中。如果 breakpoints 值为 [0, 0.5, 1],则 initialBreakpoint 值为 0.5 将有效,因为 0.5breakpoints 数组中。0.25initialBreakpoint 值无效,因为 breakpoints 数组中不存在 0.25

¥The initialBreakpoint property is required so that the sheet modal knows which breakpoint to start at when presenting. The initialBreakpoint value must also exist in the breakpoints array. Given a breakpoints value of [0, 0.5, 1], an initialBreakpoint value of 0.5 would be valid as 0.5 is in the breakpoints array. An initialBreakpoint value of 0.25 would not be valid as 0.25 does not exist in the breakpoints array.

backdropBreakpoint 属性可用于自定义 ion-backdrop 开始淡入的点。当创建在工作表下方包含应保持交互的内容的界面时,这非常有用。一个常见的用例是工作表模式,它覆盖地图,其中地图是交互式的,直到工作表完全展开。

¥The backdropBreakpoint property can be used to customize the point at which the ion-backdrop will begin to fade in. This is useful when creating interfaces that have content underneath the sheet that should remain interactive. A common use case is a sheet modal that overlays a map where the map is interactive until the sheet is fully expanded.

与背景内容交互

¥Interacting with background content

自定义板材高度

¥Custom Sheet Height

开发者应使用 --height CSS 变量来更改工作表模式的高度,而不是更改 breakpoints 数组中的最后一个断点。原因是将 breakpoints 数组中的最后一个断点更改为小于 1 的值将导致某些模式在视口之外无法访问。

¥Developers should use the --height CSS Variable to change the height of the sheet modal instead of changing the last breakpoint in the breakpoints array. The reason for this is changing the last breakpoint in the breakpoints array to a value less than 1 will cause some of the modal to be inaccessible outside of the viewport.

以下示例演示如何获取根据其内容自动调整大小的工作表模式。请注意,通过将最大断点保持在 1,我们确保整个模式可以在视口中访问。

¥The following example shows how to get a sheet modal that is automatically sized based on its content. Note that by keeping the maximum breakpoint at 1 we ensure that the entire modal is accessible in the viewport.

处理行为

¥Handle Behavior

工作表模态可以选择渲染用于在断点之间拖动工作表的句柄指示器。handleBehavior 属性可用于配置用户激活句柄时的行为。

¥Sheet modals can optionally render a handle indicator used for dragging the sheet between breakpoints. The handleBehavior property can be used to configure the behavior of when the handle is activated by the user.

样式

¥Styling

模态框显示在应用的根目录中,因此它们覆盖了整个应用。此行为适用于内联模态和控制器渲染的模态。因此,自定义模态样式不能限定于特定组件,因为它们不适用于模态。相反,样式必须全局应用。对于大多数开发者来说,将自定义样式放在 global.css 中就足够了。

¥Modals are presented at the root of your application so they overlay your entire app. This behavior applies to both inline modals and modals presented from a controller. As a result, custom modal styles can not be scoped to a particular component as they will not apply to the modal. Instead, styles must be applied globally. For most developers, placing the custom styles in global.css is sufficient.

注意

如果你正在构建 Ionic Angular 应用,则需要将样式添加到全局样式表文件中。请阅读下面 Angular 部分中的 风格布置 了解更多信息。

¥If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file. Read Style Placement in the Angular section below for more information.

注意

ion-modal 的工作原理是假设堆叠模态大小相同。因此,每个后续模态将没有框阴影且背景不透明度为 0。这是为了避免阴影和背景随着每个添加的模态而变暗的效果。这可以通过设置 --box-shadow--backdrop-opacity CSS 变量来更改:

¥ion-modal works under the assumption that stacked modals are the same size. As a result, each subsequent modal will have no box shadow and a backdrop opacity of 0. This is to avoid the effect of shadows and backdrops getting darker with each added modal. This can be changed by setting the --box-shadow and --backdrop-opacity CSS variables:

ion-modal.stack-modal {
--box-shadow: 0 28px 48px rgba(0, 0, 0, 0.4);
--backdrop-opacity: var(--ion-backdrop-opacity, 0.32);
}

动画

¥Animations

可以使用我们的动画生成器并将动画分配给 enterAnimationleaveAnimation 来自定义进入和离开动画。

¥The enter and leave animations can be customized by using our animation builder and assigning animations to enterAnimation and leaveAnimation.

自定义对话框

¥Custom Dialogs

虽然 ion-modal 最常用于全页视图、卡片或工作表,但也可以将其用于自定义对话框。如果开发者需要比 ion-alertion-loading 等组件提供的接口更复杂的接口,这非常有用。

¥While ion-modal is most often used for full-page views, cards, or sheets, it is also possible to use it for custom dialogs. This is useful if developers need an interface that is more complex than what components such as ion-alert or ion-loading provide.

创建自定义对话框时需要记住以下几点:

¥A few things to keep in mind when creating custom dialogs:

  • ion-content 旨在用于全页模式、卡片和表格。如果你的自定义对话框具有动态或未知大小,则不应使用 ion-content

    ¥ion-content is intended to be used in full-page modals, cards, and sheets. If your custom dialog has a dynamic or unknown size, ion-content should not be used.

  • 创建自定义对话框提供了一种摆脱默认模式体验的方法。因此,自定义对话框不应与卡片或工作表模式一起使用。

    ¥Creating custom dialogs provides a way of ejecting from the default modal experience. As a result, custom dialogs should not be used with card or sheet modals.

接口

¥Interfaces

ModalOptions

你将在下面找到使用 modalController 时可用的所有选项。调用 modalController.create() 时应提供这些选项。

¥Below you will find all of the options available to you when using the modalController. These options should be supplied when calling modalController.create().

interface ModalOptions {
component: any;
componentProps?: { [key: string]: any };
presentingElement?: HTMLElement;
showBackdrop?: boolean;
backdropDismiss?: boolean;
cssClass?: string | string[];
animated?: boolean;
canDismiss?: boolean | ((data?: any, role?: string) => Promise<boolean>);

mode?: 'ios' | 'md';
keyboardClose?: boolean;
id?: string;
htmlAttributes?: { [key: string]: any };

enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;

breakpoints?: number[];
initialBreakpoint?: number;
backdropBreakpoint?: number;
handle?: boolean;
}

ModalCustomEvent

虽然不是必需的,但可以使用此接口代替 CustomEvent 接口,以便使用从此组件发出的 Ionic 事件进行更强的键入。

¥While not required, this interface can be used in place of the CustomEvent interface for stronger typing with Ionic events emitted from this component.

interface ModalCustomEvent extends CustomEvent {
target: HTMLIonModalElement;
}

无障碍

¥Accessibility

键盘导航

¥Keyboard Navigation

密钥函数
Esc关闭模态

标签

¥Labels

模态框有 dialog 角色。因此,开发者必须正确标记他们的模式。如果模态框使用 ion-title,则可以通过在 ion-modal 上设置 aria-labelledby,使用内部文本来标记模态框本身。如果模态包含附加描述性文本,则可以使用 aria-describedby 将此文本与模态关联。

¥Modals have a dialog role. As a result, developers must properly label their modals. If the modal is using ion-title, the text inside can be used to label the modal itself by setting aria-labelledby on ion-modal. If the modal contains additional descriptive text, this text can be associated with the modal by using aria-describedby.

屏幕阅读器

¥Screen Readers

模态框应用了 aria-modal 属性。此属性可能会导致辅助技术限制对模态元素内容的导航。因此,使用移动到下一个或上一个项目的手势可能不会聚焦模式之外的元素。即使使用 backdropBreakpoint 属性在工作表模态中禁用背景时,这也适用。

¥Modals have the aria-modal attribute applied. This attribute can cause assistive technologies to limit navigation to the modal element's contents. As a result, using gestures that move to the next or previous items may not focus elements outside of the modal. This applies even when the backdrop is disabled in sheet modals using the backdropBreakpoint property.

如果开发者手动移动焦点,辅助技术不会限制对模态元素内容的导航。但是,对于启用了焦点捕获的模态,Ionic 不支持手动将焦点移至模态之外。

¥Assistive technologies will not limit navigation to the modal element's contents if developers manually move focus. However, manually moving focus outside of a modal is not supported in Ionic for modals that have focus trapping enabled.

请参阅 https://w3c.github.io/aria/#aria-modal 了解更多信息。

¥See https://w3c.github.io/aria/#aria-modal for more information.

焦点捕捉

¥Focus Trapping

当渲染模态时,焦点将被困在所渲染的模态内。用户可以将其他交互元素聚焦在模态内,但在渲染模态时永远无法将交互元素聚焦在模态外。对于渲染多个堆叠模态的应用,焦点将集中在最后渲染的模态上。

¥When a modal is presented, focus will be trapped inside of the presented modal. Users can focus other interactive elements inside the modal but will never be able to focus interactive elements outside the modal while the modal is presented. For applications that present multiple stacked modals, focus will be trapped on the modal that was presented last.

已通过 backdropBreakpoint 属性禁用其背景的图纸模态不受焦点陷印的影响。

¥Sheet modals that have had their backdrop disabled by the backdropBreakpoint property are not subject to focus trapping.

薄模态

¥Sheet Modals

当使用 backdropBreakpoint 属性时,工作表模态允许用户与模态后面的内容进行交互。背景将在指定的 backdropBreakpoint 之前被禁用,并在其之后启用。

¥Sheet modals allow users to interact with content behind the modal when the backdropBreakpoint property is used. The backdrop will be disabled up to and including the specified backdropBreakpoint and will be enabled after it.

禁用背景后,用户将能够使用指针或键盘与工作表模式之外的元素进行交互。由于使用了 aria-modal,默认情况下辅助技术可能不会聚焦在工作表模式之外。我们建议避免使用自动对焦等功能,因为它可能导致辅助技术在两个交互式上下文之间跳转而不警告用户。

¥When the backdrop is disabled, users will be able to interact with elements outside the sheet modal using a pointer or keyboard. Assistive technologies may not focus outside the sheet modal by default due to the usage of aria-modal. We recommend avoiding features such as autofocus here as it can cause assistive technologies to jump between two interactive contexts without warning the user.

性能

¥Performance

挂载内部内容

¥Mounting Inner Contents

关闭时,内联 ion-modal 的内容将被卸载。如果此内容的渲染成本很高,开发者可以在安装模式后立即使用 keepContentsMounted 属性来安装该内容。这可以帮助优化应用的响应能力,因为当模式打开时内部内容已经被安装。

¥The content of an inline ion-modal is unmounted when closed. If this content is expensive to render, developers can use the keepContentsMounted property to mount the content as soon as the modal is mounted. This can help optimize the responsiveness of your application as the inner contents will have already been mounted when the modal opens.

开发者在使用 keepContentsMounted 时应记住以下几点:

¥Developers should keep the following in mind when using keepContentsMounted:

  • 此功能应作为解决现有性能问题的最后手段。在使用此功能之前尝试识别并解决性能瓶颈。此外,不要用它来预测性能问题。

    ¥This feature should be used as a last resort in order to deal with existing performance problems. Try to identify and resolve performance bottlenecks before using this feature. Additionally, do not use this to anticipate performance problems.

  • 仅当使用 JavaScript 框架时才需要此功能。不使用框架的开发者可以将要渲染的内容传递到模态中,内容将自动渲染。

    ¥This feature is only needed when using a JavaScript Framework. Developers not using a framework can pass the contents to be rendered into the modal, and the contents will be rendered automatically.

  • 此功能仅适用于内联模态。使用 modalController 创建的模态不会提前创建,因此内部内容也不会创建。

    ¥This feature only works with inline modals. Modals created with the modalController are not created ahead of time, so the inner contents are not created either.

  • 内部组件上的任何 JavaScript 框架生命周期钩子都会在模式安装后立即运行,而不是在模式渲染时运行。

    ¥Any JavaScript Framework lifecycle hooks on the inner component will run as soon as the modal is mounted, not when the modal is presented.

属性

¥Properties

animated

DescriptionIf true, the modal will animate.
Attributeanimated
Typeboolean
Defaulttrue

backdropBreakpoint

DescriptionA decimal value between 0 and 1 that indicates the point after which the backdrop will begin to fade in when using a sheet modal. Prior to this point, the backdrop will be hidden and the content underneath the sheet can be interacted with. This value is exclusive meaning the backdrop will become active after the value specified.
Attributebackdrop-breakpoint
Typenumber
Default0

backdropDismiss

DescriptionIf true, the modal will be dismissed when the backdrop is clicked.
Attributebackdrop-dismiss
Typeboolean
Defaulttrue

breakpoints

DescriptionThe breakpoints to use when creating a sheet modal. Each value in the array must be a decimal between 0 and 1 where 0 indicates the modal is fully closed and 1 indicates the modal is fully open. Values are relative to the height of the modal, not the height of the screen. One of the values in this array must be the value of the initialBreakpoint property. For example: [0, .25, .5, 1]
Attributeundefined
Typenumber[] | undefined
Defaultundefined

canDismiss

DescriptionDetermines whether or not a modal can dismiss when calling the dismiss method.

If the value is true or the value's function returns true, the modal will close when trying to dismiss. If the value is false or the value's function returns false, the modal will not close when trying to dismiss.

See https://ionicframework.com/docs/troubleshooting/runtime#accessing-this if you need to access this from within the callback.
Attributecan-dismiss
Type((data?: any, role?: string | undefined) => Promise<boolean>) | boolean
Defaulttrue

enterAnimation

DescriptionAnimation to use when the modal is presented.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

focusTrap

DescriptionIf true, focus will not be allowed to move outside of this overlay. If false, focus will be allowed to move outside of the overlay.

In most scenarios this property should remain set to true. Setting this property to false can cause severe accessibility issues as users relying on assistive technologies may be able to move focus into a confusing state. We recommend only setting this to false when absolutely necessary.

Developers may want to consider disabling focus trapping if this overlay presents a non-Ionic overlay from a 3rd party library. Developers would disable focus trapping on the Ionic overlay when presenting the 3rd party overlay and then re-enable focus trapping when dismissing the 3rd party overlay and moving focus back to the Ionic overlay.
Attributefocus-trap
Typeboolean
Defaulttrue

handle

DescriptionThe horizontal line that displays at the top of a sheet modal. It is true by default when setting the breakpoints and initialBreakpoint properties.
Attributehandle
Typeboolean | undefined
Defaultundefined

handleBehavior

DescriptionThe interaction behavior for the sheet modal when the handle is pressed.

Defaults to "none", which means the modal will not change size or position when the handle is pressed. Set to "cycle" to let the modal cycle between available breakpoints when pressed.

Handle behavior is unavailable when the handle property is set to false or when the breakpoints property is not set (using a fullscreen or card modal).
Attributehandle-behavior
Type"cycle" | "none" | undefined
Default'none'

htmlAttributes

DescriptionAdditional attributes to pass to the modal.
Attributeundefined
Typeundefined | { [key: string]: any; }
Defaultundefined

initialBreakpoint

DescriptionA decimal value between 0 and 1 that indicates the initial point the modal will open at when creating a sheet modal. This value must also be listed in the breakpoints array.
Attributeinitial-breakpoint
Typenumber | undefined
Defaultundefined

isOpen

DescriptionIf true, the modal will open. If false, the modal will close. Use this if you need finer grained control over presentation, otherwise just use the modalController or the trigger property. Note: isOpen will not automatically be set back to false when the modal dismisses. You will need to do that in your code.
Attributeis-open
Typeboolean
Defaultfalse

keepContentsMounted

DescriptionIf true, the component passed into ion-modal will automatically be mounted when the modal is created. The component will remain mounted even when the modal is dismissed. However, the component will be destroyed when the modal is destroyed. This property is not reactive and should only be used when initially creating a modal.

Note: This feature only applies to inline modals in JavaScript frameworks such as Angular, React, and Vue.
Attributekeep-contents-mounted
Typeboolean
Defaultfalse

keyboardClose

DescriptionIf true, the keyboard will be automatically dismissed when the overlay is presented.
Attributekeyboard-close
Typeboolean
Defaulttrue

leaveAnimation

DescriptionAnimation to use when the modal is dismissed.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) | undefined
Defaultundefined

mode

DescriptionThe mode determines which platform styles to use.
Attributemode
Type"ios" | "md"
Defaultundefined

presentingElement

DescriptionThe element that presented the modal. This is used for card presentation effects and for stacking multiple modals on top of each other. Only applies in iOS mode.
Attributeundefined
TypeHTMLElement | undefined
Defaultundefined

showBackdrop

DescriptionIf true, a backdrop will be displayed behind the modal. This property controls whether or not the backdrop darkens the screen when the modal is presented. It does not control whether or not the backdrop is active or present in the DOM.
Attributeshow-backdrop
Typeboolean
Defaulttrue

trigger

DescriptionAn ID corresponding to the trigger element that causes the modal to open when clicked.
Attributetrigger
Typestring | undefined
Defaultundefined

事件

¥Events

NameDescriptionBubbles
didDismissEmitted after the modal has dismissed. Shorthand for ionModalDidDismiss.true
didPresentEmitted after the modal has presented. Shorthand for ionModalDidPresent.true
ionBreakpointDidChangeEmitted after the modal breakpoint has changed.true
ionModalDidDismissEmitted after the modal has dismissed.true
ionModalDidPresentEmitted after the modal has presented.true
ionModalWillDismissEmitted before the modal has dismissed.true
ionModalWillPresentEmitted before the modal has presented.true
willDismissEmitted before the modal has dismissed. Shorthand for ionModalWillDismiss.true
willPresentEmitted before the modal has presented. Shorthand for ionModalWillPresent.true

方法

¥Methods

dismiss

DescriptionDismiss the modal overlay after it has been presented.
Signaturedismiss(data?: any, role?: string) => Promise<boolean>

getCurrentBreakpoint

DescriptionReturns the current breakpoint of a sheet style modal
SignaturegetCurrentBreakpoint() => Promise<number | undefined>

onDidDismiss

DescriptionReturns a promise that resolves when the modal did dismiss.
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss

DescriptionReturns a promise that resolves when the modal will dismiss.
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present

DescriptionPresent the modal overlay after it has been created.
Signaturepresent() => Promise<void>

setCurrentBreakpoint

DescriptionMove a sheet style modal to a specific breakpoint. The breakpoint value must be a value defined in your breakpoints array.
SignaturesetCurrentBreakpoint(breakpoint: number) => Promise<void>

CSS 阴影部分

¥CSS Shadow Parts

NameDescription
backdropThe ion-backdrop element.
contentThe wrapper element for the default slot.
handleThe handle that is displayed at the top of the sheet modal when handle="true".

CSS 自定义属性

¥CSS Custom Properties

NameDescription
--backdrop-opacityOpacity of the backdrop
--backgroundBackground of the modal content
--border-colorBorder color of the modal content
--border-radiusBorder radius of the modal content
--border-styleBorder style of the modal content
--border-widthBorder width of the modal content
--heightHeight of the modal
--max-heightMaximum height of the modal
--max-widthMaximum width of the modal
--min-heightMinimum height of the modal
--min-widthMinimum width of the modal
--widthWidth of the modal

插槽

¥Slots

NameDescription
``Content is placed inside of the .modal-content element.