diff --git a/core/src/components/app/app.tsx b/core/src/components/app/app.tsx index c1c7e5366d9..9896d4bc8d0 100644 --- a/core/src/components/app/app.tsx +++ b/core/src/components/app/app.tsx @@ -14,6 +14,7 @@ import { getIonMode } from '../../global/ionic-global'; }) export class App implements ComponentInterface { private focusVisible?: FocusVisibleUtility; + private tapClickCleanup?: () => void; private loadTimeout?: ReturnType | undefined; @Element() el!: HTMLElement; @@ -23,7 +24,7 @@ export class App implements ComponentInterface { this.rIC(async () => { const isHybrid = isPlatform(window, 'hybrid'); if (!config.getBoolean('_testing')) { - import('../../utils/tap-click').then((module) => module.startTapClick(config)); + import('../../utils/tap-click').then((module) => (this.tapClickCleanup = module.startTapClick(config))); } if (config.getBoolean('statusTap', isHybrid)) { import('../../utils/status-tap').then((module) => module.startStatusTap()); @@ -65,6 +66,14 @@ export class App implements ComponentInterface { if (this.loadTimeout) { clearTimeout(this.loadTimeout); } + if (this.focusVisible) { + this.focusVisible.destroy(); + this.focusVisible = undefined; + } + if (this.tapClickCleanup) { + this.tapClickCleanup(); + this.tapClickCleanup = undefined; + } } /** diff --git a/core/src/utils/tap-click/index.ts b/core/src/utils/tap-click/index.ts index c5a10595f47..cd018892682 100644 --- a/core/src/utils/tap-click/index.ts +++ b/core/src/utils/tap-click/index.ts @@ -3,11 +3,15 @@ import { doc } from '@utils/browser'; import type { Config } from '../../interface'; import { pointerCoord } from '../helpers'; -export const startTapClick = (config: Config) => { +let currentTapClickCleanup: (() => void) | undefined; + +export const startTapClick = (config: Config): (() => void) => { if (doc === undefined) { - return; + return () => {}; } + currentTapClickCleanup?.(); + let lastActivated = 0; let activatableEle: HTMLElement | undefined; @@ -137,6 +141,20 @@ export const startTapClick = (config: Config) => { * ion-content's scroll events. */ doc.addEventListener('pointercancel', cancelActive, true); + + const cleanup = () => { + doc.removeEventListener('ionGestureCaptured', cancelActive); + doc.removeEventListener('pointerdown', pointerDown, true); + doc.removeEventListener('pointerup', pointerUp, true); + doc.removeEventListener('pointercancel', cancelActive, true); + if (currentTapClickCleanup === cleanup) { + currentTapClickCleanup = undefined; + } + }; + + currentTapClickCleanup = cleanup; + + return cleanup; }; // TODO(FW-2832): type