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(action-menu): fix keydown support #5642

Merged
merged 4 commits into from
Oct 26, 2022
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
203 changes: 203 additions & 0 deletions src/components/action-menu/action-menu.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,207 @@ describe("calcite-action-menu", () => {

expect(await tooltip.isVisible()).toBe(false);
});

describe("Keyboard navigation", () => {
it("should handle ArrowDown navigation", async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: I think the ⬇️ ⬆️ tests can be merged into one.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to separate them because ArrowDown opens and focuses the first item vs ArrowUp opens and focuses the last item.

const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");

await page.keyboard.press("ArrowDown");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(true);
expect(await actions[0].getProperty("active")).toBe(true);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("ArrowDown");

await page.waitForChanges();

expect(await actions[0].getProperty("active")).toBe(false);
expect(await actions[1].getProperty("active")).toBe(true);
expect(await actions[2].getProperty("active")).toBe(false);
});

it("should handle ArrowUp navigation", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");

await page.keyboard.press("ArrowUp");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(true);
expect(await actions[0].getProperty("active")).toBe(false);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(true);

await page.keyboard.press("ArrowUp");

await page.waitForChanges();

expect(await actions[0].getProperty("active")).toBe(false);
expect(await actions[1].getProperty("active")).toBe(true);
expect(await actions[2].getProperty("active")).toBe(false);
});

it("should handle Enter, Home, End and ESC navigation", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");

await page.keyboard.press("Enter");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(true);
expect(await actions[0].getProperty("active")).toBe(true);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("ArrowDown");

await page.waitForChanges();

expect(await actions[0].getProperty("active")).toBe(false);
expect(await actions[1].getProperty("active")).toBe(true);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("Home");

await page.waitForChanges();

expect(await actions[0].getProperty("active")).toBe(true);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("End");

await page.waitForChanges();

expect(await actions[0].getProperty("active")).toBe(false);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(true);

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

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(false);
});

it("should handle TAB navigation", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");

await page.keyboard.press("ArrowDown");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(true);
expect(await actions[0].getProperty("active")).toBe(true);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("Tab");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(false);
});

it("should click the active action and close the menu", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");

await page.keyboard.press("ArrowDown");

await page.waitForChanges();

const clickSpy = await actions[0].spyOnEvent("click");

expect(await actionMenu.getProperty("open")).toBe(true);
expect(await actions[0].getProperty("active")).toBe(true);
expect(await actions[1].getProperty("active")).toBe(false);
expect(await actions[2].getProperty("active")).toBe(false);

await page.keyboard.press("Enter");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(false);
expect(clickSpy).toHaveReceivedEventTimes(1);
});
});
});
65 changes: 30 additions & 35 deletions src/components/action-menu/action-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { DeprecatedEventPayload, Scale } from "../interfaces";
import { LogicalPlacement, EffectivePlacement, OverlayPositioning } from "../../utils/floating-ui";
import { isActivationKey } from "../../utils/key";

const SUPPORTED_BUTTON_NAV_KEYS = ["ArrowUp", "ArrowDown"];
const SUPPORTED_MENU_NAV_KEYS = ["ArrowUp", "ArrowDown", "End", "Home"];

/**
Expand Down Expand Up @@ -302,7 +301,6 @@ export class ActionMenu {
class={CSS.menu}
id={menuId}
onClick={this.handleCalciteActionClick}
onKeyDown={this.menuActionsContainerKeyDown}
role="menu"
tabIndex={-1}
>
Expand Down Expand Up @@ -394,68 +392,63 @@ export class ActionMenu {

menuButtonKeyDown = (event: KeyboardEvent): void => {
const { key } = event;
const { actionElements } = this;
const { actionElements, activeMenuItemIndex, open } = this;

if (!actionElements.length) {
return;
}

if (isActivationKey(key)) {
event.preventDefault();
this.toggleOpen();
return;
}

if (!this.isValidKey(key, SUPPORTED_BUTTON_NAV_KEYS)) {
return;
}

event.preventDefault();

this.toggleOpen(true);
this.handleActionNavigation(key, actionElements);
};
if (!open) {
this.toggleOpen();
return;
}

menuActionsContainerKeyDown = (event: KeyboardEvent): void => {
const { key } = event;
const { actionElements, activeMenuItemIndex } = this;
const action = actionElements[activeMenuItemIndex];
action ? action.click() : this.toggleOpen(false);
}

if (key === "Tab") {
this.open = false;
return;
}

if (isActivationKey(key)) {
event.preventDefault();
const action = actionElements[activeMenuItemIndex];
action ? action.click() : this.toggleOpen(false);
return;
}

if (key === "Escape") {
this.toggleOpen(false);
event.preventDefault();
return;
}

if (!actionElements.length) {
return;
}
this.handleActionNavigation(event, key, actionElements);
};

if (this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
event.preventDefault();
handleActionNavigation = (
event: KeyboardEvent,
key: string,
actions: HTMLCalciteActionElement[]
): void => {
if (!this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
return;
}

this.handleActionNavigation(key, actionElements);
};
event.preventDefault();

handleActionNavigation = (key: string, actions: HTMLCalciteActionElement[]): void => {
if (!this.open) {
this.toggleOpen();

if (key === "Home" || key === "ArrowDown") {
this.activeMenuItemIndex = 0;
}

if (key === "End" || key === "ArrowUp") {
this.activeMenuItemIndex = actions.length - 1;
}

return;
}

const currentIndex = this.activeMenuItemIndex;

if (key === "Home") {
this.activeMenuItemIndex = 0;
}
Expand All @@ -464,6 +457,8 @@ export class ActionMenu {
this.activeMenuItemIndex = actions.length - 1;
}

const currentIndex = this.activeMenuItemIndex;

if (key === "ArrowUp") {
this.activeMenuItemIndex = getRoundRobinIndex(Math.max(currentIndex - 1, -1), actions.length);
}
Expand Down