Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 6f22805

Browse files
authored
Merge pull request #14 from fritz-c/fix-touch-scroll
Enable image pan with touch events
2 parents 9075095 + 37604cb commit 6f22805

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

src/react-image-lightbox.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class ReactImageLightbox extends Component {
7575
this.handleOuterMouseDown = this.handleOuterMouseDown.bind(this);
7676
this.handleOuterMouseMove = this.handleOuterMouseMove.bind(this);
7777
this.handleOuterMousewheel = this.handleOuterMousewheel.bind(this);
78+
this.handleOuterTouchStart = this.handleOuterTouchStart.bind(this);
79+
this.handleOuterTouchMove = this.handleOuterTouchMove.bind(this);
7880
this.handleWindowResize = this.handleWindowResize.bind(this);
7981
this.handleZoomInButtonClick = this.handleZoomInButtonClick.bind(this);
8082
this.handleZoomOutButtonClick = this.handleZoomOutButtonClick.bind(this);
@@ -154,11 +156,13 @@ class ReactImageLightbox extends Component {
154156
document.addEventListener('keyup', this.handleKeyInput);
155157
window.addEventListener('resize', this.handleWindowResize);
156158
window.addEventListener('mouseup', this.handleMouseUp);
159+
window.addEventListener('touchend', this.handleMouseUp);
157160

158161
// Have to add an extra mouseup handler to catch mouseup events outside of the window
159162
// if the page containing the lightbox is displayed in an iframe
160163
if (isInIframe()) {
161164
window.top.addEventListener('mouseup', this.handleMouseUp);
165+
window.top.addEventListener('touchend', this.handleMouseUp);
162166
}
163167

164168
this.listenersAttached = true;
@@ -231,9 +235,11 @@ class ReactImageLightbox extends Component {
231235
document.removeEventListener('keyup', this.handleKeyInput);
232236
window.removeEventListener('resize', this.handleWindowResize);
233237
window.removeEventListener('mouseup', this.handleMouseUp);
238+
window.removeEventListener('touchend', this.handleMouseUp);
234239

235240
if (isInIframe()) {
236241
window.top.removeEventListener('mouseup', this.handleMouseUp);
242+
window.top.removeEventListener('touchend', this.handleMouseUp);
237243
}
238244

239245
this.listenersAttached = false;
@@ -544,31 +550,48 @@ class ReactImageLightbox extends Component {
544550
}
545551
}
546552

553+
// Handle move start over the lightbox container
554+
// This happens:
555+
// - On a mouseDown event
556+
// - On a touchstart event
557+
handleMoveStart(clientX, clientY) {
558+
// Only allow dragging when zoomed
559+
if (this.state.zoomLevel <= MIN_ZOOM_LEVEL) {
560+
return;
561+
}
562+
563+
this.isDragging = true;
564+
this.dragStartX = clientX;
565+
this.dragStartY = clientY;
566+
this.dragStartOffsetX = this.state.offsetX;
567+
this.dragStartOffsetY = this.state.offsetY;
568+
}
569+
547570
// Handle the mouse clicking down in the lightbox container
548571
handleOuterMouseDown(event) {
549572
event.preventDefault();
573+
this.handleMoveStart(event.clientX, event.clientY);
574+
}
550575

551-
// Allow dragging when zoomed
552-
if (this.state.zoomLevel > MIN_ZOOM_LEVEL) {
553-
this.isDragging = true;
554-
this.dragStartX = event.clientX;
555-
this.dragStartY = event.clientY;
556-
this.dragStartOffsetX = this.state.offsetX;
557-
this.dragStartOffsetY = this.state.offsetY;
558-
}
576+
// Touch screen version of handleOuterMouseDown()
577+
handleOuterTouchStart(event) {
578+
const touchObj = event.changedTouches[0];
579+
this.handleMoveStart(parseInt(touchObj.clientX, 10), parseInt(touchObj.clientY, 10));
559580
}
560581

561-
// Handle the mouse dragging over the lightbox container
562-
// (after a mouseDown and before a mouseUp event)
563-
handleOuterMouseMove(event) {
582+
// Handle dragging over the lightbox container
583+
// This happens:
584+
// - After a mouseDown and before a mouseUp event
585+
// - After a touchstart and before a touchend event
586+
handleMove(clientX, clientY) {
564587
if (!this.isDragging) {
565588
return;
566589
}
567590

568591
const zoomMultiplier = this.getZoomMultiplier();
569592

570-
const newOffsetX = (this.dragStartX - event.clientX) / zoomMultiplier + this.dragStartOffsetX;
571-
const newOffsetY = (this.dragStartY - event.clientY) / zoomMultiplier + this.dragStartOffsetY;
593+
const newOffsetX = (this.dragStartX - clientX) / zoomMultiplier + this.dragStartOffsetX;
594+
const newOffsetY = (this.dragStartY - clientY) / zoomMultiplier + this.dragStartOffsetY;
572595
if (this.state.offsetX !== newOffsetX || this.state.offsetY !== newOffsetY) {
573596
this.setState({
574597
offsetX: newOffsetX,
@@ -577,6 +600,25 @@ class ReactImageLightbox extends Component {
577600
}
578601
}
579602

603+
// Handle the mouse dragging over the lightbox container
604+
// (after a mouseDown and before a mouseUp event)
605+
handleOuterMouseMove(event) {
606+
this.handleMove(event.clientX, event.clientY);
607+
}
608+
609+
// Touch screen version of handleOuterMouseMove()
610+
handleOuterTouchMove(event) {
611+
event.preventDefault();
612+
613+
// We shouldn't go any further if we're not zoomed
614+
if (this.state.zoomLevel <= MIN_ZOOM_LEVEL) {
615+
return;
616+
}
617+
618+
const touchObj = event.changedTouches[0];
619+
this.handleMove(parseInt(touchObj.clientX, 10), parseInt(touchObj.clientY, 10));
620+
}
621+
580622
// Handle the window resize event
581623
handleWindowResize() {
582624
clearTimeout(this.resizeTimeout);
@@ -872,6 +914,8 @@ class ReactImageLightbox extends Component {
872914
onWheel={this.handleOuterMousewheel}
873915
onMouseMove={this.handleOuterMouseMove}
874916
onMouseDown={this.handleOuterMouseDown}
917+
onTouchStart={this.handleOuterTouchStart}
918+
onTouchMove={this.handleOuterTouchMove}
875919
className={`outer ${styles.outer} ${styles.outerAnimating}` +
876920
(this.state.isClosing ? ` closing ${styles.outerClosing}` : '')
877921
}

0 commit comments

Comments
 (0)