Skip to content

Commit

Permalink
Prevent Safari Mobile to zoom the page when trying to zoom the image
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinH committed Jun 23, 2018
1 parent 8b09adb commit a2ce6bb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MAX_ZOOM = 3

class Cropper extends React.Component {
image = null
container = null
imageSize = { width: 0, height: 0, naturalWidth: 0, naturalHeight: 0 }
dragStartPosition = { x: 0, y: 0 }
dragStartCrop = { x: 0, y: 0 }
Expand All @@ -17,10 +18,12 @@ class Cropper extends React.Component {

componentDidMount() {
window.addEventListener('resize', this.computeSizes)
this.container.addEventListener('gesturestart', this.preventZoomSafari)
}

componentWillUnmount() {
window.removeEventListener('resize', this.computeSizes)
this.container.removeEventListener('gesturestart', this.preventZoomSafari)
this.cleanEvents()
}

Expand All @@ -30,6 +33,9 @@ class Cropper extends React.Component {
}
}

// this is to prevent Safari on iOS >= 10 to zoom the page
preventZoomSafari = e => e.preventDefault()

cleanEvents = () => {
document.removeEventListener('mousemove', this.onMouseMove)
document.removeEventListener('mouseup', this.onDragStopped)
Expand Down Expand Up @@ -237,6 +243,7 @@ class Cropper extends React.Component {
onMouseDown={this.onMouseDown}
onTouchStart={this.onTouchStart}
onWheel={this.onWheel}
innerRef={el => (this.container = el)}
>
<Img
src={this.props.image}
Expand Down

0 comments on commit a2ce6bb

Please sign in to comment.