Skip to content
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

fix(panel): support cancelling the esc key event to prevent closing a panel #9032

Merged
merged 1 commit into from
Apr 5, 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
24 changes: 24 additions & 0 deletions packages/calcite-components/src/components/panel/panel.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,28 @@ describe("calcite-panel", () => {
expect(await panel.getProperty("closed")).toBe(true);
expect(await container.isVisible()).toBe(false);
});

it("should not close when Escape key is prevented and closable is true", async () => {
const page = await newE2EPage();
await page.setContent("<calcite-panel closable>test</calcite-panel>");
const panel = await page.find("calcite-panel");
const container = await page.find(`calcite-panel >>> .${CSS.container}`);

expect(await panel.getProperty("closed")).toBe(false);
expect(await container.isVisible()).toBe(true);

await page.$eval("calcite-panel", (panel: HTMLCalcitePanelElement) => {
panel.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
event.preventDefault();
}
});
});

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

expect(await panel.getProperty("closed")).toBe(false);
expect(await container.isVisible()).toBe(true);
});
});
12 changes: 7 additions & 5 deletions packages/calcite-components/src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Event,
EventEmitter,
h,
Host,
Method,
Prop,
State,
Expand Down Expand Up @@ -602,7 +603,6 @@ export class Panel
aria-busy={toAriaBoolean(loading)}
class={CSS.container}
hidden={closed}
onKeyDown={panelKeyDownHandler}
tabIndex={closable ? 0 : -1}
// eslint-disable-next-line react/jsx-sort-props -- ref should be last so node attrs/props are in sync (see https://github.com/Esri/calcite-design-system/pull/6530)
ref={this.setContainerRef}
Expand All @@ -614,10 +614,12 @@ export class Panel
);

return (
<InteractiveContainer disabled={disabled}>
{loading ? <calcite-scrim loading={loading} /> : null}
{panelNode}
</InteractiveContainer>
<Host onKeyDown={panelKeyDownHandler}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moves keydown handler to the host so it can be properly cancelled

For my own understanding, does this fix the issue because it allows the slotted content to handle and cancel the event before it bubbles up to the host?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own understanding, does this fix the issue because it allows the slotted content to handle and cancel the event before it bubbles up to the host?

Correct. Previously, since the event was internal, a user couldn't cancel it. It already happens by the time it bubbles up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dory-gi-fs-finding-nemo

Thanks for clarifying!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be helpful in the conventions doc. Would you mind adding a note on this to the native event cancellation section?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<InteractiveContainer disabled={disabled}>
{loading ? <calcite-scrim loading={loading} /> : null}
{panelNode}
</InteractiveContainer>
</Host>
);
}
}
Loading