Skip to content

fix: MenuItem is only focused when mouse cursor is moved #33725

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

Merged
merged 2 commits into from
Jan 24, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: MenuItem is only focused when mouse cursor is moved",
"packageName": "@fluentui/react-menu",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ describe('MenuItem', () => {
expect(tree).toMatchSnapshot();
});

it('should focus the item on mouseenter', () => {
it('should focus the item on mousemove', () => {
// Arrange
const { getByRole } = render(<MenuItem>Item</MenuItem>);

// Act
const menuitem = getByRole('menuitem');
fireEvent.mouseEnter(menuitem);
fireEvent.mouseMove(menuitem);

// Assert
expect(document.activeElement).toBe(menuitem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`MenuItem renders a default state 1`] = `
onClick={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseEnter={[Function]}
onMouseMove={[Function]}
role="menuitem"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export const useMenuItem_unstable = (props: MenuItemProps, ref: React.Ref<ARIABu
dismissedWithKeyboardRef.current = true;
}
}),
onMouseEnter: useEventCallback(event => {
innerRef.current?.focus();
onMouseMove: useEventCallback(event => {
if (event.currentTarget.ownerDocument.activeElement !== event.currentTarget) {
innerRef.current?.focus();
}

props.onMouseEnter?.(event);
props.onMouseMove?.(event);
}),
onClick: useEventCallback(event => {
if (!hasSubmenu && !persistOnClick) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`MenuItemCheckbox conformance renders a default state 1`] = `
onClick={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseEnter={[Function]}
onMouseMove={[Function]}
role="menuitemcheckbox"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`MenuItemRadio renders a default state 1`] = `
onClick={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseEnter={[Function]}
onMouseMove={[Function]}
role="menuitemradio"
tabIndex={0}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('MenuList', () => {
</div>,
);
cy.get(menuItemSelector).each(el => {
cy.wrap(el).trigger('mouseover').should('be.focused');
cy.wrap(el).trigger('mousemove').should('be.focused');
});
});

Expand Down
Loading