Skip to content

Commit c8a6c80

Browse files
alirezamiriansnowystingerLFDanLu
authored
fix(@react-aria/overlays): prevent exception on Cypress (#2341)
* fix(@react-aria/overlays): prevent exception on Cypress Cypress screenshot command triggers a scroll event where the event target is window, not a DOM element. closes #2340 * test(@react-aria/overlays): add two test case for close on scroll related to #2340. The first test case is related to the regression mentioned here: #2341 (comment) The second test case captures the fix for the cypress issue. * window scroll to behave same as document * remove comment that no longer applies Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent a85676b commit c8a6c80

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/@react-aria/overlays/src/useCloseOnScroll.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export function useCloseOnScroll(opts: CloseOnScrollOptions) {
3636

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

packages/@react-aria/overlays/test/useOverlayPosition.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ describe('useOverlayPosition', function () {
163163
fireEvent.scroll(document.body);
164164
expect(onClose).toHaveBeenCalledTimes(1);
165165
});
166+
167+
it('should close the overlay when the document scrolls', function () {
168+
let onClose = jest.fn();
169+
render(<Example isOpen onClose={onClose} />);
170+
171+
fireEvent.scroll(document);
172+
expect(onClose).toHaveBeenCalledTimes(1);
173+
});
174+
175+
it('should close the overlay when target is window in a scroll event', function () {
176+
let onClose = jest.fn();
177+
render(<Example isOpen onClose={onClose} />);
178+
179+
fireEvent.scroll(window);
180+
expect(onClose).toHaveBeenCalledTimes(1);
181+
});
166182
});
167183

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

0 commit comments

Comments
 (0)