Situation
I built a React Modal component using React Portals (see Docs above). Before unmounting the component when the close button is clicked, I want to run an exit
animation with Framer Motion using AnimatePresence. Unfortunately, I can't make it work and need help.
Links
What I tried
I added
exit={{ opacity: 0 }}
to a child of<RenderModal/>
. The entering animation usinginitial
andanimate
worked as expected.
- Wrap
<AnimatePresence>
around modal-root element
<div id="root"></div>
<AnimatePresence>
<div id="modal-root"></div>
</AnimatePresence>
- Wrap as child of modal-root
Error: Target container requires a DOM element
<div id="modal-root">
<AnimatePresence></AnimatePresence>
</div>
- Wrap around the component element
const Modal = ({
title,
footer,
children,
}) => {
<AnimatePresence>
{isVisible
&& (
<RenderModal
title={title}
footer={footer}
hide={hide}
>
{children}
</RenderModal>
)}
</AnimatePresence>
};
- Wrap around the used Component
return (
<>
<Button onClick={show}>Open Modal</Button>
<AnimatePresence>
<Modal {...args}></Modal>
</AnimatePresence>
<p>Lorem ipsum dolor sit amet...</p>
</>
);