-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
I want to be able to click the action button to initiate an action. However, I'm not able to generate an "onClick" event. Any suggestions?
I am using next JS. Here is my code:
import "./SwipeableListItem.css";
import React from "react";
import Link from 'next/link';
class SwipeableListItem extends React.Component {
// DOM Refs
listElement;
wrapper;
background;
// Drag & Drop
dragStartX = 0;
left = 0;
dragged = false;
// FPS Limit
startTime;
fpsInterval = 1000 / 60;
constructor(props) {
// console.log('> Props: ', props);
super(props);
this.listElement = null;
this.wrapper = null;
this.background = null;
this.onMouseMove = this.onMouseMove.bind(this);
this.onTouchMove = this.onTouchMove.bind(this);
this.onDragStartMouse = this.onDragStartMouse.bind(this);
this.onDragStartTouch = this.onDragStartTouch.bind(this);
this.onDragEndMouse = this.onDragEndMouse.bind(this);
this.onDragEndTouch = this.onDragEndTouch.bind(this);
this.onDragEnd = this.onDragEnd.bind(this);
this.updatePosition = this.updatePosition.bind(this);
this.onClicked = this.onClicked.bind(this);
this.onSwiped = this.onSwiped.bind(this);
}
componentDidMount() {
window.addEventListener("mouseup", this.onDragEndMouse);
window.addEventListener("touchend", this.onDragEndTouch);
}
componentWillUnmount() {
window.removeEventListener("mouseup", this.onDragEndMouse);
window.removeEventListener("touchend", this.onDragEndTouch);
}
onDragStartMouse(evt) {
this.onDragStart(evt.clientX);
window.addEventListener("mousemove", this.onMouseMove);
}
onDragStartTouch(evt) {
const touch = evt.targetTouches[0];
console.log('> Drag start: ', touch);
this.onDragStart(touch.clientX);
window.addEventListener("touchmove", this.onTouchMove);
}
onDragStart(clientX) {
// if (this.left <= -this.listElement.offsetWidth / 2) {
// this.left = 0;
// }
this.dragged = true;
this.dragStartX = clientX;
this.listElement.className = "ListItem";
this.startTime = Date.now();
requestAnimationFrame(this.updatePosition);
}
onDragEndMouse(evt) {
window.removeEventListener("mousemove", this.onMouseMove);
this.onDragEnd();
}
onDragEndTouch(evt) {
window.removeEventListener("touchmove", this.onTouchMove);
this.onDragEnd();
}
onDragEnd() {
if (this.dragged) {
this.dragged = false;
const threshold = this.props.threshold || 0.3;
if (this.left < this.listElement.offsetWidth * threshold * -1) {
this.left = -this.listElement.offsetWidth / 2; // set to half way point
this.onSwiped();
} else {
this.left = 0; // bring back to beginning after swipe back
}
this.listElement.className = "BouncingListItem";
this.listElement.style.transform = `translateX(${this.left}px)`;
}
}
onMouseMove(evt) {
const left = evt.clientX - this.dragStartX;
if (left < 0) {
this.left = left;
} else {
this.left = Math.min(0, this.left + left);
}
}
onTouchMove(evt) {
const touch = evt.targetTouches[0];
var left = touch.clientX - this.dragStartX;
if (left < 0) {
this.left = left;
} else {
this.left = Math.min(0, this.left + left);
}
}
updatePosition() {
if (this.dragged) requestAnimationFrame(this.updatePosition);
const now = Date.now();
const elapsed = now - this.startTime;
if (this.dragged && elapsed > this.fpsInterval) {
this.listElement.style.transform = `translateX(${this.left}px)`;
const opacity = (Math.abs(this.left) / 100).toFixed(2);
if (opacity < 1 && opacity.toString() !== this.background.style.opacity) {
this.background.style.opacity = opacity.toString();
}
if (opacity >= 1) {
this.background.style.opacity = "1";
}
this.startTime = Date.now();
}
}
onClicked() {
if (this.props.onSwipe) {
this.props.onSwipe();
}
}
onSwiped(status) {
if (this.props.onSwipe) {
this.props.onSwipe(status);
}
}
render() {
return (
<>
<div className="Wrapper" ref={div => (this.wrapper = div)}>
<div ref={div => (this.background = div)} className="Background flex items-center">
<a
href="#"
ref={div => { this.litDrop = div}}
className="bg-blue-500 h-full w-[80px] p-3"
onClick={() => console.log("Action A")}
>
Action A
</a>
<a
href="#"
ref={div => { this.notHome = div}}
className="bg-gray-500 h-full w-[80px] p-3"
onClick={() => console.log("Action B")}
>
Action B
</a>
</div>
<div
// onClick={this.onClicked}
ref={div => (this.listElement = div)}
onMouseDown={this.onDragStartMouse}
onTouchStart={this.onDragStartTouch}
className="ListItem"
>
{this.props.children}
</div>
</div>
</>
);
}
}
export default SwipeableListItem;
Metadata
Metadata
Assignees
Labels
No labels