Skip to content

Commit

Permalink
Merge pull request #2519 from rsu-bankdata/fix/page-leak
Browse files Browse the repository at this point in the history
Fix memory leak in kirby-page
  • Loading branch information
RasmusKjeldgaard authored Oct 5, 2022
2 parents 1b00cf6 + fdfbdce commit e4e52b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 12 additions & 1 deletion libs/designsystem/src/lib/components/page/page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FitHeadingDirective } from '../../directives/fit-heading/fit-heading.di
import { TestHelper } from '../../testing/test-helper';
import { WindowRef } from '../../types/window-ref';
import { ModalNavigationService } from '../modal/services/modal-navigation.service';
import { TabsComponent } from '../tabs';
import { selectedTabClickEvent, TabsComponent } from '../tabs';

import { PageComponent, PageContentComponent } from './page.component';

Expand Down Expand Up @@ -300,6 +300,17 @@ describe('PageComponent', () => {
});
});

describe('tab navigation', () => {
it('should scroll to top when tab is clicked', () => {
const scrollToTopSpy = jasmine.createSpy();
(spectator.component as any).content.scrollToTop = scrollToTopSpy;

window.dispatchEvent(new Event(selectedTabClickEvent));

expect(scrollToTopSpy).toHaveBeenCalledTimes(1);
});
});

const navigateUrls = (urls: string[]) => {
urls.forEach((url: string) => navigateToUrl(url));
};
Expand Down
17 changes: 8 additions & 9 deletions libs/designsystem/src/lib/components/page/page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class PageComponent
@Output() refresh = new EventEmitter<PullToRefreshEvent>();
@Output() backButtonClick = new EventEmitter<Event>();

@ViewChild(IonContent, { static: true }) private content: IonContent;
@ViewChild(IonContent, { static: true }) private content?: IonContent;
@ViewChild(IonContent, { static: true, read: ElementRef })
private ionContentElement: ElementRef<HTMLIonContentElement>;
@ViewChild(IonHeader, { static: true, read: ElementRef })
Expand Down Expand Up @@ -250,7 +250,6 @@ export class PageComponent
private renderer: Renderer2,
private router: Router,
private changeDetectorRef: ChangeDetectorRef,
private windowRef: WindowRef,
private modalNavigationService: ModalNavigationService,
@Optional() @SkipSelf() private tabsComponent: TabsComponent
) {}
Expand Down Expand Up @@ -294,10 +293,6 @@ export class PageComponent
}
});

this.windowRef.nativeWindow.addEventListener(selectedTabClickEvent, () => {
this.content.scrollToTop(KirbyAnimation.Duration.LONG);
});

this.interceptBackButtonClicksSetup();
}

Expand All @@ -313,9 +308,6 @@ export class PageComponent
this.ngOnDestroy$.complete();

this.pageTitleIntersectionObserverRef.disconnect();
this.windowRef.nativeWindow.removeEventListener(selectedTabClickEvent, () => {
this.content.scrollToTop(KirbyAnimation.Duration.LONG);
});
}

delegateRefreshEvent(event: any): void {
Expand Down Expand Up @@ -448,4 +440,11 @@ export class PageComponent
_onKeyboardWillHide() {
this.ionContentElement.nativeElement.style.setProperty('--keyboard-offset', '0px');
}

@HostListener(`window:${selectedTabClickEvent}`)
_onSelectedTabClick() {
if (this.content) {
this.content.scrollToTop(KirbyAnimation.Duration.LONG);
}
}
}

0 comments on commit e4e52b6

Please sign in to comment.