544f7befe0
* basic bumps * more tweaking * fix rn patch * fix crop picker patch * fix media library patch * rm unnecessary patch * fix notifications patch * update bottomsheet * Update withAppDelegateReferrer.js * Delete withNoBundleCompression.js * rm withNoBundleCompression plugin * rm findLast shim * metro package exports is enabled by default * update react/react-dom/react-compiler * fix reanimated issue * vendor expo-ized emoji popup * fix types * hackfix view full thread * Update EmojiPickerModule.podspec * more upgrades * fix multiformats package version * add baseurl * bump mmkv * bumps * update react-keyed-flatten-children * bump locale packages * fix emoji picker dark mode * rn upgrades * Revert "bump locale packages" This reverts commitfc82f0f173. * upgrade testing-library * rm test renderer * update patch name minors * rm findNodeHandle from tabbar * only do scrollview tag thing on ios * disable package exports * update expo notifications handler * memoize emoji picker styles * fix tests, mock multiformats * bump some dev deps with RC versions * completely rearchitect toasts * rm logs * layout animation config for composer footer * disable autolinking for patched libs * undo lingui changes * version bump from release candidate to 0.1 * update atproto deps * rm @did-plc/server * fix key issue (maybe) * move URL polyfill to the polyfill file * fix yarn lock * upgrade to 53.0.3 * reanimated layout anim bug patch * workletize a function that wasn't getting autoworkletized anymore (#8309) * bump to expo 53.0.4 * bump RN to 0.79.2 * fix yarn lock ci * Revert "completely rearchitect toasts" This reverts commit2e2fcaeeed. * final upgrades * chore: cleanup yarn lock * prettier --------- Co-authored-by: Samuel Newman <mozzius@protonmail.com>
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import {type ForwardedRef, useEffect, useMemo, useRef} from 'react'
|
|
import {type ScrollView} from 'react-native'
|
|
import {Platform} from 'react-native'
|
|
|
|
import {mergeRefs} from '#/lib/merge-refs'
|
|
|
|
type Props<Scrollable extends ScrollView = ScrollView> = {
|
|
cursor?: string
|
|
outerRef?: ForwardedRef<Scrollable>
|
|
}
|
|
|
|
export function useDraggableScroll<Scrollable extends ScrollView = ScrollView>({
|
|
outerRef,
|
|
cursor = 'grab',
|
|
}: Props<Scrollable> = {}) {
|
|
const ref = useRef<Scrollable>(null)
|
|
|
|
useEffect(() => {
|
|
if (Platform.OS !== 'web' || !ref.current) {
|
|
return
|
|
}
|
|
const slider = ref.current as unknown as HTMLDivElement
|
|
if (!slider) {
|
|
return
|
|
}
|
|
let isDragging = false
|
|
let isMouseDown = false
|
|
let startX = 0
|
|
let scrollLeft = 0
|
|
|
|
const mouseDown = (e: MouseEvent) => {
|
|
isMouseDown = true
|
|
startX = e.pageX - slider.offsetLeft
|
|
scrollLeft = slider.scrollLeft
|
|
|
|
slider.style.cursor = cursor
|
|
}
|
|
|
|
const mouseUp = () => {
|
|
if (isDragging) {
|
|
slider.addEventListener('click', e => e.stopPropagation(), {once: true})
|
|
}
|
|
|
|
isMouseDown = false
|
|
isDragging = false
|
|
slider.style.cursor = 'default'
|
|
}
|
|
|
|
const mouseMove = (e: MouseEvent) => {
|
|
if (!isMouseDown) {
|
|
return
|
|
}
|
|
|
|
// Require n pixels momement before start of drag (3 in this case )
|
|
const x = e.pageX - slider.offsetLeft
|
|
if (Math.abs(x - startX) < 3) {
|
|
return
|
|
}
|
|
|
|
isDragging = true
|
|
e.preventDefault()
|
|
const walk = x - startX
|
|
slider.scrollLeft = scrollLeft - walk
|
|
}
|
|
|
|
slider.addEventListener('mousedown', mouseDown)
|
|
window.addEventListener('mouseup', mouseUp)
|
|
window.addEventListener('mousemove', mouseMove)
|
|
|
|
return () => {
|
|
slider.removeEventListener('mousedown', mouseDown)
|
|
window.removeEventListener('mouseup', mouseUp)
|
|
window.removeEventListener('mousemove', mouseMove)
|
|
}
|
|
}, [cursor])
|
|
|
|
const refs = useMemo(
|
|
() => mergeRefs(outerRef ? [ref, outerRef] : [ref]),
|
|
[ref, outerRef],
|
|
)
|
|
|
|
return {
|
|
refs,
|
|
}
|
|
}
|