Skip to content

Commit f31aaeb

Browse files
fix: pass passive false for touch events in listenOnTheHost (#137)
1 parent 3b052b2 commit f31aaeb

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

projects/angular-resizable-element/src/lib/resize-handle.directive.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { fromEvent, merge, Subject } from 'rxjs';
1212
import { takeUntil } from 'rxjs/operators';
1313
import { ResizableDirective } from './resizable.directive';
1414
import { Edges } from './interfaces/edges.interface';
15+
import { getListenOptions } from './util/get-listen-options';
1516
import { IS_TOUCH_DEVICE } from './util/is-touch-device';
1617

1718
/**
@@ -173,8 +174,10 @@ export class ResizeHandleDirective implements OnInit, OnDestroy {
173174
}
174175

175176
private listenOnTheHost<T extends Event>(eventName: string) {
176-
return fromEvent<T>(this.element.nativeElement, eventName).pipe(
177-
takeUntil(this.destroy$),
178-
);
177+
return fromEvent<T>(
178+
this.element.nativeElement,
179+
eventName,
180+
getListenOptions(eventName),
181+
).pipe(takeUntil(this.destroy$));
179182
}
180183
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Options for fromEvent when listening on the host.
3+
* Touch events need passive: false so preventDefault can be called.
4+
* @hidden
5+
*/
6+
export function getListenOptions(
7+
eventName: string,
8+
): { passive: false } | Record<string, never> {
9+
const isTouchEvent =
10+
eventName === 'touchstart' ||
11+
eventName === 'touchend' ||
12+
eventName === 'touchcancel';
13+
return isTouchEvent ? { passive: false } : {};
14+
}

projects/angular-resizable-element/src/test/resizable.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ResizeEvent,
66
ResizeHandleDirective,
77
} from 'angular-resizable-element';
8+
import { getListenOptions } from '../lib/util/get-listen-options';
89
import { ComponentFixture, TestBed } from '@angular/core/testing';
910
import { expect } from 'chai';
1011
import * as sinon from 'sinon';
@@ -506,6 +507,20 @@ describe('resizable directive', () => {
506507
});
507508
});
508509

510+
describe('touch event listeners', () => {
511+
['touchstart', 'touchend', 'touchcancel'].forEach((eventName) => {
512+
it(`when eventName is ${eventName}, getListenOptions returns passive: false so fromEvent is called with passive: false`, () => {
513+
expect(getListenOptions(eventName)).to.deep.equal({ passive: false });
514+
});
515+
});
516+
517+
['mousedown', 'mouseup'].forEach((eventName) => {
518+
it(`when eventName is ${eventName}, getListenOptions returns empty options`, () => {
519+
expect(getListenOptions(eventName)).to.deep.equal({});
520+
});
521+
});
522+
});
523+
509524
describe('handle outside of element', () => {
510525
let domEvents: Array<{
511526
name: string;

0 commit comments

Comments
 (0)