Skip to content
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

Fix FocusScope missing parent element #3908

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
});
});