react-compound-slider icon indicating copy to clipboard operation
react-compound-slider copied to clipboard

Make Range slider draggable

Open zibrahimov opened this issue 3 years ago • 7 comments

Hi! How can I make slider draggable like below? I tried to create custom mode but no luck. Range-slider-component-08-Setting-the-values

zibrahimov avatar Mar 22 '21 15:03 zibrahimov

Hi @sghall . Is there any plan to add this feature? Thanks

zibrahimov avatar Apr 20 '21 16:04 zibrahimov

Did you ever find a solution? Getting refs and calculating things on your own is possible but yucky.

agusterodin avatar May 17 '21 21:05 agusterodin

@agusterodin No I didn't. I used rc-slider.

zibrahimov avatar May 18 '21 13:05 zibrahimov

Ha that is funny, we just switched off of rc-slider. I love that this library is headless and works really well with utility-first css.

agusterodin avatar May 20 '21 15:05 agusterodin

@agusterodin Lol yeah actually I don't like rc-slider. It has a lot of bugs. But unfortunately for drag functionality I had to switch because I don't have time to write it myself for this one.

zibrahimov avatar May 21 '21 08:05 zibrahimov

You can achieve it using the react-use-gesture library. Works perfectly:

  const trackDragHandler = useDrag(({ initial: [initialX], xy: [currentX], memo }) => {
    const initialRange = memo || playbackRange
    if (!playbackStarted && containerRef.current && initialRange) {
      const { left: sliderLeft, width: sliderWidth } = containerRef.current.getBoundingClientRect()
      const initialClickOffsetLeft = initialX - sliderLeft
      const currentMousePositionOffsetLeft = currentX - sliderLeft
      const initialPercentageWithinContainer = initialClickOffsetLeft / sliderWidth
      const currentPercentageWithinContainer = currentMousePositionOffsetLeft / sliderWidth
      const initialStep = TIMELINE_SELECTION_RANGE * initialPercentageWithinContainer
      const currentStep = TIMELINE_SELECTION_RANGE * currentPercentageWithinContainer
      const initialToCurrentStepDelta = currentStep - initialStep
      dispatch(setTimelinePlaybackRange([initialRange[0] + initialToCurrentStepDelta, initialRange[1] + initialToCurrentStepDelta]))
    }
    return initialRange
  })

agusterodin avatar Jun 01 '21 19:06 agusterodin

Had anyone has an draggable implementation?

alexfernandez803 avatar Apr 06 '22 22:04 alexfernandez803