20
loading...
This website collects cookies to deliver better user experience
div#modal-root
element in our root document.z-index
on the application root and the modal-root.ModalPortal
component that will link our modal to the div#modal-root
elementModalView
component that aims to handle the visible part of the componentModalAnimated
component that will handle the popup domain and the CSS appearance effects of the popup systemModalPortal
component exists to link our popup to the div#modal-root
element that we have created. Here’s the code:ref
corresponding to a simple div
element, with the goal of holding the popup content. We do not use directly the root element so we are able to create two or more different popups if we want to stack them.useEffect
hook to create the div
element. This is a security to make the system work also on SSR systems such as NextJs or Gatsby.useEffect
hook, to add the previously created div
in the portal when active, and remove it when inactive. It will prevent the div#modal-root
element to contain plenty of empty divs.div
element created does not exist or the popup is not currently active.alert
and confirm
modalaria-label
attribute.div#modal-root
DOM elementprefers-reduced-motion
media query to shutdown the animation for people asking for it.useEscape(active, onClose);
code in the ModalAnimated component, but this is yet to be implemented. Here’s the code:onEscape
callback that memoize the keyboard event by listening to the keyCode for the escape key — 27useEffect
method to bind it to the window document and unbind it as soon as the modal is unmountedModalAnimated
component with two props if we want a custom ModalView component.ModalAnimated
. I usually put the content inside another component to keep the page as light as possible. Here’s the code: