Skip to content

fix(overlays): exclude backdrop-no-scroll class when toast is presented #30123

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

Merged
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: 4 additions & 3 deletions core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,9 @@ export const present = async <OverlayPresentOptions>(
*/
if (overlay.el.tagName !== 'ION-TOAST') {
setRootAriaHidden(true);
document.body.classList.add(BACKDROP_NO_SCROLL);
}

document.body.classList.add(BACKDROP_NO_SCROLL);

hideUnderlyingOverlaysFromScreenReaders(overlay.el);
hideAnimatingOverlayFromScreenReaders(overlay.el);

Expand Down Expand Up @@ -646,6 +645,8 @@ export const dismiss = async <OverlayDismissOptions>(
return false;
}

const presentedOverlays = doc !== undefined ? getPresentedOverlays(doc) : [];

/**
* For accessibility, toasts lack focus traps and don’t receive
* `aria-hidden` on the root element when presented.
Expand All @@ -657,7 +658,7 @@ export const dismiss = async <OverlayDismissOptions>(
* Therefore, we must remove `aria-hidden` from the root element
* when the last non-toast overlay is dismissed.
*/
const overlaysNotToast = doc !== undefined ? getPresentedOverlays(doc).filter((o) => o.tagName !== 'ION-TOAST') : [];
const overlaysNotToast = presentedOverlays.filter((o) => o.tagName !== 'ION-TOAST');

const lastOverlayNotToast = overlaysNotToast.length === 1 && overlaysNotToast[0].id === overlay.el.id;

Expand Down
22 changes: 22 additions & 0 deletions core/src/utils/test/overlays/overlays-scroll-blocking.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { newSpecPage } from '@stencil/core/testing';

import { Modal } from '../../../components/modal/modal';
import { Toast } from '../../../components/toast/toast';

describe('overlays: scroll blocking', () => {
it('should not block scroll when the overlay is created', async () => {
Expand Down Expand Up @@ -85,4 +86,25 @@ describe('overlays: scroll blocking', () => {

expect(body).not.toHaveClass('backdrop-no-scroll');
});

// Fixes https://github.com/ionic-team/ionic-framework/issues/30112
it('should not block scroll when the toast overlay is presented', async () => {
const page = await newSpecPage({
components: [Toast],
html: `
<ion-toast></ion-toast>
`,
});

const toast = page.body.querySelector('ion-toast')!;
const body = page.doc.querySelector('body')!;

await toast.present();

expect(body).not.toHaveClass('backdrop-no-scroll');

await toast.dismiss();

expect(body).not.toHaveClass('backdrop-no-scroll');
});
});
Loading