-
Notifications
You must be signed in to change notification settings - Fork 810
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
Warning: react-modal: App element is not defined. #576
Comments
@StokeMasterJack we made some changes recently to api to define the |
Can you check if setting Ref: https://github.com/reactjs/react-modal#app-element |
Here is the exact log message: Warning: react-modal: App element is not defined. Please use |
Oh sorry, thought it was an error. This is related to this change #570. This is a reminder that not setting the app element will/can cause accessibility issues. |
So, you need to explicit tell the modal that you don't want to use the app element with |
I'll keep this open. Documentation is missing for this behavior. |
Can this feature be explained a bit more? When I try to use it I'm getting an error like so: Also, is there a reason this configuration can't be encapsulated into the Modal props and must use |
Ah, I think the issue was my server rendering of the app. Once I wrapped the I'm a still a bit unsure if things are working properly though especially because the docs state "If you are doing server-side rendering, you should use this property". That doesn't seem to make sense to me because the DOM is not even available at that point yet. |
@dlong500 The documentation related to this is outdated. Also, we need to tests how the modal will work on a SSR environment. I did some tests to get it working, but very superficial - just to make it work. For the multiple modal case, you can use |
I am also getting this in the console log. No code changes in terms of my use of react-modal either. Not sure why I am getting it. |
Nevermind I just read the thread again. Looks like I need the warning is correct. We need just need to specify it. |
Hello, I got the same warning, how do you guys make use of appElement? Is it |
Missing Documentation: adding Modal.setAppElement('body') to the componentWillMount lifecycle event makes Modal friendly to screen readers, and removes the associated Warning to the Consol: > "Warning: react-modal: App element is not defined. Please use `Modal.setAppElement(el)` or set `appElement={el}`" issue reactjs#133 and closes reactjs#576
add the following code: componentWillMount() { Modal.setAppElement('body'); } To prevent this accessibility warning in the console, whenever the Modal opens, > Warning: react-modal: App element is not defined. Please use `Modal. setAppElement(el)` or `set appElement={el}`. This is needed so screen readers don't see main content when modal is opened. It is not recommended, but you can opt-out by setting `ariaHideApp={false}` react-modal issue-133 describes the issue, and solutions are provided in the discussion: reactjs/react-modal#133 I also summarized these solutions on StackOverflow: https://stackoverflow.com/questions/48269381/warning-react-modal-app-element-is-not-defined-please-use-modal-setappeleme/48270342#48270342 And submitted github-issue/waffle item to Udacity: https://github.com/udacity/reactnd-issues/issues/233 Also in the process to updating the documents so I can submit a PR to for related issue-576: reactjs/react-modal#576
I was seeing this when the modal is unmounted without having been triggered. Adding this fixed the issue: Modal.setAppElement('#app-base'); |
`react-modal` introduced a new check to make sure an "app element" is specified so that it can be hidden for accessibility when the modal is open. I needed to add it to the default test html as several tests cause the modal to be rendered and it complains about the missing element otherwise. https://github.com/reactjs/react-modal#app-element reactjs/react-modal#576
@mockdeep Where did you add |
@sksundram right now I've got it in each of the files that I'm importing import Modal from 'react-modal';
import React from 'react';
function HelpModal({isOpen, closeModal}) {
return (
<Modal isOpen={isOpen} onRequestClose={closeModal}>
'modal content'
</Modal>
);
}
Modal.setAppElement('#app-base');
export default HelpModal; |
This addresses reactjs#576.
* Add more information about styling CSS classes and transitions * Add section on accessibility, including app element details (addresses reactjs#576) * Create missing `examples/README.md` and `contributing/README.md` * Reorganize initial book section (with subsection labels)
* Add more information about styling CSS classes and transitions * Add section on accessibility, including app element details (addresses reactjs#576) * Create missing `examples/README.md` and `contributing/README.md` * Reorganize initial book section (with subsection labels)
* Add more information about styling CSS classes and transitions * Add section on accessibility, including app element details (addresses #576) * Create missing `examples/README.md` and `contributing/README.md` * Reorganize initial book section (with subsection labels)
For reference, since it was a pain for me, if you are doing SSR, use the following snippet:
|
@diasbruno I wouldn't mind preparing the documentation for the ssr and appElement subjects. For the meantime, just for reference, for the projects which use react-modal, there is no need anymore to add the safety check that @NathanielHill suggested. Instead of doing:
It is safe enough to do:
|
@lbelinsk Sure, it would be really great. |
@NathanielHill @lbelinsk |
Fair point. For me the issue was just setting it to begin with. If what your saying is correct, that does sound like an accessibility issue, so if your React app has a container |
using the react-testing-library (https://testing-library.com/) I get rid of that warning with:
or, if you want to test the modal component directly:
|
Adding ariaHideApp={false} to modal works for me. |
Solved it, in my case it was Modal.setAppElement("#root"); in the modal component |
Got rid of the annoying logs in tests with |
To all... This is the correct behavior for react-modal as already mention on this thread. As @catamphetamine already mention and many others that have been working with accessibility, there is an old mistake with the default To not break users code, we decided to make This also applies to testing. You need to properly setup your tests to use react-modal when needed. This is an example to do that (note that you might need to change a little bit depending on the testing framework you use): let modalNode = null;
before(() => {
modalNode = document.createElement('div');
modalNode.id = "modals";
document.appendChild(modalNode);
ReactModal.setAppElement('#app');
}):
after(() => {
document.removeChild(modalNode);
}); |
In Next.js App Router, sometimes the client components could be pre-rendered in the server. I had been using Using a So I think the below would be the correct way to do this. Even if the component is pre-rendered in server, it would hydrate the appElement with the correct element once the pre-render result is transmitted to the client. Is this correct? import { useEffect, useState } from 'react'
import ReactModal from 'react-modal'
// in the component:
const [appElement, setAppElement] = useState<NodeListOf<Element>>()
useEffect(() => {
// When Next.js pre-renders client components in server to generate initial HTML,
// document is not defined. `?` will not work here, hence the effect & state variable.
setAppElement(document.querySelectorAll('.container'))
}, [])
return (
<ReactModal
appElement={appElement}
// ...
>
</ReactModal>
) I hope I'm not the first one to figure this out. 😆 |
In next js 13.5.4 I found the error. I solved it as follows: I added the following command to the pages/app.tsx directory import Modal from 'react-modal'; and before defining my app() function I added: Modal.setAppElement('body'); import Modal from 'react-modal';
Modal.setAppElement('body');
export default function app(){ |
I upgraded my yarn version and re-ran yarn install. And now I am getting this warning.
It's strange, because nothing in my code has changed. Something in node_modules is obviously different but who knows what. I can't do a diff on node_modules because that was never check in to git.
The text was updated successfully, but these errors were encountered: