2023-12-14 03:48:20 +01:00
|
|
|
import React, {createContext, useContext, useMemo} from 'react'
|
|
|
|
import {ScrollHandlers} from 'react-native-reanimated'
|
|
|
|
|
|
|
|
const ScrollContext = createContext<ScrollHandlers<any>>({
|
|
|
|
onBeginDrag: undefined,
|
|
|
|
onEndDrag: undefined,
|
|
|
|
onScroll: undefined,
|
2024-04-28 18:48:20 +02:00
|
|
|
onMomentumEnd: undefined,
|
2023-12-14 03:48:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export function useScrollHandlers(): ScrollHandlers<any> {
|
|
|
|
return useContext(ScrollContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProviderProps = {children: React.ReactNode} & ScrollHandlers<any>
|
|
|
|
|
|
|
|
// Note: this completely *overrides* the parent handlers.
|
|
|
|
// It's up to you to compose them with the parent ones via useScrollHandlers() if needed.
|
|
|
|
export function ScrollProvider({
|
|
|
|
children,
|
|
|
|
onBeginDrag,
|
|
|
|
onEndDrag,
|
|
|
|
onScroll,
|
2024-04-28 18:48:20 +02:00
|
|
|
onMomentumEnd,
|
2023-12-14 03:48:20 +01:00
|
|
|
}: ProviderProps) {
|
|
|
|
const handlers = useMemo(
|
|
|
|
() => ({
|
|
|
|
onBeginDrag,
|
|
|
|
onEndDrag,
|
|
|
|
onScroll,
|
2024-04-28 18:48:20 +02:00
|
|
|
onMomentumEnd,
|
2023-12-14 03:48:20 +01:00
|
|
|
}),
|
2024-04-28 18:48:20 +02:00
|
|
|
[onBeginDrag, onEndDrag, onScroll, onMomentumEnd],
|
2023-12-14 03:48:20 +01:00
|
|
|
)
|
|
|
|
return (
|
|
|
|
<ScrollContext.Provider value={handlers}>{children}</ScrollContext.Provider>
|
|
|
|
)
|
|
|
|
}
|