Skip to content

Modal: no way to veto a close before the exit animation runs #77731

Closed

Description

@ramonjd

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 in headerActions.
  • Setting shouldCloseOnEsc shouldCloseOnClickOutside to false while 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

  1. added
    [Type] Enhancement
    [Package] Components
    [Type] Question
    [Priority] Low
    and removed
    [Type] Enhancement
    on Apr 28, 2026
  2. mirka commented on May 15, 2026

    @mirka
    Member

    This should be a non-issue in the new Dialog component from @wordpress/ui, which we're working towards marking as ready for use (cc @ciampo).

  3. ramonjd commented on May 15, 2026

    @ramonjd
    MemberAuthor

    Thanks @mirka !

    I'll close for less noise and reopen if it's necessary, which appears it won't be.

  4. ciampo commented on May 15, 2026

    @ciampo
    Contributor

    @ramonjd even if we don't recommend you actually use it in the media modal just yet, it would be valuable if you took Dialog for a spin, in case you had any feedback/questions

  5. ramonjd commented on May 18, 2026

    @ramonjd
    MemberAuthor

    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

    Dialog seems 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!

    🚀

  6. ciampo commented on May 18, 2026

    @ciampo
    Contributor

    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 of Dialog) ships with integrated support for async tasks on confirmation (demo). We'll need to understand exactly what patterns to recommend for Dialog, too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Package] Components[Priority] Low[Type] Question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions