Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add opt out mouse events prop #65

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/SwipeableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SwipeableList extends PureComponent {
className = '',
fullSwipe = false,
destructiveCallbackDelay = 1000,
optOutMouseEvents = false,
style,
type = Type.ANDROID,
Tag = 'div',
Expand All @@ -49,6 +50,7 @@ class SwipeableList extends PureComponent {
destructiveCallbackDelay,
fullSwipe,
listType: type,
optOutMouseEvents,
scrollStartThreshold,
swipeStartThreshold,
threshold,
Expand All @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions src/SwipeableListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwipeableListProps>;
Expand Down Expand Up @@ -225,6 +231,12 @@ interface SwipeableListItemProps {
*/
trailingActions?: ReactNode;
className?: string;
/**
* default: `false`
*
* Disables mouse events for swiping.
*/
optOutMouseEvents?: boolean;
}

export class SwipeableListItem extends PureComponent<SwipeableListItemProps> {}
Loading