Skip to content

fix(dialog): fix escapeDisabled when the escape key is pressed on the panel #10097

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

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/calcite-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,8 @@ export namespace Components {
*/
"embedded": boolean;
/**
* When `true`, disables the default close on escape behavior. By default, an open dialog can be dismissed by pressing the Esc key. Depending on what the dialog represents, it may not be desired for this behavior. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#accessibility
* When `true`, disables the default close on escape behavior. By default, an open dialog can be dismissed by pressing the Esc key.
* @see [Dialog Accessibility](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#accessibility)
*/
"escapeDisabled": boolean;
/**
Expand Down Expand Up @@ -9692,7 +9693,8 @@ declare namespace LocalJSX {
*/
"embedded"?: boolean;
/**
* When `true`, disables the default close on escape behavior. By default, an open dialog can be dismissed by pressing the Esc key. Depending on what the dialog represents, it may not be desired for this behavior. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#accessibility
* When `true`, disables the default close on escape behavior. By default, an open dialog can be dismissed by pressing the Esc key.
* @see [Dialog Accessibility](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#accessibility)
*/
"escapeDisabled"?: boolean;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,18 @@ describe("calcite-dialog", () => {
await page.waitForChanges();

const dialog = await page.find("calcite-dialog");
const panel = await page.find(`calcite-dialog >>> calcite-panel`);
expect(await dialog.getProperty("open")).toBe(true);

await page.keyboard.press("Escape");
await panel.press("Escape");
await page.waitForChanges();

expect(await dialog.getProperty("open")).toBe(true);

dialog.setProperty("escapeDisabled", false);
await page.waitForChanges();

await page.keyboard.press("Escape");
await panel.press("Escape");
await page.waitForChanges();

expect(await dialog.getProperty("open")).toBe(false);
Expand Down
9 changes: 8 additions & 1 deletion packages/calcite-components/src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export class Dialog
messageOverrides={this.messageOverrides}
onCalcitePanelClose={this.handleCloseClick}
onCalcitePanelScroll={this.handleScroll}
onKeyDown={this.handlePanelKeyDown}
overlayPositioning={this.overlayPositioning}
ref={(el) => (this.panelEl = el)}
scale={this.scale}
Expand Down Expand Up @@ -461,10 +462,16 @@ export class Dialog
this.calciteDialogScroll.emit();
};

private handleCloseClick = () => {
private handleCloseClick = (): void => {
this.open = false;
};

private handlePanelKeyDown = (event: KeyboardEvent): void => {
if (this.escapeDisabled) {
event.preventDefault();
}
};

private async openDialog(): Promise<void> {
await componentOnReady(this.el);
this.el.addEventListener("calciteDialogOpen", this.openEnd);
Expand Down
Loading
Loading