-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(react-dialog): respect autoFocus element over first focusable on dialog open
#36038
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
Copilot
wants to merge
5
commits into
master
Choose a base branch
from
copilot/fix-autofocus-dialog-issue
base: master
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.
+87
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f836615
Initial plan
Copilot 0a9325c
fix: prefer autofocus element over findFirstFocusable in Dialog/Drawe…
Copilot add9204
fix: simplify autofocus element lookup using optional chaining
Copilot c91b181
fix: resolve lint errors in useFocusFirstElement.test.ts
Copilot 7a7a658
Merge branch 'master' into copilot/fix-autofocus-dialog-issue
tudorpopams 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-dialog-890df3cd-7a3d-4215-a779-d73397bba5f0.json
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,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "fix: prefer autofocus element over first focusable in Dialog/Drawer (#35749)", | ||
| "packageName": "@fluentui/react-dialog", | ||
| "email": "198982749+Copilot@users.noreply.github.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
packages/react-components/react-dialog/library/src/utils/useFocusFirstElement.test.ts
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,78 @@ | ||
| import type * as React from 'react'; | ||
| import { renderHook, act } from '@testing-library/react-hooks'; | ||
| import { useFocusFinders } from '@fluentui/react-tabster'; | ||
| import { useFluent_unstable } from '@fluentui/react-shared-contexts'; | ||
| import { useFocusFirstElement } from './useFocusFirstElement'; | ||
|
|
||
| jest.mock('@fluentui/react-tabster'); | ||
| jest.mock('@fluentui/react-shared-contexts'); | ||
|
|
||
| const mockFindFirstFocusable = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (useFocusFinders as jest.Mock).mockReturnValue({ findFirstFocusable: mockFindFirstFocusable }); | ||
| (useFluent_unstable as jest.Mock).mockReturnValue({ targetDocument: document }); | ||
| }); | ||
|
|
||
| describe('useFocusFirstElement', () => { | ||
| it('focuses the first focusable element when dialog opens and no autofocus element is present', () => { | ||
| const container = document.createElement('div'); | ||
| const firstButton = document.createElement('button'); | ||
| container.appendChild(firstButton); | ||
| document.body.appendChild(container); | ||
|
|
||
| mockFindFirstFocusable.mockReturnValue(firstButton); | ||
| const focusSpy = jest.spyOn(firstButton, 'focus'); | ||
|
|
||
| const { result, rerender } = renderHook(({ open }) => useFocusFirstElement(open, 'modal'), { | ||
| initialProps: { open: false }, | ||
| }); | ||
|
|
||
| // Attach the ref to the container | ||
| (result.current as React.RefObject<HTMLElement | null>).current = container; | ||
|
|
||
| act(() => { | ||
| rerender({ open: true }); | ||
| }); | ||
|
|
||
| expect(mockFindFirstFocusable).toHaveBeenCalledWith(container); | ||
| expect(focusSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| it('focuses the autofocus element instead of the first focusable element when autofocus element is present', () => { | ||
| const container = document.createElement('div'); | ||
| const firstButton = document.createElement('button'); | ||
| const autoFocusInput = document.createElement('input'); | ||
| autoFocusInput.setAttribute('autofocus', ''); | ||
|
|
||
| container.appendChild(firstButton); | ||
| container.appendChild(autoFocusInput); | ||
| document.body.appendChild(container); | ||
|
|
||
| mockFindFirstFocusable.mockReturnValue(firstButton); | ||
| const firstButtonFocusSpy = jest.spyOn(firstButton, 'focus'); | ||
| const autoFocusInputFocusSpy = jest.spyOn(autoFocusInput, 'focus'); | ||
|
|
||
| const { result, rerender } = renderHook(({ open }) => useFocusFirstElement(open, 'modal'), { | ||
| initialProps: { open: false }, | ||
| }); | ||
|
|
||
| // Attach the ref to the container | ||
| (result.current as React.RefObject<HTMLElement | null>).current = container; | ||
|
|
||
| act(() => { | ||
| rerender({ open: true }); | ||
| }); | ||
|
|
||
| // findFirstFocusable should not be called since autofocus element was found | ||
| expect(mockFindFirstFocusable).not.toHaveBeenCalled(); | ||
| // The autofocus element should be focused, not the first button | ||
| expect(autoFocusInputFocusSpy).toHaveBeenCalledTimes(1); | ||
| expect(firstButtonFocusSpy).not.toHaveBeenCalled(); | ||
|
|
||
| document.body.removeChild(container); | ||
| }); | ||
| }); |
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.
🕵🏾♀️ visual changes to review in the Visual Change Report
vr-tests-react-components/Charts-DonutChart 2 screenshots
vr-tests-react-components/Menu Converged - submenuIndicator slotted content 2 screenshots
vr-tests-react-components/Positioning 2 screenshots
vr-tests-react-components/ProgressBar converged 2 screenshots
vr-tests-react-components/TagPicker 1 screenshots
There were 1 duplicate changes discarded. Check the build logs for more information.