Skip to content

Commit

Permalink
Only prevent touchevent outside popover (#2205)
Browse files Browse the repository at this point in the history
* 🐛 Only prevent event (e.g. scroll) outside popover

* ♻️  Rename preventEvent callback for clarity

* ♻️ Make callback static

This is to avoid future problems if 'this' is referred within the
callback down the line.
  • Loading branch information
RasmusKjeldgaard authored Apr 29, 2022
1 parent 4e77187 commit 62ea66d
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ export class PopoverComponent implements AfterViewInit, OnDestroy {
}

// document.removeEventListener needs the exact same event handler & options reference:
private preventEvent(event: TouchEvent) {
event.preventDefault();
private static preventEventOutsidePopover(event: TouchEvent) {
if (event.target instanceof HTMLElement) {
const targetIsInPopover = !!event.target.closest('kirby-popover');
if (!targetIsInPopover) {
event.preventDefault();
}
}
}

private preventScroll() {
Expand All @@ -101,7 +106,7 @@ export class PopoverComponent implements AfterViewInit, OnDestroy {
// preventDefault does not work with Renderer2.listen method; add event listener directly to document instead
this.document.addEventListener(
'touchmove',
this.preventEvent,
PopoverComponent.preventEventOutsidePopover,
this.preventScrollEventListenerOptions
);
}
Expand All @@ -113,7 +118,7 @@ export class PopoverComponent implements AfterViewInit, OnDestroy {

this.document.removeEventListener(
'touchmove',
this.preventEvent,
PopoverComponent.preventEventOutsidePopover,
this.preventScrollEventListenerOptions
);
}
Expand Down

0 comments on commit 62ea66d

Please sign in to comment.