popmotion icon indicating copy to clipboard operation
popmotion copied to clipboard

Children button "onClick" not triggering when parent div "draggable" = true

Open yuensunn opened this issue 8 years ago • 18 comments

Tried reading the documentation but could not find any solution. Am I missing something? Currrently using:-

  • "react" : "^16.5.2"
  • "react-pose": "^3.3.6",

yuensunn avatar Oct 05 '18 08:10 yuensunn

Not sure what the problem is - as basic example works as expected

https://codesandbox.io/s/l3rjlvvmnm

Andarist avatar Oct 05 '18 09:10 Andarist

I just realized that it is working fine on pc but it wont work on mobile.

yuensunn avatar Oct 08 '18 03:10 yuensunn

Hey, someone has already fixed this bug? I have waiting one month but apearly this bug doesn't matter to anyone

JavierFVasquez avatar Nov 29 '18 19:11 JavierFVasquez

It seems that it matters to you. Why wont u work on fixing it? Its an open source project - nobody gets paid here for it, so dont feel entitled to demand anything from anyone in OSS space, because its free for you.

Andarist avatar Nov 29 '18 20:11 Andarist

Hey @Andarist,

Regarding your example link: not sure what's the impact but codesandbox uses [email protected] by default, but still, 4.0.0 doesn't solve anything.

Anyway, I've investigated a bit this issue, as it affects a prototype I'm trying to build and I'm observing the same problems as @yuensunn (ie onClick inside a Draggable doesn't work on mobile). I've forked the repo and run some tests locally, and it seems like this line is the culprit:

https://github.com/Popmotion/popmotion/blob/95f2cbef159a529b68d524907c9ca56c296b3bf8/packages/popmotion-pose/src/dom/events.ts#L92

On a Draggable component It preventDefault on (mouseup mousedown touchstart touchend), and I think that onClick on mobile (at least on React I guess) uses touchstart and touchend events.

~~I don't know why you would need to preventDefault on Draggable events, at least in the simple example you made, removing the line fixes the issue (all tests work fine by the way). Maybe adding an option in the config could be something to look at?~~

EDIT: just realised this is necessary to prevent scroll events...

EDIT 2: ok so I'm investigating further, and there also this line that messes with child events:

https://github.com/Popmotion/popmotion/blob/95f2cbef159a529b68d524907c9ca56c296b3bf8/packages/popmotion/src/input/multitouch/index.ts#L68

Again, not sure what could be done TBH.

Thanks for this fantastic library anyway!

dbismut avatar Dec 02 '18 13:12 dbismut

Hm, im unfortunately not sure how to fix it either :s Could u maybe track touchstart instead of click and emulate the event yourself?

This is tricky because we have to call preventDefault but at the moment touchstart happens we cant know if its intended as dragstart or just a click

Andarist avatar Dec 06 '18 20:12 Andarist

Hi @andarist, thanks for your answer. I agree, no way to know if it’s a drag or a click on touch start but popmotion preventsDefault on the start and end events for draggable elements no matter what. So in a way it’s already too late to do any check after the element has been instantiated I believe... otherwise yes, we could calculate the drag distance on drag end and prevent default if the distance is different than zero.

dbismut avatar Dec 06 '18 20:12 dbismut

We cant determine if we should call preventDefault on touchstart though. Would omitting preventDefault just on touched allow onClick to be called?

Andarist avatar Dec 06 '18 22:12 Andarist

I’m not sure what you mean by « just on touched »?

I haven’t thought this through to have a clear opinion on what to do. Maybe you could preventDefault on touchmove to prevent document scroll, and possibly at least give the option to the user to disable preventDefault on touchstart and touchend?

In my usecase, I apply position: fixed on the body of the page to prevent scrolling, so preventDefault wasn’t needed, but there’s no way to deactivate it using popmotion pose API.

dbismut avatar Dec 06 '18 23:12 dbismut

I’m not sure what you mean by « just on touched »?

I've misspelled touchend here which made it really confusing in this context 😅

Andarist avatar Dec 07 '18 08:12 Andarist

Ok so further clarification after some tests: to register a click on iOS, you need to remove preventDefault on touchstart and touchend. However, if you do this, document will then register scroll events and therefore when you drag the draggable element you will also scroll the viewport.

I thought popmotion multitouch/index.ts was preventing default on touchmove events, but it doesn't seem to have any effect and the viewport keeps scrolling on drag.

I'm not exactly sure why this works, but if you set preventDefault to false in the draggable configuration of popmotion-pose in events.ts and then implement the draggable element as follows:

