Repository navigation
Recommended way to open a Dialog imperatively (e.g. an awaitable confirm)? #3881
kimyouknow
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Dialogis controlled — you ownopenand passonClose. That's clean when a dialog lives in one component, but it gets awkward forconfirm()-style flows: "user clicks delete → show a dialog → only proceed if they confirm." You end up hoisting auseStateplus a stashed promise resolver, and repeating that per dialog.There's also no imperative open — #3428 covers this (still open): the only workaround is
static+ managing state yourself, which as that thread puts it "kind of defeats the purpose of using the component at all."useClosehelps on the closing side but doesn't hand the user's choice back.What's removed this boilerplate for us is opening the dialog imperatively and awaiting the result. We use overlay-kit (small MIT overlay manager, been running it in prod for a couple years). The
Dialogstays exactly as controlled as before — overlay-kit just owns the open lifecycle and returns the result as a promise:open/onCloseare still the Dialog's own props, so focus trapping and a11y are untouched — the only things that move out are theuseStateand the "did they say yes" plumbing. It also lets you trigger a dialog from places that aren't a component render, like an API error handler.I realize open-state management is something Headless UI deliberately leaves to userland, so this may be out of scope on purpose. But imperative open keeps coming up (#3428), so I'm curious: is a recommended pattern for this something you'd want in the docs, or is controlled-only the intended stance? There's a writeup of the overlay-kit +
Dialogsetup (transitions and cleanup included) here if it's useful: https://overlay-kit.slash.page/en/docs/more/with-design-systems/headless-uiHappy to send a docs PR if there's appetite.
All reactions