mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-09 08:09:32 +08:00
fix(overlays): prevent overlays from getting stuck open (#28069)
Issue number: resolves #27200 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> A bug occurs when you click twice quickly to open an overlay with a small timeout. In some cases, the overlay will present, dismiss, present, then not dismiss the second time, getting stuck open. You can reproduce manually this by grabbing the test HTML included in this PR and putting it in a branch that doesn't include a fix. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - When an overlay with a short timeout is triggered twice quickly, it will open-close-open-close. - The behavior is the same for all overlay components ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Relevant links: * https://github.com/ionic-team/ionic-framework/issues/27200 * https://ionic-cloud.atlassian.net/browse/FW-4374 * https://ionic-cloud.atlassian.net/browse/FW-4053 I'm not sure how to write an automated test for this bug due to the short timeout required. You can manually test the fix in [this Stackblitz](https://stackblitz.com/edit/g1kjci?file=package.json) by changing the Ionic version between 7.3.1 and 7.3.2-dev.11693262117.17edbf6d --------- Co-authored-by: Liam DeBeasi <liamdebeasi@users.noreply.github.com>
This commit is contained in:
@ -4,6 +4,7 @@ import { findIonContent, printIonContentErrorMsg } from '@utils/content';
|
||||
import { CoreDelegate, attachComponent, detachComponent } from '@utils/framework-delegate';
|
||||
import { raf, inheritAttributes, hasLazyBuild } from '@utils/helpers';
|
||||
import type { Attributes } from '@utils/helpers';
|
||||
import { createLockController } from '@utils/lock-controller';
|
||||
import { printIonWarning } from '@utils/logging';
|
||||
import { Style as StatusBarStyle, StatusBar } from '@utils/native/status-bar';
|
||||
import {
|
||||
@ -64,10 +65,10 @@ import { setCardStatusBarDark, setCardStatusBarDefault } from './utils';
|
||||
shadow: true,
|
||||
})
|
||||
export class Modal implements ComponentInterface, OverlayInterface {
|
||||
private readonly lockController = createLockController();
|
||||
private readonly triggerController = createTriggerController();
|
||||
private gesture?: Gesture;
|
||||
private coreDelegate: FrameworkDelegate = CoreDelegate();
|
||||
private currentTransition?: Promise<any>;
|
||||
private sheetTransition?: Promise<any>;
|
||||
private isSheetModal = false;
|
||||
private currentBreakpoint?: number;
|
||||
@ -422,24 +423,15 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
*/
|
||||
@Method()
|
||||
async present(): Promise<void> {
|
||||
const unlock = await this.lockController.lock();
|
||||
|
||||
if (this.presented) {
|
||||
unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
const { presentingElement, el } = this;
|
||||
|
||||
/**
|
||||
* When using an inline modal
|
||||
* and dismissing a modal it is possible to
|
||||
* quickly present the modal while it is
|
||||
* dismissing. We need to await any current
|
||||
* transition to allow the dismiss to finish
|
||||
* before presenting again.
|
||||
*/
|
||||
if (this.currentTransition !== undefined) {
|
||||
await this.currentTransition;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the modal is presented multiple times (inline modals), we
|
||||
* need to reset the current breakpoint to the initial breakpoint.
|
||||
@ -481,7 +473,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
|
||||
writeTask(() => this.el.classList.add('show-modal'));
|
||||
|
||||
this.currentTransition = present<ModalPresentOptions>(this, 'modalEnter', iosEnterAnimation, mdEnterAnimation, {
|
||||
await present<ModalPresentOptions>(this, 'modalEnter', iosEnterAnimation, mdEnterAnimation, {
|
||||
presentingEl: presentingElement,
|
||||
currentBreakpoint: this.initialBreakpoint,
|
||||
backdropBreakpoint: this.backdropBreakpoint,
|
||||
@ -532,15 +524,13 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
setCardStatusBarDark();
|
||||
}
|
||||
|
||||
await this.currentTransition;
|
||||
|
||||
if (this.isSheetModal) {
|
||||
this.initSheetGesture();
|
||||
} else if (hasCardModal) {
|
||||
this.initSwipeToClose();
|
||||
}
|
||||
|
||||
this.currentTransition = undefined;
|
||||
unlock();
|
||||
}
|
||||
|
||||
private initSwipeToClose() {
|
||||
@ -656,12 +646,20 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Because the canDismiss check below is async,
|
||||
* we need to claim a lock before the check happens,
|
||||
* in case the dismiss transition does run.
|
||||
*/
|
||||
const unlock = await this.lockController.lock();
|
||||
|
||||
/**
|
||||
* If a canDismiss handler is responsible
|
||||
* for calling the dismiss method, we should
|
||||
* not run the canDismiss check again.
|
||||
*/
|
||||
if (role !== 'handler' && !(await this.checkCanDismiss(data, role))) {
|
||||
unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -683,21 +681,9 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
this.keyboardOpenCallback = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* When using an inline modal
|
||||
* and presenting a modal it is possible to
|
||||
* quickly dismiss the modal while it is
|
||||
* presenting. We need to await any current
|
||||
* transition to allow the present to finish
|
||||
* before dismissing again.
|
||||
*/
|
||||
if (this.currentTransition !== undefined) {
|
||||
await this.currentTransition;
|
||||
}
|
||||
|
||||
const enteringAnimation = activeAnimations.get(this) || [];
|
||||
|
||||
this.currentTransition = dismiss<ModalDismissOptions>(
|
||||
const dismissed = await dismiss<ModalDismissOptions>(
|
||||
this,
|
||||
data,
|
||||
role,
|
||||
@ -711,8 +697,6 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
}
|
||||
);
|
||||
|
||||
const dismissed = await this.currentTransition;
|
||||
|
||||
if (dismissed) {
|
||||
const { delegate } = this.getDelegate();
|
||||
await detachComponent(delegate, this.usersElement);
|
||||
@ -729,8 +713,10 @@ export class Modal implements ComponentInterface, OverlayInterface {
|
||||
enteringAnimation.forEach((ani) => ani.destroy());
|
||||
}
|
||||
this.currentBreakpoint = undefined;
|
||||
this.currentTransition = undefined;
|
||||
this.animation = undefined;
|
||||
|
||||
unlock();
|
||||
|
||||
return dismissed;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user