class Example extends Component {
  componentDidMount() {
    this.el.addEventListener('touchmove', () => {});
   // this should do nothing but it actually prevents the scroll for some reason
  }

  render() {
    return (
      <Box ref={el => (this.el = el)} className="box">
        <button onClick={() => alert('click')}>Click me</button>
      </Box>
    );
  }
}

... then the draggable registers clicks and doesn't scroll the viewport (on my iPhone at least).

dbismut avatar Dec 07 '18 13:12 dbismut

Web programming at its finest 😂Great investigation! Im not sure what w should do about it though - I guess we at least should make preventDefault configurable, right?

Andarist avatar Dec 07 '18 14:12 Andarist

That would be a temp fix but it would be a start I guess, but yeah I know, that's just silly! Actually I think the listener works because it implicitly passes passive: false as a third argument to addEventListener.

Also note that having a functional overflowing-x div within the draggable requires another tweak 🙈:

class Example extends Component {
  startPosition = null;
  lockHasBeenCalculated = false;
  preventDrag = false;

  componentDidMount() {
    this.el.addEventListener('touchmove', () => {}, { passive: false });
    this.scroll.addEventListener('touchstart', e => {
      this.preventDrag = false;
      this.lockHasBeenCalculated = false;

      if (e.touches.length > 1) this.lockHasBeenCalculated = true;
      
      else {
        const { pageX, pageY } = e.touches[0];
        this.startPosition = { x: pageX, y: pageY };
      }

    });

    this.scroll.addEventListener('touchmove', e => {
      if (!this.lockHasBeenCalculated) {
        const { pageX, pageY } = e.touches[0];
        const distanceX = Math.abs(this.startPosition.x - pageX);
        const distanceY = Math.abs(this.startPosition.y - pageY);
        if (distanceX > distanceY) {
          // means the finger is moving on the x axis, so scrolling within the scrolling element
          this.preventDrag = true;
          this.lockHasBeenCalculated = true;
        } else if (distanceX < distanceY) {
          this.lockHasBeenCalculated = true;
        }
      }
      if (this.preventDrag) {
        e.stopPropagation();
      }
    });
  }

  render() {
    return (
      <Box ref={el => (this.el = el)} className="box">
        <button onClick={() => alert('click')}>Click me</button>
        <div className="overflow" ref={scroll => (this.scroll = scroll)}>
          <div />
          <div />
          <div />
          <div />
          <div />
          <div />
          <div />
          <div />
          <div />
        </div>
      </Box>
    );
  }
}

dbismut avatar Dec 07 '18 15:12 dbismut

Hey @andarist, I’ve just issued a pull request that should fix the click problem on mobile. It involves adding a touch move listener to the draggable element and preventing default from there instead of in touch start of end.

dbismut avatar Dec 14 '18 07:12 dbismut

Hi there!

I'm still having this exact problem. I didn't follow the whole thread, but try clicking this button on an ANDROID mobile device: https://codesandbox.io/s/3ypx3zzpxp

I'm running the latest react-pose (4.0.4 as per npm).

Am I missing something?

(it does seems to work on iphone though)

alxsavard avatar Jan 04 '19 20:01 alxsavard

Also still have the problem. A draggable posed.div with a link as child; the link is not clickable on a touch device (ipad, smartphone, even in chrome dev tools with device toolbar.

I know this is an old thread, but hope to find some answers!

publicJorn avatar Jul 02 '19 12:07 publicJorn

i have the same problem but happens when the view is 1440p

matiasj899 avatar May 06 '22 00:05 matiasj899

Use the cancel prop on the component to exclude the delete button (and anything else you want to make clickable) from being draggable.

You can do this using a CSS selector. Here's how:

✅ Fix Implementation

Add a class to the delete button (e.g., not-draggable).

Use the cancel prop in Draggable to ignore dragging on that class.

<Draggable
  key={data._id}
  axis="both"
  allowAnyClick
  defaultPosition={{ x: 10, y: 10 }}
  onStop={(e, d) => handleDragStop(e, d, index)}
  cancel=".not-draggable" // 👈 Add this
>

Then, update the delete button like this:

<button
  type="button"
  title="Delete"
  className="not-draggable"
  onClick={(e) => {
    e.stopPropagation();
    handleDelete(data._id);
  }}
>
  Delete
</button>

💡 Why It Works

The cancel prop tells react-draggable to ignore drag events on elements that match the selector, letting normal click events happen instead.

daxdesai avatar Jun 10 '25 12:06 daxdesai