https://github.com/itsdouges/react-best-modal
Simple is best. Accessible out of the box, tiny api, bring your own styles.
https://github.com/itsdouges/react-best-modal
a11y accessibility best dialog modal modal-dialog react react-dialog react-modal react-modal-dialog react-portal simple
Last synced: 9 months ago
JSON representation
Simple is best. Accessible out of the box, tiny api, bring your own styles.
- Host: GitHub
- URL: https://github.com/itsdouges/react-best-modal
- Owner: itsdouges
- Created: 2017-12-27T20:39:26.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-12T10:52:37.000Z (almost 8 years ago)
- Last Synced: 2025-03-28T13:44:45.772Z (10 months ago)
- Topics: a11y, accessibility, best, dialog, modal, modal-dialog, react, react-dialog, react-modal, react-modal-dialog, react-portal, simple
- Language: TypeScript
- Homepage: https://madou.github.io/react-best-modal/
- Size: 1.22 MB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# react-best-modal
[](https://www.npmjs.com/package/react-best-modal)
[](https://www.npmjs.com/package/react-best-modal)
[](https://travis-ci.org/madou/react-best-modal)
[](https://codecov.io/gh/madou/react-best-modal)
Simple is best. Accessible out of the box, tiny api, bring your own styles. Don't let libraries you use get in the way of what you want, build up, not down. **React 16+ only.**
## Usage
```sh
npm install react-focus-lock react-best-modal --save
```
```javascript
import React from 'react';
import BestModal from 'react-best-modal';
import { render } from 'react-dom';
const appRoot = document.getElementById('root');
const App = ({ showModal, closeModal }) => (
Hi Mom!
{showModal && (
Hello, World!
)}
);
render(, appRoot);
```
**NOTE**: Make sure you have an element inside your modal that is focusable. Essentially, any input, button, or element with `tabIndex="0"`. This is to ensure focus is set correctly inside the modal - while this isn't enforced, it's strongly advised.
## Styling
It's up to you to style and position your modal. Want to disable scrolling content behind your modal? You have to do it yourself. [There's an example implementation here.](https://madou.github.io/react-best-modal/?selectedKind=Styling&selectedStory=and%20finally%20also%20disabling%20body%20from%20scrolling%20behind)
### Transitions
`react-best-modal` works perfectly with [`ReactTransitionGroup`](https://reactcommunity.org/react-transition-group/).
## Accessibility
If you're driving home for the ultimate accessible modal, make sure to use `aria-labelledby` and `aria-describedby`, for title and description of the modal, like so:
```javascript
close
Hello, World!
This is a modal, hello worlding!
```
### Pro tips
* Have an element that is focusable (for example there is a button above).
* Make sure to embrace `aria-labelledby` and `aria-describedby`.
## Focus
* Focus will be trapped inside the modal when mounted.
* Focus will automatically jump to the modal's first element when mounted, and will return to the previous focused element when unmounted.
## Multiple modals (modals in modals)
This isn't supported. It's also bad practice to show many modals at a time.
## API
### `BestModal`
```javascript
import BestModal from 'react-best-modal';
```
#### Props
| prop | type | required | description |
| ---------------- | -------------------------- | -------- | ----------------------------------------------------------------------------------------------- |
| children | ReactNode | yes | Your modal markup. |
| onRequestClose | (e: KeyboardEvent) => void | yes | Callback when the modal wants to close. Up to you to action it. |
| appRoot | HTMLElement | yes | Root of your application. The modal will add and remove accessible attributes when appropriate. |
| className | string | no | |
| disableFocusLock | boolean | no | Disables focus lock. Useful if something else wants to trap focus. |
All other props that are valid on a `HTMLElement` are passed through.