Skip to content

Commit

Permalink
Fix FocusScope missing parent element (#3908)
Browse files Browse the repository at this point in the history
* Fix FocusScope requestAnimationFrame leak
  • Loading branch information
snowystinger authored Jan 17, 2023
1 parent d2e60c8 commit b6786da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/@react-aria/focus/src/FocusScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ function useFocusContainment(scopeRef: RefObject<Element[]>, contain: boolean) {

let onBlur = (e) => {
// Firefox doesn't shift focus back to the Dialog properly without this
if (raf.current) {
cancelAnimationFrame(raf.current);
}
raf.current = requestAnimationFrame(() => {
// Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
if (shouldContainFocus(scopeRef) && !isElementInChildScope(document.activeElement, scopeRef)) {
Expand Down
23 changes: 23 additions & 0 deletions packages/@react-aria/focus/test/FocusScope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1547,3 +1547,26 @@ describe('FocusScope', function () {
});
});
});

describe('Unmounting cleanup', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.runAllTimers();
});

// this test will fail in the 'afterAll' if there are any rafs left over
it('should not leak request animation frames', () => {
let tree = render(
<FocusScope restoreFocus contain>
<button>Focus me</button>
<button>Then Focus me</button>
</FocusScope>
);
let buttons = tree.getAllByRole('button');
act(() => buttons[0].focus());
act(() => buttons[1].focus());
act(() => buttons[1].blur());
});
});

1 comment on commit b6786da

@rspbot
Copy link

@rspbot rspbot commented on b6786da Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.