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(toast): remove backdrop-no-scroll for last toast overlay #30123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ export const dismiss = async <OverlayDismissOptions>(
return false;
}

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

const isLastOverlay = presentedOverlays.length === 1;

if (isLastOverlay) {
document.body.classList.remove(BACKDROP_NO_SCROLL);
}

/**
* For accessibility, toasts lack focus traps and don’t receive
* `aria-hidden` on the root element when presented.
Expand All @@ -657,7 +665,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 All @@ -667,7 +675,6 @@ export const dismiss = async <OverlayDismissOptions>(
*/
if (lastOverlayNotToast) {
setRootAriaHidden(false);
document.body.classList.remove(BACKDROP_NO_SCROLL);
}

overlay.presented = false;
Expand Down
31 changes: 31 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,34 @@ describe('overlays: scroll blocking', () => {

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

it('should not enable scroll until last toast overlay is dismissed', async () => {
const page = await newSpecPage({
components: [Toast],
html: `
<ion-toast id="one"></ion-toast>
<ion-toast id="two"></ion-toast>
`,
});

const toastOne = page.body.querySelector('#one') as HTMLIonToastElement;
const toastTwo = page.body.querySelector('#two') as HTMLIonToastElement;
const body = page.doc.querySelector('body')!;

await toastOne.present();

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

await toastTwo.present();

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

await toastOne.dismiss();

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

await toastTwo.dismiss();

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