跳到主要内容

Modals

Add dialogs to your site for lightboxes, user notifications, or completely custom content.

Overview

  • Modals are positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.
  • Modals are unmounted when closed.
  • Bootstrap only supports one modal window at a time. Nested modals aren't supported, but if you really need them, the underlying @restart/ui library can support them if you're willing.
  • Modal's "trap" focus in them, ensuring the keyboard navigation cycles through the modal, and not the rest of the page.
  • Unlike vanilla Bootstrap, autoFocus works in Modals because React handles the implementation.

Examples

Static Markup

Below is a static modal dialog (without the positioning) to demonstrate the look and feel of the Modal.

结果
Loading...
实时编辑器

Live demo

A modal with header, body, and set of actions in the footer. Use <Modal/> in combination with other components to show or hide your Modal. The <Modal/> Component comes with a few convenient "sub components": <Modal.Header/>, <Modal.Title/>, <Modal.Body/>, and <Modal.Footer/>, which you can use to build the Modal content.

结果
Loading...
实时编辑器

Static backdrop

When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.

结果
Loading...
实时编辑器

Without Animation

A Modal can also be without an animation. For that set the animation prop to false.

结果
Loading...
实时编辑器

Vertically centered

You can vertically center a modal by passing the centered prop.

结果
Loading...
实时编辑器

Using the grid

You can use grid layouts within a model using regular grid components inside the modal content.

结果
Loading...
实时编辑器

Focus on specific element

You can focus on an element inside the modal using autoFocus attribute on the element.

结果
Loading...
实时编辑器

Optional Sizes

You can specify a Bootstrap large or small modal by using the size prop.

结果
Loading...
实时编辑器

Fullscreen Modal

You can use the fullscreen prop to make the modal fullscreen. Specifying a breakpoint will only set the modal as fullscreen below the breakpoint size.

结果
Loading...
实时编辑器

Sizing modals using custom CSS

You can apply custom css to the modal dialog div using the dialogClassName prop. Example is using a custom css class with width set to 90%.

结果
Loading...
实时编辑器

API

ModalDialog

ModalHeader

ModalTitle

ModalBody

ModalFooter