-
-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add keepMounted option and fix screen glitch #516
Open
pikaju
wants to merge
6
commits into
pradel:master
Choose a base branch
from
pikaju:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bcf60cd
Add keepMounted option
pikaju c9a1da9
Fix keepMounted being required
pikaju 53d933b
Fix bugs
pikaju d2c9bb4
Fix modal not being scrollable
pikaju b0a9d6f
Add example for keepMounted option
pikaju 11fd42a
Improve how modals are centered
pikaju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ A simple responsive and accessible react modal. | |||||
- Multiple modals. | ||||||
- Accessible modals. | ||||||
- Easily customizable via props. | ||||||
- Optionally keep hidden modals mounted to the DOM | ||||||
- Typescript support | ||||||
- [Small bundle size](https://bundlephobia.com/result?p=react-responsive-modal) | ||||||
|
||||||
|
@@ -103,6 +104,26 @@ You can also set to trap focus within the modal, but decide where to put focus w | |||||
|
||||||
``` | ||||||
|
||||||
### Keeping a hidden modal mounted | ||||||
|
||||||
By setting the `keepMounted` property to `true`, you can specify that the modal should always be mounted to the DOM, even when hidden it is hidden. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found a small typo:
Suggested change
|
||||||
Instead of removing the DOM nodes, the modal is hidden using `display: none`. | ||||||
|
||||||
```js | ||||||
<Modal | ||||||
... | ||||||
keepMounted | ||||||
> | ||||||
... | ||||||
</Modal> | ||||||
``` | ||||||
|
||||||
This is useful when you want the state of the DOM nodes (e.g. the text inside an `<input>` element) to stay alive, as well as for search engine optimization (Google can see the content of the hidden modal). | ||||||
|
||||||
Press the button below and watch the element tree in your browser. The modal is a `<div>` at the very bottom of the `<body>`, and will not disappear even when the modal is closed! | ||||||
|
||||||
<ExampleRendered name="keepMounted" /> | ||||||
|
||||||
### Custom styling with css | ||||||
|
||||||
Customising the Modal style via css is really easy. For example if you add the following css to your app you will get the following result: | ||||||
|
@@ -205,6 +226,7 @@ By default, the Modal will be rendered at the end of the html body tag. If you w | |||||
| **onEscKeyDown\*** | `(event: KeyboardEvent) => void` | | Callback fired when the escape key is pressed. | | ||||||
| **onOverlayClick\*** | `(event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void` | | Callback fired when the overlay is clicked. | | ||||||
| **onAnimationEnd\*** | `() => void` | | Callback fired when the Modal has exited and the animation is finished. | | ||||||
| **keepMounted** | `boolean` | false | Keeps the Modal mounted to the DOM tree even when it is hidden. This is useful for keeping DOM state inside the Modal as well as for SEO purposes. | | ||||||
|
||||||
## License | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import { Modal } from 'react-responsive-modal'; | ||
|
||
const App = () => { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
return ( | ||
<> | ||
<button className="button" onClick={() => setOpen(true)}> | ||
Open modal | ||
</button> | ||
|
||
<Modal open={open} onClose={() => setOpen(false)} keepMounted center> | ||
<div style={{ padding: '24px' }}> | ||
This modal will stay mounted even when closed. | ||
<br /> | ||
<br /> | ||
<input | ||
defaultValue="And DOM state of this <input> will be kept alive!" | ||
style={{ | ||
border: '1px solid black', | ||
borderRadius: '4px', | ||
padding: '2px', | ||
}} | ||
/> | ||
</div> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please explain why this is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a separate issue #495. I copied the solution from one of the people in that thread. To be honest, I'm not sure why it helps, but it does.