fix(AlertDialog): fire onCancel when dialog is dismissed via Escape key#10136
Open
EduardF1 wants to merge 8 commits into
Open
fix(AlertDialog): fire onCancel when dialog is dismissed via Escape key#10136EduardF1 wants to merge 8 commits into
EduardF1 wants to merge 8 commits into
Conversation
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>
TypeScript infers the parameter type from DialogContextValue.onKeyDown (React.KeyboardEventHandler<HTMLElement>), so the explicit annotation is not needed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: