#How to use animations library like 'locomotive-scroll' in nextjs app router?

7 messages ยท Page 1 of 1 (latest)

normal nicheBOT
#

๐Ÿ”Ž This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

๐Ÿ•ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

โœ… You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

true wigeon
#

bump

signal harbor
#

can you show me your best attempt so far?

#

what have you tried and still failed to get it to work?

true wigeon
true wigeon
#
//my context
'use client';

import { ReactNode, createContext, useEffect, useMemo, useState } from 'react';

export const LocoScrollContext = createContext({
  scroll: null
});

type SmoothScrollProviderProps = {
  children: ReactNode;
  options?: any;
};

export const LocoScrollProvider = ({
  children,
  options
}: SmoothScrollProviderProps): JSX.Element => {
  const [scroll, setScroll] = useState<any>(null);

  useEffect(() => {
    if (!scroll) {
      (async () => {
        try {
          const LocomotiveScroll = (await import('locomotive-scroll')).default;

          const scrollInstance = new LocomotiveScroll({
            el: document.querySelector('[data-scroll-container]') as HTMLElement,
            ...options
          });

          setScroll(scrollInstance);
        } catch (error) {
          throw new Error(`[SmoothScrollProvider]: ${error}`);
        }
      })();
    }

    return () => {
      if (scroll !== null) {
        scroll.destroy();
      }
    };
  }, [scroll]); // eslint-disable-line react-hooks/exhaustive-deps

  return (
    <LocoScrollContext.Provider value={useMemo(() => ({ scroll }), [scroll])}>
      {children}
    </LocoScrollContext.Provider>
  );
};

LocoScrollContext.displayName = 'LocoScrollContext';
LocoScrollProvider.displayName = 'LocoScrollProvider';
#
//my root layout
<html>
      <body className="bg-c2">
        <div data-scroll-container>
          <Navbar />
          <main>{children}</main>
          <Footer dict={dict?.Shared?.Footer} />
        </div>
      </body>
    </html>