Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add page loader that prevents default route change #7

Open
igorbumba opened this issue Apr 26, 2023 · 1 comment
Open

Add page loader that prevents default route change #7

igorbumba opened this issue Apr 26, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@igorbumba
Copy link
Collaborator

PageLoader.tsx

import { useEffect, useRef } from 'react';
import cn from 'classnames';
import { gsap } from 'gsap/dist/gsap';
import SingletonRouter, { Router } from 'next/router';
import styles from './PageLoader.module.scss';

const PageLoader = () => {
    const $loader = useRef<HTMLDivElement>();

    useEffect(() => {
        // @ts-ignore
        if (!SingletonRouter.router?.change) {
            return;
        }

        // @ts-ignore
        const originalChangeFunction = SingletonRouter.router.change;
        const originalOnBeforeUnloadFunction = window.onbeforeunload;

        // @ts-ignore
        SingletonRouter.router.change = async (...args) => {
            // get variables from the args
            const [, , as, options] = args;

            // check if shallow routing
            const isShallow = !!options?.shallow;

            // check if you are on the same URL
            const changedUrl = as.split('?')[0];
            const sameURL = SingletonRouter.router.pathname === changedUrl;

            if ((sameURL && isShallow) || (sameURL && !isShallow)) {
                // @ts-ignore
                Router.prototype.change.apply(SingletonRouter.router, args);
            } else {
                gsap.to($loader?.current, {
                    duration: 0.3,
                    autoAlpha: 1,
                    onComplete: () => {
                        // @ts-ignore
                        Router.prototype.change.apply(SingletonRouter.router, args);
                    },
                });
            }
        };

        const handleRouteChangeComplete = () => {
            gsap.to($loader?.current, {
                duration: 0.3,
                autoAlpha: 0,
            });
        };

        SingletonRouter.router.events.on('routeChangeComplete', handleRouteChangeComplete);

        /*
         * When the component is unmounted, the original change function is assigned back.
         */
        return () => {
            // @ts-ignore
            SingletonRouter.router.change = originalChangeFunction;
            window.onbeforeunload = originalOnBeforeUnloadFunction;
            SingletonRouter.router.events.off('routeChangeComplete', handleRouteChangeComplete);
        };
    }, [$loader]);

    return <div ref={$loader} className={cn(styles.wrapper, 'js-loader')} />;
};

export default PageLoader;
@igorbumba
Copy link
Collaborator Author

PageLoader.module.scss

.wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    background-color: var(--body-bg-color);
    z-index: 99;
}

@igorbumba igorbumba added the enhancement New feature or request label Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants