From 7a1a2fd407f8ccf3488d3e90af6490a44eec8305 Mon Sep 17 00:00:00 2001 From: Davide Negretti Date: Thu, 30 Nov 2023 19:11:42 +0100 Subject: [PATCH] [DURACOM-195] Improve dropdown behaviour on mouse events --- .../expandable-navbar-section.component.html | 6 +-- .../expandable-navbar-section.component.ts | 50 +++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.html b/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.html index 1d4fadbfe2c..822843b2716 100644 --- a/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.html +++ b/src/app/navbar/expandable-navbar-section/expandable-navbar-section.component.html @@ -3,10 +3,10 @@ *ngVar="(active | async) as isActive" (keyup.enter)="isActive ? deactivateSection($event) : activateSection($event)" (keyup.space)="isActive ? deactivateSection($event) : activateSection($event)" - (click)="!isActive && activateSection($event)" + (click)="toggleSection($event)" (keydown.space)="$event.preventDefault()" - (mouseenter)="activateSection($event)" - (mouseleave)="deactivateSection($event)"> + (mouseenter)="onMouseEnter($event, isActive)" + (mouseleave)="onMouseLeave($event, isActive)"> ; + + @HostListener('window:resize', ['$event']) + onResize() { + this.isMobile$.pipe( + first() + ).subscribe((isMobile) => { + if (isMobile && !this.wasMobile) { + this.wasMobile = true; + this.menuService.deactivateSection(this.menuID, this.section.id); + this.mouseEntered = false; + } + }); + } + constructor(@Inject('sectionDataProvider') menuSection, protected menuService: MenuService, protected injector: Injector, private windowService: HostWindowService ) { super(menuSection, menuService, injector); + this.isMobile$ = this.windowService.isMobile(); } ngOnInit() { super.ngOnInit(); } + onMouseEnter($event: Event, isActive: boolean) { + if (!isActive && !this.mouseEntered) { + this.activateSection($event); + } + this.mouseEntered = true; + } + + onMouseLeave($event: Event, isActive: boolean) { + if (isActive && this.mouseEntered) { + this.deactivateSection($event); + } + this.mouseEntered = false; + } + /** * Overrides the super function that activates this section (triggered on hover) * Has an extra check to make sure the section can only be activated on non-mobile devices * @param {Event} event The user event that triggered this function */ activateSection(event): void { - this.windowService.isXsOrSm().pipe( + this.isMobile$.pipe( first() ).subscribe((isMobile) => { if (!isMobile) { @@ -54,7 +98,7 @@ export class ExpandableNavbarSectionComponent extends NavbarSectionComponent imp * @param {Event} event The user event that triggered this function */ deactivateSection(event): void { - this.windowService.isXsOrSm().pipe( + this.isMobile$.pipe( first() ).subscribe((isMobile) => { if (!isMobile) {