-
Notifications
You must be signed in to change notification settings - Fork 67
Export useFocusTrap hook
#1420
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
Export useFocusTrap hook
#1420
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f6fc3c
Expose `useFocusTrap`
danielguillan a80dbf1
Address `useFocusTrap` review findings
danielguillan 16a7629
Move focus capture into effect
danielguillan 14fd6ba
Address focus trap review feedback
danielguillan ee8ccad
Deduplicate useFocusTrap test component
danielguillan bbb6f4b
Fix useFocusTrap test helper shadowing
danielguillan 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 @@ | ||
| --- | ||
| '@primer/react-brand': minor | ||
| --- | ||
|
|
||
| Exported `useFocusTrap` from the package root and created stable container and initial-focus refs when they are not provided. |
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,16 @@ | ||
| /** @jest-environment node */ | ||
|
|
||
| import React from 'react' | ||
| import {renderToString} from 'react-dom/server' | ||
| import {useFocusTrap} from './useFocusTrap' | ||
|
|
||
| describe('useFocusTrap SSR', () => { | ||
| it('renders without a document', () => { | ||
| const TestComponent = () => { | ||
| useFocusTrap() | ||
| return null | ||
| } | ||
|
|
||
| expect(() => renderToString(<TestComponent />)).not.toThrow() | ||
| }) | ||
| }) |
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,112 @@ | ||
| import React from 'react' | ||
| import {focusTrap} from '@primer/behaviors' | ||
| import {render, renderHook} from '@testing-library/react' | ||
| import {useFocusTrap} from './useFocusTrap' | ||
|
|
||
| jest.mock('@primer/behaviors', () => ({ | ||
| focusTrap: jest.fn(), | ||
| })) | ||
|
|
||
| const mockFocusTrap = jest.mocked(focusTrap) | ||
|
|
||
| describe('useFocusTrap', () => { | ||
| beforeEach(() => { | ||
| mockFocusTrap.mockReturnValue(new AbortController()) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('creates stable refs when refs are not provided', () => { | ||
| const {result, rerender} = renderHook(() => useFocusTrap({disabled: true})) | ||
| const initialContainerRef = result.current.containerRef | ||
| const initialFocusRef = result.current.initialFocusRef | ||
|
|
||
| expect(initialContainerRef.current).toBeNull() | ||
| expect(initialFocusRef.current).toBeNull() | ||
|
|
||
| rerender() | ||
|
|
||
| expect(result.current.containerRef).toBe(initialContainerRef) | ||
| expect(result.current.initialFocusRef).toBe(initialFocusRef) | ||
| }) | ||
|
|
||
| it('preserves provided refs', () => { | ||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| const initialFocusRef = React.createRef<HTMLButtonElement>() | ||
| const {result} = renderHook(() => useFocusTrap({containerRef, initialFocusRef, disabled: true})) | ||
|
|
||
| expect(result.current.containerRef).toBe(containerRef) | ||
| expect(result.current.initialFocusRef).toBe(initialFocusRef) | ||
| }) | ||
|
|
||
| it('passes generated ref elements to the focus trap', () => { | ||
| const TestComponent = () => { | ||
| const {containerRef, initialFocusRef} = useFocusTrap<HTMLDivElement, HTMLButtonElement>() | ||
|
|
||
| return ( | ||
| <div ref={containerRef}> | ||
| <button ref={initialFocusRef} /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const {container} = render(<TestComponent />) | ||
|
|
||
| expect(mockFocusTrap).toHaveBeenCalledWith(container.querySelector('div'), container.querySelector('button')) | ||
| }) | ||
|
|
||
| it('starts and aborts the focus trap when disabled changes', () => { | ||
| const abortController = new AbortController() | ||
| const abortSpy = jest.spyOn(abortController, 'abort') | ||
| mockFocusTrap.mockReturnValue(abortController) | ||
|
|
||
| const TestComponent = ({disabled}: {disabled: boolean}) => { | ||
| const {containerRef} = useFocusTrap<HTMLDivElement>({disabled}) | ||
| return <div ref={containerRef} /> | ||
| } | ||
|
|
||
| const {rerender} = render(<TestComponent disabled />) | ||
|
|
||
| expect(mockFocusTrap).not.toHaveBeenCalled() | ||
|
|
||
| rerender(<TestComponent disabled={false} />) | ||
|
|
||
| expect(mockFocusTrap).toHaveBeenCalledTimes(1) | ||
|
|
||
| rerender(<TestComponent disabled />) | ||
|
|
||
| expect(abortSpy).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('captures fresh focus after cleaning up a non-HTMLElement active element', () => { | ||
| const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') | ||
| svg.setAttribute('tabindex', '0') | ||
| document.body.append(svg) | ||
| svg.focus() | ||
|
|
||
| const nextFocusedElement = document.createElement('button') | ||
| document.body.append(nextFocusedElement) | ||
|
|
||
| const TestComponent = ({disabled}: {disabled: boolean}) => { | ||
|
danielguillan marked this conversation as resolved.
Outdated
|
||
| const {containerRef} = useFocusTrap<HTMLDivElement>({disabled, restoreFocusOnCleanUp: true}) | ||
| return <div ref={containerRef} /> | ||
| } | ||
|
|
||
| const {rerender} = render(<TestComponent disabled={false} />) | ||
|
|
||
| rerender(<TestComponent disabled />) | ||
| nextFocusedElement.focus() | ||
|
|
||
| const focusSpy = jest.spyOn(nextFocusedElement, 'focus') | ||
|
|
||
| rerender(<TestComponent disabled={false} />) | ||
| rerender(<TestComponent disabled />) | ||
|
|
||
| expect(focusSpy).toHaveBeenCalled() | ||
|
|
||
| svg.remove() | ||
| nextFocusedElement.remove() | ||
| }) | ||
| }) | ||
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
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.
Does this need to be a separate file @danielguillan? Can we add the test case to the useFocusTrap.test.tsx file?
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.
I believe so. My understanding is that Jest’s node environment is file-scoped.