Skip to content

Dialog: How to lazy import modals without breaking Transitions? #2142

Answered by mcgrealife
mcgrealife asked this question in Help
Discussion options

You must be logged in to vote

I found one solution:

In the parent component:
{showLazyModal && <LazyModal />}

Inside the LazyModal:

  1. initialize the <Transition.Root/> open state as false.
    const [open, setOpen] = useState(false)

  2. UseEffect on first mount to setOpen(true)
    useEffect(() => setOpen(true), [])

This keeps the intro transition

Then in the <Dialog/>'s onClose handler:

onClose={() => {
  setOpen(false) // local state
  setTimeout(() => setShowLazyModal(false), 300) // parent state
}}

Where the timeout duration is the same duration used in the <Transition.Child/>'s leave classes

It feels like an anti-pattern but works

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Phyllon
Comment options

Answer selected by mcgrealife
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants