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

Menu should close and move to next focusable element on <Tab> #1639

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/@headlessui-react/src/components/menu/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ describe('Keyboard interactions', () => {

describe('`Tab` key', () => {
it(
'should focus trap when we use Tab',
'should close when we use Tab',
suppressConsoleLogs(async () => {
render(
<Menu>
Expand Down Expand Up @@ -1401,9 +1401,9 @@ describe('Keyboard interactions', () => {
// Try to tab
await press(Keys.Tab)

// Verify it is still open
assertMenuButton({ state: MenuState.Visible })
assertMenu({ state: MenuState.Visible })
// Verify it is closed
assertMenuButton({ state: MenuState.InvisibleUnmounted })
assertMenu({ state: MenuState.InvisibleUnmounted })
})
)

Expand Down Expand Up @@ -1450,9 +1450,9 @@ describe('Keyboard interactions', () => {
// Try to Shift+Tab
await press(shift(Keys.Tab))

// Verify it is still open
assertMenuButton({ state: MenuState.Visible })
assertMenu({ state: MenuState.Visible })
// Verify it is closed
assertMenuButton({ state: MenuState.InvisibleUnmounted })
assertMenu({ state: MenuState.InvisibleUnmounted })
})
)
})
Expand Down
3 changes: 1 addition & 2 deletions packages/@headlessui-react/src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ let Items = forwardRefWithAs(function Items<TTag extends ElementType = typeof DE
break

case Keys.Tab:
event.preventDefault()
event.stopPropagation()
dispatch({ type: ActionTypes.CloseMenu })
break

default:
Expand Down
16 changes: 8 additions & 8 deletions packages/@headlessui-vue/src/components/menu/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ describe('Keyboard interactions', () => {
})

describe('`Tab` key', () => {
it('should focus trap when we use Tab', async () => {
it('should not focus trap when we use Tab', async () => {
renderTemplate(jsx`
<Menu>
<MenuButton>Trigger</MenuButton>
Expand Down Expand Up @@ -1697,12 +1697,12 @@ describe('Keyboard interactions', () => {
// Try to tab
await press(Keys.Tab)

// Verify it is still open
assertMenuButton({ state: MenuState.Visible })
assertMenu({ state: MenuState.Visible })
// Verify it is closed
assertMenuButton({ state: MenuState.InvisibleUnmounted })
assertMenu({ state: MenuState.InvisibleUnmounted })
})

it('should focus trap when we use Shift+Tab', async () => {
it('should not focus trap when we use Shift+Tab', async () => {
renderTemplate(jsx`
<Menu>
<MenuButton>Trigger</MenuButton>
Expand Down Expand Up @@ -1743,9 +1743,9 @@ describe('Keyboard interactions', () => {
// Try to Shift+Tab
await press(shift(Keys.Tab))

// Verify it is still open
assertMenuButton({ state: MenuState.Visible })
assertMenu({ state: MenuState.Visible })
// Verify it is closed
assertMenuButton({ state: MenuState.InvisibleUnmounted })
assertMenu({ state: MenuState.InvisibleUnmounted })
})
})

Expand Down
3 changes: 1 addition & 2 deletions packages/@headlessui-vue/src/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ export let MenuItems = defineComponent({
break

case Keys.Tab:
event.preventDefault()
event.stopPropagation()
api.closeMenu()
break

default:
Expand Down