Last active
September 24, 2022 23:28
-
-
Save nicubarbaros/942588544ae2f36d97c50ad1cc0570ae to your computer and use it in GitHub Desktop.
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
import { useEffect, useRef } from "react"; | |
import LocomotiveScroll, { | |
LocomotiveScrollOptions, | |
Scroll, | |
} from "locomotive-scroll"; | |
import "locomotive-scroll/src/locomotive-scroll.scss"; | |
type UseLocomotiveScrollHook = [React.RefObject<Scroll>]; | |
type Props = { | |
ref: React.RefObject<Element>; | |
} & Omit<LocomotiveScrollOptions, "el">; | |
const useLocomotiveScroll = ({ | |
ref, | |
...otherProps | |
}: Props): UseLocomotiveScrollHook => { | |
const locomotiveScrollRef = useRef<Scroll | null>(null); | |
useEffect(() => { | |
if (ref.current) { | |
locomotiveScrollRef.current = new LocomotiveScroll({ | |
...otherProps, | |
el: ref.current, | |
}); | |
} | |
return () => { | |
locomotiveScrollRef.current?.destroy(); | |
}; | |
}, [ref]); | |
return [locomotiveScrollRef]; | |
}; | |
export default useLocomotiveScroll; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment