mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {createContext, useContext, useMemo} from 'react'
2import {ScrollHandlers} from 'react-native-reanimated'
3
4const ScrollContext = createContext<ScrollHandlers<any>>({
5 onBeginDrag: undefined,
6 onEndDrag: undefined,
7 onScroll: undefined,
8 onMomentumEnd: undefined,
9})
10
11export function useScrollHandlers(): ScrollHandlers<any> {
12 return useContext(ScrollContext)
13}
14
15type ProviderProps = {children: React.ReactNode} & ScrollHandlers<any>
16
17// Note: this completely *overrides* the parent handlers.
18// It's up to you to compose them with the parent ones via useScrollHandlers() if needed.
19export function ScrollProvider({
20 children,
21 onBeginDrag,
22 onEndDrag,
23 onScroll,
24 onMomentumEnd,
25}: ProviderProps) {
26 const handlers = useMemo(
27 () => ({
28 onBeginDrag,
29 onEndDrag,
30 onScroll,
31 onMomentumEnd,
32 }),
33 [onBeginDrag, onEndDrag, onScroll, onMomentumEnd],
34 )
35 return (
36 <ScrollContext.Provider value={handlers}>{children}</ScrollContext.Provider>
37 )
38}