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

Make Range slider draggable #135

Open
zibrahimov opened this issue Mar 22, 2021 · 7 comments
Open

Make Range slider draggable #135

zibrahimov opened this issue Mar 22, 2021 · 7 comments

Comments

@zibrahimov
Copy link

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
Copy link
Author

zibrahimov commented Apr 20, 2021

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

@agusterodin
Copy link

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

@zibrahimov
Copy link
Author

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

@agusterodin
Copy link

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.

@zibrahimov
Copy link
Author

@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.

@agusterodin
Copy link

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
  })

@alexfernandez803
Copy link

Had anyone has an draggable implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants