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: Menubar navigation between menus while a menu is open #990

Merged
merged 1 commit into from
Dec 10, 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
5 changes: 5 additions & 0 deletions .changeset/gold-paws-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: issue where you were unable to navigate to the previous menu from within a menu of the menubar via arrow keys
2 changes: 2 additions & 0 deletions packages/bits-ui/src/lib/bits/menubar/menubar.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MenubarRootState {
id: this.id,
ref: this.ref,
});

this.rovingFocusGroup = useRovingFocus({
rootNodeId: this.id,
candidateAttr: TRIGGER_ATTR,
Expand Down Expand Up @@ -386,6 +387,7 @@ class MenubarContentState {
"--bits-menubar-anchor-height": "var(--bits-floating-anchor-height)",
},
onkeydown: this.onkeydown,
"data-menu-content": "",
}) as const
);
}
Expand Down
52 changes: 38 additions & 14 deletions packages/tests/src/tests/menubar/menubar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ const kbd = getTestKbd();
/**
* Helper function to reduce boilerplate in tests
*/
function setup(props: Menubar.RootProps = {}, menuId: string = "1") {
function setup(props: Menubar.RootProps = {}) {
const user = userEvent.setup();
const returned = render(MenubarTest, { ...props });
const { getByTestId } = returned;
const trigger = getByTestId(`${menuId}-trigger`);
return { user, ...returned, trigger };

const getTrigger = (id: string) => getByTestId(`${id}-trigger`);
const getContent = (id: string) => getByTestId(`${id}-content`);
const getSubTrigger = (id: string) => getByTestId(`${id}-sub-trigger`);
const getSubContent = (id: string) => getByTestId(`${id}-sub-content`);

return { user, ...returned, getTrigger, getContent, getSubTrigger, getSubContent };
}

describe("menubar", () => {
Expand All @@ -28,7 +33,8 @@ describe("menubar", () => {

it.skip("should have bits data attrs", async () => {
const menuId = "1";
const { user, trigger, getByTestId, queryByTestId } = setup({}, menuId);
const { user, getTrigger, getByTestId, queryByTestId } = setup();
const trigger = getTrigger(menuId);
await user.click(trigger);
await user.click(trigger);
const content = queryByTestId("1-content");
Expand Down Expand Up @@ -65,28 +71,46 @@ describe("menubar", () => {
});

it("should navigate triggers within the menubar using arrow keys", async () => {
const { user, trigger, getByTestId } = setup({}, "1");
const { user, getTrigger } = setup();
const trigger = getTrigger("1");
trigger.focus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("2-trigger")).toHaveFocus();
expect(getTrigger("2")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("3-trigger")).toHaveFocus();
expect(getTrigger("3")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("4-trigger")).toHaveFocus();
expect(getTrigger("4")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("1-trigger")).toHaveFocus();
expect(getTrigger("1")).toHaveFocus();
});

it("should respect the loop prop", async () => {
const { user, trigger, getByTestId } = setup({ loop: false }, "1");
const { user, getTrigger } = setup({ loop: false });
const trigger = getTrigger("1");
trigger.focus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("2-trigger")).toHaveFocus();
expect(getTrigger("2")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getTrigger("3")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("3-trigger")).toHaveFocus();
expect(getTrigger("4")).toHaveFocus();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("4-trigger")).toHaveFocus();
expect(getTrigger("4")).toHaveFocus();
});

it("should navigate between menus when using the arrow keys and focus is within a menu", async () => {
const { user, getTrigger, getContent } = setup();
const trigger = getTrigger("1");
trigger.focus();
await user.keyboard(kbd.ARROW_DOWN);
const content1 = getContent("1");
expect(content1).toBeVisible();
await user.keyboard(kbd.ARROW_RIGHT);
expect(getByTestId("4-trigger")).toHaveFocus();
const content2 = getContent("2");
expect(content2).toBeVisible();
expect(content1).not.toBeVisible();
await user.keyboard(kbd.ARROW_LEFT);
expect(getContent("1")).toBeVisible();
expect(content2).not.toBeVisible();
});
});
Loading