Skip to content

feat(dialog): add onBeforeButtonClick hook to support validation before closing dialog #251900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ensorrow
Copy link

@ensorrow ensorrow commented Jun 19, 2025

This PR fixes #251899 and adds an optional onBeforeButtonClick callback to the Dialog component, allowing consumers to intercept button clicks and determine whether the dialog should close. This enables scenarios such as form validation, where the dialog should only close if the input is valid.

Motivation

Currently, the Dialog component always resolves its promise and closes when a button is clicked, with no way to prevent this based on user input or validation logic. This makes it impossible to implement dialogs that require validation before closing.

Changes

  • Added an optional onBeforeButtonClick?: (index: number, values?: string[], checkboxChecked?: boolean) => boolean | Promise<boolean> property to IDialogOptions.
  • Updated the button click handler in Dialog.show() to call this callback (if provided) before resolving and closing the dialog.
  • If the callback returns false (or resolves to false), the dialog remains open and the user can correct their input.

Example Usage

const dialog = new Dialog(container, "Please enter your name", ["OK", "Cancel"], {
  onBeforeButtonClick: (index, values) => {
    if (index === 0 && (!values || !values[0])) {
      alert("Name cannot be empty!");
      return false;
    }
    return true;
  },
  // ...other options
});

Benefits

  • Enables form validation and other pre-close logic in dialogs.
  • Improves user experience for all scenarios requiring input validation.

Backward Compatibility

This change is fully backward compatible. If onBeforeButtonClick is not provided, the dialog behaves as before.

@bpasero
Copy link
Member

bpasero commented Jun 19, 2025

Is there an issue that this change addresses that users are hitting today?

@bpasero bpasero added the info-needed Issue requires more information from poster label Jun 19, 2025
@ensorrow
Copy link
Author

Is there an issue that this change addresses that users are hitting today?

Thank you for your question!
Currently, there is no specific issue in the VS Code product that depends on this feature, and users are not directly blocked by the lack of this hook. However, this change addresses a common UI/UX requirement that is present in many modern applications: the ability to validate user input before closing a dialog. Adding this hook now makes the dialog component more robust and flexible. And I think this feature will be useful for the products built on top of vscode oss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed Issue requires more information from poster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dialog component does not support preventing close on button click for validation scenarios
3 participants