Answer the question
In order to leave comments, you need to log in
How to handle click and mousemove on the same dom element at the same time?
How it is implemented and generally works.
const LONG_CLICK_TOLERANCE_MS = 250;
const HEIGHT_TOLERANCE = 45;
const getCurrentTimeMS = () => {
return (new Date()).getTime();
};
const getClientY = (e) => {
if (e.clientY !== undefined) {
return e.clientY;
}
return e.touches[0].clientY;
};
const getDefaultMouseMove = () => {
return {
startedAtMS: null,
y: null,
initialHeight: null,
};
};
handleMoveStart = e => {
this.mouseMove = {
startedAtMS: getCurrentTimeMS(),
y: getClientY(e),
initialHeight: this.state.height,
};
document.addEventListener('mousemove', this.handleMoveProcess);
document.addEventListener('touchmove', this.handleMoveProcess);
document.addEventListener('mouseup', this.handleMoveEnd);
document.addEventListener('touchend', this.handleMoveEnd);
};
handleMoveProcess = e => {
if (this.mouseMove.startedAtMS === null) {
return;
}
if (getCurrentTimeMS() - this.mouseMove.startedAtMS >= LONG_CLICK_TOLERANCE_MS) {
const clientY = getClientY(e);
const heightChange = this.mouseMove.y - clientY;
const newHeight = this.mouseMove.initialHeight + heightChange;
if (document.body.clientHeight < newHeight + HEIGHT_TOLERANCE * 1.5) {
return;
}
if (newHeight < HEIGHT_TOLERANCE) {
return;
}
this.setState({
height: newHeight,
isResizing: true,
});
}
};
handleMoveEnd = e => {
if (getCurrentTimeMS() - this.mouseMove.startedAtMS < LONG_CLICK_TOLERANCE_MS) {
const newValue = !this.props.open;
this.setState({
height: newValue ? parseInt(document.body.clientHeight / 2) : HEIGHT_TOLERANCE,
isResizing: false,
}, () => {
this.props.onToggle(newValue);
});
}
this.mouseMove = getDefaultMouseMove();
document.removeEventListener('mousemove', this.handleMoveProcess);
document.removeEventListener('touchmove', this.handleMoveProcess);
document.removeEventListener('mouseup', this.handleMoveEnd);
document.removeEventListener('touchend', this.handleMoveEnd);
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question