Skip to content

Instantly share code, notes, and snippets.

@nandorojo
Last active November 6, 2024 13:26
Show Gist options
  • Save nandorojo/92e7301a49a8b9575bb24b3b1ddc19bf to your computer and use it in GitHub Desktop.
Save nandorojo/92e7301a49a8b9575bb24b3b1ddc19bf to your computer and use it in GitHub Desktop.
Make a horizontal `ScrollView` draggable with a mouse (`react-native-web`)

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.

Warning

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.

import React, { ComponentProps } from 'react'
import { ScrollView } from 'react-native'
import { useDraggableScroll } from './use-draggable-scroll'
export const DraggableScrollView = React.forwardRef<
ScrollView,
ComponentProps<typeof ScrollView>
>(function DraggableScrollView(props, ref) {
const { refs } = useDraggableScroll<ScrollView>({
outerRef: ref,
cursor: 'grab', // optional, default
})
return <ScrollView ref={refs} horizontal {...props} />
})
import { RefObject, useEffect, useRef, useMemo } from 'react'
import { Platform, findNodeHandle } from 'react-native'
import type { ScrollView } from 'react-native'
import mergeRefs from 'react-merge-refs'
type Props<Scrollable extends ScrollView = ScrollView> = {
cursor?: string
outerRef?: RefObject<Scrollable>
}
export function useDraggableScroll<Scrollable extends ScrollView = ScrollView>({
outerRef,
cursor = 'grab',
}: Props<Scrollable> = {}) {
const ref = useRef<Scrollable>(null)
useEffect(
function listeners() {
if (Platform.OS !== 'web' || !ref.current) {
return
}
const slider = (findNodeHandle(ref.current) as unknown) as HTMLDivElement
if (!slider) {
return
}
let isDragging = false
let startX = 0
let scrollLeft = 0
const mouseDown = (e: MouseEvent) => {
isDragging = true
startX = e.pageX - slider.offsetLeft
scrollLeft = slider.scrollLeft
slider.style.cursor = cursor
}
const mouseLeave = () => {
isDragging = false
}
const mouseUp = () => {
isDragging = false
slider.style.cursor = 'default'
}
const mouseMove = (e: MouseEvent) => {
if (!isDragging) return
e.preventDefault()
const x = e.pageX - slider.offsetLeft
const walk = x - startX
slider.scrollLeft = scrollLeft - walk
}
slider.addEventListener('mousedown', mouseDown)
slider.addEventListener('mouseleave', mouseLeave)
slider.addEventListener('mouseup', mouseUp)
slider.addEventListener('mousemove', mouseMove)
return () => {
slider.removeEventListener('mousedown', mouseDown)
slider.removeEventListener('mouseleave', mouseLeave)
slider.removeEventListener('mouseup', mouseUp)
slider.removeEventListener('mousemove', mouseMove)
}
},
[cursor]
)
const refs = useMemo(() => mergeRefs(outerRef ? [ref, outerRef] : [ref]), [
ref,
outerRef,
])
return {
refs,
}
}
@SaxenaShiv
Copy link

SaxenaShiv commented Jun 18, 2023

Is the end result is in .ts file or I can also add this code in .js file?

@SkySails
Copy link

It is Typescript syntax and won't work in JS. But if you remove the types, all should be good!

@itsramiel
Copy link

Sweet 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment