Repository navigation
fix(menu): don't show focus ring when MenuItem is focused by hover #4368
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
Open
udit (UditDewan)
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
UditDewan:fix/menuitem-stale-focus-ring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@fluentui-react-native/menu': patch | ||
| --- | ||
|
|
||
| Fix `MenuItem` showing a focus ring when focus moves to it from mouse hover on win32. The hover-initiated focus is now tracked with a ref instead of stale pressable state. |
64 changes: 64 additions & 0 deletions
64
packages/components/Menu/src/MenuItem/useMenuItem.test.win32.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import * as React from 'react'; | ||
| import { act } from 'react'; | ||
| import * as renderer from 'react-test-renderer'; | ||
|
|
||
| import type { MenuItemInfo } from './MenuItem.types'; | ||
| import { useMenuItem } from './useMenuItem'; | ||
|
|
||
| describe('useMenuItem focus ring (win32)', () => { | ||
| let info: MenuItemInfo; | ||
| const Probe = () => { | ||
| info = useMenuItem({ componentRef: React.useRef({ focus: jest.fn(), blur: jest.fn() } as any) }); | ||
| return null; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| act(() => { | ||
| renderer.create(<Probe />); | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps the focus ring hidden when hover moves focus before React re-renders', () => { | ||
| // Simulate the native focus event firing with the handlers from the same render as onHoverIn. | ||
| const { onHoverIn, onFocus } = info.props; | ||
| act(() => { | ||
| onHoverIn({} as any); | ||
| onFocus({} as any); | ||
| }); | ||
| expect(info.props.enableFocusRing).toBe(false); | ||
| }); | ||
|
|
||
| it('shows the focus ring for a keyboard focus after a hover focus', () => { | ||
| act(() => { | ||
| info.props.onHoverIn({} as any); | ||
| }); | ||
| act(() => { | ||
| info.props.onFocus({} as any); | ||
| }); | ||
| expect(info.props.enableFocusRing).toBe(false); | ||
|
|
||
| act(() => { | ||
| info.props.onBlur({} as any); | ||
| }); | ||
| act(() => { | ||
| info.props.onHoverOut({} as any); | ||
| }); | ||
| act(() => { | ||
| info.props.onFocus({} as any); | ||
| }); | ||
| expect(info.props.enableFocusRing).toBe(true); | ||
| }); | ||
|
|
||
| it('shows the focus ring for a keyboard focus after a hover that never focused the item', () => { | ||
| // Hover in and out before the hover focus lands, so no focus event comes from the hover. | ||
| const { onHoverIn, onHoverOut } = info.props; | ||
| act(() => { | ||
| onHoverIn({} as any); | ||
| onHoverOut({} as any); | ||
| }); | ||
| act(() => { | ||
| info.props.onFocus({} as any); | ||
| }); | ||
| expect(info.props.enableFocusRing).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Cancel pending hover-focus intent when the hover ends without focus
This ref is cleared only by a subsequent
onFocus, not byonHoverOutor cancellation of the focus request. If hover-in and hover-out are processed before a commit,useHoverFocusEffectsees the finalhovered=falseand never requests focus, butisHoverFocus.currentremains true. The next genuine keyboardonFocusthen consumes the stale flag and leavesenableFocusRing=false. The same stale-intent issue occurs when the imperative focus request does not deliver a focus event. Please clear or otherwise invalidate pending hover intent when that hover/request ends, and extend the new hook tests with hover-in, hover-out, then keyboard focus without an intervening hover-focus event. The existing second test always supplies that event, so it misses this path.