Why is onClose not called when clicking outside or clicking the X in an Obsidian modal?

Hi Obsidian community,
I’m working on an Obsidian plugin that uses modals, and I’ve encountered an issue with the onClose event. I’ve noticed that onClose is not consistently called when the modal is closed in certain ways. Specifically, it doesn’t seem to be triggered when:

  • Clicking outside the modal.
  • Clicking the “X” button to close the modal.
    Here’s a simplified version of my modal code:
import { Modal } from "obsidian";

export class MyModal extends Modal {
  onOpen() {
    const { contentEl } = this;
    contentEl.setText("My Modal");
  }

  onClose() {
    console.log("Modal closed!");
  }
}

I would expect the “Modal closed!” message to appear in the console every time the modal is closed, regardless of how it’s closed. However, this only happens when I explicitly call this.close() within the modal.
Is this the expected behavior? If so, is there a recommended way to reliably detect when a modal is closed, regardless of the method used?
Any insights or suggestions would be greatly appreciated!
Thanks in advance.

Works for me in both scenarios

PowerToys_F3fUiEINK3

Oh, that’s weird.
I blindly assumed the problem was in the handling and not in my particular implementation.
Thank you for confirm, I will investigate it further