-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gesture/spring/dinamic lib loading with animation provider
- Loading branch information
Showing
8 changed files
with
290 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/shared/lib/components/AnimationProvider/AnimationProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
type GestureType = typeof import("@use-gesture/react"); | ||
type SpringType = typeof import("@react-spring/web"); | ||
|
||
interface AnimationContextPayload { | ||
Gesture?: GestureType; | ||
Spring?: SpringType; | ||
isLoaded?: boolean; | ||
} | ||
|
||
const getAsyncAnimationModules = () => { | ||
const Gesture = import("@use-gesture/react"); | ||
const Spring = import("@react-spring/web"); | ||
|
||
return Promise.all([Gesture, Spring]); | ||
}; | ||
|
||
const AnimationContext = createContext<AnimationContextPayload>({}); | ||
|
||
export const useAnimationLibs = () => { | ||
return useContext(AnimationContext) as Required<AnimationContextPayload>; | ||
}; | ||
|
||
export const AnimationProvider = ({ children }: { children: ReactNode }) => { | ||
const GestureRef = useRef<GestureType>(); | ||
const SpringRef = useRef<SpringType>(); | ||
|
||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
getAsyncAnimationModules().then(([Gesture, Spring]) => { | ||
GestureRef.current = Gesture; | ||
SpringRef.current = Spring; | ||
setIsLoaded(true); | ||
}); | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
Gesture: GestureRef.current, | ||
Spring: SpringRef.current, | ||
isLoaded, | ||
}), | ||
[isLoaded] | ||
); | ||
|
||
return ( | ||
<AnimationContext.Provider value={value}> | ||
{children} | ||
</AnimationContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useAnimationLibs, AnimationProvider } from "./AnimationProvider"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.