From ae4899a5b551308c4b2e68f7a61d1f163121059a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 14 Nov 2022 08:30:45 +0100 Subject: [PATCH] MOBILE-4183 user-tours: Allow dismissing with backdrop click --- .../components/user-tour/user-tour.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core/features/usertours/components/user-tour/user-tour.ts b/src/core/features/usertours/components/user-tour/user-tour.ts index cda15c4d2..5fcb9c2eb 100644 --- a/src/core/features/usertours/components/user-tour/user-tour.ts +++ b/src/core/features/usertours/components/user-tour/user-tour.ts @@ -38,6 +38,7 @@ import { COLLAPSIBLE_HEADER_UPDATED } from '@directives/collapsible-header'; const ANIMATION_DURATION = 200; const USER_TOURS_BACK_BUTTON_PRIORITY = 100; +const BACKDROP_DISMISS_SAFETY_TRESHOLD = 1000; /** * User Tour wrapper component. @@ -78,11 +79,15 @@ export class CoreUserToursUserTourComponent implements AfterViewInit, OnDestroy protected resizeListener?: CoreEventObserver; protected scrollListener?: EventListener; protected content?: HTMLIonContentElement | null; + protected lastActivatedTime = 0; constructor({ nativeElement: element }: ElementRef) { this.element = element; CoreComponentsRegistry.register(element, this); + + this.element.addEventListener('click', (event) => + this.dismissOnBackOrBackdrop(event.target as HTMLElement)); } /** @@ -238,6 +243,7 @@ export class CoreUserToursUserTourComponent implements AfterViewInit, OnDestroy } this.active = true; + this.lastActivatedTime = Date.now(); if (!this.backButtonListener) { document.addEventListener( @@ -245,7 +251,7 @@ export class CoreUserToursUserTourComponent implements AfterViewInit, OnDestroy this.backButtonListener = ({ detail }) => detail.register( USER_TOURS_BACK_BUTTON_PRIORITY, () => { - // Silence back button. + this.dismissOnBackOrBackdrop(); }, ), ); @@ -295,4 +301,23 @@ export class CoreUserToursUserTourComponent implements AfterViewInit, OnDestroy } } + /** + * Dismiss the tour because backdrop or back button was clicked. + * + * @param target Element clicked (if any). + */ + protected dismissOnBackOrBackdrop(target?: HTMLElement): void { + if (!this.active || Date.now() - this.lastActivatedTime < BACKDROP_DISMISS_SAFETY_TRESHOLD) { + // Not active or was recently activated, ignore. + return; + } + + if (target && target.closest('.user-tour-wrapper')) { + // Click on tour wrapper, don't dismiss. + return; + } + + this.dismiss(true); + } + }