ScrollView
s 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 FlatList
s, 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:
RefObject
was causing type issues because of the use ofReact.forwardRef
, replacing it withReact.ForwardedRef
got rid of them.click
events 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
RefObject
withForwardedRef
Allow cursor to move outside container once dragging has started
Prevent
click
events from firing after dragHere, the goal was to somehow prevent the
click
event 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 = true
state 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: