I am investigating how to build an accessible dialog modal in React.
I saw few sources suggesting to place the modal at the end of the DOM tree as direct child of the
E.g: https://assortment.io/posts/accessible-modal-component-react-portals-part-1
(Look for the When rendered, the Modal is appended to the end of document.body.. section.)
My question is.. WHY? What benefit is this bringing? If I am not wrong, some of these sources meant that in this way the screen reader would ignore the other children of the body, so save the user from focusing on not desired elements.
If this is the reason, is this the suggested and only way of doing it? My idea was to simply "trap" the user inside the modal not allowing them to focus on anything else outside the modal. My idea was that if the user is on the first or last element of of the modal and is trying to go back or forward they will still be focusing on the first or last element of the modal. That using JS.
So, going back to the main question, why should I place the modal as a direct child of the DOM? Thank you!