Skip to content

Instantly share code, notes, and snippets.

@nicubarbaros
Last active September 24, 2022 23:28
Show Gist options
  • Save nicubarbaros/942588544ae2f36d97c50ad1cc0570ae to your computer and use it in GitHub Desktop.
Save nicubarbaros/942588544ae2f36d97c50ad1cc0570ae to your computer and use it in GitHub Desktop.
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