Modal
Displays a dialog with a custom content that requires attention or provides additional information.
Installation
npx nextui-cli@latest add modal
The above command is for individual installation only. You may skip this step if @nextui-org/react is already installed globally.
Import
NextUI exports 5 modal-related components:
- Modal: The main component to display a modal.
- ModalContent: The wrapper of the other modal components.
- ModalHeader: The header of the modal.
- ModalBody: The body of the modal.
- ModalFooter: The footer of the modal.
Usage
When the modal opens:
- Focus is bounded within the modal and set to the first tabbable element.
- Content behind a modal dialog is inert, meaning that users cannot interact with it.
Sizes
Non-dismissible
By default, the modal can be closed by clicking on the overlay or pressing the Esc key. You can disable this behavior by setting the following properties:
- Set the
isDismissableproperty tofalseto prevent the modal from closing when clicking on the overlay. - Set the
isKeyboardDismissDisabledproperty totrueto prevent the modal from closing when pressing the Esc key.
Modal placement
By default the modal is centered on screens higher than sm and is at the bottom of the screen on mobile. This placement is called auto, but
you can change it by using the placement prop.
Note: The
top-centerandbottom-centerpositions mean that the modal is positioned at the top / bottom of the screen on mobile and at the center of the screen on desktop.
Overflow scroll
You can use the scrollBehavior prop to set the scroll behavior of the modal.
- inside: The modal content will be scrollable.
- outside: The modal content will be scrollable and the modal will be fixed.
With Form
The Modal handles the focus within the modal content. It means that you can use the modal with
form elements without any problem. the focus returns to the trigger when the modal closes.
Note: You can add the
autoFocusprop to the firstInputcomponent to focus it when the modal opens.
Backdrop
The Modal component has a backdrop prop to show a backdrop behind the modal. The backdrop can be
either transparent, opaque or blur. The default value is opaque.
Custom Backdrop
You can customize the backdrop by using the backdrop slot.
Custom Motion
Modal offers a motionProps property to customize the enter / exit animation.
Learn more about Framer motion variants here.
Draggable
Try to drag the header part.
Draggable Overflow
Set overflow to true can drag overflow the viewport.
Slots
- wrapper: The wrapper slot of the modal. It wraps the
baseand thebackdropslots. - base: The main slot of the modal content.
- backdrop: The backdrop slot, it is displayed behind the modal.
- header: The header of the modal, it is displayed at the top of the modal.
- body: The body of the modal, it is displayed in the middle of the modal.
- footer: The footer of the modal, it is displayed at the bottom of the modal.
- closeButton: The close button of the modal.
Custom Styles
You can customize the Modal component by passing custom Tailwind CSS classes to the component slots.
Data Attributes
Modal has the following attributes on the base element:
- data-open: When the modal is open. Based on modal state.
- data-dismissable:
When the modal is dismissable. Based on
isDismissableprop.
Accessibility
- Content outside the modal is hidden from assistive technologies while it is open.
- The modal optionally closes when interacting outside, or pressing the Esc key.
- Focus is moved into the modal on mount, and restored to the trigger element on unmount.
- While open, focus is contained within the modal, preventing the user from tabbing outside.
- Scrolling the page behind the modal is prevented while it is open, including in mobile browsers.
API
Modal Props
| Attribute | Type | Description | Default |
|---|---|---|---|
| children* | ReactNode | The content of the modal. It's usually the ModalContent. | - |
| size | xs | sm | md | lg | xl | 2xl | 3xl | 4xl | 5xl | full | The modal size. This changes the modal max-width and height (full). | md |
| radius | none | sm | md | lg | The modal border radius. | lg |
| shadow | none | sm | md | lg | The modal shadow. | lg |
| backdrop | transparent | opaque | blur | The modal backdrop type. | opaque |
| scrollBehavior | normal | inside | outside | The modal scroll behavior. | normal |
| placement | auto | top | center | bottom | The modal position. | auto |
| isOpen | boolean | Whether the modal is open by default (controlled). | - |
| defaultOpen | boolean | Whether the modal is open by default (uncontrolled). | - |
| isDismissable | boolean | Whether the modal can be closed by clicking on the overlay or pressing the Esc key. | true |
| isKeyboardDismissDisabled | boolean | Whether pressing the Esc key to close the modal should be disabled. | false |
| shouldBlockScroll | boolean | Whether the modal should block the scroll of the page on open. | true |
| hideCloseButton | boolean | Whether to hide the modal close button. | false |
| closeButton | ReactNode | Custom close button to display on top right corner. | - |
| motionProps | MotionProps | The props to modify the framer motion animation. Use the variants API to create your own animation. | |
| portalContainer | HTMLElement | The container element in which the overlay portal will be placed. | document.body |
| disableAnimation | boolean | Whether the modal should not have animations. | false |
| classNames | Record<"wrapper"| "base"| "backdrop"| "header" | "body" | "footer" | "closeButton", string> | Allows to set custom class names for the modal slots. | - |
Modal Events
| Attribute | Type | Description |
|---|---|---|
| onOpenChange | (isOpen: boolean) => void | Handler that is called when the modal's open state changes. |
| onClose | () => void | Handler that is called when the modal is closed. |
Modal types
Motion Props
export type MotionProps = HTMLMotionProps<"div">; // @see https://www.framer.com/motion/

