Skip to content

Commit

Permalink
feat(js): add MouseSwipeDetector and TouchSwipeDetector classes
Browse files Browse the repository at this point in the history
Implemented MouseSwipeDetector and TouchSwipeDetector in swiperia-js, extending AbstractSwipeDetector. These classes handle swipe detection using mouse events and touch events respectively. Each class includes methods to initialize event listeners (`listen`), handle event start, move, and end (`_start`, `_move`, `_end`), and clean up listeners (`destroy`), providing specific point calculation for their event types.
  • Loading branch information
samavati committed Apr 13, 2024
1 parent 72e958c commit c06c780
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/swiperia-js/src/lib/MouseSwipeDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Point, SwipeCallback, SwipeConfig } from 'swiperia-core';
import { AbstractSwipeDetector } from './AbstractSwipeDetector';

export class MouseSwipeDetector extends AbstractSwipeDetector {
constructor(el: HTMLElement, config?: SwipeConfig) {
super(el, config);
}

point(e: MouseEvent): Point {
return [e.pageX, e.pageY];
}

listen(callback: SwipeCallback): void {
this._callback = callback;
this.el.addEventListener('mousedown', this._start, false);
this.el.addEventListener('mousemove', this._move, false);
this.el.addEventListener('mouseup', this._end, false);
}

destroy(): void {
this.el.removeEventListener('mousedown', this._start, false);
this.el.removeEventListener('mousemove', this._move, false);
this.el.removeEventListener('mouseup', this._end, false);
}
}
26 changes: 26 additions & 0 deletions packages/swiperia-js/src/lib/TouchSwipeDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Point, SwipeCallback, SwipeConfig } from 'swiperia-core';
import { AbstractSwipeDetector } from './AbstractSwipeDetector';

export class TouchSwipeDetector extends AbstractSwipeDetector {
constructor(el: HTMLElement, config?: SwipeConfig) {
super(el, config);
}

point(e: TouchEvent): Point {
const touch = e.touches[0];
return [touch.pageX, touch.pageY];
}

listen(callback: SwipeCallback): void {
this._callback = callback;
this.el.addEventListener('touchstart', this._start, false);
this.el.addEventListener('touchmove', this._move, false);
this.el.addEventListener('touchend', this._end, false);
}

destroy(): void {
this.el.removeEventListener('touchstart', this._start, false);
this.el.removeEventListener('touchmove', this._move, false);
this.el.removeEventListener('touchend', this._end, false);
}
}

0 comments on commit c06c780

Please sign in to comment.