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 cleanup for overlay2 component #6753

Merged
merged 1 commit into from
Apr 19, 2024
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
7 changes: 3 additions & 4 deletions packages/core/src/components/overlay2/overlay2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,10 @@ export const Overlay2 = React.forwardRef<OverlayInstance, Overlay2Props>((props,
// run once on unmount
React.useEffect(() => {
return () => {
if (isOpen) {
// if the overlay is still open, we need to run cleanup code to remove some event handlers
overlayWillClose();
}
// we need to run cleanup code to remove some event handlers from the overlay element
overlayWillClose();
};

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/overlay2/overlay2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,47 @@ describe("<Overlay2>", () => {
});
});

describe("after closing by navigating out of the view", () => {
it("doesn't keep scrolling disabled if user navigated from the component where Overlay was opened", () => {
function WrapperWithNavigation(props: { renderOverlayView: boolean; isOverlayOpen: boolean }) {
return (
<div>
{/* Emulating the router behavior */}
{props.renderOverlayView ? (
<OverlayWrapper isOpen={props.isOverlayOpen}>{createOverlayContents()}</OverlayWrapper>
) : (
<div>Another View</div>
)}
</div>
);
}

// Rendering the component with closed overlay (how it will happen in most of the real apps)
const wrapperWithNavigation = mount(
<WrapperWithNavigation renderOverlayView={true} isOverlayOpen={false} />,
{
attachTo: testsContainerElement,
},
);

// Opening the overlay manually to emulate the real app behavior
wrapperWithNavigation.setProps({ isOverlayOpen: true });
assert.isTrue(
document.body.classList.contains(Classes.OVERLAY_OPEN),
`expected overlay element to have ${Classes.OVERLAY_OPEN} class`,
);
assertBodyScrollingDisabled(true);

// Emulating the user navigation to another view
wrapperWithNavigation.setProps({ renderOverlayView: false });
assert.isFalse(
document.body.classList.contains(Classes.OVERLAY_OPEN),
`expected overlay element to not have ${Classes.OVERLAY_OPEN} class`,
);
assertBodyScrollingDisabled(false);
});
});

function renderBackdropOverlay(hasBackdrop?: boolean, usePortal?: boolean) {
return (
<OverlayWrapper hasBackdrop={hasBackdrop} isOpen={true} usePortal={usePortal}>
Expand Down