react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

Slider content should not be clickable while the user is dragging the slides on Desktop.

Open vikingair opened this issue 9 years ago • 30 comments

I replicated the issue here: https://jsfiddle.net/20bumb4g/1988/

All pictures were wrapped into a link which "would" link to an external page. (google at this example)

If you start dragging any slide without changing the slide after releasing the click (no index update!) the event will be propagated down to the inner content. This makes dragging act differently and unwanted on desktop.

Please fix this issue. Btw.: your library is awesome.

vikingair avatar Jan 19 '17 14:01 vikingair

@akiran Please solve this problem

minooo avatar Apr 17 '17 03:04 minooo

Maybe you could pass the event through to the beforeChange callback so that we can call stopPropagation on it?

adamshone avatar Apr 26 '17 17:04 adamshone

@adamshone Good idea, but I found that still doesn't work as the beforeChange accepts only two parameters currentSlide and nextSlide.

minooo avatar Apr 28 '17 02:04 minooo

Sorry, by "you" I was referring to the project authors, I thought they could add the event as a third param to beforeChange.

Having had a very quick scan of the code I think unfortunately it's not that simple, it looks like it's using some animationEnd callbacks rather than listening to any mouse event that could be passed on to the user :/

adamshone avatar Apr 28 '17 08:04 adamshone

I tried using the beforeChange and afterChange event handlers to toggle a flag this.setState({ignoreClicks: true/false}), and pass state.ignoreClicks to the carousel items as a prop.

Unfortunately triggering a render from the beforeChange event seems to break the carousel, it cuts the animation off and seems to reset some internal state so that afterChange is never fired.

So... I have a workaround but it's very hacky - you can use it if you're desperate:

import Carousel from 'react-slick';

class MyCarousel extends PureComponent {
  midDrag = false;

  toggleMidDrag = () => {
    this.midDrag = !this.midDrag;
  }

  render() {
    return (
      <Carousel beforeChange={this.toggleMidDrag} afterChange={this.toggleMidDrag}>
        <Item key="0" parentCarousel={this} />
        <Item key="1" parentCarousel={this} />
        ...
      />
    );
}

class Item extends PureComponent {
  onClick = () => {
    if (this.props.parentCarousel.midDrag) {
      // ignore clicks
      return;
    }

    // ...your usual click handling code
  }

  render() { ... }
}

adamshone avatar Apr 29 '17 08:04 adamshone

@akiran please, this is critical. I think the easier solution is to stop the event propagation on dragEnd in the event handler mixin.

erasmo-marin avatar Jun 02 '17 18:06 erasmo-marin

@akiran any update's on this issue ?

vTrip avatar Aug 24 '17 01:08 vTrip

@akiran this feature is critically necessary, please update it!

dmoli avatar Sep 01 '17 14:09 dmoli

@fdc-viktor-luft did you found a solution or another library? thanks!

dmoli avatar Sep 01 '17 16:09 dmoli

@skumblue I decided to avoid any bugs on cost of comfort. Therefore I disabled Drag-&-Drop on Desktop by setting "draggable" to "false". Hint: The property "draggable" influences only Desktop behaviour.

In case that this issues gets cleanly solved, I will consider an update.

vikingair avatar Sep 04 '17 07:09 vikingair

You can try something like this:

class Carousel extends Component {
  constructor(props) {
    super(props);
    this.state = {isDragging: false};
  }

  shouldComponentUpdate(nextProps, nextState) {
    return this.state.isDragging === nextState.isDragging
  }

  onClickFunction = (e) => {
    if (this.state.isDragging) {
      e.preventDefault()
    }
  };

  render() {
    const settings = {
      ... // the rest of the settings
      beforeChange: () => this.setState({isDragging: true}),
      afterChange: () => this.setState({isDragging: false})
    };

    return (
      <div>
        <Slider {...settings}>
            <div><a href="#" onClick={this.onClickFunction}>one</a></div>
            <div><a href="#" onClick={this.onClickFunction}>two</a></div>
            <div><a href="#" onClick={this.onClickFunction}>three</a></div>
        </Slider>
      </div>
    )
  }
}

trumko avatar Nov 27 '17 16:11 trumko

I have implemented a similar fix to the one above for this issue. Works perfectly.

dwaltz avatar Dec 14 '17 16:12 dwaltz

I did something similar to @trumko but I set isDragging as a property on the class rather than in state so changing it never causes a re-render. shouldComponentUpdate was causing some weird glitching still

chrisjcodes avatar Feb 28 '18 02:02 chrisjcodes

It seems to be almost perfect with these hacks, although the slides need to slide completely off the screen, as in entire slide width, in order to prevent default click. If you just slide it around slightly, it will click anyway, since beforeChange and afterChange don't event trigger that way 😞

pimmey avatar Mar 02 '18 12:03 pimmey

The referenced commit should solve the mentioned problem. The changes will be released soon.

laveesingh avatar Mar 29 '18 09:03 laveesingh

@laveesingh

Can this feature (from 7918201) be release as of now?

JeffWeim avatar May 31 '18 22:05 JeffWeim

Looks like this commit partly breaks carousel functionality on mobile devices. because of

clickHandler = e => {
  if (this.clickable === false) {
    e.stopPropagation()
    e.preventDefault()
  }
  this.clickable = true
}

now after swiping user must tap 2 times on slider to trigger link.

trumko avatar Jun 26 '18 15:06 trumko

The bug still can be reproduced if I finish dragging on the slide. This happens because clickHandler fires after swipeMove is finished. Mac OS, Chrome 69

alexeyMohnatkin avatar Sep 21 '18 15:09 alexeyMohnatkin

Is there any update on this issue?

lm2almeida avatar Oct 08 '18 12:10 lm2almeida

This can be fixed if you don't mind changing some internals of <InnerSlider>

onSwipe() {
    if (this.slider) {
      this.slider.innerSlider.clickable = true;
    }
  }

settings = {
    onSwipe: this.onSwipe.bind(this),
    swipeToSlide: true,
  };

// in render()
const sliderRef = slider => {this.slider = slider;};

// in return
<Slider {...settings} ref={sliderRef}>{this.slides}</Slider>

fmoulton avatar Oct 12 '18 00:10 fmoulton