### Install react-responsive-modal with npm or yarn
Source: https://react-responsive-modal.leopradel.com/
Install react-responsive-modal v7.2.0 in your project. Use either npm or yarn.
```bash
npm install react-responsive-modal --save
# Or with yarn
yarn add react-responsive-modal
```
--------------------------------
### Custom container with ref
Source: https://react-responsive-modal.leopradel.com/
Use the container prop to render the modal inside a specific DOM element instead of the end of the body. The example uses a ref to target a div.
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = React.useState(false);
const myRef = React.useRef(null);
return (
<>
setOpen(false)}
center
container={myRef.current}
>
Take a look with the devtools, you can see that the modal is inside
the div we are targeting and not at the end of the body tag.
>
);
};
export default App;
```
--------------------------------
### Custom styling with CSS using classNames
Source: https://react-responsive-modal.leopradel.com/
Customize the modal and overlay appearance by defining CSS classes and passing them via the `classNames` prop. The CSS file must be imported in the component (commented out in the example).
```css
/* examples/custom-styling.css */
.customOverlay {
background: rgba(36, 123, 160, 0.7);
}
.customModal {
background: #b2dbbf;
max-width: 500px;
width: 100%;
}
```
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
// import './examples/custom-styling.css';
const [open, setOpen] = React.useState(false);
return (
<>
setOpen(false)}
center
classNames={{
overlay: 'customOverlay',
modal: 'customModal',
}}
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
pulvinar risus non risus hendrerit venenatis. Pellentesque sit amet
hendrerit risus, sed porttitor quam.
>
);
};
export default App;
```
--------------------------------
### Custom close icon with SVG
Source: https://react-responsive-modal.leopradel.com/
Use the closeIcon prop to replace the default close icon with your own SVG. The example shows a React component with a button that opens the modal and a custom SVG icon.
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = React.useState(false);
const closeIcon = (
);
return (
<>
setOpen(false)}
center
closeIcon={closeIcon}
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
pulvinar risus non risus hendrerit venenatis. Pellentesque sit amet
hendrerit risus, sed porttitor quam.
>
);
};
export default App;
```
--------------------------------
### Open a simple centered modal with react-responsive-modal
Source: https://react-responsive-modal.leopradel.com/
Basic usage: renders a centered modal with a button to open it. Requires importing 'react-responsive-modal/styles.css'. For Next.js, import the styles in pages/_app.js or pages/_app.tsx; for Create React App, import them in index.js or index.tsx. Wrap modal content in an element so the close icon is not shown on top.
```jsx
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import 'react-responsive-modal/styles.css';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = useState(false);
const onOpenModal = () => setOpen(true);
const onCloseModal = () => setOpen(false);
return (
Simple centered modal
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
pulvinar risus non risus hendrerit venenatis. Pellentesque sit amet
hendrerit risus, sed porttitor quam.
);
};
ReactDOM.render(, document.getElementById('app'));
```
--------------------------------
### Custom animation with CSS keyframes and classNames
Source: https://react-responsive-modal.leopradel.com/
Define custom enter and leave animations for the overlay and modal using CSS keyframes, then reference them via the `classNames` prop. The `animationDuration` prop sets the animation length (800ms here). The CSS file must be imported (commented out).
```css
/* examples/custom-animation.css */
@keyframes customEnterOverlayAnimation {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes customLeaveOverlayAnimation {
0% {
transform: scale(1);
}
100% {
transform: scale(0);
}
}
@keyframes customEnterModalAnimation {
0% {
transform: scale(0.2);
}
100% {
transform: scale(1);
}
}
@keyframes customLeaveModalAnimation {
0% {
transform: scale(1);
}
100% {
transform: scale(0.2);
}
}
```
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
// import './examples/custom-animation.css';
const [open, setOpen] = React.useState(false);
return (
<>
setOpen(false)}
center
classNames={{
overlayAnimationIn: 'customEnterOverlayAnimation',
overlayAnimationOut: 'customLeaveOverlayAnimation',
modalAnimationIn: 'customEnterModalAnimation',
modalAnimationOut: 'customLeaveModalAnimation',
}}
animationDuration={800}
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
pulvinar risus non risus hendrerit venenatis. Pellentesque sit amet
hendrerit risus, sed porttitor quam.
>
);
};
export default App;
```
--------------------------------
### Focus Trapped modal with initialFocusRef
Source: https://react-responsive-modal.leopradel.com/
Uses the `initialFocusRef` prop to set focus to the modal root element when opened, instead of the first focusable element. Requires a ref to the modal element and passes it to both `ref` and `initialFocusRef`.
```tsx
import React, { useRef } from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = React.useState(false);
const modalRef = useRef(null);
return (
<>
setOpen(false)}
initialFocusRef={modalRef}
>
Try tabbing/shift-tabbing thru elements
>
);
};
export default App;
```
--------------------------------
### Modal Component
Source: https://react-responsive-modal.leopradel.com/
The Modal component is the main API surface. It accepts props to control open state, styling, accessibility, and callbacks. The component follows ARIA best practices and provides focus trapping and scroll blocking.
```APIDOC
## Modal Component
### Description
The Modal component is the main API surface. It accepts props to control open state, styling, accessibility, and callbacks. The component follows ARIA best practices and provides focus trapping and scroll blocking.
### Props
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| **open*** | `boolean` | | Control if the modal is open or not. |
| **center** | `boolean` | false | Should the dialog be centered. |
| **closeOnEsc** | `boolean` | true | Is the modal closable when user press esc key. |
| **closeOnOverlayClick** | `boolean` | true | Is the modal closable when user click on overlay. |
| **blockScroll** | `boolean` | true | Whether to block scrolling when dialog is open. |
| **showCloseIcon** | `boolean` | true | Show the close icon. |
| **closeIconId** | `string` | | id attribute for the close icon button. |
| **closeIcon** | `React.ReactNode` | | Custom icon to render (svg, img, etc...). |
| **focusTrapped** | `boolean` | true | When the modal is open, trap focus within it. |
| **initialFocusRef** | `React.RefElement` | undefined | Sets focus on this specific element when modal opens if focus trap is used. |
| **container** | `Element` | | You can specify a container prop which should be of type `Element`. The portal will be rendered inside that element. The default behavior will create a div node and render it at the at the end of document.body. |
| **classNames** | `{ root?: string; overlay?: string; overlayAnimationIn?: string; overlayAnimationOut?: string; modal?: string; modalAnimationIn?: string; modalAnimationOut?: string; closeButton?: string; closeIcon?: string; }` | | An object containing classNames to style the modal. |
| **styles** | `{ root?: React.CSSProperties; overlay?: React.CSSProperties; overlay?: React.CSSProperties; modalContainer?: React.CSSProperties; modal?: React.CSSProperties; closeButton?: React.CSSProperties; closeIcon?: React.CSSProperties; }` | | An object containing the styles objects to style the modal. |
| **animationDuration** | `number` | 300 | Animation duration in milliseconds. |
| **role** | `string` | "dialog" | ARIA role for modal |
| **ref** | `React.RefElement` | undefined | Ref for modal dialog element |
| **ariaLabel** | `string` | | ARIA label for modal |
| **ariaLabelledby** | `string` | | ARIA labelledby for modal |
| **ariaDescribedby** | `string` | | ARIA description for modal |
| **dir** | `"ltr" \| "rtl" \| "auto"` | | Text direction of the modal content. |
| **containerId** | `string` | | id attribute for modal container |
| **modalId** | `string` | | id attribute for modal |
| **onClose*** | `() => void` | | Callback fired when the Modal is requested to be closed by a click on the overlay or when user press esc key. |
| **onEscKeyDown*** | `(event: KeyboardEvent) => void` | | Callback fired when the escape key is pressed. |
| **onOverlayClick*** | `(event: React.MouseEvent) => void` | | Callback fired when the overlay is clicked. |
| **onAnimationEnd*** | `(event: React.AnimationEvent) => void` | | Callback fired when the Modal has exited and the animation is finished. |
### Accessibility
- Follow the ARIA best practices by using either:
- `ariaLabelledby` and `ariaDescribedby` props to reference visible content, OR
- `ariaLabel` prop for a simple accessible label
- `aria-modal` is set to true automatically.
- When the modal is open the focus is trapped within it.
- Clicking on the overlay closes the Modal.
- Pressing the "Esc" key closes the Modal.
- When the modal is open the page scroll is blocked for the elements behind the modal.
- Closing the modal will unblock the scroll.
- The modal is rendered in a portal at the end of the `body`.
### Usage Examples
#### Option 1: Using ariaLabelledby and ariaDescribedby
```jsx
My Title
My Description
```
#### Option 2: Using ariaLabel
```jsx
Preferences
Update your settings below
```
```
--------------------------------
### Create a modal with a lot of scrollable content
Source: https://react-responsive-modal.leopradel.com/
Shows a modal with a large amount of content. When the content overflows the window, you can scroll the modal content, but the body scroll remains locked until the modal is closed.
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = React.useState(false);
const lorem = (
Mauris ac arcu sit amet dui interdum bibendum a sed diam. Praesent rhoncus
congue ipsum elementum lobortis. Ut ligula purus, ultrices id condimentum
quis, tincidunt quis purus. Proin quis enim metus. Nunc feugiat odio at
eros porta, ut rhoncus lorem tristique. Nunc et ipsum eu ex vulputate
consectetur vel eu nisi. Donec ultricies rutrum lectus, sit ame feugiat
est semper vitae. Proin varius imperdiet consequat. Proin eu metus nisi.
In hac habitasse platea dictumst. Vestibulum ac ultrices risus.
Pellentesque arcu sapien, aliquet sed orci sit amet, pulvinar interdum
velit. Nunc a rhoncus ipsum, maximus fermentum dolor. Praesent aliquet
justo vitae rutrum volutpat. Ut quis pulvinar est.
);
return (
<>
setOpen(false)}>
Big modal
{lorem}
{lorem}
{lorem}
{lorem}
{lorem}
{lorem}
{lorem}
{lorem}
{lorem}
>
);
};
export default App;
```
--------------------------------
### Accessible Modal with ariaLabelledby, ariaDescribedby, or ariaLabel
Source: https://react-responsive-modal.leopradel.com/
Shows two ways to make the modal accessible: referencing visible content with ariaLabelledby and ariaDescribedby, or providing a simple ariaLabel. The modal automatically sets aria-modal to true, traps focus, closes on overlay click or Esc, blocks page scroll, and renders in a portal at the end of the body.
```jsx
// Option 1: Using ariaLabelledby and ariaDescribedby
My Title
My Description
// Option 2: Using ariaLabel
Preferences
Update your settings below
```
--------------------------------
### Render multiple modals simultaneously
Source: https://react-responsive-modal.leopradel.com/
Renders two modals at the same time. Clicking the overlay or pressing 'esc' will only close the last modal.
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [openFirst, setOpenFirst] = React.useState(false);
const [openSecond, setOpenSecond] = React.useState(false);
const littleLorem = (
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pulvinar
risus non risus hendrerit venenatis. Pellentesque sit amet hendrerit
risus, sed porttitor quam.
);
return (
<>
setOpenFirst(false)} center>
First modal
{littleLorem}
setOpenSecond(false)} center>
Second modal
{littleLorem}
>
);
};
export default App;
```
--------------------------------
### Focus Trapped modal with default focus
Source: https://react-responsive-modal.leopradel.com/
Default focus behavior: the first focusable element is focused when the modal opens, and Tab cycles through elements inside the modal while preventing focus outside. Set the `focusTrapped` prop to `false` to disable this behavior.
```jsx
import React from 'react';
import { Modal } from 'react-responsive-modal';
const App = () => {
const [open, setOpen] = React.useState(false);
return (
<>
setOpen(false)}>
Try tabbing/shift-tabbing thru elements
>
);
};
export default App;
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.