From c25020bd16be7d47312ccaa6bb5396342928c7c5 Mon Sep 17 00:00:00 2001 From: Marek Rozmus Date: Fri, 11 Oct 2024 18:42:26 +0100 Subject: [PATCH] feat: add opt out mouse events prop --- src/SwipeableList.js | 3 +++ src/SwipeableListItem.js | 15 ++++++++++----- src/module.d.ts | 12 ++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/SwipeableList.js b/src/SwipeableList.js index 794942a..d3418fe 100644 --- a/src/SwipeableList.js +++ b/src/SwipeableList.js @@ -33,6 +33,7 @@ class SwipeableList extends PureComponent { className = '', fullSwipe = false, destructiveCallbackDelay = 1000, + optOutMouseEvents = false, style, type = Type.ANDROID, Tag = 'div', @@ -49,6 +50,7 @@ class SwipeableList extends PureComponent { destructiveCallbackDelay, fullSwipe, listType: type, + optOutMouseEvents, scrollStartThreshold, swipeStartThreshold, threshold, @@ -70,6 +72,7 @@ SwipeableList.propTypes = { className: PropTypes.string, fullSwipe: PropTypes.bool, destructiveCallbackDelay: PropTypes.number, + optOutMouseEvents: PropTypes.bool, style: PropTypes.object, type: PropTypes.oneOf(Object.values(Type)), Tag: PropTypes.string, diff --git a/src/SwipeableListItem.js b/src/SwipeableListItem.js index cd7f022..a929cf9 100644 --- a/src/SwipeableListItem.js +++ b/src/SwipeableListItem.js @@ -111,7 +111,9 @@ class SwipeableListItem extends PureComponent { } componentDidMount() { - this.listElement.addEventListener('mousedown', this.handleDragStartMouse); + if (!this.props.optOutMouseEvents) { + this.listElement.addEventListener('mousedown', this.handleDragStartMouse); + } this.listElement.addEventListener('touchstart', this.handleDragStartTouch, { passive: true, @@ -155,10 +157,12 @@ class SwipeableListItem extends PureComponent { this.requestedAnimationFrame = null; } - this.listElement.removeEventListener( - 'mousedown', - this.handleDragStartMouse - ); + if (!this.props.optOutMouseEvents) { + this.listElement.removeEventListener( + 'mousedown', + this.handleDragStartMouse + ); + } this.listElement.removeEventListener( 'touchstart', @@ -872,6 +876,7 @@ SwipeableListItem.propTypes = { onSwipeEnd: PropTypes.func, onSwipeProgress: PropTypes.func, onSwipeStart: PropTypes.func, + optOutMouseEvents: PropTypes.bool, scrollStartThreshold: PropTypes.number, swipeStartThreshold: PropTypes.number, threshold: PropTypes.number, diff --git a/src/module.d.ts b/src/module.d.ts index 4f2667b..5eaaacd 100644 --- a/src/module.d.ts +++ b/src/module.d.ts @@ -122,6 +122,12 @@ interface SwipeableListProps { * It can be set for the whole list or for every item. See `threshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence. */ threshold?: number; + /** + * default: `false` + * + * Disables mouse events for swiping. + */ + optOutMouseEvents?: boolean; } export const SwipeableList: FunctionComponent; @@ -225,6 +231,12 @@ interface SwipeableListItemProps { */ trailingActions?: ReactNode; className?: string; + /** + * default: `false` + * + * Disables mouse events for swiping. + */ + optOutMouseEvents?: boolean; } export class SwipeableListItem extends PureComponent {}