跳转到内容

ModalUnstyled API

想要学习更多组件属性和 CSS API,可以查阅 React ModalUnstyled 组件的 API 文档。

导入

import ModalUnstyled from '@mui/base/ModalUnstyled';
// 或
import { ModalUnstyled } from '@mui/base';
你可以阅读这个关于最小化捆绑包的指南以了解以上二者的差异。

Modal is a lower-level construct that is leveraged by the following components:

If you are creating a modal dialog, you probably want to use the Dialog component rather than directly using Modal.

This component shares many concepts with react-overlays.

属性

名称类型默认值描述
children*element
A single child content element.
⚠️ Needs to be able to hold a ref.
open*boolfalse
Control the popup` open state.
classesobject
Override or extend the styles applied to the component. See CSS API below for more details.
closeAfterTransitionboolfalse
When set to true the Modal waits until a nested Transition is completed before closing.
componentelementType
The component used for the root node. Either a string to use a HTML element or a component.
containerHTML element
| func
A HTML element or function that returns one. The container will have the portal children appended to it.
By default, it uses the body of the top-level document object, so it's simply document.body most of the time.
disableAutoFocusboolfalse
If true, the modal will not automatically shift focus to itself when it opens, and replace it to the last focused element when it closes. This also works correctly with any modal children that have the disableAutoFocus prop.
Generally this should never be set to true as it makes the modal less accessible to assistive technologies, like screen readers.
disableEnforceFocusboolfalse
If true, the modal will not prevent focus from leaving the modal while open.
Generally this should never be set to true as it makes the modal less accessible to assistive technologies, like screen readers.
disableEscapeKeyDownboolfalse
If true, hitting escape will not fire the onClose callback.
disablePortalboolfalse
The children will be inside the DOM hierarchy of the parent component.
disableRestoreFocusboolfalse
If true, the modal will not restore focus to previously focused element once modal is hidden or unmounted.
disableScrollLockboolfalse
Disable the scroll lock behavior.
hideBackdropboolfalse
If true, the backdrop is not rendered.
keepMountedboolfalse
Always keep the children in the DOM. This prop can be useful in SEO situation or when you want to maximize the responsiveness of the Modal.
onBackdropClickfunc
Callback fired when the backdrop is clicked.
onClosefunc
Callback fired when the component requests to be closed. The reason parameter can optionally be used to control the response to onClose.

Signature:
function(event: object, reason: string) => void
event: The event source of the callback.
reason: Can be: "escapeKeyDown", "backdropClick".
slotProps{ backdrop?: func
| object, root?: func
| object }
{}
slots{ backdrop?: elementType, root?: elementType }{}

ref 则会被传递到根元素中。

CSS

规则名称全局类名描述
root.MuiModal-rootStyles applied to the root element.
hidden.MuiModal-hiddenStyles applied to the root element if the Modal has exited.

您可以使用组件自定义选项对组件进行个性化:

演示项目