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(@react-aria/overlays): prevent exception on Cypress #2341

Merged
merged 7 commits into from Oct 28, 2021
5 changes: 3 additions & 2 deletions packages/@react-aria/overlays/src/useCloseOnScroll.ts
Expand Up @@ -36,8 +36,9 @@ export function useCloseOnScroll(opts: CloseOnScrollOptions) {

let onScroll = (e: MouseEvent) => {
// Ignore if scrolling an scrollable region outside the trigger's tree.
let target = e.target as HTMLElement;
if (!triggerRef.current || !target.contains(triggerRef.current)) {
let target = e.target;
// window is not a Node and doesn't have contain, but window contains everything
if (!triggerRef.current || ((target instanceof Node) && !target.contains(triggerRef.current))) {
return;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/@react-aria/overlays/test/useOverlayPosition.test.js
Expand Up @@ -163,6 +163,22 @@ describe('useOverlayPosition', function () {
fireEvent.scroll(document.body);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('should close the overlay when the document scrolls', function () {
let onClose = jest.fn();
render(<Example isOpen onClose={onClose} />);

fireEvent.scroll(document);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('should close the overlay when target is window in a scroll event', function () {
let onClose = jest.fn();
render(<Example isOpen onClose={onClose} />);

fireEvent.scroll(window);
expect(onClose).toHaveBeenCalledTimes(1);
});
});

describe('useOverlayPosition with positioned container', () => {
Expand Down