Skip to content

Commit 0c7746d

Browse files
committed
added preventDefault prop
1 parent 22b0615 commit 0c7746d

File tree

6 files changed

+131
-33
lines changed

6 files changed

+131
-33
lines changed

CHANGELOG.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 4.4.4 (July 4, 2021)
4+
5+
- Add `preventDefault` prop to allow touch scroll
6+
37
### 4.4.3 (June 8, 2020)
48

59
- Add `nodeRef` to TypeScript definitions
@@ -41,16 +45,16 @@
4145
`nodeRef` is also available on `<DraggableCore>`.
4246
- Remove "browser" field in "package.json":
4347
- There is nothing special in the browser build that is actually practical
44-
for modern use. The "browser" field, as defined in
48+
for modern use. The "browser" field, as defined in
4549
https://github.com/defunctzombie/package-browser-field-spec#overview,
4650
indicates that you should use it if you are directly accessing globals,
4751
using browser-specific features, dom manipulation, etc.
48-
52+
4953
React components like react-draggable are built to do minimal raw
5054
DOM manipulation, and to always gate this behind conditionals to ensure
5155
that server-side rendering still works. We don't make any changes
5256
to any of that for the "browser" build, so it's entirely redundant.
53-
57+
5458
This should also fix the "Super expression must either be null or
5559
a function" error (#472) that some users have experienced with particular
5660
bundler configurations.
@@ -61,7 +65,7 @@
6165
- The browser build will likely be removed entirely in 5.0.
6266
- Fix: Make `bounds` optional in TypeScript [#473](https://github.com/strml/react-draggable/pull/473)
6367

64-
### 4.3.1 (Apr 11, 2020)
68+
### 4.3.1 (Apr 11, 2020)
6569

6670
> This is a bugfix release.
6771

@@ -72,7 +76,7 @@
7276
return React.cloneElement(this.props.children, {style: this.props.children.props.style});
7377
```
7478
, `style` ends up undefined.
75-
- Fixed a bug that caused debug output to show up in the build.
79+
- Fixed a bug that caused debug output to show up in the build.
7680
- `babel-loader` cache does not invalidate when it should. I had modified webpack.config.js in the last version but it reused stale cache.
7781

7882
### 4.3.0 (Apr 10, 2020)
@@ -82,7 +86,7 @@
8286
- Thanks @schnerd, [#450](https://github.com/mzabriskie/react-draggable/pull/450)
8387
- Fix an issue where the insides of a `<Draggable>` were not scrollable on touch devices due to the outer container having `touch-action: none`.
8488
- This was a long-standing hack for mobile devices. Without it, the page will scroll while you drag the element.
85-
- The new solution will simply cancel the touch event `e.preventDefault()`. However, due to changes in Chrome >= 56, this is only possible on
89+
- The new solution will simply cancel the touch event `e.preventDefault()`. However, due to changes in Chrome >= 56, this is only possible on
8690
non-passive event handlers. To fix this, we now add/remove the touchEvent on lifecycle events rather than using React's event system.
8791
- [#465](https://github.com/mzabriskie/react-draggable/pull/465)
8892
- Upgrade devDeps and fix security warnings. None of them actually applied to this module.
@@ -106,7 +110,7 @@
106110
* **`"module"`**: ES6-compatible build using import/export.
107111

108112
This should fix issues like https://github.com/STRML/react-resizable/issues/113 while allowing modern bundlers to consume esm modules in the future.
109-
113+
110114
No compatibility changes are expected.
111115

112116
### 4.0.3 (Sep 10, 2019)

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ positionOffset: {x: number | string, y: number | string},
264264
// Specifies the scale of the canvas your are dragging this element on. This allows
265265
// you to, for example, get the correct drag deltas while you are zoomed in or out via
266266
// a transform or matrix in the parent of this element.
267-
scale: number
267+
scale: number,
268+
269+
// If set to false, the input event will not be default-prevented.
270+
// You should call `.preventDefault() `within the `onStart`, `onDrag`, and `onEnd` event handlers.
271+
// This allows for touch scrolling to work when the event originates on a draggable element.
272+
preventDefault: boolean
268273
}
269274
```
270275

@@ -321,7 +326,8 @@ on itself and thus must have callbacks attached to be useful.
321326
onDrag: DraggableEventHandler,
322327
onStop: DraggableEventHandler,
323328
onMouseDown: (e: MouseEvent) => void,
324-
scale: number
329+
scale: number,
330+
preventDefault: boolean
325331
}
326332
```
327333

example/example.js

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ class App extends React.Component {
150150
Both parent padding and child margin work properly.
151151
</div>
152152
</Draggable>
153+
<Draggable bounds="parent" {...dragHandlers} preventDefault={false}>
154+
<div className="box">
155+
I don't prevent touches from scrolling the container.
156+
</div>
157+
</Draggable>
153158
</div>
154159
</div>
155160
<Draggable bounds="body" {...dragHandlers}>

lib/DraggableCore.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type DraggableCoreDefaultProps = {
5555
onDrag: DraggableEventHandler,
5656
onStop: DraggableEventHandler,
5757
onMouseDown: (e: MouseEvent) => void,
58+
preventDefault: true,
5859
scale: number,
5960
};
6061

@@ -65,6 +66,7 @@ export type DraggableCoreProps = {
6566
offsetParent: HTMLElement,
6667
grid: [number, number],
6768
handle: string,
69+
preventDefault: boolean,
6870
nodeRef?: ?React.ElementRef<any>,
6971
};
7072

@@ -208,6 +210,8 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
208210
*/
209211
scale: PropTypes.number,
210212

213+
preventDefault: PropTypes.bool,
214+
211215
/**
212216
* These properties should be defined on the child, not here.
213217
*/
@@ -224,6 +228,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
224228
onDrag: function(){},
225229
onStop: function(){},
226230
onMouseDown: function(){},
231+
preventDefault: true,
227232
scale: 1,
228233
};
229234

@@ -292,7 +297,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
292297

293298
// Prevent scrolling on mobile devices, like ipad/iphone.
294299
// Important that this is after handle/cancel.
295-
if (e.type === 'touchstart') e.preventDefault();
300+
if (this.props.preventDefault && e.type === 'touchstart') e.preventDefault();
296301

297302
// Set touch identifier in component state if this is a touch event. This allows us to
298303
// distinguish between individual touches on multitouch screens by identifying which

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-draggable",
3-
"version": "4.4.3",
3+
"version": "4.4.4",
44
"description": "React draggable component",
55
"main": "build/cjs/cjs.js",
66
"unpkg": "build/web/react-draggable.min.js",

0 commit comments

Comments
 (0)