ScrollViews with react-native-web let mobile devices drag to scroll, and let you use your mac trackpad on desktop.
For horizontal scrollable content, such as carousels, I often find myself wanting to drag with my mouse.
This gist provides a simple hook that makes your ScrollView draggable with a mouse.
It hasn't been tested with pagingEnabled on FlatLists, but it should work for normal a FlatList on web.
Here's an example video.
This won't work with react@17 because it uses findNodeHandle. Maybe try it without that and see if it still works? I haven't tried yet.
Thanks a lot for this! I tried using it but had a few issues that I believe to have solved:
RefObjectwas causing type issues because of the use ofReact.forwardRef, replacing it withReact.ForwardedRefgot rid of them.clickevents would be fired directly after dragging (mousedown -> mouseup on the same element). This was unexpected/unwanted behavior in my case at least.Here are the steps I took to solve the above (TL;DR):
Replacing
RefObjectwithForwardedRefAllow cursor to move outside container once dragging has started
Prevent
clickevents from firing after dragHere, the goal was to somehow prevent the
clickevent in the case where dragging was going on. This below was the most convenient solution I could come up with. With this change, we only transition into theisDragging = truestate when the dragging movement starts, not when the mouse button is held down. This allows us to correctly dismiss the upcoming click event only when dragging has actually occurred, in this case for more than 3 pixels.End result
Combined, the above diffs look like this: