Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add allowMobileScroll prop to allow for clicks to optionally pass through on mobile #760

Merged
merged 4 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ type DraggableData = {
// If set to `true`, will allow dragging on non left-button clicks.
allowAnyClick: boolean,

// Default `false` and default behavior before 4.5.0.
// If set to `true`, the 'touchstart' event will not be prevented,
// which will allow scrolling inside containers. We recommend
// using the 'handle' / 'cancel' props when possible instead of enabling this.
//
// See https://github.com/react-grid-layout/react-draggable/issues/728
allowMobileScroll: boolean,

// Determines which axis the draggable can move. This only affects
// flushing to the DOM. Callbacks will still include all values.
// Accepted values:
Expand Down Expand Up @@ -202,6 +210,9 @@ defaultPosition: {x: number, y: number},
// If true, will not call any drag handlers.
disabled: boolean,

// Default `true`. Adds "user-select: none" while dragging to avoid selecting text.
enableUserSelectHack: boolean,

// Specifies the x and y that dragging should snap to.
grid: [number, number],

Expand Down Expand Up @@ -324,10 +335,10 @@ on itself and thus must have callbacks attached to be useful.
```js
{
allowAnyClick: boolean,
allowMobileScroll: boolean,
cancel: string,
disabled: boolean,
enableUserSelectHack: boolean,
allowMobileScroll: boolean,
offsetParent: HTMLElement,
grid: [number, number],
handle: string,
Expand Down
11 changes: 11 additions & 0 deletions lib/DraggableCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type PositionOffsetControlPosition = {x: number|string, y: number|string}

export type DraggableCoreDefaultProps = {
allowAnyClick: boolean,
allowMobileScroll: boolean,
disabled: boolean,
enableUserSelectHack: boolean,
onStart: DraggableEventHandler,
Expand Down Expand Up @@ -81,6 +82,15 @@ export default class DraggableCore extends React.Component<DraggableCoreProps> {
*/
allowAnyClick: PropTypes.bool,

/**
* `allowMobileScroll` turns off cancellation of the 'touchstart' event
* on mobile devices. Only enable this if you are having trouble with click
* events. Prefer using 'handle' / 'cancel' instead.
*
* Defaults to `false`.
*/
allowMobileScroll: PropTypes.bool,

children: PropTypes.node.isRequired,

/**
Expand Down Expand Up @@ -213,6 +223,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps> {

static defaultProps: DraggableCoreDefaultProps = {
allowAnyClick: false, // by default only accept left click
allowMobileScroll: false,
disabled: false,
enableUserSelectHack: true,
onStart: function(){},
Expand Down
12 changes: 12 additions & 0 deletions specs/draggable.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,18 @@ describe('react-draggable', function () {
assert.equal(drag.state.dragging, true);
});

it('should *not* call preventDefault on touchStart event if "allowMobileScroll"', function () {
drag = TestUtils.renderIntoDocument(<Draggable allowMobileScroll={true}><div/></Draggable>);

const e = new Event('touchstart');
// Oddly `e.defaultPrevented` is not changing here. Maybe because we're not mounted to a real doc?
let pdCalled = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one @STRML , thanks for adding a test! my Achilles heel!

e.preventDefault = function() { pdCalled = true; };
ReactDOM.findDOMNode(drag).dispatchEvent(e);
assert(!pdCalled);
assert.equal(drag.state.dragging, true);
});

it('should not call preventDefault on touchStart event if not on handle', function () {
drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ declare module 'react-draggable' {

export interface DraggableCoreProps {
allowAnyClick: boolean,
allowMobileScroll: boolean,
cancel: string,
children?: React.ReactNode,
disabled: boolean,
enableUserSelectHack: boolean,
allowMobileScroll: boolean,
offsetParent: HTMLElement,
grid: [number, number],
handle: string,
Expand Down
4 changes: 2 additions & 2 deletions typings/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ ReactDOM.render(
onDrag={handleDrag}
onStop={handleStop}
offsetParent={document.body}
allowAnyClick={true}
onMouseDown={handleMouseDown}
allowAnyClick={true}
allowMobileScroll={false}
disabled={true}
enableUserSelectHack={false}
allowMobileScroll={false}
bounds={false}
defaultClassName={'draggable'}
defaultClassNameDragging={'dragging'}
Expand Down