Dialog: How to lazy import modals without breaking Transitions? #2142
-
The examples show rendering a mounted, with its internal open state initialized as false. This works well, however, some of my modals have grown in size and are clogging the initial bundle. So I'd like to lazy import these large dialogs. So I wrap the entire modal in state like: And change the 's internal open state to initialize as true. The problem with this pattern is that the Transitions (enterFrom, enterTo, etc.) are ignored. Maybe because showModal mounts/unmounts the component immediately. Does anyone know of a better pattern to lazy import modals? One way might be to keep the non-lazy mounted with internal show state initialized as false (like example docs), and instead Lazy import the Dialog content? Any advice is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I found one solution: In the parent component: Inside the LazyModal:
This keeps the intro transition Then in the
Where the timeout duration is the same duration used in the It feels like an anti-pattern but works |
Beta Was this translation helpful? Give feedback.
I found one solution:
In the parent component:
{showLazyModal && <LazyModal />}
Inside the LazyModal:
initialize the
<Transition.Root/>
open state as false.const [open, setOpen] = useState(false)
UseEffect on first mount to setOpen(true)
useEffect(() => setOpen(true), [])
This keeps the intro transition
Then in the
<Dialog/>
's onClose handler:Where the timeout duration is the same duration used in the
<Transition.Child/>
's leave classesIt feels like an anti-pattern but works