Skip to content

fix(AlertDialog): fire onCancel when dialog is dismissed via Escape key#10136

Open
EduardF1 wants to merge 8 commits into
adobe:mainfrom
EduardF1:fix/alert-dialog-escape-fires-on-cancel
Open

fix(AlertDialog): fire onCancel when dialog is dismissed via Escape key#10136
EduardF1 wants to merge 8 commits into
adobe:mainfrom
EduardF1:fix/alert-dialog-escape-fires-on-cancel

Conversation

@EduardF1
Copy link
Copy Markdown

Summary

Fixes #1773

When a user presses Escape to close an AlertDialog, the onCancel callback was not being invoked. This happened because the Escape key is intercepted by the Modal/Tray overlay (which calls state.close() directly) and completely bypasses the cancel button's onPress handler that chains onClose() + onCancel().

Root Cause

In AlertDialog.tsx, the cancel button wires up:
sx onPress={() => chain(onClose(), onCancel())}

But when Escape is pressed, the Modal overlay handles it by calling state.close() directly, never touching onCancel. The AlertDialog had no mechanism to intercept this.

Fix

Provide a custom DialogContext inside AlertDialog that injects an onKeyDown handler. When the dialog's section element receives a keydown event for Escape and onCancel was provided, the callback fires before the overlay closes the dialog.

The onKeyDown reaches the section element via the existing useDialog(mergeProps(contextProps, props)) call in Dialog.tsx, which passes all valid DOM attributes (including onKeyDown) through filterDOMProps into the rendered element.

Tests

Added two new test cases to AlertDialog.test.js:

  • Escape fires onCancel when the prop is provided
  • No unexpected calls when onCancel is not provided

EduardF1 and others added 2 commits May 30, 2026 02:32
When a user presses Escape to close an AlertDialog, the onCancel callback
was not being invoked. This happened because Escape is handled by the
Modal/Tray overlay (which calls state.close() directly) and bypasses the
cancel button's onPress handler that chains onClose() + onCancel().

Fix: Provide a custom DialogContext value inside AlertDialog that injects
an onKeyDown handler into the dialog's merged props. When the dialog's
<section> element receives a keydown event for Escape and an onCancel prop
was provided, the onCancel callback fires before the overlay closes the
dialog.

The onKeyDown is merged into the dialog element's props via the existing
useDialog(mergeProps(contextProps, props)) call in Dialog.tsx, which
passes through all valid DOM attributes (including onKeyDown) from context.

Adds two new tests:
- Escape fires onCancel when the prop is provided
- No unexpected calls when onCancel is not provided

Fixes adobe#1773

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
filterDOMProps strips event handlers, so onKeyDown injected via
DialogContext (e.g., by AlertDialog for Escape key handling) was lost.
Explicitly extract and merge contextOnKeyDown into dialogProps so it
reaches the rendered <section> element.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

onCancel should fire in AlertDialog if Escape is pressed

1 participant