Repository navigation
Description
Problem
Note
Framing this as a question as I might be completely off here. Please close if it can't or shouldn't be fix.
Modal's onRequestClose fires after the close animation completes. Consumers that want to show a "discard changes?" confirm dialog on close end up with a visible flicker: the modal animates away, the confirm appears, and if the user picks "keep editing" the modal snaps back into place.
All four close paths runcloseModal().then(onRequestClose):
- X button (
packages/components/src/modal/index.tsx:330) - ESC key (
:200) - Backdrop click (
:239)
There's no prop to intercept the close intent before the animation, so the consumer has no way to cancel without paying the visual cost.
Kapture.2026-04-28.at.12.49.17.mp4
Workarounds today
In #77730 (media editor) we ended up:
- Setting
isDismissible={false}and rendering a custom X inheaderActions. - Setting
shouldCloseOnEscshouldCloseOnClickOutsidetofalsewhile there are unsaved changes (so ESC/backdrop become no-ops rather than flickering).
That works but reimplements behavior that should belong to Modal, and means each consumer reinvents it.
Proposal
Add an opt-in prop that fires before the exit animation and can veto the close:
onConfirmClose?: () => boolean | Promise<boolean>;Returning false cancels the close (no animation, no onRequestClose). Returning true (or omitting the prop) preserves today's flow.
Alternatives considered: onBeforeClose(close) callback-style, but onConfirmClose is narrower and matches similar patterns in router/dialog libs.
Use cases beyond media editor: post editor unsaved-changes prompts, custom forms in modals, anywhere the consumer wants to inspect state at close time.
Activity
mirka commentedon May 15, 2026
This should be a non-issue in the new
Dialogcomponent from@wordpress/ui, which we're working towards marking as ready for use (cc @ciampo).ramonjd commentedon May 15, 2026
Thanks @mirka !
I'll close for less noise and reopen if it's necessary, which appears it won't be.
ciampo commentedon May 15, 2026
@ramonjd even if we don't recommend you actually use it in the media modal just yet, it would be valuable if you took
Dialogfor a spin, in case you had any feedback/questionsramonjd commentedon May 18, 2026
I spun up a simple demo for the use case outlined in this issue, and didn't run into the old closing sequence bug.
2026-05-18.12.11.30.mp4
Dialogseems to work a lot better because I can control its open state directly, i.e., I'm checking whether closing is allowed before the animation runs. And then the animation only runs if I set it to closed.Looking forward to this component. Thanks, folks!
🚀
ciampo commentedon May 18, 2026
Neat!
We may ask for some extra feedback about building solid confirmation UX.
For example, the new
AlertDialog(which is a higher-level, focused version ofDialog) ships with integrated support for async tasks on confirmation (demo). We'll need to understand exactly what patterns to recommend forDialog, too