Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/react/src/menu/root/MenuRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ describe('<Menu.Root />', () => {
await user.keyboard('[ArrowDown]');
await user.keyboard('[ArrowDown]');
await user.keyboard('[ArrowDown]');
await user.keyboard('[ArrowDown]');

const submenuTrigger1 = await screen.findByTestId('submenu-trigger');
await waitFor(() => {
Expand Down Expand Up @@ -999,6 +998,31 @@ describe('<Menu.Root />', () => {
});
});

it('focuses the first item when the menu is opened programmatically', async () => {
function App() {
const [open, setOpen] = React.useState(false);
return (
<div>
<button type="button" onClick={() => setOpen(true)}>
external
</button>
<TestMenu rootProps={{ open, onOpenChange: setOpen }} />
</div>
);
}

const { user } = await render(<App />);

await user.click(screen.getByRole('button', { name: 'external' }));

const [firstItem, ...otherItems] = await screen.findAllByRole('menuitem');
await waitFor(() => expect(firstItem).toHaveFocus());
expect(firstItem.tabIndex).toBe(0);
otherItems.forEach((item) => {
expect(item.tabIndex).toBe(-1);
});
});

it('focuses the trigger after the menu is closed', async () => {
const { user } = await render(
<div>
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/menu/root/MenuRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ export const MenuRoot = fastComponent(function MenuRoot<Payload>(props: MenuRoot
openOnArrowKeyDown: parent.type !== 'context-menu',
externalTree: nested ? floatingTreeRoot : undefined,
focusItemOnHover: highlightItemOnHover,
// Ensure an item is highlighted on open even when opened programmatically (no trigger keydown).
focusItemOnOpen: true,
});

const onTyping = React.useCallback(
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/menubar/Menubar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,11 @@ describe('<Menubar />', () => {
const fileTrigger = screen.getByTestId('file-trigger');
await user.click(fileTrigger);

// Menu should be open
// Menu should be open with the first item focused
await waitFor(() => {
expect(screen.queryByTestId('file-menu')).not.toBe(null);
});

// Navigate with keyboard
await user.keyboard('{ArrowDown}');
const firstItem = screen.getByTestId('file-item-1');
await waitFor(() => {
expect(firstItem).toHaveFocus();
Expand Down
Loading