-
Notifications
You must be signed in to change notification settings - Fork 839
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
[EuiPopover][EuiContextMenu] Attempt to manually restore focus to the toggling button on popover close if focus becomes stranded #5760
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b60e810
Upgrade `tabbable` to latest to get `focusable` API
cee-chen 8eb8105
Update popovers to manually attempt to always focus back onto the tog…
cee-chen ac56282
Update downstream snapshots
cee-chen a429a4c
Add changelog entry
cee-chen ae21121
Move `refocusButtonOnClose` logic to only occur on escape key
cee-chen 254dc79
Narrow down scope of bugfix further
cee-chen 75342b4
hello snapshots my old friend
cee-chen c01d2f4
Tweak changelog entry
cee-chen 39e49e4
Add clearTimeout for unmount/open of stranded focus logic
cee-chen 148ddb2
[PR feedback] fix accidental deletion
cee-chen 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import React, { ReactNode } from 'react'; | ||
import { render, mount } from 'enzyme'; | ||
import { requiredProps } from '../../test/required_props'; | ||
import { EuiFocusTrap } from '../'; | ||
|
||
import { | ||
EuiPopover, | ||
|
@@ -383,33 +384,36 @@ describe('EuiPopover', () => { | |
}); | ||
|
||
describe('listener cleanup', () => { | ||
let _raf: typeof window['requestAnimationFrame']; | ||
let _caf: typeof window['cancelAnimationFrame']; | ||
let rafSpy: jest.SpyInstance; | ||
let cafSpy: jest.SpyInstance; | ||
const activeAnimationFrames = new Map<number, number>(); | ||
let nextAnimationFrameId = 0; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
_raf = window.requestAnimationFrame; | ||
_caf = window.cancelAnimationFrame; | ||
|
||
const activeAnimationFrames = new Map<number, number>(); | ||
let nextAnimationFrameId = 0; | ||
window.requestAnimationFrame = (fn) => { | ||
const animationFrameId = nextAnimationFrameId++; | ||
activeAnimationFrames.set(animationFrameId, setTimeout(fn)); | ||
return animationFrameId; | ||
}; | ||
window.cancelAnimationFrame = (id: number) => { | ||
const timeoutId = activeAnimationFrames.get(id); | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
activeAnimationFrames.delete(id); | ||
} | ||
}; | ||
jest.spyOn(window, 'clearTimeout'); | ||
rafSpy = jest | ||
.spyOn(window, 'requestAnimationFrame') | ||
.mockImplementation((fn) => { | ||
const animationFrameId = nextAnimationFrameId++; | ||
activeAnimationFrames.set(animationFrameId, setTimeout(fn)); | ||
return animationFrameId; | ||
}); | ||
cafSpy = jest | ||
.spyOn(window, 'cancelAnimationFrame') | ||
.mockImplementation((id: number) => { | ||
const timeoutId = activeAnimationFrames.get(id); | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
activeAnimationFrames.delete(id); | ||
} | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
window.requestAnimationFrame = _raf; | ||
window.cancelAnimationFrame = _caf; | ||
rafSpy.mockRestore(); | ||
cafSpy.mockRestore(); | ||
}); | ||
|
||
it('cleans up timeouts and rAFs on unmount', () => { | ||
|
@@ -422,10 +426,21 @@ describe('EuiPopover', () => { | |
isOpen={false} | ||
/> | ||
); | ||
expect(window.clearTimeout).toHaveBeenCalledTimes(0); | ||
|
||
component.setProps({ isOpen: true }); | ||
expect(window.clearTimeout).toHaveBeenCalledTimes(3); | ||
expect(rafSpy).toHaveBeenCalledTimes(1); | ||
expect(activeAnimationFrames.size).toEqual(1); | ||
|
||
jest.advanceTimersByTime(10); | ||
expect(rafSpy).toHaveBeenCalledTimes(2); | ||
expect(activeAnimationFrames.size).toEqual(2); | ||
|
||
component.unmount(); | ||
expect(window.clearTimeout).toHaveBeenCalledTimes(10); | ||
expect(cafSpy).toHaveBeenCalledTimes(2); | ||
expect(activeAnimationFrames.size).toEqual(0); | ||
|
||
// EUI's jest configuration throws an error if there are any console.error calls, like | ||
// React's setState on an unmounted component warning | ||
|
@@ -436,7 +451,89 @@ describe('EuiPopover', () => { | |
|
||
// execute any pending timeouts or animation frame callbacks | ||
// and validate the timeout/rAF clearing done by EuiPopover | ||
jest.advanceTimersByTime(10); | ||
jest.advanceTimersByTime(300); | ||
}); | ||
}); | ||
|
||
describe('onEscapeKey', () => { | ||
const closePopover = jest.fn(); | ||
const closingTransitionTime = 250; // TODO: DRY out var when converting to CSS-in-JS | ||
|
||
const mockEvent = { | ||
preventDefault: () => {}, | ||
stopPropagation: () => {}, | ||
} as Event; | ||
|
||
beforeAll(() => jest.useFakeTimers()); | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(document.activeElement as HTMLElement)?.blur(); // Reset focus between tests | ||
}); | ||
afterAll(() => jest.useRealTimers()); | ||
|
||
it('closes the popover and refocuses the toggle button', () => { | ||
const toggleButtonEl = React.createRef<HTMLButtonElement>(); | ||
const toggleButton = <button ref={toggleButtonEl} />; | ||
|
||
const component = mount( | ||
<EuiPopover | ||
isOpen={true} | ||
button={toggleButton} | ||
closePopover={closePopover} | ||
{...requiredProps} | ||
/> | ||
); | ||
component.find(EuiFocusTrap).invoke('onEscapeKey')!(mockEvent); | ||
component.setProps({ isOpen: false }); | ||
jest.advanceTimersByTime(closingTransitionTime); | ||
|
||
expect(closePopover).toHaveBeenCalled(); | ||
expect(document.activeElement).toEqual(toggleButtonEl.current); | ||
}); | ||
|
||
it('refocuses the first nested toggle button on focus trap deactivation', () => { | ||
const toggleButtonEl = React.createRef<HTMLButtonElement>(); | ||
const toggleDiv = ( | ||
<div> | ||
<button ref={toggleButtonEl} tabIndex={-1} /> | ||
Comment on lines
+494
to
+498
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just want to note that this |
||
<button tabIndex={-1} /> | ||
</div> | ||
); | ||
|
||
const component = mount( | ||
<EuiPopover | ||
isOpen={true} | ||
button={toggleDiv} | ||
closePopover={closePopover} | ||
{...requiredProps} | ||
/> | ||
); | ||
component.find(EuiFocusTrap).invoke('onEscapeKey')!(mockEvent); | ||
component.setProps({ isOpen: false }); | ||
jest.advanceTimersByTime(closingTransitionTime); | ||
|
||
expect(closePopover).toHaveBeenCalled(); | ||
expect(document.activeElement).toEqual(toggleButtonEl.current); | ||
}); | ||
|
||
it('does not refocus if the toggle button is not focusable', () => { | ||
const toggleDivEl = React.createRef<HTMLDivElement>(); | ||
const toggleDiv = <div ref={toggleDivEl} />; | ||
|
||
const component = mount( | ||
<EuiPopover | ||
button={toggleDiv} | ||
isOpen={true} | ||
closePopover={closePopover} | ||
{...requiredProps} | ||
/> | ||
); | ||
component.find(EuiFocusTrap).invoke('onEscapeKey')!(mockEvent); | ||
component.setProps({ isOpen: false }); | ||
jest.advanceTimersByTime(closingTransitionTime); | ||
|
||
expect(closePopover).toHaveBeenCalled(); | ||
expect(document.activeElement).not.toEqual(toggleDivEl.current); | ||
}); | ||
}); | ||
}); | ||
|
This file contains 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 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,3 @@ | ||
**Bug fixes** | ||
|
||
- Fixed keyboard focus being stranded on `EuiContextMenu` popover close |
This file contains 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
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.
I upgraded tabbable because I wanted their new
focusable
API, and also generally just to get us on the latest. This has some amount of risk but I've briefly QA'd potentially affected components (EuiContextMenu, EuiDataGrid, EuiComboBox, EuiSuperSelect) and have not found any UX issues.