GithubHelp home page GithubHelp logo

run4w4y / nextjs-router-events Goto Github PK

View Code? Open in Web Editor NEW
35.0 3.0 3.0 20 KB

A router events alternative for Next 13+ with app directory

License: MIT License

JavaScript 7.98% TypeScript 92.02%
app-directory navigation nextjs router-events typescript

nextjs-router-events's People

Contributors

run4w4y avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nextjs-router-events's Issues

Issues not applicable to backspace event

Hi! I am currently studying front-end development. I used your library during the project, and I opened the issue because I had a question. (The project is using Next.js 13, and App router.)

After install your nextjs-router-events library, and I want to prevent by pop-up a modal window when a user leaves the write page.

The modal pops up well when I try to leave via <Link> in navigation bar, but the problem is that it doesn't pop up normally when leaves using browser-native navigation methods(refresh or backspace).

My current code is as follows.

// app/layout.tsx (Root-layout)
import QueryProvider from "./QueryProvider";
import Header from "@/components/Header";
import "@/styles/globals.css";
import type { Metadata } from "next";
import Recoil from "./Recoil";
import { ToastContainer } from "react-toastify";
import "react-toastify/ReactToastify.css";
import { RouteChangesProvider } from "nextjs-router-events";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <Recoil>
          <QueryProvider>
            {/* wrapped in provider */}
            <RouteChangesProvider>
              <Header />
              {children}
              <ToastContainer />
            </RouteChangesProvider>
          </QueryProvider>
        </Recoil>
      </body>
      />
    </html>
  );
}
// app/community/write/page.tsx
"use client";
import { NextPage } from "next";
import AddPost from "@/components/community/write/AddPost";
import { useEffect, useState } from "react";
import supabase from "@/libs/supabase";
import { useRouter } from "next/navigation";
import useLeaveConfirm from "@/hooks/useLeaveConfirm";

const Write: NextPage = () => {
  const router = useRouter();
  const [sessionState, setSessionState] = useState<any>(null);

  useEffect(() => {
    const getSessionState = async () => {
      const { data: session, error } = await supabase.auth.getSession();
      if (!session.session) router.push("/");
      setSessionState(session.session);
    }
    getSessionState();
  }, [router]);

  const preventClose = (e: BeforeUnloadEvent) => {
    e.preventDefault();
    e.returnValue = '';
  };

  useEffect(() => {
    window.addEventListener('beforeunload', preventClose);
    return () => {
      window.removeEventListener('beforeunload', preventClose);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const confirmModal = useLeaveConfirm(true);

  return (
    <>
      <AddPost />
      {confirmModal}
    </>
  )
}

export default Write;
// app/hooks/useBeforeUnload.tsx
import { useEffect } from "react"

// NOTE: although there is a message argument, you really should not be relying on it, as most, if not all, modern browsers completely ignore it anyways
const useBeforeUnload = (shouldPreventUnload: boolean, message?: string) => {
  useEffect(() => {
    const abortController = new AbortController()

    if (shouldPreventUnload)
      window.addEventListener('beforeunload', (ev) => {
        ev.preventDefault()

        return (ev.returnValue = message ?? '')
      }, { capture: true, signal: abortController.signal })

    return () => abortController.abort()
  }, [shouldPreventUnload, message])
}

export default useBeforeUnload;
// app/hooks/useLeaveConfirm.tsx
import { useCallback, useState } from "react"
import { useRouteChangeEvents } from "nextjs-router-events"
import useBeforeUnload from './useBeforeUnload' // read further for an explanation
import FreezeModal from "@/components/community/ui/FreezeModal";
import { useRouter } from "next/navigation";

const useLeaveConfirm = (shouldPreventRouteChange: boolean) => {
  const router = useRouter();
  const [openModal, setOpenModal] = useState(false);
  const onBeforeRouteChange = useCallback(() => {
    if (shouldPreventRouteChange) {
      setOpenModal(true)
      return false
    }
    return true
  }, [shouldPreventRouteChange])

  const { allowRouteChange } = useRouteChangeEvents({ onBeforeRouteChange })
  useBeforeUnload(shouldPreventRouteChange)

  console.log("custom hook openModal >>> ", openModal);

  return (
    <FreezeModal
      open={openModal}
      onOpenChange={setOpenModal}
      onClick={() => {
        allowRouteChange();
      }}
      onClose={() => setOpenModal(false)}
    />
  )
}

export default useLeaveConfirm;
// app/components/community/ui/FreezeModal.tsx
"use client";

import { Dispatch, SetStateAction } from "react";

interface FreezeModalProps {
  open?: boolean;
  onOpenChange?: Dispatch<SetStateAction<boolean>>;
  onClose: () => void;
  onClick?: () => void;
}

const FreezeModal = ({ open, onOpenChange, onClose, onClick }: FreezeModalProps) => {
  console.log("Modal Component open >>> ", open);
  if (open)
  return (
      <div
        id="modal-container"
        className="fixed top-0 left-0 w-screen h-screen bg-black/30 flex justify-center items-center z-10"
      >
        <div
          id="modal-content"
          className="bg-white w-96 h-64 px-4 py-2 flex flex-col space-y-8 justify-center items-center rounded-xl relative"
        >
          <div className="flex flex-col items-center justify-center space-y-2">
            <h2 className="text-md font-bold">Warning</h2>
            <div className="flex flex-col items-center justify-center">
              <h3 className="text-lg font-extrabold">
                Are you sure you want to stop writing?
              </h3>
              <p className="text-sm text-gray-600">
                Changes are not saved.
              </p>
            </div>
          </div>
          <div className="flex space-x-4 w-full justify-center">
            <div>
              <button
                onClick={onClick}
                className="bg-gray-100 px-10 py-3 rounded-md"
              >
                quit
              </button>
            </div>
            <div>
              <button
                onClick={onClose}
                className="bg-mainGreen text-white px-10 py-3 rounded-md"
              >
                keep writing
              </button>
            </div>
          </div>
        </div>
      </div>
  );
};

export default FreezeModal;

I'm sorry I couldn't summarize the problem and brought you soooo long codes.
This is my first Next.js project, so there are many things I don't know yet, and this summary was my best shot...๐Ÿ˜‚

You can also see the whole code below.
https://github.com/Savers-Save-Earth/Savers/tree/163-fix-community-page

Thank you very much for the wonderful library. โœจ Have a nice day!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.